@astryxdesign/cli 0.7.15-canary.g061b2fa → 0.7.15-canary.g06b7e61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/api/doctor/doctor.mjs +2 -2
  3. package/api/integration/add-helpers.mjs +5 -0
  4. package/api/integration/add-theme.mjs +15 -110
  5. package/api/theme/_adapter.d.mts +20 -4
  6. package/api/theme/_adapter.mjs +199 -20
  7. package/api/theme/_fork.d.mts +90 -0
  8. package/api/theme/_fork.mjs +246 -0
  9. package/api/theme/build/build.d.mts +11 -1
  10. package/api/theme/build/build.mjs +52 -2
  11. package/api/theme/eject/eject.mjs +26 -9
  12. package/api/theme/list/list.mjs +3 -6
  13. package/api/theme/palette/generate/generator.d.mts +13 -0
  14. package/api/theme/palette/generate/generator.mjs +32 -2
  15. package/api/theme-edit/_adapter.mjs +157 -36
  16. package/api/theme-edit/_catalog.d.mts +1 -6
  17. package/api/theme-edit/_catalog.mjs +5 -268
  18. package/api/theme-edit/_check.d.mts +3 -1
  19. package/api/theme-edit/_check.mjs +733 -308
  20. package/api/theme-edit/_color.d.mts +19 -0
  21. package/api/theme-edit/_color.mjs +277 -33
  22. package/api/theme-edit/_compiler.d.mts +1 -0
  23. package/api/theme-edit/_compiler.mjs +25 -0
  24. package/api/theme-edit/_context.d.mts +2 -0
  25. package/api/theme-edit/_context.mjs +83 -9
  26. package/api/theme-edit/_css.d.mts +84 -0
  27. package/api/theme-edit/_css.mjs +700 -0
  28. package/api/theme-edit/_edit.mjs +370 -85
  29. package/api/theme-edit/_how-commands.d.mts +5 -0
  30. package/api/theme-edit/_how-commands.mjs +20 -13
  31. package/api/theme-edit/_how-question.d.mts +29 -0
  32. package/api/theme-edit/_how-question.mjs +1573 -182
  33. package/api/theme-edit/_how.mjs +306 -22
  34. package/api/theme-edit/_overview.mjs +2 -7
  35. package/api/theme-edit/_palette.d.mts +27 -5
  36. package/api/theme-edit/_palette.mjs +231 -97
  37. package/api/theme-edit/_parts.d.mts +42 -10
  38. package/api/theme-edit/_parts.mjs +224 -69
  39. package/api/theme-edit/_preview-app.d.mts +8 -0
  40. package/api/theme-edit/_preview-app.mjs +549 -96
  41. package/api/theme-edit/_preview.d.mts +7 -16
  42. package/api/theme-edit/_preview.mjs +77 -84
  43. package/api/theme-edit/_project.d.mts +20 -1
  44. package/api/theme-edit/_project.mjs +43 -13
  45. package/api/theme-edit/_settings.d.mts +96 -20
  46. package/api/theme-edit/_settings.mjs +646 -141
  47. package/api/theme-edit/_tables.d.mts +18 -12
  48. package/api/theme-edit/_tables.mjs +144 -147
  49. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  50. package/api/theme-edit/theme-edit.type.mjs +3 -1
  51. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  52. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  53. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  54. package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
  55. package/api/theme-edit/themeEditSet.doc.mjs +1 -1
  56. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  57. package/assets/docs/tree/publishing.doc.mjs +1 -0
  58. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  59. package/assets/docs.generated.json +2 -2
  60. package/assets/preview-app/next.config.mjs +7 -1
  61. package/assets/self-audit.generated.json +1 -1
  62. package/clients/cli/build-id.generated.json +1 -1
  63. package/clients/cli/commands/experimental/index.mjs +10 -10
  64. package/clients/cli/commands/experimental/theme.mjs +22 -3
  65. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  66. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
  67. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  68. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  69. package/clients/cli/commands/integration.mjs +8 -0
  70. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  71. package/clients/cli/commands/theme.mjs +65 -24
  72. package/clients/cli/index.mjs +18 -3
  73. package/clients/cli/lib/cli-error.mjs +118 -3
  74. package/clients/cli/manifest.generated.json +1 -1
  75. package/foundation/config/project.mjs +34 -8
  76. package/foundation/discovery/theme-discovery.mjs +14 -2
  77. package/foundation/discovery/theme-imports.d.mts +16 -0
  78. package/foundation/discovery/theme-imports.mjs +30 -2
  79. package/foundation/fs/gather-cache.mjs +63 -21
  80. package/foundation/integrations/autolink.d.mts +1 -24
  81. package/foundation/integrations/autolink.mjs +7 -60
  82. package/foundation/integrations/declared-dependencies.d.mts +27 -0
  83. package/foundation/integrations/declared-dependencies.mjs +73 -0
  84. package/foundation/integrations/may-have-integrations.d.mts +8 -0
  85. package/foundation/integrations/may-have-integrations.mjs +47 -0
  86. package/foundation/release/cache.d.mts +21 -2
  87. package/foundation/release/cache.mjs +48 -8
  88. package/foundation/release/folder-lock.d.mts +7 -0
  89. package/foundation/release/folder-lock.mjs +60 -26
  90. package/foundation/release/lock-lease.d.mts +2 -1
  91. package/foundation/release/lock-lease.mjs +7 -2
  92. package/foundation/response/error-codes.doc.mjs +1 -1
  93. package/foundation/response/response-types.doc.mjs +4 -4
  94. package/package.json +6 -6
  95. package/api/theme-edit/_css-properties.d.mts +0 -10
  96. package/api/theme-edit/_css-properties.mjs +0 -556
