One product per machine: the CLI and the desktop app share one version, the store accounts and the app folder list

- one version: package.json's version is the desktop app's too (desktopVersion removed); tools/desktop-version.ts
  keeps the plugin manifests and the app's copies equal, desktop-release publish refuses another version, release
  order app first then npm publish; the unpublished desktop 0.1.0 candidate is noted in the docs and CHANGELOG
- storebound desktop installs (per user, no admin: macOS DMG -> /Applications or ~/Applications, Windows NSIS /S,
  Linux AppImage + .desktop with the storebound:// handler), updates an older app, notes a newer one (downgrade only
  with --force), asks once ([Y/n], --yes, exit 3 without a terminal), opens on the folder; --dry / --json / --download;
  the install steps are pure plans (cli/lib/desktop-install.ts); the version's own manifest dl/<version>/latest.json
- credentials: one store, the CLI's credential profiles in ~/.storebound/credentials; the desktop writes and reads
  them (profiles.rs, files 0600), moves old keychain accounts into profiles on first start, shows profiles the CLI
  made; precedence env > named profile > project file > default profile; config `account`, "account" in the
  project's credentials.json; storebound accounts list | import (dedup by key id / service account e-mail, key files
  copied 0600, project file keeps keystore and review contact, backup, --dry, never prints values) | use
- app folders: one list ~/.storebound/projects.json shared by the desktop (registry.rs), storebound desktop,
  apps folders | add | remove, ws init and ws without a workspace file
- desktop: notice when a folder pins another storebound version; storebound-desktop --version
- tests: version drift, desktop decisions and per-OS plans, credential precedence and import, registry (TS + Rust),
  keychain migration and profile round trip (Rust), boot check with old keychain state and the new shared state
This commit is contained in:
CRTkafa 2026-10-13 06:17:31 +08:00
parent bba46b5c7f
commit d2ce185441
60 changed files with 2443 additions and 246 deletions

View file

