storebound/apps/desktop/src/lib/normalize.ts
CRT a7a749c1fe 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
2026-10-12 22:17:37 +00:00

68 lines
4.3 KiB
TypeScript

// What the host sends, made complete for the screens. The Rust host already fills defaults (store.rs migrates old
// state files), but the window must not depend on that: a field an older or newer host leaves out, or a value of the
// wrong type, becomes its default here instead of a "cannot read properties of undefined" at render time.
import { type Account, DEFAULT_SETTINGS, type Project, type Settings, STUDIO_COLORS, type Studio, type Workspace } from '../api';
import { NO_OVERRIDES, type Overrides } from './identity';
type Rec = Record<string, unknown>;
const isRec = (v: unknown): v is Rec => !!v && typeof v === 'object' && !Array.isArray(v);
const str = (v: unknown, d = ''): string => (typeof v === 'string' ? v : d);
const strOrNull = (v: unknown): string | null => (typeof v === 'string' && v ? v : null);
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
const folderName = (p: string): string => p.split(/[\\/]/).filter(Boolean).pop() ?? p;
export function normalizeProjects(v: unknown): Project[] {
const seen = new Set<string>();
return arr(v).filter(isRec).filter(p => typeof p.id === 'string' && typeof p.path === 'string' && !seen.has(p.id) && !!seen.add(p.id)).map(p => {
const path = p.path as string;
return { id: p.id as string, path, name: str(p.name).trim() || folderName(path), playAccount: strOrNull(p.playAccount), appleAccount: strOrNull(p.appleAccount), added: str(p.added), missing: p.missing === true, storeboundPin: strOrNull(p.storeboundPin) };
});
}
export function normalizeAccounts(v: unknown): Account[] {
return arr(v).filter(isRec).filter(a => typeof a.id === 'string' && (a.kind === 'play' || a.kind === 'apple'))
.map(a => ({ id: a.id as string, label: str(a.label) || (a.id as string), kind: a.kind as Account['kind'], created: str(a.created), studio: strOrNull(a.studio), profile: strOrNull(a.profile) }));
}
function studios(v: unknown): Studio[] {
return arr(v).filter(isRec).filter(s => typeof s.id === 'string').map(s => ({
id: s.id as string,
name: str(s.name) || (s.id as string),
color: (STUDIO_COLORS as readonly string[]).includes(str(s.color)) ? (s.color as Studio['color']) : 'slate',
logo: typeof s.logo === 'string' && s.logo.startsWith('data:image/') ? s.logo : null,
created: str(s.created),
}));
}
function overrides(v: unknown): Overrides {
if (!isRec(v)) return NO_OVERRIDES;
const pairs = (x: unknown) => arr(x).filter(isRec).filter(p => typeof p.ios === 'string' && typeof p.android === 'string').map(p => ({ ios: p.ios as string, android: p.android as string }));
return {
pairs: pairs(v.pairs),
unpairs: pairs(v.unpairs),
links: arr(v.links).filter(isRec).filter(l => typeof l.key === 'string' && typeof l.project === 'string').map(l => ({ key: l.key as string, project: l.project as string })),
appStudio: arr(v.appStudio).filter(isRec).filter(l => typeof l.key === 'string' && typeof l.studio === 'string').map(l => ({ key: l.key as string, studio: l.studio as string })),
ignored: arr(v.ignored).filter((x): x is string => typeof x === 'string'),
};
}
function pick<T>(v: unknown, d: T, ok: (x: unknown) => boolean): T { return ok(v) ? (v as T) : d; }
const isNum = (x: unknown) => typeof x === 'number' && Number.isFinite(x);
const isBool = (x: unknown) => typeof x === 'boolean';
export function normalizeSettings(v: unknown): Settings {
const d = DEFAULT_SETTINGS;
if (!isRec(v)) return d;
const r = isRec(v.refresh) ? v.refresh : {}, c = isRec(v.cache) ? v.cache : {}, n = isRec(v.notify) ? v.notify : {};
return {
refresh: { autoMinutes: pick(r.autoMinutes, d.refresh.autoMinutes, isNum), onLaunch: pick(r.onLaunch, d.refresh.onLaunch, isBool), parallel: pick(r.parallel, d.refresh.parallel, isNum), icons: pick(r.icons, d.refresh.icons, isBool) },
cache: { keepDays: pick(c.keepDays, d.cache.keepDays, isNum) },
notify: { reviewState: pick(n.reviewState, d.notify.reviewState, isBool), lowReviews: pick(n.lowReviews, d.notify.lowReviews, isBool), crash: pick(n.crash, d.notify.crash, isBool) },
channel: v.channel === 'beta' ? 'beta' : 'stable',
};
}
export function normalizeWorkspace(v: unknown): Workspace {
const w = isRec(v) ? v : {};
return { studios: studios(w.studios), overrides: overrides(w.overrides), settings: normalizeSettings(w.settings) };
}