An open source design system that's fully customizable and agent ready
TypeScript
docs(cli): state init's exit codes and flag interactions (#6589)
Joey16458c40f85b
diff --git a/.changeset/cli-spec-init-doc-exit-codes.md b/.changeset/cli-spec-init-doc-exit-codes.mdnew file mode 100644index 0000000000..130a1e4087--- /dev/null+++ b/.changeset/cli-spec-init-doc-exit-codes.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[docs] `astryx init --help` and the manifest now say how init's flags interact: `--remove-agents` only removes the managed block and ignores the install flags, `--all` overrides `--features`, and `--agent` and `--agent-docs-path` apply only when agent docs are installed, with an explicit path taking precedence. The documented exit codes now include an `--agent-docs-path` outside the project (exit 1) and no longer list two template cases the CLI cannot reach.++@josephfarinadiff --git a/packages/cli/clients/cli/commands/init.doc.mjs b/packages/cli/clients/cli/commands/init.doc.mjsindex 9cc978ebd6..9382349cc7 100644--- a/packages/cli/clients/cli/commands/init.doc.mjs+++ b/packages/cli/clients/cli/commands/init.doc.mjs@@ -19,36 +19,44 @@ export const doc = {'agents, and CI). By default it installs the AGENTS.md/CLAUDE.md agent-docs, ' +'including guidance from configured integrations, and prints getting-started ' +'guidance; features/--all add theme and page-building ' +- 'guidance and can scaffold a starter template.',+ 'guidance and write an annotated theme template.',fn: 'init',options: [{flag: '--features <list>',param: 'options.features',description:- 'Comma-separated features to install (agents, theme, template)',+ 'Comma-separated features to install (agents, theme, template). An unknown feature exits 1 with ERR_UNKNOWN_FEATURE. ' ++ 'Ignored with --all or --remove-agents',},{flag: '--all',param: 'options.all',- description: 'Install all features, no prompts',+ description: 'Install all features (agents, theme, template); overrides --features',},{flag: '--remove-agents',param: 'options.removeAgents',- description: 'Remove AI agent docs from all agent doc files',+ description:+ 'Remove the managed block from AGENTS.md, CLAUDE.md, .claude/CLAUDE.md, .cursorrules, .hermes.md and HERMES.md ' ++ "(deleting AGENTS.md or .claude/CLAUDE.md when only init's heading is left) and do nothing else. " ++ '--features, --all, --agent and --agent-docs-path are ignored; a file written with --agent-docs-path keeps its block',},{flag: '--agent <tool>',param: 'options.agent',choices: ['claude', 'cursor', 'codex', 'hermes', 'muse', 'all'],description:- 'Target AI tool for agent docs: claude, cursor, codex, hermes, muse, all',+ 'Target AI tool for agent docs: claude, cursor, codex, hermes, muse, all. An unknown tool exits 1 with ERR_UNKNOWN_AGENT. ' ++ 'Used only when agent docs are installed (the default, --all, or --features agents); --agent-docs-path takes precedence',},{flag: '--agent-docs-path <path...>',param: 'options.agentDocsPath',- description: 'Explicit file path(s) for agent docs',+ description:+ 'Explicit file path(s) for agent docs, inside the project; takes precedence over --agent. ' ++ 'If any path is outside the project, no agent docs are written and the command exits 1. ' ++ 'Used only when agent docs are installed',},],examples: [@@ -57,10 +65,13 @@ export const doc = {{label: 'Machine-readable receipt', cli: 'astryx init --json'},],exitCodes: [- {code: 0, when: 'success'},+ {+ code: 0,+ when: 'success, including agent docs that could not be written for a reason other than a path escape (reported as docsError)',+ },{code: 1,- when: 'unknown --agent, an unknown feature or template, or a starter template would overwrite an existing page',+ when: 'an unknown --agent or feature, or an --agent-docs-path outside the project',},],related: ['doctor', 'upgrade', 'build', 'theme'],diff --git a/packages/cli/clients/cli/commands/init.flag-help.test.mjs b/packages/cli/clients/cli/commands/init.flag-help.test.mjsnew file mode 100644index 0000000000..47b59f1949--- /dev/null+++ b/packages/cli/clients/cli/commands/init.flag-help.test.mjs@@ -0,0 +1,153 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file `astryx init` documents how its flags interact and when it exits 1, and+ * behaves as documented. Option text is read from the generated manifest (the+ * same strings `--help` prints); exit codes from the CommandDoc.+ */++import {describe, it, expect, beforeEach, afterEach} from 'vitest';+import * as fs from 'node:fs';+import * as os from 'node:os';+import * as path from 'node:path';+import {runCli} from '../../../test-utils/run-cli.mjs';+import {doc as initDoc} from './init.doc.mjs';++const MARKER_START = '<!-- ASTRYX:START -->';++/** @type {string} */+let tmpDir;++beforeEach(() => {+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-init-flags-'));+});++afterEach(() => {+ fs.rmSync(tmpDir, {recursive: true, force: true});+});++/** @param {string} rel */+const exists = rel => fs.existsSync(path.join(tmpDir, rel));+/** @param {string} rel */+const read = rel => fs.readFileSync(path.join(tmpDir, rel), 'utf8');++/**+ * init's option descriptions from the manifest, keyed by long flag.+ * @returns {Promise<Record<string, string>>}+ */+async function initOptions() {+ const {stdout} = await runCli(['--json', 'manifest'], tmpDir);+ const init = JSON.parse(stdout).data.commands.find(+ (/** @type {{name: string}} */ c) => c.name === 'init',+ );+ return Object.fromEntries(+ init.options.map((/** @type {{flag: string, description: string}} */ o) => [+ o.flag.match(/--[a-z-]+/)?.[0],+ o.description,+ ]),+ );+}++/** @param {string[]} args */+async function initJson(args) {+ const result = await runCli(['--json', 'init', ...args], tmpDir);+ return {status: result.status, envelope: JSON.parse(result.stdout)};+}++/** @param {number} code */+const exitWhen = code => initDoc.exitCodes?.find(e => e.code === code)?.when ?? '';++describe('astryx init flag interactions', () => {+ it('--remove-agents: help says it only removes the block, and it ignores every install flag', async () => {+ const help = (await initOptions())['--remove-agents'];+ for (const flag of ['--features', '--all', '--agent', '--agent-docs-path']) {+ expect(help).toContain(flag);+ }+ for (const file of ['AGENTS.md', '.claude/CLAUDE.md', '.cursorrules', '.hermes.md', 'HERMES.md']) {+ expect(help).toContain(file);+ }+ expect(help).toMatch(/--agent-docs-path keeps its block/);++ await initJson(['--agent-docs-path', 'docs/AI.md']);+ await initJson([]);+ expect(exists('AGENTS.md')).toBe(true);++ const removed = await initJson([+ '--remove-agents',+ '--features',+ 'bogus',+ '--all',+ '--agent',+ 'bogus',+ '--agent-docs-path',+ 'docs/AI.md',+ ]);+ expect(removed.status).toBe(0);+ expect(removed.envelope.type).toBe('init.remove');+ expect(exists('AGENTS.md')).toBe(false);+ expect(exists('theme.template.ts')).toBe(false);+ expect(read('docs/AI.md')).toContain(MARKER_START);+ });++ it('--all: help says it overrides --features, and an unknown feature beside it is not checked', async () => {+ const options = await initOptions();+ expect(options['--all']).toMatch(/overrides --features/);+ expect(options['--features']).toMatch(/Ignored with --all or --remove-agents/);+ expect(options['--features']).toContain('ERR_UNKNOWN_FEATURE');++ const {status, envelope} = await initJson(['--all', '--features', 'bogus']);+ expect(status).toBe(0);+ expect(envelope.data.features).toEqual(['agents', 'theme', 'template']);+ });++ it('--agent and --agent-docs-path: help states their scope and precedence, and the CLI follows it', async () => {+ const options = await initOptions();+ expect(options['--agent']).toMatch(/Used only when agent docs are installed/);+ expect(options['--agent']).toMatch(/--agent-docs-path takes precedence/);+ expect(options['--agent']).toContain('ERR_UNKNOWN_AGENT');+ expect(options['--agent-docs-path']).toMatch(/takes precedence over --agent/);+ expect(options['--agent-docs-path']).toMatch(/outside the project.*exits 1/);+ expect(options['--agent-docs-path']).toMatch(/Used only when agent docs are installed/);++ const unused = await initJson([+ '--features',+ 'theme',+ '--agent',+ 'bogus',+ '--agent-docs-path',+ '../outside.md',+ ]);+ expect(unused.status).toBe(0);+ expect(unused.envelope.data.docsWritten).toEqual([]);++ const explicit = await initJson(['--agent', 'claude', '--agent-docs-path', 'docs/AI.md']);+ expect(explicit.status).toBe(0);+ expect(explicit.envelope.data.docsWritten).toEqual(['docs/AI.md']);+ expect(exists('CLAUDE.md') || exists('.claude/CLAUDE.md')).toBe(false);+ });+});++describe('astryx init exit codes', () => {+ it('exit 1: the CommandDoc lists an --agent-docs-path escape, which exits 1 in both modes and writes nothing', async () => {+ expect(exitWhen(1)).toMatch(/--agent-docs-path outside the project/);+ // The CLI never scaffolds a page template, so no template case can exit 1.+ expect(exitWhen(1)).not.toMatch(/template/);++ const text = await runCli(['init', '--agent-docs-path', 'ok.md', '../outside.md'], tmpDir);+ const json = await initJson(['--agent-docs-path', 'ok.md', '../outside.md']);+ expect(text.status).toBe(1);+ expect(json.status).toBe(1);+ expect(json.envelope.data.docsError.kind).toBe('path-safety');+ expect(exists('ok.md')).toBe(false);+ });++ it('exit 0: the CommandDoc says a soft agent-docs failure still exits 0, and it does', async () => {+ expect(exitWhen(0)).toMatch(/docsError/);++ // A directory where AGENTS.md belongs makes the write fail without a path escape.+ fs.mkdirSync(path.join(tmpDir, 'AGENTS.md'));+ const {status, envelope} = await initJson([]);+ expect(status).toBe(0);+ expect(envelope.data.docsError).toEqual({kind: 'install-failed'});+ });+});