UNPKG

@swell/cli

Version:

Swell's command line interface/utility

61 lines (60 loc) 2.18 kB
import { HEX24 } from './object-id.js'; export class ScopeError extends Error { constructor(message) { super(message); this.name = 'ScopeError'; } } /** * Resolve the scope of an `inspect` subcommand. * * Grammar: * (no --app) → global listing, no filter * --app=<slug> → filter by the named app * --app=. → filter by the app in the current directory's swell.json * * The `.` sentinel is the only place swell.json influences behavior; the * default is always global so the same invocation produces the same output * regardless of cwd. */ export async function resolveInspectScope(ctx, flags = {}) { if (!flags.app) { return { query: {} }; } if (flags.app === '.') { const slug = await ctx.readCurrentAppSlug(); if (!slug) { throw new ScopeError(`--app=. requires a swell.json with an 'id' field in the current directory. Pass --app=<slug> or cd into an app.`); } const appId = await ctx.resolveAppId(slug); return { appId, appSlug: slug, query: { app_id: appId } }; } const appId = await ctx.resolveAppId(flags.app); return { appId, appSlug: flags.app, query: { app_id: appId } }; } /** * Classify an inspect-subcommand identifier argument. * * - `id`: 24-char hex (Mongo ObjectId) * - `slug`: dotted form `app.<appPart>.<name>` where appPart can be either * an ObjectId or a public/private app slug * - `name`: bare identifier (letters, digits, hyphen, underscore) — * requires app context to resolve * * Resources whose stored ids are non-hex dotted strings (notifications) * handle dispatch in their own `showDetail` override; the base classifier * stays narrow. */ export function classifyIdentifier(identifier) { if (HEX24.test(identifier)) { return { kind: 'id', id: identifier }; } const slugMatch = identifier.match(/^app\.([^.]+)\.(.+)$/); if (slugMatch) { return { kind: 'slug', appPart: slugMatch[1], name: slugMatch[2] }; } if (/^[\w-]+$/i.test(identifier)) { return { kind: 'name', name: identifier }; } return { kind: 'invalid', input: identifier }; }