Ignore dormant apps: per-app and per-store 'ignored' in the app list; apps ignore/unignore, watch, ws and apps list skip them; MCP tools
This commit is contained in:
parent
75bf62e125
commit
13b91dd423
7 changed files with 278 additions and 30 deletions
|
|
@ -16,10 +16,15 @@ import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameS
|
|||
import { homedir } from 'node:os';
|
||||
import { basename, dirname, join, resolve } from 'node:path';
|
||||
|
||||
export interface RegistryProject { id: string; path: string; name: string; added: string }
|
||||
/** The store sides an entry can be ignored on. */
|
||||
export type IgnoreStore = 'apple' | 'play';
|
||||
export const IGNORE_STORES: IgnoreStore[] = ['apple', 'play'];
|
||||
/** `ignored` (optional, on both kinds of entry): the store sides storebound leaves out of watch, events, portfolio reads and the
|
||||
* desktop lists. Both listed = the whole app is ignored. Storebound-side visibility only: nothing changes in the stores. */
|
||||
export interface RegistryProject { id: string; path: string; name: string; added: string; ignored?: IgnoreStore[] }
|
||||
/** A store app without a local project: store + app id (Apple: the numeric app id; Play: the package name), the
|
||||
* credential profile that reads it. */
|
||||
export interface StoreOnlyApp { id: string; store: 'apple' | 'play'; appId: string; account: string | null; name: string; bundleId: string | null; added: string }
|
||||
export interface StoreOnlyApp { id: string; store: 'apple' | 'play'; appId: string; account: string | null; name: string; bundleId: string | null; added: string; ignored?: IgnoreStore[] }
|
||||
export interface Registry { version: 1; projects: RegistryProject[]; storeApps?: StoreOnlyApp[] }
|
||||
|
||||
export const registryFile = (env: Record<string, string | undefined> = process.env): string => env.STOREBOUND_REGISTRY || join(homedir(), '.storebound', 'projects.json');
|
||||
|
|
@ -34,6 +39,9 @@ export const storeAppId = (store: 'apple' | 'play', appId: string): string => cr
|
|||
export const APPLE_APP_ID = /^\d{6,12}$/;
|
||||
export const PACKAGE_NAME = /^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/;
|
||||
|
||||
/** The valid, unique, ordered store sides of a raw `ignored` value. */
|
||||
const parseIgnored = (v: unknown): IgnoreStore[] => (Array.isArray(v) ? IGNORE_STORES.filter(x => v.includes(x)) : []);
|
||||
|
||||
/** Store-only entries of the file's JSON; malformed ones are dropped. Pure. */
|
||||
function parseStoreApps(list: unknown): StoreOnlyApp[] {
|
||||
const out: StoreOnlyApp[] = [];
|
||||
|
|
@ -44,7 +52,7 @@ function parseStoreApps(list: unknown): StoreOnlyApp[] {
|
|||
if (!(o.store === 'apple' ? APPLE_APP_ID : PACKAGE_NAME).test(appId) || out.some(a => a.store === o.store && a.appId === appId)) continue;
|
||||
out.push({ id: storeAppId(o.store, appId), store: o.store, appId, account: typeof o.account === 'string' && o.account ? o.account : null,
|
||||
name: typeof o.name === 'string' && o.name.trim() ? o.name : appId, bundleId: typeof o.bundleId === 'string' && o.bundleId ? o.bundleId : o.store === 'play' ? appId : null,
|
||||
added: typeof o.added === 'string' ? o.added : '' });
|
||||
added: typeof o.added === 'string' ? o.added : '', ...(parseIgnored(o.ignored).length ? { ignored: parseIgnored(o.ignored) } : {}) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -60,7 +68,8 @@ export function parseRegistry(text: string): Registry {
|
|||
if (typeof o?.path !== 'string' || !o.path.startsWith('/') && !/^[A-Za-z]:[\\/]/.test(o.path)) continue;
|
||||
if (out.some(x => x.path === o.path)) continue;
|
||||
out.push({ id: typeof o.id === 'string' && ID.test(o.id) && !out.some(x => x.id === o.id) ? o.id : folderId(o.path), path: o.path,
|
||||
name: typeof o.name === 'string' && o.name.trim() ? o.name : basename(o.path), added: typeof o.added === 'string' ? o.added : '' });
|
||||
name: typeof o.name === 'string' && o.name.trim() ? o.name : basename(o.path), added: typeof o.added === 'string' ? o.added : '',
|
||||
...(parseIgnored(o.ignored).length ? { ignored: parseIgnored(o.ignored) } : {}) });
|
||||
}
|
||||
const storeApps = parseStoreApps((raw as { storeApps?: unknown })?.storeApps);
|
||||
return storeApps.length ? { version: 1, projects: out, storeApps } : { version: 1, projects: out };
|
||||
|
|
@ -98,7 +107,7 @@ export function withStoreApp(r: Registry, a: { store: 'apple' | 'play'; appId: s
|
|||
const list = r.storeApps ?? [];
|
||||
const have = list.find(x => x.store === a.store && x.appId === appId);
|
||||
const app: StoreOnlyApp = { id: storeAppId(a.store, appId), store: a.store, appId, account: a.account ?? have?.account ?? null, name: a.name?.trim() || have?.name || appId,
|
||||
bundleId: a.bundleId ?? have?.bundleId ?? (a.store === 'play' ? appId : null), added: have?.added ?? now };
|
||||
bundleId: a.bundleId ?? have?.bundleId ?? (a.store === 'play' ? appId : null), added: have?.added ?? now, ...(have?.ignored?.length ? { ignored: have.ignored } : {}) };
|
||||
if (have && JSON.stringify(have) === JSON.stringify(app)) return { registry: r, app: have, added: false };
|
||||
return { registry: { ...r, version: 1, storeApps: [...list.filter(x => x !== have), app] }, app, added: !have };
|
||||
}
|
||||
|
|
@ -112,6 +121,86 @@ export function withoutStoreApp(r: Registry, key: string): { registry: Registry;
|
|||
return { registry: rest.length ? { ...base, storeApps: rest } : base, removed };
|
||||
}
|
||||
|
||||
// ---- ignore: dormant apps (whole app or one store side) ----
|
||||
|
||||
/** The store sides of an entry that are ignored. A store-only entry has one side: its store. */
|
||||
export const ignoredStores = (e: RegistryProject | StoreOnlyApp): IgnoreStore[] => (e.ignored ?? []).filter(x => !('store' in e) || x === e.store);
|
||||
|
||||
/** Whether the entry is ignored on `store`, or (no store) entirely: every side it has. */
|
||||
export function isIgnored(e: RegistryProject | StoreOnlyApp, store?: IgnoreStore): boolean {
|
||||
const have = ignoredStores(e);
|
||||
if (store) return have.includes(store);
|
||||
return 'store' in e ? have.length > 0 : IGNORE_STORES.every(x => have.includes(x));
|
||||
}
|
||||
|
||||
type Found = { entry: RegistryProject | StoreOnlyApp; kind: 'project' | 'storeApp' };
|
||||
|
||||
/** Finds one entry by id, folder path, "<store>:<app id>" or (unique, case-insensitive) name. Throws when it is ambiguous. */
|
||||
export function findEntry(r: Registry, key: string): Found | null {
|
||||
const all: Found[] = [...r.projects.map(entry => ({ entry, kind: 'project' as const })), ...(r.storeApps ?? []).map(entry => ({ entry, kind: 'storeApp' as const }))];
|
||||
const abs = ID.test(key) ? '' : resolve(key);
|
||||
const exact = all.filter(x => x.entry.id === key || ('path' in x.entry ? x.entry.path === abs : `${x.entry.store}:${x.entry.appId}` === key));
|
||||
if (exact.length === 1) return exact[0]!;
|
||||
const named = all.filter(x => x.entry.name.toLowerCase() === key.toLowerCase());
|
||||
if (named.length > 1) throw new Error(`"${key}" matches ${named.length} apps (${named.map(x => ('path' in x.entry ? x.entry.path : `${x.entry.store}:${x.entry.appId}`)).join(', ')}): use the id, folder or <store>:<app id>`);
|
||||
return named[0] ?? null;
|
||||
}
|
||||
|
||||
/** Ignores (on) or restores one entry, on one store side or on all of it. Pure. */
|
||||
export function withIgnore(r: Registry, key: string, store: IgnoreStore | undefined, on: boolean): { registry: Registry; entry: RegistryProject | StoreOnlyApp; changed: boolean } {
|
||||
const f = findEntry(r, key);
|
||||
if (!f) throw new Error(`${key}: not in the app list (storebound apps folders)`);
|
||||
if (store && !IGNORE_STORES.includes(store)) throw new Error('--store: apple or play');
|
||||
const only = f.kind === 'storeApp' ? (f.entry as StoreOnlyApp).store : null;
|
||||
if (only && store && store !== only) throw new Error(`${f.entry.name} is a ${only} app only: it has no ${store} side to ${on ? 'ignore' : 'restore'}`);
|
||||
const sides: IgnoreStore[] = only ? [only] : store ? [store] : IGNORE_STORES;
|
||||
const cur = f.entry.ignored ?? [];
|
||||
const next = IGNORE_STORES.filter(x => (on ? cur.includes(x) || sides.includes(x) : cur.includes(x) && !sides.includes(x)));
|
||||
if (next.join() === cur.join()) return { registry: r, entry: f.entry, changed: false };
|
||||
const { ignored: _drop, ...rest } = f.entry;
|
||||
const entry = { ...rest, ...(next.length ? { ignored: next } : {}) } as RegistryProject | StoreOnlyApp;
|
||||
const swap = <T extends RegistryProject | StoreOnlyApp>(list: T[]): T[] => list.map(x => (x === f.entry ? entry as T : x));
|
||||
return {
|
||||
registry: { ...r, projects: f.kind === 'project' ? swap(r.projects) : r.projects, ...(r.storeApps ? { storeApps: f.kind === 'storeApp' ? swap(r.storeApps) : r.storeApps } : {}) },
|
||||
entry, changed: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function setIgnore(key: string, store: IgnoreStore | undefined, on: boolean, file = registryFile()): { entry: RegistryProject | StoreOnlyApp; changed: boolean } {
|
||||
const { registry, entry, changed } = withIgnore(readRegistry(file), key, store, on);
|
||||
if (changed) writeRegistry(registry, file);
|
||||
return { entry, changed };
|
||||
}
|
||||
|
||||
/** The folders that are not ignored on `store` (no store: not ignored at all). */
|
||||
export const activeProjects = (r: Registry, store?: IgnoreStore): RegistryProject[] => r.projects.filter(p => !isIgnored(p, store));
|
||||
/** The store-only apps that are not ignored. */
|
||||
export const activeStoreApps = (r: Registry): StoreOnlyApp[] => (r.storeApps ?? []).filter(a => !isIgnored(a));
|
||||
|
||||
/** Store ids the config of a folder names (read as text, never run): Apple app ids / bundle ids and the Play package. */
|
||||
export function configStoreIds(dir: string): { apple: string[]; play: string[] } {
|
||||
const out = { apple: [] as string[], play: [] as string[] };
|
||||
for (const n of ['storebound.config.ts', 'storebound.config.mts', 'storebound.config.js', 'storebound.config.mjs', 'storebound.config.json']) {
|
||||
let t: string;
|
||||
try { t = readFileSync(join(dir, n), 'utf8'); } catch { continue; }
|
||||
for (const m of t.matchAll(/\b(?:bundleId|appId)["']?\s*:\s*["'`]([A-Za-z0-9._-]+)["'`]/g)) out.apple.push(m[1]!);
|
||||
for (const m of t.matchAll(/\bpackageName["']?\s*:\s*["'`]([A-Za-z]\w*(?:\.[A-Za-z]\w*)+)["'`]/g)) out.play.push(m[1]!);
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The store ids (Apple app ids and bundle ids, Play packages) of everything ignored on that store. */
|
||||
export function ignoredStoreIds(r: Registry): { apple: Set<string>; play: Set<string> } {
|
||||
const o = { apple: new Set<string>(), play: new Set<string>() };
|
||||
for (const p of r.projects) {
|
||||
const ids = configStoreIds(p.path);
|
||||
for (const st of IGNORE_STORES) if (isIgnored(p, st)) for (const id of ids[st]) o[st].add(id);
|
||||
}
|
||||
for (const a of r.storeApps ?? []) if (isIgnored(a)) { o[a.store].add(a.appId); if (a.bundleId) o[a.store].add(a.bundleId); }
|
||||
return o;
|
||||
}
|
||||
|
||||
export function addStoreApp(a: Parameters<typeof withStoreApp>[1], file = registryFile()): { app: StoreOnlyApp; added: boolean; changed: boolean } {
|
||||
const cur = readRegistry(file);
|
||||
const { registry, app, added } = withStoreApp(cur, a, new Date().toISOString());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue