@astryxdesign/cli 0.7.14 → 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 (203) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/README.md +63 -63
  3. package/api/build/_adapter.d.mts +4 -0
  4. package/api/build/_adapter.mjs +28 -2
  5. package/api/build/blocks/blocks.d.mts +10 -1
  6. package/api/build/blocks/blocks.mjs +84 -7
  7. package/api/build/build.mjs +6 -1
  8. package/api/build/tree.d.mts +4 -0
  9. package/api/build/tree.mjs +9 -2
  10. package/api/doctor/_adapter.d.mts +3 -1
  11. package/api/doctor/_adapter.mjs +14 -1
  12. package/api/doctor/_authoring-audit.d.mts +42 -0
  13. package/api/doctor/_authoring-audit.mjs +106 -0
  14. package/api/doctor/doctor.d.mts +7 -0
  15. package/api/doctor/doctor.mjs +24 -39
  16. package/api/doctor/theme-checks.mjs +56 -21
  17. package/api/integration/add-helpers.mjs +5 -0
  18. package/api/integration/add-theme.mjs +15 -110
  19. package/api/integration/validate-integration.mjs +17 -3
  20. package/api/integration/validateIntegration.doc.mjs +1 -1
  21. package/api/search/_adapter.mjs +124 -3
  22. package/api/search/_candidate-score.mjs +63 -9
  23. package/api/search/_entries.d.mts +10 -0
  24. package/api/search/_entries.mjs +68 -1
  25. package/api/search/_gather-templates.d.mts +11 -0
  26. package/api/search/_gather-templates.mjs +22 -2
  27. package/api/search/_words.mjs +26 -6
  28. package/api/swizzle/copy/copy.mjs +1 -1
  29. package/api/theme/_adapter.d.mts +37 -5
  30. package/api/theme/_adapter.mjs +381 -57
  31. package/api/theme/_fork.d.mts +90 -0
  32. package/api/theme/_fork.mjs +246 -0
  33. package/api/theme/build/build.d.mts +40 -2
  34. package/api/theme/build/build.mjs +477 -66
  35. package/api/theme/eject/eject.mjs +36 -15
  36. package/api/theme/list/list.mjs +3 -6
  37. package/api/theme/palette/generate/generator.d.mts +13 -0
  38. package/api/theme/palette/generate/generator.mjs +32 -2
  39. package/api/theme/targets/targets.d.mts +8 -0
  40. package/api/theme/targets/targets.mjs +24 -4
  41. package/api/theme/template/template.mjs +48 -2
  42. package/api/theme/theme.type.d.mts +8 -0
  43. package/api/theme/theme.type.mjs +5 -1
  44. package/api/theme/themeBuild.doc.mjs +7 -4
  45. package/api/theme/themeEject.doc.mjs +3 -3
  46. package/api/theme/themeRemove.doc.mjs +5 -2
  47. package/api/theme/themeTargets.doc.mjs +1 -1
  48. package/api/theme/themeTemplate.doc.mjs +2 -2
  49. package/api/theme/themeUse.doc.mjs +8 -2
  50. package/api/theme-edit/_adapter.d.mts +46 -3
  51. package/api/theme-edit/_adapter.mjs +496 -112
  52. package/api/theme-edit/_catalog.d.mts +1 -6
  53. package/api/theme-edit/_catalog.mjs +5 -268
  54. package/api/theme-edit/_check.d.mts +9 -4
  55. package/api/theme-edit/_check.mjs +1232 -229
  56. package/api/theme-edit/_color.d.mts +19 -0
  57. package/api/theme-edit/_color.mjs +280 -34
  58. package/api/theme-edit/_compiler.d.mts +3 -1
  59. package/api/theme-edit/_compiler.mjs +52 -13
  60. package/api/theme-edit/_context.d.mts +4 -1
  61. package/api/theme-edit/_context.mjs +126 -13
  62. package/api/theme-edit/_css.d.mts +84 -0
  63. package/api/theme-edit/_css.mjs +700 -0
  64. package/api/theme-edit/_edit.d.mts +9 -3
  65. package/api/theme-edit/_edit.mjs +806 -103
  66. package/api/theme-edit/_how-commands.d.mts +38 -7
  67. package/api/theme-edit/_how-commands.mjs +64 -45
  68. package/api/theme-edit/_how-question.d.mts +33 -0
  69. package/api/theme-edit/_how-question.mjs +2553 -381
  70. package/api/theme-edit/_how.mjs +402 -31
  71. package/api/theme-edit/_overview.mjs +2 -7
  72. package/api/theme-edit/_palette.d.mts +60 -13
  73. package/api/theme-edit/_palette.mjs +826 -295
  74. package/api/theme-edit/_parts.d.mts +42 -10
  75. package/api/theme-edit/_parts.mjs +280 -69
  76. package/api/theme-edit/_png.d.mts +4 -4
  77. package/api/theme-edit/_png.mjs +4 -2
  78. package/api/theme-edit/_preview-app.d.mts +14 -0
  79. package/api/theme-edit/_preview-app.mjs +1675 -235
  80. package/api/theme-edit/_preview.d.mts +7 -16
  81. package/api/theme-edit/_preview.mjs +79 -86
  82. package/api/theme-edit/_project.d.mts +32 -2
  83. package/api/theme-edit/_project.mjs +80 -19
  84. package/api/theme-edit/_settings.d.mts +199 -52
  85. package/api/theme-edit/_settings.mjs +1422 -217
  86. package/api/theme-edit/_tables.d.mts +19 -6
  87. package/api/theme-edit/_tables.mjs +150 -92
  88. package/api/theme-edit/theme-edit.type.d.mts +20 -1
  89. package/api/theme-edit/theme-edit.type.mjs +6 -3
  90. package/api/theme-edit/themeEditHow.doc.mjs +6 -0
  91. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  92. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  93. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  94. package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
  95. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +5 -1
  96. package/api/theme-edit/themeEditSet.doc.mjs +1 -1
  97. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  98. package/api/upgrade/_adapter.d.mts +1 -0
  99. package/assets/codemods/run-codemod.mjs +61 -0
  100. package/assets/codemods/runner.mjs +5 -4
  101. package/assets/codemods/transforms/v0.0.14/rename-status-variants.mjs +3 -0
  102. package/assets/codemods/transforms/v0.1.8/rename-avatar-size-scale.mjs +5 -1
  103. package/assets/codemods/transforms/v0.6.4/migrate-native-picker-to-presentation.mjs +3 -0
  104. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +4 -0
  105. package/assets/codemods/transforms/v0.7.0/rename-empty-search-results-text.mjs +17 -12
  106. package/assets/codemods/transforms/v0.7.2/migrate-list-edge-compensation-to-edge.mjs +3 -0
  107. package/assets/codemods/transforms/v0.7.5/migrate-codeblock-tokenizer-to-language.mjs +3 -0
  108. package/assets/codemods/transforms/v0.7.8/promote-tokenizer-presentation.mjs +3 -0
  109. package/assets/docs/tree/calendars.doc.mjs +1 -3
  110. package/assets/docs/tree/checks.doc.mjs +22 -1
  111. package/assets/docs/tree/publishing.doc.mjs +1 -0
  112. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  113. package/assets/docs.generated.json +2 -1
  114. package/assets/preview-app/next.config.mjs +7 -1
  115. package/assets/self-audit.generated.json +1 -0
  116. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.doc.mjs +14 -0
  117. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.tsx +31 -0
  118. package/assets/templates/components.generated.json +1 -1
  119. package/authoring/integration/integration.doc.mjs +8 -0
  120. package/authoring/integration/schema.d.mts +8 -0
  121. package/authoring/integration/schema.mjs +1 -0
  122. package/authoring/integration/type.ts +9 -0
  123. package/clients/cli/bin/astryx.mjs +19 -15
  124. package/clients/cli/build-id.generated.json +1 -0
  125. package/clients/cli/commands/ensure-core-built.mjs +12 -5
  126. package/clients/cli/commands/experimental/index.mjs +10 -10
  127. package/clients/cli/commands/experimental/theme.mjs +200 -81
  128. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  129. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +1 -1
  130. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
  131. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -2
  132. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  133. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  134. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  135. package/clients/cli/commands/experimental-theme-eject.doc.mjs +3 -3
  136. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  137. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  138. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +1 -1
  139. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +1 -1
  140. package/clients/cli/commands/experimental-theme-remove.doc.mjs +1 -1
  141. package/clients/cli/commands/experimental-theme-template.doc.mjs +4 -3
  142. package/clients/cli/commands/integration.mjs +8 -0
  143. package/clients/cli/commands/manifest.mjs +5 -2
  144. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  145. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  146. package/clients/cli/commands/theme-template.doc.mjs +1 -1
  147. package/clients/cli/commands/theme.mjs +439 -66
  148. package/clients/cli/index.mjs +27 -6
  149. package/clients/cli/lib/cli-error.mjs +118 -3
  150. package/clients/cli/lib/manifest.mjs +37 -0
  151. package/clients/cli/lib/resolve-theme.mjs +2 -1
  152. package/clients/cli/manifest.generated.json +1 -0
  153. package/foundation/config/project.mjs +34 -8
  154. package/foundation/config/theme-state.d.mts +16 -0
  155. package/foundation/config/theme-state.mjs +84 -13
  156. package/foundation/discovery/module-exports.mjs +16 -2
  157. package/foundation/discovery/stored-self-audit.d.mts +31 -0
  158. package/foundation/discovery/stored-self-audit.mjs +119 -0
  159. package/foundation/discovery/template-adapter.d.mts +48 -2
  160. package/foundation/discovery/template-adapter.mjs +51 -19
  161. package/foundation/discovery/template-needs.d.mts +19 -0
  162. package/foundation/discovery/template-needs.mjs +50 -16
  163. package/foundation/discovery/template-source-tree.d.mts +7 -0
  164. package/foundation/discovery/template-source-tree.mjs +15 -0
  165. package/foundation/discovery/theme-discovery.d.mts +2 -1
  166. package/foundation/discovery/theme-discovery.mjs +16 -10
  167. package/foundation/discovery/theme-imports.d.mts +16 -0
  168. package/foundation/discovery/theme-imports.mjs +30 -2
  169. package/foundation/discovery/theme-module-extensions.d.mts +18 -0
  170. package/foundation/discovery/theme-module-extensions.mjs +30 -0
  171. package/foundation/doc-compiler/import.mjs +22 -15
  172. package/foundation/doc-compiler/stored-docs.d.mts +18 -12
  173. package/foundation/doc-compiler/stored-docs.mjs +52 -19
  174. package/foundation/env/private-dir.d.mts +17 -0
  175. package/foundation/env/private-dir.mjs +42 -0
  176. package/foundation/fs/gather-cache.d.mts +108 -0
  177. package/foundation/fs/gather-cache.mjs +783 -0
  178. package/foundation/fs/jiti-cache.d.mts +21 -0
  179. package/foundation/fs/jiti-cache.mjs +66 -0
  180. package/foundation/fs/module-loader.mjs +14 -3
  181. package/foundation/fs/read-record.d.mts +31 -1
  182. package/foundation/fs/read-record.mjs +100 -7
  183. package/foundation/integrations/autolink.d.mts +1 -24
  184. package/foundation/integrations/autolink.mjs +7 -60
  185. package/foundation/integrations/declared-dependencies.d.mts +27 -0
  186. package/foundation/integrations/declared-dependencies.mjs +73 -0
  187. package/foundation/integrations/integrations.d.mts +8 -0
  188. package/foundation/integrations/integrations.mjs +4 -0
  189. package/foundation/integrations/may-have-integrations.d.mts +8 -0
  190. package/foundation/integrations/may-have-integrations.mjs +47 -0
  191. package/foundation/integrations/validate-contributions.d.mts +62 -1
  192. package/foundation/integrations/validate-contributions.mjs +476 -40
  193. package/foundation/release/cache.d.mts +32 -4
  194. package/foundation/release/cache.mjs +105 -16
  195. package/foundation/release/folder-lock.d.mts +28 -0
  196. package/foundation/release/folder-lock.mjs +205 -0
  197. package/foundation/release/lock-lease.d.mts +2 -1
  198. package/foundation/release/lock-lease.mjs +7 -2
  199. package/foundation/response/error-codes.doc.mjs +1 -1
  200. package/foundation/response/response-types.doc.mjs +5 -5
  201. package/package.json +8 -6
  202. package/api/theme-edit/_css-properties.d.mts +0 -10
  203. package/api/theme-edit/_css-properties.mjs +0 -556
