An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): read family members from the source condition so the official list does not depend on a build (#7996)
Joey557ee8f460d6
diff --git a/.changeset/family-members-from-source.md b/.changeset/family-members-from-source.mdnew file mode 100644index 0000000000..877f540d6f--- /dev/null+++ b/.changeset/family-members-from-source.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] A component family lists the same members whether or not its package has been built. When the package's `exports` give a `source` condition and that file is in the package, the CLI reads the family's members from it instead of from the built output. Before, a member that only the package's entry exports, such as Lab's `TourStep`, was listed only after a local build. The official package list now includes `TourStep`.++@josephfarinadiff --git a/packages/cli/assets/discover/official-packages.json b/packages/cli/assets/discover/official-packages.jsonindex 9263bf420a..1bc625e358 100644--- a/packages/cli/assets/discover/official-packages.json+++ b/packages/cli/assets/discover/official-packages.json@@ -196,6 +196,10 @@"kind": "component","name": "Tour"},+ {+ "kind": "component",+ "name": "TourStep"+ },{"kind": "component","name": "TransferList"diff --git a/packages/cli/foundation/discovery/component-discovery.export-names.test.mjs b/packages/cli/foundation/discovery/component-discovery.export-names.test.mjsindex 8bee2c2b2a..8c5c804b58 100644--- a/packages/cli/foundation/discovery/component-discovery.export-names.test.mjs+++ b/packages/cli/foundation/discovery/component-discovery.export-names.test.mjs@@ -705,6 +705,47 @@ describe('integration components a family doc lists in components', () => {expect(namesIn(loaded)).toEqual(['AcmeList']);});+ it('reads members through the source condition, the same with or without built output', () => {+ // The family's member is exported only from the root entry, as Lab's+ // TourStep is.+ const loaded = writePackage(+ {+ exports: {+ '.': {+ source: './src/index.ts',+ types: './dist/index.d.ts',+ default: './dist/index.js',+ },+ },+ },+ {+ 'src/index.ts':+ "export {AcmeList} from './list/AcmeList';\nexport {AcmeListRow} from './list/row';\n",+ 'src/list/AcmeList.doc.mjs': family(+ 'AcmeList',+ "{name: 'AcmeList', description: 'x'}, {name: 'AcmeListRow', description: 'x'}",+ ),+ 'src/list/AcmeList.tsx':+ 'export function AcmeList() { return null; }\n',+ 'src/list/row.tsx': 'export function AcmeListRow() { return null; }\n',+ },+ );+ expect(namesIn(loaded)).toEqual(['AcmeList', 'AcmeListRow']);+ expect(findIntegrationComponentSource(loaded, 'AcmeListRow')).toBe(+ path.join(root, 'src', 'list', 'row.tsx'),+ );+ // Built output, even a stale one without the member, changes nothing.+ fs.mkdirSync(path.join(root, 'dist'), {recursive: true});+ fs.writeFileSync(+ path.join(root, 'dist', 'index.js'),+ 'export function AcmeList() { return null; }\n',+ );+ expect(namesIn(loaded)).toEqual(['AcmeList', 'AcmeListRow']);+ // A package that does not ship its source reads the built output.+ fs.rmSync(path.join(root, 'src', 'index.ts'));+ expect(namesIn(loaded)).toEqual(['AcmeList']);+ });+it('lists a member another folder declares, with no source', async () => {const loaded = writePackage({exports: {'./menu': './src/menu/index.ts'}},diff --git a/packages/cli/foundation/discovery/component-discovery.mjs b/packages/cli/foundation/discovery/component-discovery.mjsindex 64752262e2..c1e6ff3f5b 100644--- a/packages/cli/foundation/discovery/component-discovery.mjs+++ b/packages/cli/foundation/discovery/component-discovery.mjs@@ -18,7 +18,10 @@ import {moduleExportsName,staticExportObject,} from './module-exports.mjs';-import {inspectPackageExport} from './package-exports.mjs';+import {+ inspectPackageExport,+ sourceExportTarget,+} from './package-exports.mjs';const require = createRequire(import.meta.url);@@ -996,7 +999,8 @@ function packageMain(packageDir) {/*** The local module a doc's import names: the doc's own `import`, else the* subpath its directory publishes, read through the package's `exports` (its- * `main` when it has none). Undefined when the package directory is unknown;+ * `main` when it has none). A subpath's `source` condition wins when its file+ * is in the package, so the answer does not depend on a build. Undefined when the package directory is unknown;* null when the import names another package or resolves to no file.** @param {{name?: string, __packageDir?: string, __packageExports?: Record<string, unknown>|null}} integration@@ -1027,11 +1031,13 @@ export function importedModule(integration, docPath, doc) {specifier === packageName? '.': `./${specifier.slice(packageName.length + 1)}`;- return inspectPackageExport(- {name: packageName, exports: exportsMap ?? undefined},- packageDir,- subpath,- ).target;+ const pkg = {name: packageName, exports: exportsMap ?? undefined};+ // The `source` condition first, when its file is there, so a family lists+ // the same members whether or not the package has been built.+ return (+ sourceExportTarget(pkg, packageDir, subpath) ??+ inspectPackageExport(pkg, packageDir, subpath).target+ );}/**diff --git a/packages/cli/foundation/discovery/package-exports.mjs b/packages/cli/foundation/discovery/package-exports.mjsindex 181ca8b275..902be6ef2e 100644--- a/packages/cli/foundation/discovery/package-exports.mjs+++ b/packages/cli/foundation/discovery/package-exports.mjs@@ -8,7 +8,8 @@* their legacy public deep paths and are resolved from files on disk.** @input Parsed package.json, package directory, and a public subpath- * @output The public specifier and confined target file, when exported+ * @output The public specifier and confined target file, when exported, and+ * the file a subpath's `source` condition names, when the package has it* @position packages/cli/foundation/discovery — shared package import metadata*/@@ -165,6 +166,44 @@ function resolveLegacy(packageDir, subpath) {return candidates.find(regularFile) ?? null;}+/**+ * The raw export value one subpath maps to, with the wildcard it matched.+ * @param {unknown} exportsField+ * @param {string} subpath+ * @returns {{value: unknown, wildcard: string} | null}+ */+function rawExportValue(exportsField, subpath) {+ if (exportsField == null) return null;+ if (!isRecord(exportsField)) {+ return subpath === '.' ? {value: exportsField, wildcard: ''} : null;+ }+ if (!Object.keys(exportsField).some(key => key.startsWith('.'))) {+ return subpath === '.' ? {value: exportsField, wildcard: ''} : null;+ }+ const selected = subpathValue(exportsField, subpath);+ return selected.found+ ? {value: selected.value, wildcard: selected.wildcard}+ : null;+}++/**+ * The file a subpath's `source` condition names, when that file is in the+ * package. A package that ships its source (or a workspace package before it+ * is built) declares it beside the built target, so reading it answers the+ * same with or without a build. Null when there is no such condition or file.+ * @param {{exports?: unknown}} pkg+ * @param {string} packageDir+ * @param {string} subpath `.` or a `./`-prefixed subpath+ * @returns {string|null}+ */+export function sourceExportTarget(pkg, packageDir, subpath) {+ const selected = rawExportValue(pkg.exports, subpath);+ if (!selected || !isRecord(selected.value)) return null;+ const source = selected.value.source;+ if (typeof source !== 'string') return null;+ return resolveTarget(packageDir, source.replaceAll('*', selected.wildcard));+}+/*** @typedef {object} PackageExportResult* @property {string} subpathdiff --git a/scripts/check-cli-official-packages.test.mjs b/scripts/check-cli-official-packages.test.mjsindex 522f81dde5..c4f4ef9c71 100644--- a/scripts/check-cli-official-packages.test.mjs+++ b/scripts/check-cli-official-packages.test.mjs@@ -171,6 +171,42 @@ describe('buildOfficialList on a stand-in repository', () => {});});+ it('lists the same family members with or without a build of the package', async () => {+ // A canary-only package whose family member only its root entry exports,+ // as Lab's TourStep is: the list must not depend on a local build.+ const dir = path.join(root, 'packages', 'kit');+ writeJSON(path.join(dir, 'package.json'), {+ name: '@astryxdesign/kit',+ private: true,+ astryx: {canaryOnly: true},+ exports: {+ '.': {source: './src/index.ts', default: './dist/index.js'},+ },+ });+ const files = {+ 'astryx.integration.mjs': "export default {components: './src'};\n",+ 'src/index.ts':+ "export {Steps} from './Steps/Steps';\nexport {Step} from './Steps/Step';\n",+ 'src/Steps/Steps.doc.mjs':+ "export default {type: 'component', name: 'Steps', usage: {description: 'x'}, components: [{name: 'Steps', description: 'x'}, {name: 'Step', description: 'x'}]};\n",+ 'src/Steps/Steps.tsx': 'export function Steps() { return null; }\n',+ 'src/Steps/Step.tsx': 'export function Step() { return null; }\n',+ };+ for (const [file, text] of Object.entries(files)) {+ fs.mkdirSync(path.dirname(path.join(dir, file)), {recursive: true});+ fs.writeFileSync(path.join(dir, file), text);+ }+ const unbuilt = serializeOfficialList(await buildOfficialList(root));+ expect(unbuilt).toContain('"name": "Step"');++ fs.mkdirSync(path.join(dir, 'dist'), {recursive: true});+ fs.writeFileSync(+ path.join(dir, 'dist', 'index.js'),+ 'export function Steps() { return null; }\nexport function Step() { return null; }\n',+ );+ expect(serializeOfficialList(await buildOfficialList(root))).toBe(unbuilt);+ });+it('refuses a published theme package with no descriptor', async () => {theme('bare', {name: '@astryxdesign/theme-bare'}, {descriptor: false});await expect(buildOfficialList(root)).rejects.toThrow(