@swell/cli
Version:
Swell's command line interface/utility
57 lines (56 loc) • 1.96 kB
JavaScript
const PATH_SEGMENT = /^[\w.-]+$/;
const APP_PREFIX = /^app\.([^.]+)$/;
export function parseSettingsKey(input) {
const appMatch = input.match(APP_PREFIX);
if (appMatch) {
return { kind: 'app', ref: appMatch[1] };
}
if (input.startsWith('app.')) {
return { kind: 'invalid', input };
}
if (PATH_SEGMENT.test(input)) {
return { kind: 'path', segment: input };
}
return { kind: 'invalid', input };
}
/**
* Produce the column-1 paste-back key for a settings record.
*
* System (no app_id): `record.name` (e.g. `taxes`).
* App with slug: `app.<slug>`.
* App without slug: `app.<hex>` (still pasteable — `app.<hex>` resolves).
*
* App records carry `name === app_id` (the hex), which is meaningless to a
* human; we never surface it. Ultimate fallback is `record.id` for degenerate
* records that have neither `app_id` nor `name`.
*/
export function formatSettingsKey(record, appSlugById) {
if (!record.app_id) {
return record.name ?? record.id ?? '-';
}
const slug = appSlugById[record.app_id] ?? record.app_id;
return `app.${slug}`;
}
/**
* Group a settings record. System rows (`app_id` null/undefined) belong to the
* platform and render under a `system` divider at the top, mirroring the
* notifications shape.
*/
export function groupSettingsRecord(record, appSlugById) {
if (!record.app_id) {
return { slug: '<system>', label: 'system', order: 0 };
}
const resolved = appSlugById[record.app_id];
if (!resolved) {
return { slug: '<not resolved>', order: 2 };
}
return { slug: resolved };
}
/**
* `general` and `admin` are flagged `deprecated: true` in the base schema and
* still come back from the default list (no server-side filter equivalent —
* `deprecated[$ne]=true` doesn't take). Filter them out client-side.
*/
export function isDeprecatedRecord(record) {
return record.deprecated === true;
}