package/CHANGELOG.md CHANGED
@@ -3088,8 +3088,8 @@ Thanks to everyone who contributed to this release:
3088
3088
 
3089
3089
  ```
3090
3090
 
3091
- - Remove the internal `drop-xds-meta-prefix` codemod from the OSS repo (#2970)
3092
- This codemod has been moved to its own package's tooling, where it belongs. It was registered as an optional, version-independent transform and is not part of any standard upgrade path, so removing it does not affect the public `0.0.13 → 0.0.15` migration.
3091
+ - Remove an internal-only codemod from the public repository (#2970)
3092
+ It was an optional, version-independent transform and not part of any standard upgrade path, so removing it does not affect the public `0.0.13 → 0.0.15` migration.
3093
3093
  - Rename the npm package scope from `@xds/*` to `@astryxdesign/*`
3094
3094
  All published packages move to the new `@astryxdesign` scope (e.g. `@xds/core` → `@astryxdesign/core`), along with the workspace lockfile, build/runtime scope-directory scans, and docsite slug derivation. Consumers must update their imports and dependency names. The internal ESLint plugin namespace (`@xds/*` rules) is intentionally untouched and tracked separately. Existing `@xds/*` codemods continue to target the old scope so projects still on `@xds/*` can migrate.
3095
3095
 
@@ -3106,7 +3106,7 @@ Thanks to everyone who contributed to this release:
3106
3106
 
3107
3107
  #### Breaking Changes
3108
3108
 
3109
- - **New `astryx upgrade` codemods** — This release ships codemods for the DatePicker→Input rename (`rename-date-picker-to-input`), Stack `element`→`as` (`rename-stack-element-to-as`), Chat `isStreaming`→`isStopShown` (`rename-isStreaming-to-isStopShown`), imperative `ref`→`handleRef` (`rename-imperative-ref-to-handleRef`), the menu/selector `children`→`endContent` move (`migrate-item-children-to-endcontent`), and the selector function-children→`renderOption` move (`migrate-selector-children-to-render-option`). The bare-name migration (`drop-xds-prefix-imports`, `drop-xds-meta-prefix`) and the theme `migrate-theme-selectors-to-data-attrs` codemod ship as optional, run them explicitly. (#2879, #2957)
3109
+ - **New `astryx upgrade` codemods** — This release ships codemods for the DatePicker→Input rename (`rename-date-picker-to-input`), Stack `element`→`as` (`rename-stack-element-to-as`), Chat `isStreaming`→`isStopShown` (`rename-isStreaming-to-isStopShown`), imperative `ref`→`handleRef` (`rename-imperative-ref-to-handleRef`), the menu/selector `children`→`endContent` move (`migrate-item-children-to-endcontent`), and the selector function-children→`renderOption` move (`migrate-selector-children-to-render-option`). The bare-name migration (`drop-xds-prefix-imports`) and the theme `migrate-theme-selectors-to-data-attrs` codemod ship as optional, run them explicitly. (#2879, #2957)
3110
3110
 
3111
3111
  #### Upgrade
3112
3112
 
@@ -2190,9 +2190,9 @@ export function checkGatherCache() {
2190
2190
  const size = `${(summary.bytes / (1024 * 1024)).toFixed(1)} MB`;
2191
2191
  return {
2192
2192
  id: 'gather-cache',
2193
- label: 'Search catalog cache',
2193
+ label: 'Kept catalog cache',
2194
2194
  status: 'info',
2195
- message: `search and build keep the catalogs they gather in ${summary.dir} (${summary.entries} ${summary.entries === 1 ? 'entry' : 'entries'}, ${size})${summary.pnp ? ", but not in a run under Yarn Plug'n'Play's loader, such as `yarn astryx`, which gathers every time" : ''}; a catalog is used only while nothing it read has changed, and deleting the folder clears it.`,
2195
+ message: `search, build, component, template, theme and discover keep what they gather (search's catalogs and each integration's check results) in ${summary.dir} (${summary.entries} ${summary.entries === 1 ? 'entry' : 'entries'}, ${size})${summary.pnp ? ", but not in a run under Yarn Plug'n'Play's loader, such as `yarn astryx`, which gathers every time" : ''}; each is used only while nothing it read has changed, and deleting the folder clears it.`,
2196
2196
  };
2197
2197
  }
2198
2198
 
@@ -12,6 +12,7 @@
12
12
  * same transaction. Side-effect-free outside of {@link applyWrites}.
13
13
  */