@ -0,0 +1,140 @@
//! The app folders of this machine, shared with the storebound CLI: `~/.storebound/projects.json`
//! (STOREBOUND_REGISTRY), the same file as `storebound apps folders` / `apps add` / `storebound desktop` / `ws`
//! (cli/lib/registry.ts). The list of folders is the registry's; the app keeps its per-folder extras (mapped accounts)
//! in state.json under the same id. `sync` runs at start, when the list is shown and before a deep link is handled;
//! adding, removing and locating a folder in the app write the registry too. The first start with a registry missing
//! writes the app's own list into it.
//! { "version": 1, "projects": [{ "id": "<16 hex>", "path": "/abs/folder", "name": "Star Dash", "added": "<iso>" }] }
use std::fs;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use crate::store::{Project, State};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
pub id: String,
pub path: String,
pub name: String,
#[serde(default)]
pub added: String,
}
pub fn default_file(home: Option<&Path>) -> PathBuf {
if let Some(f) = std::env::var_os("STOREBOUND_REGISTRY").filter(|f| !f.is_empty()) {
return PathBuf::from(f);
}
home.map(Path::to_path_buf).unwrap_or_default().join(".storebound").join("projects.json")
}
/// The CLI's id for a folder it adds: sha256("project:<path>"), 16 hex (cli/lib/registry.ts folderId).
pub fn folder_id(path: &str) -> String {
Sha256::digest(format!("project:{path}").as_bytes()).iter().take(8).map(|b| format!("{b:02x}")).collect()
}
fn hex16(s: &str) -> bool {
s.len() == 16 && s.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
}
/// Parses the file (pure): entries without an absolute path or with a repeated path are dropped.
pub fn parse(text: &str) -> Vec<Entry> {
let Ok(v) = serde_json::from_str::<Value>(text) else { return vec![] };
let mut out: Vec<Entry> = vec![];
for p in v.get("projects").and_then(Value::as_array).into_iter().flatten() {
let Some(path) = p.get("path").and_then(Value::as_str) else { continue };
if !Path::new(path).is_absolute() || out.iter().any(|e| e.path == path) {
continue;
}
let id = p.get("id").and_then(Value::as_str).filter(|i| hex16(i) && !out.iter().any(|e| e.id == *i)).map(str::to_string).unwrap_or_else(|| folder_id(path));
let name = p.get("name").and_then(Value::as_str).filter(|n| !n.trim().is_empty()).map(str::to_string)
.unwrap_or_else(|| Path::new(path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "App".into()));
out.push(Entry { id, path: path.to_string(), name, added: p.get("added").and_then(Value::as_str).unwrap_or_default().to_string() });
}
out
}
/// None = the file does not exist (first start); Some(list) otherwise (an unreadable file reads as empty).
pub fn read(file: &Path) -> Option<Vec<Entry>> {
match fs::read_to_string(file) {
Ok(t) => Some(parse(&t)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(_) => Some(vec![]),
}
}
pub fn write(file: &Path, entries: &[Entry]) -> Result<(), String> {
if let Some(d) = file.parent() {
fs::create_dir_all(d).map_err(|e| e.to_string())?;
}
let text = serde_json::to_string_pretty(&serde_json::json!({ "version": 1, "projects": entries })).map_err(|e| e.to_string())? + "\n";
let tmp = file.with_extension(format!("tmp-{}", std::process::id()));
fs::write(&tmp, text).map_err(|e| e.to_string())?;
fs::rename(&tmp, file).map_err(|e| e.to_string())
}
fn entry_of(p: &Project) -> Entry {
Entry { id: p.id.clone(), path: p.path.clone(), name: p.name.clone(), added: p.added.clone() }
}
/// Makes the app's folder list the registry's (pure on the values). A registry folder the app has (by path) keeps the
/// app's id, name and accounts; a new one comes in with the registry's id; a folder that left the registry leaves the
/// app (with its links, ignore entries and studio moves). Returns (changed state, the registry to write back if it
/// needs the app's ids or names).
pub fn merge(state: &mut State, reg: Option<Vec<Entry>>) -> (bool, Option<Vec<Entry>>) {
let Some(reg) = reg else {
// first start with a shared list: the app's folders become it
return (false, Some(state.projects.iter().map(entry_of).collect()));
};
let mut projects = Vec::with_capacity(reg.len());
let mut write_back = false;
for e in &reg {
match state.projects.iter().find(|p| p.path == e.path) {
Some(p) => {
if p.id != e.id || p.name != e.name {
write_back = true;
}
projects.push(p.clone());
}
None => {
// an id the app already uses for another folder gets the CLI's id instead
let id = if state.projects.iter().any(|p| p.id == e.id) { folder_id(&e.path) } else { e.id.clone() };
write_back |= id != e.id;
projects.push(Project { id, path: e.path.clone(), name: e.name.clone(), play_account: None, apple_account: None, added: e.added.clone() });
}
}
}
let gone: Vec<String> = state.projects.iter().filter(|p| !projects.iter().any(|x| x.id == p.id)).map(|p| p.id.clone()).collect();
let changed = projects != state.projects;
for id in &gone {
let key = format!("project:{id}");
state.overrides.links.retain(|l| &l.project != id);
state.overrides.ignored.retain(|i| i != &key);
state.overrides.app_studio.retain(|a| a.key != key);
}
state.projects = projects;
(changed, if write_back { Some(state.projects.iter().map(entry_of).collect()) } else { None })
}
/// Reads the registry, merges, writes it back when needed. Returns whether the state changed.
pub fn sync(state: &mut State, file: &Path) -> Result<bool, String> {
let (changed, back) = merge(state, read(file));
if let Some(list) = back {
write(file, &list)?;
}
Ok(changed)
}
/// After the app changed its list (add, remove, locate): the registry gets the app's list, keeping folders another
/// tool added since the last sync.
pub fn publish(state: &State, file: &Path, removed: &[String]) -> Result<(), String> {
let mut list: Vec<Entry> = state.projects.iter().map(entry_of).collect();
for e in read(file).unwrap_or_default() {
if !list.iter().any(|x| x.path == e.path || x.id == e.id) && !removed.contains(&e.path) {
list.push(e);
}
}
write(file, &list)
}