An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): an empty-string argument no longer discards the rest of the command (#6620)
Joeye1f1b482180e
diff --git a/.changeset/cli-reject-empty-string-argument.md b/.changeset/cli-reject-empty-string-argument.mdnew file mode 100644index 0000000000..61759be5cc--- /dev/null+++ b/.changeset/cli-reject-empty-string-argument.md@@ -0,0 +1,11 @@+---+'@astryxdesign/cli': minor+---++[breaking] An empty-string argument no longer discards the rest of the command.++- incompatible-fix `IFIX-0003` / cleanup `CLN-0009`++Migration: remove the empty string from the invocation; the documented way to list is to omit the argument.++@josephfarinadiff --git a/packages/cli/clients/cli/commands/discover.mjs b/packages/cli/clients/cli/commands/discover.mjsindex 3ec814fb32..d1ba439da1 100644--- a/packages/cli/clients/cli/commands/discover.mjs+++ b/packages/cli/clients/cli/commands/discover.mjs@@ -26,7 +26,7 @@ import {list,code,} from '../formatters/index.mjs';-import {cliError} from '../lib/cli-error.mjs';+import {cliError, rejectEmptyArgument} from '../lib/cli-error.mjs';import {discover as discoverApi} from '../../../api/discover/discover.mjs';import {Project} from '../../../foundation/config/project.mjs';import {warnOnIntegrationIssues} from '../../../foundation/integrations/integration-warnings.mjs';@@ -212,6 +212,7 @@ export function registerDiscover(program) {* @param {{components?: boolean, type?: string, installed?: boolean, available?: boolean, limit?: string}} options*/async (query, options) => {+ rejectEmptyArgument('query', query, 'astryx discover');const detail = program.opts().detail || 'full';const json = program.opts().json || false;const lang = program.opts().lang || null;diff --git a/packages/cli/clients/cli/commands/empty-argument.test.mjs b/packages/cli/clients/cli/commands/empty-argument.test.mjsnew file mode 100644index 0000000000..fb099941af--- /dev/null+++ b/packages/cli/clients/cli/commands/empty-argument.test.mjs@@ -0,0 +1,16 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+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';+let dir;+beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-ea-')); fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({name:'s',version:'1.0.0',dependencies:{'@astryxdesign/core':'0.6.3'}})); fs.mkdirSync(path.join(dir,'src'),{recursive:true}); const c=path.join(dir,'node_modules','@astryxdesign','core'); fs.mkdirSync(c,{recursive:true}); fs.writeFileSync(path.join(c,'package.json'),JSON.stringify({name:'@astryxdesign/core',version:'0.6.3'})); });+afterEach(() => fs.rmSync(dir, {recursive:true,force:true}));+const json = async a => { const {status,stdout}=await runCli(['--json',...a],{cwd:dir}); return {status,body:JSON.parse(stdout)}; };+describe('an empty-string argument is rejected, not ignored', () => {+ it.each([['template',['template','','src/z.tsx'],'name'],['template path',['template','ai-chat',''],'path'],['swizzle',['swizzle',''],'component'],['discover',['discover',''],'query']])('%s', async (_,args,an) => { const {status,body}=await json(args); expect(status).toBe(1); expect(body.code).toBe('ERR_INVALID_ARGUMENT'); expect(body.error).toContain(`<${an}>`); });+ it('writes nothing', async () => { const b=fs.readdirSync(path.join(dir,'src')); await json(['template','ai-chat','']); expect(fs.readdirSync(path.join(dir,'src'))).toEqual(b); });+ it('exits same without --json', async () => { const {status,stderr}=await runCli(['template','','src/z.tsx'],{cwd:dir}); expect(status).toBe(1); expect(stderr).toContain('<name>'); });+ it('ignoring modes pass', async () => { for (const [a,t] of [[['template','','--list'],'template.list'],[['template','','--cdn'],'template.cdn'],[['template','ai-chat','','--skeleton'],'template.skeleton'],[['swizzle','','--list'],'swizzle.list']]) { const {status,body}=await json(a); expect(status).toBe(0); expect(body.type).toBe(t); } });+ it('bare --cdn empty path rejected', async () => { const {status,body}=await json(['template','ai-chat','','--cdn']); expect(status).toBe(1); expect(body.code).toBe('ERR_INVALID_ARGUMENT'); });+ it('omitted lists', async () => { for (const [a,t] of [[['template','--list'],'template.list'],[['discover'],'discover.list'],[['swizzle','--list'],'swizzle.list']]) { const {status,body}=await json(a); expect(status).toBe(0); expect(body.type).toBe(t); } });+});diff --git a/packages/cli/clients/cli/commands/swizzle.mjs b/packages/cli/clients/cli/commands/swizzle.mjsindex 075c992668..c83b4ae945 100644--- a/packages/cli/clients/cli/commands/swizzle.mjs+++ b/packages/cli/clients/cli/commands/swizzle.mjs@@ -12,7 +12,7 @@import {jsonOut} from '../../../foundation/response/json.mjs';import {emit, section, list, text, WARN, record} from '../formatters/index.mjs';-import {cliError} from '../lib/cli-error.mjs';+import {cliError, rejectEmptyArgument} from '../lib/cli-error.mjs';import {getCliInvocation} from '../../../foundation/env/package-manager.mjs';import {swizzle as swizzleApi} from '../../../api/swizzle/swizzle.mjs';import {defineCommand} from '../lib/define-command.mjs';@@ -27,6 +27,9 @@ export function registerSwizzle(program) {defineCommand(program, swizzleCommand, {fn: swizzleFn,action: async (/** @type {string | undefined} */ component, /** @type {{output: string, package?: string, list?: boolean, overwrite?: boolean}} */ options) => {+ if (!options.list) {+ rejectEmptyArgument('component', component, 'astryx swizzle');+ }const json = program.opts().json || false;const run = getCliInvocation();diff --git a/packages/cli/clients/cli/commands/template.mjs b/packages/cli/clients/cli/commands/template.mjsindex 8b9560cea1..fc2cc4bdc3 100644--- a/packages/cli/clients/cli/commands/template.mjs+++ b/packages/cli/clients/cli/commands/template.mjs@@ -6,7 +6,7 @@import {jsonOut} from '../../../foundation/response/json.mjs';import {emit, section, text, records, code, record} from '../formatters/index.mjs';-import {cliError} from '../lib/cli-error.mjs';+import {cliError, rejectEmptyArgument} from '../lib/cli-error.mjs';import {template as templateApi} from '../../../api/template/template.mjs';import {Project} from '../../../foundation/config/project.mjs';import {warnOnIntegrationIssues} from '../../../foundation/integrations/integration-warnings.mjs';@@ -66,6 +66,12 @@ export function registerTemplate(program) {* @param {{list?: boolean, type?: string, package?: string, skeleton?: boolean, cdn?: boolean | string, overwrite?: boolean}} options*/async (name, targetPath, options) => {+ const usesName = !options.cdn && !options.list;+ const usesPath = options.cdn+ ? typeof options.cdn !== 'string'+ : !options.list && !options.skeleton;+ if (usesName) rejectEmptyArgument('name', name, 'astryx template');+ if (usesPath) rejectEmptyArgument('path', targetPath, 'astryx template');const json = program.opts().json || false;const run = getCliInvocation();diff --git a/packages/cli/clients/cli/lib/cli-error.mjs b/packages/cli/clients/cli/lib/cli-error.mjsindex 22abcba3a9..303a29e2b6 100644--- a/packages/cli/clients/cli/lib/cli-error.mjs+++ b/packages/cli/clients/cli/lib/cli-error.mjs@@ -135,6 +135,25 @@ export function cliError(message, options = {}) {return /** @type {never} */ (undefined);}+/**+ * Reject a positional argument the user typed as `""`.+ *+ * @param {string} name the argument as the help spells it, without brackets+ * @param {unknown} value what Commander passed+ * @param {string} command the command as a user types it, e.g. `astryx template`+ * @returns {void} returns only when the argument is not an empty string+ */+export function rejectEmptyArgument(name, value, command) {+ if (typeof value !== 'string' || value.length > 0) return;+ cliError(+ `Argument <${name}> is an empty string. ` ++ 'An empty argument is not the same as an omitted one, and ignoring it ' ++ 'would silently discard the rest of the command: pass a value, or leave ' ++ `<${name}> out of \`${command}\` entirely.`,+ {code: ERROR_CODES.ERR_INVALID_ARGUMENT},+ );+}+/*** Clean exit. Mostly a marker for intent — `process.exit(0)` works fine,* but using cliExit at success boundaries makes greps for "exit policy"