14
14
 
15
+ import {stillHolds} from '../../foundation/release/folder-lock.mjs';
15
16
  import * as fs from 'node:fs';
16
17
  import * as path from 'node:path';
17
18
  import {randomUUID} from 'node:crypto';
@@ -376,6 +377,10 @@ export function applyWrites(plans) {
376
377
  staged.push({...plan, temporary, original, mode});
377
378
  }
378
379
 
380
+ // Under a folder lock (the theme editor's), a run that lost the lock
381
+ // while it was stopped publishes nothing. Outside a lock this does
382
+ // nothing.
383
+ stillHolds();
379
384
  for (const plan of staged) {
380
385
  if (plan.original == null) {
381
386
  publishNewFile(plan.temporary, plan.path);
@@ -18,7 +18,6 @@ import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
18
18
  import {
19
19
  assertWithin,
20
20
  PathSafetyError,
21
- sanitizeName,
22
21
  } from '../../foundation/fs/path-safety.mjs';
23
22
  import {
24
23
  discoverBundledThemes,
@@ -46,47 +45,14 @@ import {
46
45
  projectPath,
47
46
  } from './add-helpers.mjs';
48
47
 
49
- const DEFAULT_THEMES_ROOT = './themes';
48
+ import {
49
+ renameDefinedTheme,
50
+ renameThemeFile,
51
+ rewriteThemeSource,
52
+ themeIdentity,
53
+ } from '../theme/_fork.mjs';
50
54
 
51
- /** @param {string} slug */
52
- function themeIdentity(slug) {
53
- try {
54
- sanitizeName(slug, {label: 'theme name'});
55
- } catch (error) {
56
- if (error instanceof PathSafetyError) {
57
- throw new AstryxError(
58
- error.message,
59
- undefined,
60
- ERROR_CODES.ERR_INVALID_ARGUMENT,
61
- );
62
- }
63
- throw error;
64
- }
65
- if (!/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u.test(slug)) {
66
- throw new AstryxError(
67
- `Invalid theme name "${slug}": use lowercase kebab-case starting with a letter.`,
68
- undefined,
69
- ERROR_CODES.ERR_INVALID_ARGUMENT,
70
- );
71
- }
72
- const identifier = slug.replace(/-([a-z0-9])/gu, (_, character) =>
73
- character.toUpperCase(),
74
- );
75
- const displayName = slug
76
- .split('-')
77
- .map(part =>
78
- part === 'y2k' ? 'Y2K' : part[0].toUpperCase() + part.slice(1),
79
- )
80
- .join(' ');
81
- return {
82
- slug,
83
- identifier,
84
- displayName,
85
- exportName: `${identifier}Theme`,
86
- entry: `${identifier}Theme.ts`,
87
- descriptor: `${identifier}Theme.doc.mjs`,
88
- };
89
- }
55
+ const DEFAULT_THEMES_ROOT = './themes';
90
56
 
91
57
  /** @param {{slug: string, exportName: string}} identity */
92
58
  function themeSource(identity) {
@@ -129,7 +95,10 @@ function resolveBaseTheme(slug, packageDir, owner, themesRoot) {
129
95
  // Also check the package's own themes root.
130
96
  if (themesRoot && fs.existsSync(themesRoot)) {
131
97
  try {
132
- candidates = [...candidates, ...discoverThemeDirectory(themesRoot, owner)];
98
+ candidates = [
99
+ ...candidates,
100
+ ...discoverThemeDirectory(themesRoot, owner),
101
+ ];
133
102
  } catch {
134
103
  // A broken local root is not the caller's problem here.
135
104
  }
@@ -149,74 +118,6 @@ function resolveBaseTheme(slug, packageDir, owner, themesRoot) {
149
118
  return match;
150
119
  }
151
120
 
152
- /**
153
- * Rename a file from the base theme's namespace to the new theme's namespace.
154
- * Files whose name starts with the base identifier get the new identifier;
155
- * others keep their name as-is.
156
- * @param {string} file POSIX relative path inside the theme directory
157
- * @param {string} baseIdentifier
158
- * @param {string} newIdentifier
159
- * @returns {string}
160
- */
161
- function renameThemeFile(file, baseIdentifier, newIdentifier) {
162
- const basename = path.posix.basename(file);
163
- const dir = path.posix.dirname(file);
164
- if (!basename.startsWith(baseIdentifier)) return file;
165
- const renamed = newIdentifier + basename.slice(baseIdentifier.length);
166
- return dir === '.' ? renamed : `${dir}/${renamed}`;
167
- }
168
-
169
- /**
170
- * Rewrite a source file's contents from the base theme's names to the new
171
- * theme's names. This replaces:
172
- * 1. Identifier prefixes (camelCase variables, import paths): baseIdentifier
173
- * followed by an uppercase letter.
174
- * 2. The theme slug in string literals: 'baseSlug' → 'newSlug' and
175
- * 'astryx-baseSlug' → 'astryx-newSlug'.
176
- * 3. CSS custom properties scoped to the theme: --astryx-theme-baseSlug-.
177
- * @param {string} source
178
- * @param {string} baseSlug
179
- * @param {string} baseIdentifier
180
- * @param {string} newSlug
181
- * @param {string} newIdentifier
182
- * @returns {string}
183
- */
184
- function rewriteThemeSource(
185
- source,
186
- baseSlug,
187
- baseIdentifier,
188
- newSlug,
189
- newIdentifier,
190
- ) {
191
- let result = source;
192
- // 1. Identifier prefixes: neutralPalettes → oceanPalettes, neutralTheme → oceanTheme
193
- const identRe = new RegExp(`${escapeRegExp(baseIdentifier)}(?=[A-Z])`, 'gu');
194
- result = result.replace(identRe, newIdentifier);
195
- // 2. String-literal theme names
196
- result = result.replace(
197
- new RegExp(`'astryx-${escapeRegExp(baseSlug)}'`, 'gu'),
198
- `'astryx-${newSlug}'`,
199
- );
200
- result = result.replace(
201
- new RegExp(`'${escapeRegExp(baseSlug)}'`, 'gu'),
202
- `'${newSlug}'`,
203
- );
204
- // 3. CSS custom properties scoped to the base theme
205
- result = result.replace(
206
- new RegExp(
207
- `--astryx-theme-${escapeRegExp(baseSlug)}-`,
208
- 'gu',
209
- ),
210
- `--astryx-theme-${newSlug}-`,
211
- );
212
- return result;
213
- }
214
-
215
- /** @param {string} s */
216
- function escapeRegExp(s) {
217
- return s.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
218
- }
219
-
220
121
  /** What every app that uses Astryx already has: Core, and React through Core's peers. */
221
122
  const APP_PROVIDED = new Set(['@astryxdesign/core', 'react', 'react-dom']);
222
123
 
@@ -332,6 +233,10 @@ function forkThemePlans(base, identity, themeDir) {
332
233
  identity.slug,
333
234
  identity.identifier,
334
235
  );
236
+ // The entry's defineTheme name, wherever it sits (a double-quoted name,
237
+ // one after `extends`), is the fork's, as the build names its files.
238
+ if (file === base.files[0])
239
+ contents = renameDefinedTheme(contents, identity.slug) ?? contents;
335
240
  } else {
336
241
  contents = bytes;
337
242
  }
@@ -6,6 +6,15 @@
6
6
  * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
7
7
  */
8
8
  export function themeRecordOwner(theme: import("../../foundation/discovery/theme-discovery.mjs").DiscoveredTheme): string;
9
+ /**
10
+ * Whether `--package <name>` selects a theme. It takes either name the CLI
11
+ * shows for it: the package that provides it (`@astryxdesign/cli` for a
12
+ * bundled theme) or the package the app imports it from, which the record
13
+ * holds (`@astryxdesign/theme-butter`).
14
+ * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
15
+ * @param {string | undefined} name
16
+ */
17
+ export function themeInPackage(theme: import("../../foundation/discovery/theme-discovery.mjs").DiscoveredTheme, name: string | undefined): boolean;
9
18
  /**
10
19
  * A bundled theme entry kept for the historical public `listThemes()` helper.
11
20
  * @typedef {Pick<import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme, 'slug' | 'displayName' | 'description' | 'maintained' | 'entry' | 'exportName' | 'files'>} BundledTheme
@@ -66,14 +75,21 @@ export function listUnmigratedThemeCopies(cwd: string): import("../../foundation
66
75
  /**
67
76
  * Publish one theme's complete source inventory as an atomic local fork.
68
77
  * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
69
- * @param {{cwd: string, targetPath?: string, overwrite?: boolean}} options
70
- * @returns {string} project-relative output directory
78
+ * @param {{cwd: string, targetPath?: string, overwrite?: boolean, takenSlugs?: Set<string>}} options
79
+ * `takenSlugs`: the slugs of the themes the project can use, which a fork's
80
+ * new folder name may not take
81
+ * @returns {{outputDir: string, files: string[]}} the project-relative output
82
+ * directory and the files written, named as the fork names them
71
83
  */
72
- export function ejectThemeFiles(theme: import("../../foundation/discovery/theme-discovery.mjs").DiscoveredTheme, { cwd, targetPath, overwrite }: {
84
+ export function ejectThemeFiles(theme: import("../../foundation/discovery/theme-discovery.mjs").DiscoveredTheme, { cwd, targetPath: typedPath, overwrite, takenSlugs }: {
73
85
  cwd: string;
74
86
  targetPath?: string;
75
87
  overwrite?: boolean;
76
- }): string;
88
+ takenSlugs?: Set<string>;
89
+ }): {
90
+ outputDir: string;
91
+ files: string[];
92
+ };
77
93
  /**
78
94
  * The packages the fork imports that the app does not declare. A fork that
79
95
  * imports a missing package (bundled icon sets use `lucide-react`) fails to
@@ -23,6 +23,7 @@ import {Project} from '../../foundation/config/project.mjs';
23
23
  import {
24
24
  BUNDLED_THEME_PACKAGE,
25
25
  THEMES_DIR,
26
+ THEME_SLUG_RE,
26
27
  discoverBundledThemes,
27
28
  discoverIntegrationThemes,
28
29
  discoverUnmigratedThemeCopies,
@@ -51,6 +52,7 @@ import {stripCopyrightHeader} from '../../foundation/text/copyright-header.mjs';
51
52
  import {loadIntegrations} from '../../foundation/integrations/integrations.mjs';
52
53
  import {importThemeArtifact} from '../../foundation/fs/module-loader.mjs';
53
54
  import {applyWrites} from '../integration/add-helpers.mjs';
55
+ import {forkFileNames, renamedEntry} from './_fork.mjs';
54
56
  import {
55
57
  formatCliCommand,
56
58
  quoteCommandArgument,
@@ -69,6 +71,20 @@ export function themeRecordOwner(theme) {
69
71
  return themeImportPackage(theme);
70
72
  }
71
73
 
74
+ /**
75
+ * Whether `--package <name>` selects a theme. It takes either name the CLI
76
+ * shows for it: the package that provides it (`@astryxdesign/cli` for a
77
+ * bundled theme) or the package the app imports it from, which the record
78
+ * holds (`@astryxdesign/theme-butter`).
79
+ * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
80
+ * @param {string | undefined} name
81
+ */
82
+ export function themeInPackage(theme, name) {
83
+ return (
84
+ name == null || theme.package === name || themeImportPackage(theme) === name
85
+ );
86
+ }
87
+
72
88
  /**
73
89
  * A bundled theme entry kept for the historical public `listThemes()` helper.
74
90
  * @typedef {Pick<import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme, 'slug' | 'displayName' | 'description' | 'maintained' | 'entry' | 'exportName' | 'files'>} BundledTheme
@@ -434,12 +450,14 @@ function scaffoldContents(bytes) {
434
450
  /**
435
451
  * Describe an ejected theme as a new, unmaintained local fork.
436
452
  * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
453
+ * @param {string} [name] the fork's name: the folder it takes in the local
454
+ * themes root, which discovery reads as the theme's name
437
455
  */
438
- function localThemeDescriptor(theme) {
456
+ function localThemeDescriptor(theme, name = theme.slug) {
439
457
  return `/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */
440
458
  export default {
441
459
  type: 'theme',
442
- name: ${JSON.stringify(theme.slug)},
460
+ name: ${JSON.stringify(name)},
443
461
  displayName: ${JSON.stringify(theme.displayName)},
444
462
  description: ${JSON.stringify(theme.description)},
445
463
  maintained: false,
@@ -457,8 +475,9 @@ export default {
457
475
  * @param {string} dir
458
476
  * @param {string} cwd
459
477
  * @param {string} projectDir the project root the default target is in
478
+ * @param {string[]} [forkNames] the fork's own file names, when they differ
460
479
  */
461
- function assertEjectTargetFree(theme, dir, cwd, projectDir) {
480
+ function assertEjectTargetFree(theme, dir, cwd, projectDir, forkNames = []) {
462
481
  /** @type {string[]} */
463
482
  let entries;
464
483
  try {
@@ -487,7 +506,9 @@ function assertEjectTargetFree(theme, dir, cwd, projectDir) {
487
506
  ERROR_CODES.ERR_FILE_EXISTS,
488
507
  );
489
508
  }
490
- const ownFiles = new Set(theme.files.map(name => path.basename(name)));
509
+ const ownFiles = new Set(
510
+ [...theme.files, ...forkNames].map(name => path.basename(name)),
511
+ );
491
512
  for (const name of entries) {
492
513
  const otherDescriptor =
493
514
  name.endsWith('.doc.mjs') && !ownFiles.has(name)
@@ -495,7 +516,13 @@ function assertEjectTargetFree(theme, dir, cwd, projectDir) {
495
516
  : null;
496
517
  const otherEntry =
497
518
  /Theme\.(?:ts|tsx|mts)$/.test(name) && !ownFiles.has(name);
498
- if ((otherDescriptor && otherDescriptor !== theme.slug) || otherEntry) {
519
+ // A fork in the local themes root carries its folder's name.
520
+ if (
521
+ (otherDescriptor &&
522
+ otherDescriptor !== theme.slug &&
523
+ otherDescriptor !== path.basename(dir)) ||
524
+ otherEntry
525
+ ) {
499
526
  throw new AstryxError(
500
527
  `${rel} holds another theme (${name}). Eject does not mix two themes in one folder, even with --overwrite.`,
501
528
  suggestion,
@@ -505,6 +532,98 @@ function assertEjectTargetFree(theme, dir, cwd, projectDir) {
505
532
  }
506
533
  }
507
534
 
535
+ /**
536
+ * Refuse a fork target that `theme list` and `theme add` would then misread:
537
+ * the local themes folder itself, where every theme has its own folder, or a
538
+ * folder in it named after another theme the project can use.
539
+ * @param {string} slug
540
+ * @param {string} dir absolute target
541
+ * @param {string} projectDir
542
+ * @param {string} cwd
543
+ * @param {Set<string>} takenSlugs the slugs of the themes the project can use
544
+ * @param {boolean} [sameFork] whether the folder holds no fork or a fork of
545
+ * this theme
546
+ */
547
+ function assertEjectTargetPlace(
548
+ slug,
549
+ dir,
550
+ projectDir,
551
+ cwd,
552
+ takenSlugs,
553
+ sameFork = true,
554
+ ) {
555
+ const themesRoot = path.resolve(projectDir, LOCAL_THEME_ROOT);
556
+ const resolved = path.resolve(dir);
557
+ const own = defaultEjectTarget(slug).split(path.sep).join('/');
558
+ const suggestion = [
559
+ {
560
+ name: formatCliCommand(`theme eject ${slug}`, cwd),
561
+ reason: `fork into ${own}`,
562
+ },
563
+ ];
564
+ if (resolved === themesRoot) {
565
+ throw new AstryxError(
566
+ `${path.relative(cwd, dir) || '.'} is the folder that holds the local themes, each in its own folder. Fork ${slug} into ${own}.`,
567
+ suggestion,
568
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
569
+ );
570
+ }
571
+ // A fork may take a new name; only another theme's name is refused.
572
+ const name = path.basename(resolved);
573
+ // The theme of that name may be the one in this very folder (a re-fork,
574
+ // which the folder checks decide).
575
+ const ownFolder = () => {
576
+ try {
577
+ return fs
578
+ .readdirSync(resolved)
579
+ .some(
580
+ file =>
581
+ file.endsWith('.doc.mjs') &&
582
+ themeDescriptorName(path.join(resolved, file)) === name,
583
+ );
584
+ } catch {
585
+ return false;
586
+ }
587
+ };
588
+ if (
589
+ path.dirname(resolved) === themesRoot &&
590
+ name !== slug &&
591
+ takenSlugs.has(name) &&
592
+ !(sameFork && ownFolder())
593
+ ) {
594
+ throw new AstryxError(
595
+ `${path.relative(cwd, dir)} is named after the theme ${name}, and a local theme's folder carries its name. Fork ${slug} into ${own}, or into a folder with a new name.`,
596
+ suggestion,
597
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
598
+ );
599
+ }
600
+ }
601
+
602
+ /**
603
+ * Whether a folder holds no fork, or a fork of `theme`: its descriptor
604
+ * carries the theme's display name, as eject writes it.
605
+ * @param {string} dir
606
+ * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
607
+ */
608
+ function holdsForkOf(dir, theme) {
609
+ let docs;
610
+ try {
611
+ docs = fs.readdirSync(dir).filter(file => file.endsWith('.doc.mjs'));
612
+ } catch {
613
+ return true;
614
+ }
615
+ return docs.every(file => {
616
+ try {
617
+ const text = fs.readFileSync(path.join(dir, file), 'utf-8');
618
+ return (
619
+ /\bdisplayName:\s*(['"])(.*?)\1/.exec(text)?.[2] === theme.displayName
620
+ );
621
+ } catch {
622
+ return false;
623
+ }
624
+ });
625
+ }
626
+
508
627
  /**
509
628
  * The `name` of a theme descriptor, read as text so a broken file cannot run.
510
629
  * @param {string} file
@@ -523,14 +642,23 @@ function themeDescriptorName(file) {
523
642
  /**
524
643
  * Publish one theme's complete source inventory as an atomic local fork.
525
644
  * @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} theme
526
- * @param {{cwd: string, targetPath?: string, overwrite?: boolean}} options
527
- * @returns {string} project-relative output directory
645
+ * @param {{cwd: string, targetPath?: string, overwrite?: boolean, takenSlugs?: Set<string>}} options
646
+ * `takenSlugs`: the slugs of the themes the project can use, which a fork's
647
+ * new folder name may not take
648
+ * @returns {{outputDir: string, files: string[]}} the project-relative output
649
+ * directory and the files written, named as the fork names them
528
650
  */
529
- export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
651
+ export function ejectThemeFiles(
652
+ theme,
653
+ {cwd, targetPath: typedPath, overwrite = false, takenSlugs = new Set()},
654
+ ) {
530
655
  // The fork lands in the project, as list and add read it: the default root
531
656
  // is the project's, and a relative [path] is read from the working folder
532
657
  // but must stay inside the project.
533
658
  const projectDir = findPackageRoot(cwd) ?? path.resolve(cwd);
659
+ // A Windows-style path names the same folders on every platform, as
660
+ // compile reads it; a folder name with a backslash in it is never meant.
661
+ const targetPath = typedPath?.replace(/\\/g, '/');
534
662
  const target = !targetPath
535
663
  ? defaultEjectTarget(theme.slug)
536
664
  : path.isAbsolute(targetPath)
@@ -559,13 +687,33 @@ export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
559
687
  throw error;
560
688
  }
561
689
 
690
+ // A fork in the local themes root is named after its folder, as `theme
691
+ // list` and `theme add` read it; elsewhere it keeps the theme's name.
692
+ const inThemesRoot =
693
+ path.dirname(path.resolve(resolvedDir)) ===
694
+ path.resolve(projectDir, LOCAL_THEME_ROOT);
695
+ const forkName = inThemesRoot ? path.basename(resolvedDir) : theme.slug;
696
+ if (inThemesRoot && !THEME_SLUG_RE.test(forkName)) {
697
+ throw new AstryxError(
698
+ `A local theme's folder is its name, which is lowercase kebab-case starting with a letter; ${forkName} is not.`,
699
+ [
700
+ {
701
+ name: formatCliCommand(`theme eject ${theme.slug}`, cwd),
702
+ reason: `fork into ${defaultEjectTarget(theme.slug).split(path.sep).join('/')}`,
703
+ },
704
+ ],
705
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
706
+ );
707
+ }
708
+
709
+ const names = forkFileNames(theme, forkName);
562
710
  let writes;
563
711
  try {
564
- writes = theme.files.map(name => ({
565
- name,
566
- src: path.join(theme.sourceDir, name),
567
- dest: assertWithin(name, resolvedDir, {
568
- label: `theme destination for ${name}`,
712
+ writes = theme.files.map((src, index) => ({
713
+ name: names[index],
714
+ src: path.join(theme.sourceDir, src),
715
+ dest: assertWithin(names[index], resolvedDir, {
716
+ label: `theme destination for ${names[index]}`,
569
717
  }),
570
718
  }));
571
719
  } catch (error) {
@@ -590,8 +738,24 @@ export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
590
738
  }
591
739
  }
592
740
 
593
- assertEjectTargetFree(theme, resolvedDir, cwd, projectDir);
594
-
741
+ // The fork's own names count as its files only in a folder that holds
742
+ // no fork or a fork of this theme.
743
+ const sameFork = holdsForkOf(resolvedDir, theme);
744
+ assertEjectTargetFree(
745
+ theme,
746
+ resolvedDir,
747
+ cwd,
748
+ projectDir,
749
+ sameFork ? names : [],
750
+ );
751
+ assertEjectTargetPlace(
752
+ theme.slug,
753
+ resolvedDir,
754
+ projectDir,
755
+ cwd,
756
+ takenSlugs,
757
+ sameFork,
758
+ );
595
759
  if (!overwrite) {
596
760
  const existing = writes.find(write => fs.existsSync(write.dest));
597
761
  if (existing) {
@@ -621,6 +785,17 @@ export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
621
785
  }
622
786
  }
623
787
 
788
+ // A fork under another name renames its entry first, so an entry that
789
+ // names no theme is refused before any folder is made.
790
+ const entrySrc = path.resolve(theme.sourceDir, theme.files[0]);
791
+ const renamed =
792
+ forkName !== theme.slug
793
+ ? renamedEntry(
794
+ String(scaffoldContents(fs.readFileSync(entrySrc))),
795
+ theme,
796
+ forkName,
797
+ )
798
+ : null;
624
799
  try {
625
800
  fs.mkdirSync(resolvedDir, {recursive: true});
626
801
  const plans = writes.map(write => {
@@ -632,8 +807,10 @@ export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
632
807
  path: dest,
633
808
  contents:
634
809
  path.resolve(write.src) === path.resolve(theme.docPath)
635
- ? localThemeDescriptor(theme)
636
- : scaffoldContents(fs.readFileSync(write.src)),
810
+ ? localThemeDescriptor(theme, forkName)
811
+ : renamed != null && path.resolve(write.src) === entrySrc
812
+ ? renamed
813
+ : scaffoldContents(fs.readFileSync(write.src)),
637
814
  createOnly: !overwrite,
638
815
  };
639
816
  });
@@ -653,7 +830,7 @@ export function ejectThemeFiles(theme, {cwd, targetPath, overwrite = false}) {
653
830
  throw new AstryxError(message, undefined, ERROR_CODES.ERR_WRITE_FAILED);
654
831
  }
655
832
 
656
- return path.relative(cwd, resolvedDir) || '.';
833
+ return {outputDir: path.relative(cwd, resolvedDir) || '.', files: names};
657
834
  }
658
835
 
659
836
  /**
@@ -692,7 +869,9 @@ function asThemeError(error) {
692
869
  ? ERROR_CODES.ERR_FILE_EXISTS
693
870
  : ERROR_CODES.ERR_THEME_INVALID;
694
871
  const suggestions =
695
- error instanceof ThemeStateError ? error.suggestions : undefined;
872
+ error instanceof ThemeStateError || error instanceof ThemeImportError
873
+ ? error.suggestions
874
+ : undefined;
696
875
  return new AstryxError(message, suggestions, code);
697
876
  }
698
877
 
@@ -716,7 +895,7 @@ export async function findTheme(slug, options = {}) {
716
895
  const matches = themes.filter(
717
896
  theme =>
718
897
  theme.slug.toLowerCase() === normalized &&
719
- (options.package == null || theme.package === options.package),
898
+ themeInPackage(theme, options.package),
720
899
  );
721
900
  const broken = skipped.find(entry => entry.slug.toLowerCase() === normalized);
722
901
  if (
@@ -0,0 +1,90 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * A theme name as the identifier `theme build` gives its export: each run
6
+ * of dashes goes, and the letter after it is capitalized.
7
+ * @param {string} name
8
+ */
9
+ export function themeIdentifier(name: string): string;
10
+ /**
11
+ * The binding a theme named `name` exports, as `theme build` names it.
12
+ * @param {string} name
13
+ */
14
+ export function themeBinding(name: string): string;
15
+ /**
16
+ * A new theme's names, from a checked kebab-case slug.
17
+ * @param {string} slug
18
+ */
19
+ export function themeIdentity(slug: string): {
20
+ slug: string;
21
+ identifier: string;
22
+ displayName: string;
23
+ exportName: string;
24
+ entry: string;
25
+ descriptor: string;
26
+ };
27
+ /**
28
+ * Rename a file from the base theme's namespace to the new theme's namespace.
29
+ * Files whose name starts with the base identifier get the new identifier;
30
+ * others keep their name as-is.
31
+ * @param {string} file POSIX relative path inside the theme directory
32
+ * @param {string} baseIdentifier
33
+ * @param {string} newIdentifier
34
+ * @returns {string}
35
+ */
36
+ export function renameThemeFile(file: string, baseIdentifier: string, newIdentifier: string): string;
37
+ /**
38
+ * Rewrite a source file's contents from the base theme's names to the new
39
+ * theme's names. This replaces:
40
+ * 1. Identifier prefixes (camelCase variables, import paths): baseIdentifier
41
+ * followed by an uppercase letter.
42
+ * 2. The theme slug in string literals: 'baseSlug' → 'newSlug' and
43
+ * 'astryx-baseSlug' → 'astryx-newSlug'.
44
+ * 3. CSS custom properties scoped to the theme: --astryx-theme-baseSlug-.
45
+ * @param {string} source
46
+ * @param {string} baseSlug
47
+ * @param {string} baseIdentifier
48
+ * @param {string} newSlug
49
+ * @param {string} newIdentifier
50
+ * @returns {string}
51
+ */
52
+ export function rewriteThemeSource(source: string, baseSlug: string, baseIdentifier: string, newSlug: string, newIdentifier: string): string;
53
+ /**
54
+ * The theme source with its `defineTheme({...})` name set to `to`, wherever
55
+ * the `name` property sits in the object (after `extends`, under a
56
+ * comment), or null when no `defineTheme` call names a theme. Only that
57
+ * one string changes.
58
+ * @param {string} source
59
+ * @param {string} to
60
+ * @returns {string | null}
61
+ */
62
+ export function renameDefinedTheme(source: string, to: string): string | null;
63
+ /**
64
+ * The file names of a fork: the theme's own, except that a fork named other
65
+ * than the theme names its entry and descriptor after its binding
66
+ * (`acmeOceanTheme.ts`, `acmeOceanTheme.doc.mjs`), as discovery reads them.
67
+ * @param {{slug: string, docPath: string, files: string[]}} theme
68
+ * @param {string} forkName
69
+ * @returns {string[]} in the order of theme.files
70
+ */
71
+ export function forkFileNames(theme: {
72
+ slug: string;
73
+ docPath: string;
74
+ files: string[];
75
+ }, forkName: string): string[];
76
+ /**
77
+ * The theme entry renamed for a fork: its `defineTheme` name is the fork's,
78
+ * so the build writes `<fork>.css` and `<fork>.js`, and its export binding is
79
+ * the build's (`acmeOceanTheme`), the name discovery and `theme add` read.
80
+ * Refused when the entry names no theme to rename, since the fork would
81
+ * then build under the base theme's name.
82
+ * @param {string} contents
83
+ * @param {{slug: string, docPath: string}} theme
84
+ * @param {string} to
85
+ * @returns {string}
86
+ */
87
+ export function renamedEntry(contents: string, theme: {
88
+ slug: string;
89
+ docPath: string;
90
+ }, to: string): string;