An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): refuse a component name another doc under the root already uses (#6557)
Joeyf0cff83c1da1
diff --git a/.changeset/cli-spec-component-name-collision.md b/.changeset/cli-spec-component-name-collision.mdnew file mode 100644index 0000000000..ea1ad80d90--- /dev/null+++ b/.changeset/cli-spec-component-name-collision.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx integration add component <Name>` now refuses with `ERR_FILE_EXISTS` when a component doc anywhere under the components root already uses that name, for example `components/<Name>/<Name>.doc.mjs`. Before, it wrote a second `<Name>` beside the first and reported success, and `astryx component <Name>` then showed the new scaffold instead of the authored component. `--dry-run` refuses the same way.++@josephfarinadiff --git a/packages/cli/api/integration/add-contribution.component-names.test.mjs b/packages/cli/api/integration/add-contribution.component-names.test.mjsnew file mode 100644index 0000000000..45f7baa5c0--- /dev/null+++ b/packages/cli/api/integration/add-contribution.component-names.test.mjs@@ -0,0 +1,120 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {afterEach, beforeEach, describe, expect, it} from 'vitest';+import * as fs from 'node:fs';+import * as path from 'node:path';+import {integrationAdd, integrationAddComponent} from './add-contribution.mjs';+import {discoverIntegrationComponents} from '../../foundation/discovery/component-discovery.mjs';++let tmpDir;++beforeEach(() => {+ tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-add-component-'));+ fs.writeFileSync(+ path.join(tmpDir, 'package.json'),+ `${JSON.stringify({name: '@acme/widgets', version: '1.0.0', files: ['dist']}, null, 2)}\n`,+ );+ fs.writeFileSync(+ path.join(tmpDir, 'astryx.integration.mjs'),+ "export default {\n components: './components',\n};\n",+ );+});++afterEach(() => {+ fs.rmSync(tmpDir, {recursive: true, force: true});+});++/** @param {string} relativeDoc */+function writeAuthoredComponent(relativeDoc) {+ const doc = path.join(tmpDir, relativeDoc);+ fs.mkdirSync(path.dirname(doc), {recursive: true});+ fs.writeFileSync(+ doc,+ "export default {\n type: 'component',\n name: 'AcmeWidget',\n description: 'The authored AcmeWidget.',\n props: [],\n};\n",+ );+ fs.writeFileSync(+ doc.replace(/\.doc\.(?:ts|mjs|js)$/u, '.tsx'),+ 'export function AcmeWidget() {\n return <div>Authored</div>;\n}\n',+ );+}++/** Every file under the package, with its bytes. */+function snapshot() {+ /** @type {Record<string, string>} */+ const files = {};+ /** @param {string} dir */+ const walk = dir => {+ for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {+ const full = path.join(dir, entry.name);+ if (entry.isDirectory()) walk(full);+ else files[path.relative(tmpDir, full)] = fs.readFileSync(full, 'utf-8');+ }+ };+ walk(tmpDir);+ return files;+}++describe('integrationAddComponent name collisions', () => {+ it('refuses a name a nested component doc already defines', async () => {+ writeAuthoredComponent('components/AcmeWidget/AcmeWidget.doc.mjs');+ const before = snapshot();++ await expect(+ integrationAddComponent('AcmeWidget', {cwd: tmpDir}),+ ).rejects.toMatchObject({+ code: 'ERR_FILE_EXISTS',+ message: expect.stringContaining(+ 'components/AcmeWidget/AcmeWidget.doc.mjs',+ ),+ });+ expect(snapshot()).toEqual(before);+ expect(+ discoverIntegrationComponents({+ name: '@acme/widgets',+ components: path.join(tmpDir, 'components'),+ }).map(component => path.relative(tmpDir, component.docPath)),+ ).toEqual([path.join('components', 'AcmeWidget', 'AcmeWidget.doc.mjs')]);+ });++ it('refuses the same collision under --dry-run', async () => {+ writeAuthoredComponent('components/widgets/AcmeWidget.doc.mjs');++ await expect(+ integrationAdd('component', 'AcmeWidget', {cwd: tmpDir, dryRun: true}),+ ).rejects.toMatchObject({code: 'ERR_FILE_EXISTS'});+ });++ it('refuses a same-stem doc with another extension in the root', async () => {+ fs.mkdirSync(path.join(tmpDir, 'components'));+ fs.writeFileSync(+ path.join(tmpDir, 'components', 'AcmeWidget.doc.ts'),+ "export default {type: 'component', name: 'AcmeWidget', props: []};\n",+ );+ const before = snapshot();++ await expect(+ integrationAddComponent('AcmeWidget', {cwd: tmpDir}),+ ).rejects.toMatchObject({code: 'ERR_FILE_EXISTS'});+ expect(snapshot()).toEqual(before);+ });++ it('still adds a component whose name is free', async () => {+ writeAuthoredComponent('components/AcmeWidget/AcmeWidget.doc.mjs');++ const result = await integrationAddComponent('AcmeBadge', {cwd: tmpDir});++ expect(result.data.files).toEqual([+ 'components/AcmeBadge.doc.mjs',+ 'components/AcmeBadge.tsx',+ 'package.json',+ ]);+ expect(+ discoverIntegrationComponents({+ name: '@acme/widgets',+ components: path.join(tmpDir, 'components'),+ })+ .map(component => component.name)+ .sort(),+ ).toEqual(['AcmeBadge', 'AcmeWidget']);+ });+});diff --git a/packages/cli/api/integration/add-contribution.mjs b/packages/cli/api/integration/add-contribution.mjsindex 5a293c49bd..591b25c8b7 100644--- a/packages/cli/api/integration/add-contribution.mjs+++ b/packages/cli/api/integration/add-contribution.mjs@@ -35,6 +35,7 @@ import {} from '../../foundation/integrations/integrations.mjs';import {isValidSemver} from '../../foundation/env/semver.mjs';import {assertContributionVisible} from '../../foundation/integrations/contribution-inventory.mjs';+import {findIntegrationComponentDoc} from '../../foundation/discovery/component-discovery.mjs';import {parseAgentDocsField} from '../../authoring/integration/schema.mjs';import {integrationAddTheme} from './add-theme.mjs';import {@@ -248,6 +249,16 @@ async function addComponent(name, options) {ERROR_CODES.ERR_FILE_EXISTS,);}+ // Discovery keys components by doc stem anywhere under the root, so a second+ // doc with this name would shadow one of the two.+ const existingDoc = findIntegrationComponentDoc({components: root}, name);+ if (existingDoc != null) {+ throw new AstryxError(+ `Refusing to add component "${name}": ${projectPath(path.relative(packageDir, existingDoc))} already defines it.`,+ undefined,+ ERROR_CODES.ERR_FILE_EXISTS,+ );+ }const sourcePath = projectPath(path.relative(packageDir, sourceFile));const extensionlessPath = sourcePath.replace(/\.tsx?$/u, '');diff --git a/packages/cli/api/integration/integrationAddComponent.doc.mjs b/packages/cli/api/integration/integrationAddComponent.doc.mjsindex 5916492259..1c4f7d895e 100644--- a/packages/cli/api/integration/integrationAddComponent.doc.mjs+++ b/packages/cli/api/integration/integrationAddComponent.doc.mjs@@ -8,7 +8,7 @@ export const doc = {displayName: 'integrationAddComponent()',summary: 'Add a complete component contribution.',description:- 'Creates the same-stem component source and metadata pair, declares the components root on first use, preserves existing package allowlists, and publishes an exact source subpath when the package already has an exports map.',+ 'Creates the same-stem component source and metadata pair, declares the components root on first use, preserves existing package allowlists, and publishes an exact source subpath when the package already has an exports map. Refuses with ERR_FILE_EXISTS when a component doc anywhere under the components root already uses the name.',importPath: '@astryxdesign/cli/api',signature:'integrationAddComponent(name: string, options?: IntegrationAddComponentOptions): Promise<IntegrationAddResponse>',