@@ -18,7 +18,12 @@
18
18
  import * as fs from 'node:fs';
19
19
  import * as path from 'node:path';
20
20
  import module from 'node:module';
21
- import {parseTemplateSource, walk} from './template-source-tree.mjs';
21
+ import {
22
+ parseTemplateSource,
23
+ TEMPLATE_COMPONENTS_FILE,
24
+ walk,
25
+ } from './template-source-tree.mjs';
26
+ import {readStore, storedKey} from '../doc-compiler/stored-docs.mjs';
22
27
  import {CLI_ROOT, findWorkspaceRoot} from '../fs/paths.mjs';
23
28
  import {detectStylingSystem} from '../agent-docs/agent-docs.mjs';
24
29
  import {
@@ -81,6 +86,20 @@ export function extractImportedPackages(source) {
81
86
  * @returns {string[]}
82
87
  */
83
88
  function moduleSpecifiers(source) {
89
+ // A template Astryx ships answers from what parsing it returned at publish.
90
+ const stored = readStore(TEMPLATE_COMPONENTS_FILE).get(
91
+ `imports:${storedKey(source)}`,
92
+ );
93
+ if (Array.isArray(stored)) return [...stored];
94
+ return parsedSpecifiers(source);
95
+ }
96
+
97
+ /**
98
+ * moduleSpecifiers, read from the source's syntax tree.
99
+ * @param {string} source
100
+ * @returns {string[]}
101
+ */
102
+ export function parsedSpecifiers(source) {
84
103
  let file;
85
104
  try {
86
105
  file = parseTemplateSource(source, {errorRecovery: true});
@@ -226,6 +245,35 @@ function cliPackageRanges() {
226
245
  }
227
246
  }
228
247
 
248
+ /**
249
+ * The command that installs `packages` in the project at `cwd`, in its package
250
+ * manager and with the CLI workspace's version ranges. Null, with the reason,
251
+ * when several lockfiles leave the package manager undetermined; null with no
252
+ * reason when there is nothing to install.
253
+ * @param {string[]} packages
254
+ * @param {string} cwd
255
+ * @returns {{installCommand: string | null, noCommandReason: string | null}}
256
+ */
257
+ export function installCommandFor(packages, cwd) {
258
+ if (packages.length === 0) {
259
+ return {installCommand: null, noCommandReason: null};
260
+ }
261
+ const resolved = resolveInstallVerb(cwd);
262
+ const ranges = cliPackageRanges();
263
+ const specs = packages.map(p => (ranges[p] ? `${p}@"${ranges[p]}"` : p));
264
+ if (resolved.verb !== null) {
265
+ return {
266
+ installCommand: `${resolved.verb} ${specs.join(' ')}`,
267
+ noCommandReason: null,
268
+ };
269
+ }
270
+ const where = path.relative(cwd, resolved.dir) || '.';
271
+ return {
272
+ installCommand: null,
273
+ noCommandReason: `Install missing dependencies: ${specs.join(' ')}. No install command: ${where} has ${resolved.lockfiles.join(' and ')}, and nothing there says which package manager to use.`,
274
+ };
275
+ }
276
+
229
277
  /**
230
278
  * Analyze what a template source needs that the target project lacks.
231
279
  *
@@ -266,21 +314,7 @@ export function analyzeTemplateNeeds(source, cwd) {
266
314
 
267
315
  const missing = external.filter(p => !(p in projectDeps));
268
316
 
269
- // Build the install command with version ranges from the CLI workspace.
270
- let installCommand = null;
271
- /** @type {string | null} */
272
- let noCommandReason = null;
273
- if (missing.length > 0) {
274
- const resolved = resolveInstallVerb(cwd);
275
- const ranges = cliPackageRanges();
276
- const specs = missing.map(p => (ranges[p] ? `${p}@"${ranges[p]}"` : p));
277
- if (resolved.verb !== null) {
278
- installCommand = `${resolved.verb} ${specs.join(' ')}`;
279
- } else {
280
- const where = path.relative(cwd, resolved.dir) || '.';
281
- noCommandReason = `Install missing dependencies: ${specs.join(' ')}. No install command: ${where} has ${resolved.lockfiles.join(' and ')}, and nothing there says which package manager to use.`;
282
- }
283
- }
317
+ const {installCommand, noCommandReason} = installCommandFor(missing, cwd);
284
318
 
285
319
  // ── StyleX compiler ────────────────────────────────────────────────────
286
320
  const usesStylex = imported.has(STYLEX_RUNTIME);
@@ -50,3 +50,10 @@ export function isReference(node: any, parent: any, key: string): any;
50
50
  * @returns {Set<string>}
51
51
  */
52
52
  export function writtenNames(file: any): Set<string>;
53
+ /**
54
+ * Where what parsing returns for each template Astryx ships is stored at
55
+ * publish (scripts/generate-template-components.mjs): its component list
56
+ * under its source's key, and the module specifiers it imports under
57
+ * `imports:` and that key.
58
+ */
59
+ export const TEMPLATE_COMPONENTS_FILE: string;
@@ -12,6 +12,21 @@
12
12
  */
13
13
 
14
14
  import {createRequire} from 'node:module';
15
+ import * as path from 'node:path';
16
+ import {CLI_ROOT} from '../fs/paths.mjs';
17
+
18
+ /**
19
+ * Where what parsing returns for each template Astryx ships is stored at
20
+ * publish (scripts/generate-template-components.mjs): its component list
21
+ * under its source's key, and the module specifiers it imports under
22
+ * `imports:` and that key.
23
+ */
24
+ export const TEMPLATE_COMPONENTS_FILE = path.join(
25
+ CLI_ROOT,
26
+ 'assets',
27
+ 'templates',
28
+ 'components.generated.json',
29
+ );
15
30
 
16
31
  const require = createRequire(import.meta.url);
17
32
 
@@ -147,7 +147,7 @@ export function discoverIntegrationThemes(integration: import("../integrations/i
147
147
  export const BUNDLED_THEME_PACKAGE: "@astryxdesign/cli";
148
148
  export const THEMES_DIR: string;
149
149
  export const THEME_DOC_SUFFIX: ".doc.mjs";
150
- export const THEME_MODULE_EXTENSIONS: string[];
150
+ export { THEME_MODULE_EXTENSIONS };
151
151
  /** A theme directory's name: lower-kebab, starting with a letter. */
152
152
  export const THEME_SLUG_RE: RegExp;
153
153
  export type DiscoveredTheme = {
@@ -196,3 +196,4 @@ export type UnmigratedThemeCopy = {
196
196
  exportName: string;
197
197
  descriptor: string;
198
198
  };
199
+ import { THEME_MODULE_EXTENSIONS } from './theme-module-extensions.mjs';
@@ -26,6 +26,7 @@ import {listDirEntries} from '../fs/listing-cache.mjs';
26
26
  import {formatCliCommand} from '../env/package-manager.mjs';
27
27
  import {LOCAL_THEME_ROOT} from '../config/theme-state.mjs';
28
28
  import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
29
+ import {THEME_MODULE_EXTENSIONS} from './theme-module-extensions.mjs';
29
30
 
30
31
  export const BUNDLED_THEME_PACKAGE = '@astryxdesign/cli';
31
32
  export const THEMES_DIR = path.join(CLI_ROOT, 'assets', 'templates', 'themes');
@@ -114,14 +115,7 @@ function resolveThemePath(value, root, label) {
114
115
  }
115
116
  }
116
117
 
117
- export const THEME_MODULE_EXTENSIONS = [
118
- '.mjs',
119
- '.js',
120
- '.mts',
121
- '.ts',
122
- '.tsx',
123
- '.jsx',
124
- ];
118
+ export {THEME_MODULE_EXTENSIONS};
125
119
 
126
120
  /** A theme directory's name: lower-kebab, starting with a letter. */
127
121
  export const THEME_SLUG_RE = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u;
@@ -1378,8 +1372,20 @@ export function discoverThemeDirectory(
1378
1372
  entry => entry.isFile() && entry.name.endsWith(THEME_DOC_SUFFIX),
1379
1373
  );
1380
1374
  if (rootDescriptor) {
1375
+ const stem = rootDescriptor.name
1376
+ .slice(0, -THEME_DOC_SUFFIX.length)
1377
+ .replace(/Theme$/, '');
1378
+ // The folder is under the theme root on disk. The owner names that
1379
+ // root only for the project's own local themes; a package's root is
1380
+ // named by its path.
1381
+ const slug =
1382
+ stem.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase() || '<slug>';
1383
+ const folder =
1384
+ owner === LOCAL_THEME_ROOT
1385
+ ? `${owner}/${slug}`
1386
+ : path.join(themeRoot, slug).split(path.sep).join('/');
1381
1387
  throw new Error(
1382
- `Theme descriptor "${rootDescriptor.name}" for ${owner} must be inside a lower-kebab theme directory.`,
1388
+ `Theme descriptor "${rootDescriptor.name}" for ${owner} must be inside a lower-kebab theme directory. Move that theme's files into their own folder, such as ${folder}/.`,
1383
1389
  );
1384
1390
  }
1385
1391
  const rootSymlink = rootEntries.find(entry => entry.isSymbolicLink());
@@ -1490,7 +1496,7 @@ export function discoverThemeDirectory(
1490
1496
  const doc = readThemeDoc(docPath, owner);
1491
1497
  if (doc.name !== slug) {
1492
1498
  throw new Error(
1493
- `${label} names "${doc.name}" but its directory is "${slug}".`,
1499
+ `${label} names "${doc.name}" but its directory is "${slug}". A theme's folder carries its name: move its files into a folder named "${doc.name}".`,
1494
1500
  );
1495
1501
  }
1496
1502
  const entry = sources[0];
@@ -12,4 +12,20 @@ export function resolveThemeImports(theme: import("./theme-discovery.mjs").Disco
12
12
  ownerThemeCount?: number;
13
13
  }): import("../config/theme-state.mjs").ResolvedAppTheme;
14
14
  export class ThemeImportError extends Error {
15
+ /**
16
+ * @param {string} message
17
+ * @param {{cause?: unknown, suggestions?: Array<{name: string, reason: string}>}} [options]
18
+ */
19
+ constructor(message: string, options?: {
20
+ cause?: unknown;
21
+ suggestions?: Array<{
22
+ name: string;
23
+ reason: string;
24
+ }>;
25
+ });
26
+ /** Commands that move past the refusal. */
27
+ suggestions: {
28
+ name: string;
29
+ reason: string;
30
+ }[];
15
31
  }
@@ -20,8 +20,22 @@ import {inspectPackageExport} from './package-exports.mjs';
20
20
  import {themeImportPackage} from './theme-discovery.mjs';
21
21
  import {isLocalThemeOwner, projectPath} from '../config/theme-state.mjs';
22
22
  import {formatCliCommand} from '../env/package-manager.mjs';
23
+ import {installCommandFor} from './template-needs.mjs';
23
24
 
24
- export class ThemeImportError extends Error {}
25
+ export class ThemeImportError extends Error {
26
+ /**
27
+ * @param {string} message
28
+ * @param {{cause?: unknown, suggestions?: Array<{name: string, reason: string}>}} [options]
29
+ */
30
+ constructor(message, options = {}) {
31
+ super(
32
+ message,
33
+ options.cause === undefined ? undefined : {cause: options.cause},
34
+ );
35
+ /** Commands that move past the refusal. */
36
+ this.suggestions = options.suggestions ?? [];
37
+ }
38
+ }
25
39
 
26
40
  /** @param {string} file */
27
41
  function readPackage(file) {
@@ -133,8 +147,22 @@ function resolvePackageTheme(theme, cwd, ownerThemeCount) {
133
147
  /** @type {any} */ (theme).packageDir ??
134
148
  findInstalledPackage(cwd, importPackage);
135
149
  if (!packageDir) {
150
+ // The app imports a theme from its package, bundled themes included, so
151
+ // the package is installed in the app before the theme is added.
152
+ const {installCommand} = installCommandFor([importPackage], cwd);
153
+ const add = formatCliCommand(`theme add ${theme.slug}`, cwd);
154
+ const install = installCommand ?? `npm install ${importPackage}`;
136
155
  throw new ThemeImportError(
137
- `Theme package "${importPackage}" is not installed. Run \`npm install ${importPackage}\` (or yarn/pnpm/bun), then run \`${formatCliCommand(`theme add ${theme.slug}`, cwd)}\`.`,
156
+ `Theme package "${importPackage}" is not installed. The app imports ${theme.slug} from it, so install it, then add the theme: \`${install}\`, then \`${add}\`.`,
157
+ {
158
+ suggestions: [
159
+ {
160
+ name: install,
161
+ reason: `install ${importPackage}, which the app imports`,
162
+ },
163
+ {name: add, reason: 'add the theme once it is installed'},
164
+ ],
165
+ },
138
166
  );
139
167
  }
140
168
  const pkg = readPackage(path.join(packageDir, 'package.json'));
@@ -0,0 +1,18 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * @file The file extensions a theme source module can have.
6
+ *
7
+ * @input none
8
+ * @output THEME_MODULE_EXTENSIONS and THEME_SOURCE_EXTENSIONS
9
+ * @position read by theme discovery (which theme sources it accepts) and by
10
+ * `theme build` (which inputs it loads), so both accept the same files
11
+ */
12
+ /** The extensions of a discoverable theme source, `<name>Theme.<ext>`. */
13
+ export const THEME_MODULE_EXTENSIONS: string[];
14
+ /**
15
+ * The extensions `theme build` loads as a theme source: the discoverable ones,
16
+ * and their CommonJS forms, which a theme may also be written in.
17
+ */
18
+ export const THEME_SOURCE_EXTENSIONS: string[];
@@ -0,0 +1,30 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The file extensions a theme source module can have.
5
+ *
6
+ * @input none
7
+ * @output THEME_MODULE_EXTENSIONS and THEME_SOURCE_EXTENSIONS
8
+ * @position read by theme discovery (which theme sources it accepts) and by
9
+ * `theme build` (which inputs it loads), so both accept the same files
10
+ */
11
+
12
+ /** The extensions of a discoverable theme source, `<name>Theme.<ext>`. */
13
+ export const THEME_MODULE_EXTENSIONS = [
14
+ '.mjs',
15
+ '.js',
16
+ '.mts',
17
+ '.ts',
18
+ '.tsx',
19
+ '.jsx',
20
+ ];
21
+
22
+ /**
23
+ * The extensions `theme build` loads as a theme source: the discoverable ones,
24
+ * and their CommonJS forms, which a theme may also be written in.
25
+ */
26
+ export const THEME_SOURCE_EXTENSIONS = [
27
+ ...THEME_MODULE_EXTENSIONS,
28
+ '.cjs',
29
+ '.cts',
30
+ ];
@@ -26,20 +26,24 @@
26
26
 
27
27
  import * as fs from 'node:fs';
28
28
  import {pathToFileURL} from 'node:url';
29
- import {createJiti} from 'jiti';
29
+ import {createRequire} from 'node:module';
30
30
  import {importUrl} from '../fs/import-url.mjs';
31
+ import {jitiCacheOptions} from '../fs/jiti-cache.mjs';
31
32
  import {
32
33
  importUserModule,
33
34
  readFreshInTurn,
34
35
  readInTurn,
35
36
  } from '../fs/module-loader.mjs';
36
37
  import {fileStamp, recordContentRead} from '../fs/read-record.mjs';
37
- import {
38
- readStore,
39
- storedKey,
40
- SHIPPED_ASSETS,
41
- STORED_DOCS_FILE,
42
- } from './stored-docs.mjs';
38
+ import {readStore, storedKey, STORED_DOCS_FILE} from './stored-docs.mjs';
39
+
40
+ const requireFromHere = createRequire(import.meta.url);
41
+
42
+ /**
43
+ * jiti, loaded on first use: only TypeScript doc files need it.
44
+ * @type {typeof import('jiti').createJiti}
45
+ */
46
+ const createJiti = (...args) => requireFromHere('jiti').createJiti(...args);
43
47
 
44
48
  /** @type {ReturnType<typeof createJiti> | undefined} */
45
49
  let jsxJiti;
@@ -101,16 +105,14 @@ function loadFirst(file, load) {
101
105
  const storedModules = new Map();
102
106
 
103
107
  /**
104
- * The module of a shipped doc file whose text has stored exports: an object
108
+ * The module of a doc file whose text has stored exports: an object
105
109
  * shaped as its namespace (no prototype, the export names in order, tagged
106
110
  * `Module`, closed to new names). Null for any other file.
107
111
  * @param {string} file
108
112
  * @returns {Record<string, unknown> | null}
109
113
  */
110
114
  function storedModule(file) {
111
- if (!file.startsWith(SHIPPED_ASSETS) || !file.endsWith('.doc.mjs')) {
112
- return null;
113
- }
115
+ if (!file.endsWith('.doc.mjs')) return null;
114
116
  let text;
115
117
  try {
116
118
  text = fs.readFileSync(file, 'utf-8');
@@ -194,16 +196,21 @@ export function importTemplateModule(file) {
194
196
  'template',
195
197
  first => {
196
198
  if (first.endsWith('.ts')) {
197
- jsxJiti ??= createJiti(import.meta.url, {jsx: true});
199
+ jsxJiti ??= createJiti(import.meta.url, {
200
+ ...jitiCacheOptions(),
201
+ jsx: true,
202
+ });
198
203
  return jsxJiti.import(first);
199
204
  }
200
205
  return importInTurn(first, `file://${first}`);
201
206
  },
202
207
  changed =>
203
208
  changed.endsWith('.ts')
204
- ? createJiti(import.meta.url, {jsx: true, moduleCache: false}).import(
205
- changed,
206
- )
209
+ ? createJiti(import.meta.url, {
210
+ ...jitiCacheOptions(),
211
+ jsx: true,
212
+ moduleCache: false,
213
+ }).import(changed)
207
214
  : importFresh(changed),
208
215
  );
209
216
  }
@@ -5,23 +5,29 @@
5
5
  * The key of a source file's text in a store written at publish (the stored
6
6
  * docs, the stored template component lists): a hash of the text, so an
7
7
  * edited or unknown file is never answered from a store.
8
- * @param {string} text
8
+ * @param {string | Buffer} text
9
9
  * @returns {string}
10
10
  */
11
- export function storedKey(text: string): string;
11
+ export function storedKey(text: string | Buffer): string;
12
12
  /**
13
- * A store written at publish, by key, read once per process. A missing or
14
- * unreadable file reads as empty, so every source is worked out as before.
13
+ * A store written at publish, by key, read once per process. A store is
14
+ * either one JSON object of key to value, or (the stored docs) a first line
15
+ * mapping each key to its byte range, then the values: a lookup parses only
16
+ * the value it reads. A missing or unreadable file reads as empty, so every
17
+ * source is worked out as before.
15
18
  * @param {string} file
16
- * @returns {Map<string, any>}
19
+ * @returns {{get: (key: string) => any}}
17
20
  */
18
- export function readStore(file: string): Map<string, any>;
21
+ export function readStore(file: string): {
22
+ get: (key: string) => any;
23
+ };
19
24
  /**
20
- * Where the exports of the doc files Astryx ships are stored: each doc that
21
- * imports nothing, runs nothing as it loads and exports only plain data, by a
22
- * hash of its text. A stored doc then needs no import; an edited or unknown
23
- * file is imported as before.
25
+ * Where the exports of the doc files Astryx ships (the CLI's and Core's) are
26
+ * stored: each doc that imports nothing, reads nothing of where it sits, and
27
+ * exports only plain data, by a hash of its text. Such a doc's exports depend
28
+ * on its text alone, so any file with that exact text reads them; an edited
29
+ * or unknown file is imported as before. The first line maps each key to the
30
+ * byte range of its exports, as JSON, in the rest of the file, so a read
31
+ * parses only the docs it needs.
24
32
  */
25
33
  export const STORED_DOCS_FILE: string;
26
- /** The folder of the files Astryx ships; only its doc files are stored. */
27
- export const SHIPPED_ASSETS: string;
@@ -15,10 +15,13 @@ import * as path from 'node:path';
15
15
  import {CLI_ROOT} from '../fs/paths.mjs';
16
16
 
17
17
  /**
18
- * Where the exports of the doc files Astryx ships are stored: each doc that
19
- * imports nothing, runs nothing as it loads and exports only plain data, by a
20
- * hash of its text. A stored doc then needs no import; an edited or unknown
21
- * file is imported as before.
18
+ * Where the exports of the doc files Astryx ships (the CLI's and Core's) are
19
+ * stored: each doc that imports nothing, reads nothing of where it sits, and
20
+ * exports only plain data, by a hash of its text. Such a doc's exports depend
21
+ * on its text alone, so any file with that exact text reads them; an edited
22
+ * or unknown file is imported as before. The first line maps each key to the
23
+ * byte range of its exports, as JSON, in the rest of the file, so a read
24
+ * parses only the docs it needs.
22
25
  */
23
26
  export const STORED_DOCS_FILE = path.join(
24
27
  CLI_ROOT,
@@ -26,40 +29,70 @@ export const STORED_DOCS_FILE = path.join(
26
29
  'docs.generated.json',
27
30
  );
28
31
 
29
- /** The folder of the files Astryx ships; only its doc files are stored. */
30
- export const SHIPPED_ASSETS = path.join(CLI_ROOT, 'assets') + path.sep;
31
-
32
32
  /**
33
33
  * The key of a source file's text in a store written at publish (the stored
34
34
  * docs, the stored template component lists): a hash of the text, so an
35
35
  * edited or unknown file is never answered from a store.
36
- * @param {string} text
36
+ * @param {string | Buffer} text
37
37
  * @returns {string}
38
38
  */
39
39
  export function storedKey(text) {
40
40
  return createHash('sha256').update(text).digest('hex').slice(0, 32);
41
41
  }
42
42
 
43
- /** @type {Map<string, Map<string, any>>} */
43
+ /** @type {Map<string, {get: (key: string) => any}>} */
44
44
  const stores = new Map();
45
45
 
46
46
  /**
47
- * A store written at publish, by key, read once per process. A missing or
48
- * unreadable file reads as empty, so every source is worked out as before.
47
+ * A store written at publish, by key, read once per process. A store is
48
+ * either one JSON object of key to value, or (the stored docs) a first line
49
+ * mapping each key to its byte range, then the values: a lookup parses only
50
+ * the value it reads. A missing or unreadable file reads as empty, so every
51
+ * source is worked out as before.
49
52
  * @param {string} file
50
- * @returns {Map<string, any>}
53
+ * @returns {{get: (key: string) => any}}
51
54
  */
52
55
  export function readStore(file) {
53
56
  let store = stores.get(file);
54
57
  if (!store) {
55
- try {
56
- store = new Map(
57
- Object.entries(JSON.parse(fs.readFileSync(file, 'utf-8'))),
58
- );
59
- } catch {
60
- store = new Map();
61
- }
58
+ store = openStore(file);
62
59
  stores.set(file, store);
63
60
  }
64
61
  return store;
65
62
  }
63
+
64
+ /**
65
+ * @param {string} file
66
+ * @returns {{get: (key: string) => any}}
67
+ */
68
+ function openStore(file) {
69
+ /** @type {Buffer} */
70
+ let bytes;
71
+ try {
72
+ bytes = fs.readFileSync(file);
73
+ } catch {
74
+ return new Map();
75
+ }
76
+ const end = bytes.indexOf(0x0a);
77
+ try {
78
+ if (end !== -1 && end < bytes.length - 1) {
79
+ /** @type {Map<string, [number, number]>} */
80
+ const ranges = new Map(
81
+ Object.entries(JSON.parse(bytes.toString('utf-8', 0, end))),
82
+ );
83
+ const data = bytes.subarray(end + 1);
84
+ return {
85
+ get(key) {
86
+ const range = ranges.get(key);
87
+ if (!range) return undefined;
88
+ return JSON.parse(
89
+ data.toString('utf-8', range[0], range[0] + range[1]),
90
+ );
91
+ },
92
+ };
93
+ }
94
+ return new Map(Object.entries(JSON.parse(bytes.toString('utf-8'))));
95
+ } catch {
96
+ return new Map();
97
+ }
98
+ }
@@ -0,0 +1,17 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * The current user's id, or null where the system has none (Windows).
6
+ * @returns {number | null}
7
+ */
8
+ export function currentUid(): number | null;
9
+ /**
10
+ * Whether `dir` is a real folder (not a link) that the current user owns and
11
+ * no one else can reach, so nothing in it can have been planted by another
12
+ * account. False where the system has no user ids (Windows): ownership cannot
13
+ * be checked there, so such a folder is never used.
14
+ * @param {string} dir
15
+ * @returns {boolean}
16
+ */
17
+ export function isPrivateDir(dir: string): boolean;
@@ -0,0 +1,42 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The check a folder of cached code or data must pass before the CLI
5
+ * reads from it: Node's compile cache (clients/cli/bin/astryx.mjs) and the
6
+ * kept search catalogs (foundation/fs/gather-cache.mjs). Built-ins only, so
7
+ * the bin can load it before anything else.
8
+ */
9
+
10
+ import * as fs from 'node:fs';
11
+
12
+ /**
13
+ * The current user's id, or null where the system has none (Windows).
14
+ * @returns {number | null}
15
+ */
16
+ export function currentUid() {
17
+ return typeof process.getuid === 'function' ? process.getuid() : null;
18
+ }
19
+
20
+ /**
21
+ * Whether `dir` is a real folder (not a link) that the current user owns and
22
+ * no one else can reach, so nothing in it can have been planted by another
23
+ * account. False where the system has no user ids (Windows): ownership cannot
24
+ * be checked there, so such a folder is never used.
25
+ * @param {string} dir
26
+ * @returns {boolean}
27
+ */
28
+ export function isPrivateDir(dir) {
29
+ const uid = currentUid();
30
+ if (uid == null) return false;
31
+ try {
32
+ const stat = fs.lstatSync(dir);
33
+ return (
34
+ stat.isDirectory() &&
35
+ !stat.isSymbolicLink() &&
36
+ stat.uid === uid &&
37
+ (stat.mode & 0o077) === 0
38
+ );
39
+ } catch {
40
+ return false;
41
+ }
42
+ }