@astryxdesign/cli 0.7.15 → 0.7.16-canary.g07c77c0

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 (159) hide show
  1. package/README.md +5 -3
  2. package/api/build/_adapter.d.mts +1 -0
  3. package/api/build/blocks/blocks.d.mts +1 -0
  4. package/api/build/blocks/blocks.mjs +18 -1
  5. package/api/component/_adapter.d.mts +1 -1
  6. package/api/component/_adapter.mjs +28 -1
  7. package/api/doctor/_adapter.d.mts +17 -0
  8. package/api/doctor/_adapter.mjs +43 -0
  9. package/api/doctor/built-checks.mjs +1 -1
  10. package/api/doctor/doctor.mjs +18 -4
  11. package/api/doctor/theme-checks.mjs +1 -1
  12. package/api/integration/add-contribution.mjs +3 -3
  13. package/api/integration/pack-check.mjs +14 -8
  14. package/api/integration/validate-integration.mjs +1 -1
  15. package/api/search/_adapter.mjs +15 -16
  16. package/api/search/_entries.d.mts +2 -1
  17. package/api/search/_entries.mjs +28 -1
  18. package/api/swizzle/_moduleGraph.mjs +1 -1
  19. package/api/theme/_adapter.d.mts +5 -2
  20. package/api/theme/_adapter.mjs +43 -12
  21. package/api/theme/_fork.mjs +1 -1
  22. package/api/theme/build/build.d.mts +116 -32
  23. package/api/theme/build/build.mjs +449 -102
  24. package/api/theme/eject/eject.mjs +22 -1
  25. package/api/theme/template/template.d.mts +19 -0
  26. package/api/theme/template/template.mjs +102 -25
  27. package/api/theme/theme.type.d.mts +5 -1
  28. package/api/theme/theme.type.mjs +2 -1
  29. package/api/theme/themeBuild.doc.mjs +3 -3
  30. package/api/theme/themeEject.doc.mjs +1 -1
  31. package/api/theme/themeTemplate.doc.mjs +9 -3
  32. package/api/theme-edit/_adapter.d.mts +32 -2
  33. package/api/theme-edit/_adapter.mjs +213 -36
  34. package/api/theme-edit/_check.d.mts +69 -3
  35. package/api/theme-edit/_check.mjs +1517 -218
  36. package/api/theme-edit/_color.d.mts +2 -8
  37. package/api/theme-edit/_color.mjs +292 -219
  38. package/api/theme-edit/_compiler.d.mts +1 -0
  39. package/api/theme-edit/_compiler.mjs +20 -1
  40. package/api/theme-edit/_context.d.mts +29 -0
  41. package/api/theme-edit/_context.mjs +426 -40
  42. package/api/theme-edit/_css.d.mts +60 -1
  43. package/api/theme-edit/_css.mjs +404 -28
  44. package/api/theme-edit/_edit.d.mts +9 -0
  45. package/api/theme-edit/_edit.mjs +1300 -135
  46. package/api/theme-edit/_how-commands.d.mts +11 -26
  47. package/api/theme-edit/_how-commands.mjs +30 -2
  48. package/api/theme-edit/_how-question.d.mts +7 -0
  49. package/api/theme-edit/_how-question.mjs +1304 -176
  50. package/api/theme-edit/_how.mjs +144 -30
  51. package/api/theme-edit/_overview.mjs +2 -0
  52. package/api/theme-edit/_palette.d.mts +11 -0
  53. package/api/theme-edit/_palette.mjs +194 -65
  54. package/api/theme-edit/_parts.d.mts +33 -0
  55. package/api/theme-edit/_parts.mjs +138 -9
  56. package/api/theme-edit/_png.d.mts +18 -0
  57. package/api/theme-edit/_png.mjs +95 -14
  58. package/api/theme-edit/_preview-app.d.mts +9 -0
  59. package/api/theme-edit/_preview-app.mjs +1191 -71
  60. package/api/theme-edit/_preview.d.mts +16 -3
  61. package/api/theme-edit/_preview.mjs +335 -30
  62. package/api/theme-edit/_project.d.mts +11 -0
  63. package/api/theme-edit/_project.mjs +46 -12
  64. package/api/theme-edit/_settings.d.mts +240 -5
  65. package/api/theme-edit/_settings.mjs +2561 -115
  66. package/api/theme-edit/_tables.d.mts +24 -0
  67. package/api/theme-edit/_tables.mjs +211 -149
  68. package/api/theme-edit/_vet.d.mts +42 -0
  69. package/api/theme-edit/_vet.mjs +144 -0
  70. package/api/theme-edit/theme-edit.type.d.mts +2 -1
  71. package/api/theme-edit/theme-edit.type.mjs +1 -1
  72. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  73. package/api/theme-edit/themeEditShow.doc.mjs +11 -3
  74. package/api/upgrade/registry/registry.mjs +6 -1
  75. package/assets/codemods/parse.mjs +2 -2
  76. package/assets/docs/tree/single-component.doc.mjs +11 -2
  77. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  78. package/assets/docs.generated.json +2 -2
  79. package/assets/preview-app/next.config.mjs +7 -1
  80. package/assets/preview-app/package.json +1 -0
  81. package/assets/preview-app/src/app/globals.css +1 -0
  82. package/assets/self-audit.generated.json +1 -1
  83. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
  84. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
  85. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
  86. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
  87. package/assets/templates/components.generated.json +1 -1
  88. package/assets/templates/pages/dashboard/page.tsx +1 -1
  89. package/authoring/doctypes/_schema.d.mts +52 -5
  90. package/authoring/doctypes/_schema.mjs +62 -7
  91. package/authoring/doctypes/base/type.ts +10 -0
  92. package/authoring/doctypes/component/component.doc.mjs +1 -1
  93. package/authoring/doctypes/function/function.doc.mjs +6 -0
  94. package/authoring/doctypes/function/type.ts +3 -0
  95. package/authoring/doctypes/legacy.mjs +5 -1
  96. package/authoring/index.d.ts +1 -0
  97. package/authoring/shadcn/source-variants.d.mts +1 -1
  98. package/authoring/shadcn/source-variants.mjs +32 -10
  99. package/clients/cli/build-id.generated.json +1 -1
  100. package/clients/cli/commands/doctor.doc.mjs +2 -2
  101. package/clients/cli/commands/experimental/theme.mjs +237 -45
  102. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  103. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
  104. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
  105. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +3 -3
  106. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  107. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  108. package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
  109. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  110. package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
  111. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  112. package/clients/cli/commands/theme-eject.doc.mjs +1 -1
  113. package/clients/cli/commands/theme-template.doc.mjs +3 -1
  114. package/clients/cli/commands/theme.mjs +190 -46
  115. package/clients/cli/formatters/index.mjs +7 -4
  116. package/clients/cli/lib/cli-error.mjs +9 -3
  117. package/clients/cli/lib/component-format.mjs +27 -9
  118. package/clients/cli/lib/json-shim.mjs +72 -2
  119. package/clients/cli/manifest.generated.json +1 -1
  120. package/clients/cli/run.mjs +49 -0
  121. package/foundation/agent-docs/agent-docs-target.d.mts +2 -6
  122. package/foundation/agent-docs/agent-docs-target.mjs +9 -17
  123. package/foundation/agent-docs/agent-docs.mjs +3 -2
  124. package/foundation/config/config-writer.mjs +2 -2
  125. package/foundation/config/project.mjs +60 -14
  126. package/foundation/discovery/component-discovery.mjs +5 -17
  127. package/foundation/discovery/icon-set-writer.mjs +1 -1
  128. package/foundation/discovery/module-exports.mjs +1 -15
  129. package/foundation/discovery/template-adapter.mjs +44 -19
  130. package/foundation/discovery/template-source-tree.mjs +3 -16
  131. package/foundation/discovery/theme-discovery.d.mts +5 -1
  132. package/foundation/discovery/theme-discovery.mjs +30 -35
  133. package/foundation/fs/exact-codec.d.mts +35 -0
  134. package/foundation/fs/exact-codec.mjs +148 -0
  135. package/foundation/fs/gather-cache.d.mts +15 -5
  136. package/foundation/fs/gather-cache.mjs +123 -30
  137. package/foundation/fs/paths.d.mts +39 -0
  138. package/foundation/fs/paths.mjs +44 -11
  139. package/foundation/fs/read-record.mjs +11 -1
  140. package/foundation/integrations/contribution-fixes.mjs +1 -16
  141. package/foundation/integrations/may-have-integrations.mjs +3 -2
  142. package/foundation/integrations/public-imports.mjs +1 -1
  143. package/foundation/render/embedded-imports.mjs +1 -1
  144. package/foundation/render/host-exit.d.mts +8 -0
  145. package/foundation/render/host-exit.mjs +11 -0
  146. package/foundation/render/page-host.mjs +56 -10
  147. package/foundation/render/page-worker.mjs +14 -0
  148. package/foundation/render/render-page.mjs +37 -2
  149. package/foundation/response/error-codes.d.mts +2 -1
  150. package/foundation/response/error-codes.d.ts +1 -0
  151. package/foundation/response/error-codes.doc.mjs +5 -0
  152. package/foundation/response/error-codes.mjs +3 -0
  153. package/foundation/response/json.d.mts +15 -0
  154. package/foundation/response/json.mjs +52 -1
  155. package/foundation/response/response-types.doc.mjs +1 -1
  156. package/foundation/runtime/lazy-dependencies.d.mts +14 -0
  157. package/foundation/runtime/lazy-dependencies.mjs +82 -0
  158. package/foundation/xle/browser.d.mts +2 -2
  159. package/package.json +7 -6
@@ -28,6 +28,55 @@
28
28
  * when, in a hosted run, that release's host entry answered
29
29
  */
30
30
  export async function runCli({cliRoot, argv, declinedProjectCli}) {
31
+ // A folder deleted under the shell (another terminal, `git clean`): every
32
+ // command reads the working folder first, so say so once, plainly, in
33
+ // text and in --json, instead of a stack trace.
34
+ try {
35
+ process.cwd();
36
+ } catch (e) {
37
+ if (/** @type {any} */ (e)?.code !== 'ENOENT') throw e;
38
+ const fs = await import('node:fs');
39
+ const path = await import('node:path');
40
+ const os = await import('node:os');
41
+ let up = process.env.PWD ? path.dirname(process.env.PWD) : null;
42
+ while (up && !fs.existsSync(up) && path.dirname(up) !== up)
43
+ up = path.dirname(up);
44
+ const cd = up && fs.existsSync(up) ? up : '~';
45
+ // Help, the version and docs read no folder: run them from an existing
46
+ // one.
47
+ const first = argv.find(a => !a.startsWith('-'));
48
+ if (
49
+ argv.length === 0 ||
50
+ argv.some(a => ['--help', '-h', '--version', '-V'].includes(a)) ||
51
+ first === 'docs' ||
52
+ first === 'help'
53
+ ) {
54
+ process.chdir(cd === '~' ? os.homedir() : cd);
55
+ return runCli({cliRoot, argv, declinedProjectCli});
56
+ }
57
+ const message =
58
+ 'The current folder no longer exists (it was deleted or moved). Nothing was written. cd to an existing folder and run the command again.';
59
+ const {quoteCommandArgument} =
60
+ await import('../../foundation/env/package-manager.mjs');
61
+ const suggestions = [
62
+ {
63
+ name: `cd ${cd === '~' ? cd : quoteCommandArgument(cd)}`,
64
+ reason: 'an existing folder',
65
+ },
66
+ ];
67
+ if (argv.includes('--json')) {
68
+ const {toErrorEnvelope} =
69
+ await import('../../foundation/response/json.mjs');
70
+ fs.writeSync(
71
+ 1,
72
+ `${JSON.stringify(toErrorEnvelope(message, suggestions, 'ERR_PROJECT_REQUIRED'), null, 2)}\n`,
73
+ );
74
+ } else {
75
+ fs.writeSync(2, `Error: ${message}\n ${suggestions[0].name}\n`);
76
+ }
77
+ process.exitCode = 1;
78
+ return undefined;
79
+ }
31
80
  const {startRun} = await import('../../foundation/release/handoff.mjs');
32
81
  const answered = await startRun({cliRoot, argv, declinedProjectCli});
33
82
  if (answered) return answered;
@@ -1,12 +1,6 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
- /**
5
- * `p` resolved, through symlinks when it exists.
6
- * @param {string} p
7
- * @returns {string}
8
- */
9
- export function realDir(p: string): string;
10
4
  /**
11
5
  * Throw {@link AgentDocsHomeError} when `targetDir` is the home folder, or a
12
6
  * settings folder in it outside a project.
@@ -32,6 +26,7 @@ export function resolveAgentDocsRoot(cwd: string): string | null;
32
26
  * @returns {string}
33
27
  */
34
28
  export function agentDocsTargetDir(cwd: string): string;
29
+ export { realDir };
35
30
  /**
36
31
  * Thrown when agent docs would be written into the home folder, or into a
37
32
  * settings folder in it such as `~/.gemini` outside a project. Agents read the
@@ -47,3 +42,4 @@ export class AgentDocsHomeError extends Error {
47
42
  }
48
43
  /** Folders that mark a repository root; the project search stops there. */
49
44
  export const REPOSITORY_MARKERS: string[];
45
+ import { realDir } from '../fs/paths.mjs';
@@ -5,15 +5,20 @@
5
5
  * @input The folder a run starts in, the home folder, and the package.json
6
6
  * files and repository markers above the folder
7
7
  * @output AgentDocsHomeError, assertAgentDocsTarget, resolveAgentDocsRoot and
8
- * agentDocsTargetDir, with the realDir and REPOSITORY_MARKERS helpers
9
- * @position The folder agent docs go to. It imports node builtins only, so the
10
- * per-command setup nudge (clients/cli/index.mjs) loads it without
11
- * agent-docs.mjs's discovery and config. agent-docs.mjs re-exports it.
8
+ * agentDocsTargetDir, with the REPOSITORY_MARKERS helper (and realDir,
9
+ * re-exported from ../fs/paths.mjs)
10
+ * @position The folder agent docs go to. It imports node builtins and the
11
+ * CLI's paths module only, so the per-command setup nudge
12
+ * (clients/cli/index.mjs) loads it without agent-docs.mjs's discovery and
13
+ * config. agent-docs.mjs re-exports it.
12
14
  */
13
15
 
14
16
  import * as fs from 'node:fs';
15
17
  import * as os from 'node:os';
16
18
  import * as path from 'node:path';
19
+ import {realDir} from '../fs/paths.mjs';
20
+
21
+ export {realDir};
17
22
 
18
23
  /**
19
24
  * Thrown when agent docs would be written into the home folder, or into a
@@ -34,19 +39,6 @@ export class AgentDocsHomeError extends Error {
34
39
  }
35
40
  }
36
41
 
37
- /**
38
- * `p` resolved, through symlinks when it exists.
39
- * @param {string} p
40
- * @returns {string}
41
- */
42
- export function realDir(p) {
43
- try {
44
- return fs.realpathSync(path.resolve(p));
45
- } catch {
46
- return path.resolve(p);
47
- }
48
- }
49
-
50
42
  /**
51
43
  * `dir` relative to the home folder, compared without symlinks (and without
52
44
  * case on Windows). null when there is no home folder.
@@ -83,8 +83,9 @@ import {
83
83
  export {discoverAgentDocs, isAstryxInitialized, isAstryxPromptInReach};
84
84
 
85
85
  // The folder the docs go to lives in ./agent-docs-target.mjs, with node
86
- // builtins only. So the per-command setup nudge loads it without this
87
- // module's discovery and config. Re-exported here for existing importers.
86
+ // builtins and the CLI's paths module only. So the per-command setup nudge
87
+ // loads it without this module's discovery and config. Re-exported here for
88
+ // existing importers.
88
89
  export {
89
90
  AgentDocsHomeError,
90
91
  agentDocsTargetDir,
@@ -28,7 +28,7 @@
28
28
  import * as fs from 'node:fs';
29
29
  import * as path from 'node:path';
30
30
  import {randomUUID} from 'node:crypto';
31
- import jscodeshift from 'jscodeshift';
31
+ import {jscodeshift as loadJscodeshift} from '../runtime/lazy-dependencies.mjs';
32
32
  import {
33
33
  findManifestObject,
34
34
  unwrapManifestExpression,
@@ -180,7 +180,7 @@ export function setConfigValue(projectDir, key, value) {
180
180
  }
181
181
  assertWritable(projectDir, existing);
182
182
  const source = fs.readFileSync(existing, 'utf-8');
183
- const j = jscodeshift.withParser('tsx');
183
+ const j = loadJscodeshift().withParser('tsx');
184
184
  const ast = j(source);
185
185
  let object = findManifestObject(j, ast);
186
186
  const refuse = () =>
@@ -118,6 +118,16 @@ import {
118
118
  const ISSUES_CODEC = {
119
119
  encode: (/** @type {any} */ value) => value,
120
120
  decode: (/** @type {any} */ value) => value,
121
+ /** A list of issue records, each with its code, severity and message. */
122
+ valid: (/** @type {any} */ value) =>
123
+ Array.isArray(value) &&
124
+ value.every(
125
+ issue =>
126
+ issue != null &&
127
+ typeof issue.code === 'string' &&
128
+ typeof issue.severity === 'string' &&
129
+ typeof issue.message === 'string',
130
+ ),
121
131
  };
122
132
 
123
133
  export {CONFIG_BASENAMES, findConfigPath} from './config-path.mjs';
@@ -1320,6 +1330,28 @@ export class Project {
1320
1330
  return integration?.issuesUrl;
1321
1331
  }
1322
1332
 
1333
+ /**
1334
+ * The passes that find every integration issue: template and component
1335
+ * replacement validity (over Core plus every integration), then each
1336
+ * integration's own checks.
1337
+ */
1338
+ async #runIssuePasses() {
1339
+ // Template replacement validity depends on the combined Core +
1340
+ // integration catalog. Resolve it first so issue results never depend
1341
+ // on which discovery method the caller happened to invoke earlier. Only
1342
+ // an integration's own templates can replace one, so with none, Core's
1343
+ // templates stay unread.
1344
+ if (this.#loadedIntegrations.some(hasTemplatesRoot)) {
1345
+ await this.templates();
1346
+ }
1347
+ // Component replacement validity likewise depends on Core plus every
1348
+ // integration's components.
1349
+ await this.componentReplacements();
1350
+ for (const integration of this.#loadedIntegrations) {
1351
+ await this.#collectIssues(integration);
1352
+ }
1353
+ }
1354
+
1323
1355
  /**
1324
1356
  * The accumulated integration issues (deduped by package, code, message).
1325
1357
  * When called directly, also validates any configured integration not yet
@@ -1332,20 +1364,34 @@ export class Project {
1332
1364
  // templates and components declare no replacement. A project with no
1333
1365
  // integration skips them.
1334
1366
  if (this.#loadedIntegrations.length > 0) {
1335
- // Template replacement validity depends on the combined Core +
1336
- // integration catalog. Resolve it first so issue results never depend
1337
- // on which discovery method the caller happened to invoke earlier. Only
1338
- // an integration's own templates can replace one, so with none, Core's
1339
- // templates stay unread.
1340
- if (this.#loadedIntegrations.some(hasTemplatesRoot)) {
1341
- await this.templates();
1342
- }
1343
- // Component replacement validity likewise depends on Core plus every
1344
- // integration's components.
1345
- await this.componentReplacements();
1346
- for (const integration of this.#loadedIntegrations) {
1347
- await this.#collectIssues(integration);
1348
- }
1367
+ // A function of the files the passes read and of the issues already
1368
+ // found (which the key holds): the kept gather stamps the files, so a
1369
+ // later run with every one unchanged reads the whole list back and
1370
+ // skips the passes, discovery included.
1371
+ const before = this.#issues.map(i => ({...i}));
1372
+ const all = await persistedGather(
1373
+ [
1374
+ 'project-issues',
1375
+ this.#cwd,
1376
+ // The Core the replacement pass reads: a carried Core for everyday
1377
+ // commands, the project's own for doctor.
1378
+ this.#core,
1379
+ this.coreDir() ?? null,
1380
+ this.#loadedIntegrations.map(
1381
+ integration => integration.__packageDir ?? integration.name ?? null,
1382
+ ),
1383
+ Boolean(this.#config.experimental?.iconSet),
1384
+ before,
1385
+ ],
1386
+ async () => {
1387
+ await this.#runIssuePasses();
1388
+ return this.#issues.map(i => ({...i}));
1389
+ },
1390
+ ISSUES_CODEC,
1391
+ );
1392
+ this.#issues = all.map((/** @type {ProjectIntegrationIssue} */ i) => ({
1393
+ ...i,
1394
+ }));
1349
1395
  }
1350
1396
  // Return a stable copy so callers can't mutate internal state.
1351
1397
  return this.#issues.map(i => ({...i}));
@@ -5,7 +5,6 @@
5
5
  */
6
6
 
7
7
  import * as fs from 'node:fs';
8
- import {createRequire} from 'node:module';
9
8
  import * as path from 'node:path';
10
9
  import {DOC_EXPORTS} from '../doc-compiler/read.mjs';
11
10
  import {
@@ -25,12 +24,8 @@ import {
25
24
  moduleExportsName,
26
25
  staticExportObject,
27
26
  } from './module-exports.mjs';
28
- import {
29
- inspectPackageExport,
30
- sourceExportTarget,
31
- } from './package-exports.mjs';
32
-
33
- const require = createRequire(import.meta.url);
27
+ import {inspectPackageExport, sourceExportTarget} from './package-exports.mjs';
28
+ import {babelParser} from '../runtime/lazy-dependencies.mjs';
34
29
 
35
30
  const SKIP_DIRS = new Set(['hooks', 'utils', '__tests__', 'node_modules']);
36
31
 
@@ -176,9 +171,6 @@ function readDocMeta(docPath) {
176
171
  }
177
172
  }
178
173
 
179
- /** @type {typeof import('@babel/parser') | undefined} */
180
- let babelParser;
181
-
182
174
  /**
183
175
  * A property of an object literal, by its plain key.
184
176
  * @param {any} object - an ObjectExpression node
@@ -211,13 +203,9 @@ function docMemberNames(source) {
211
203
  /** @type {any} */
212
204
  let ast;
213
205
  try {
214
- babelParser ??= require('@babel/parser');
215
- ast = /** @type {typeof import('@babel/parser')} */ (babelParser).parse(
216
- source,
217
- {
218
- sourceType: 'module',
219
- },
220
- );
206
+ ast = babelParser().parse(source, {
207
+ sourceType: 'module',
208
+ });
221
209
  } catch {
222
210
  return [];
223
211
  }
@@ -25,7 +25,7 @@ import * as fs from 'node:fs';
25
25
  import * as path from 'node:path';
26
26
  import {randomUUID} from 'node:crypto';
27
27
  import {AsyncLocalStorage} from 'node:async_hooks';
28
- import {parse} from '@babel/parser';
28
+ import {babelParse as parse} from '../runtime/lazy-dependencies.mjs';
29
29
  import {parseIconSetData} from '../../authoring/doctypes/icon-set/parse.mjs';
30
30
  import {ICON_NAMES} from '../../authoring/doctypes/icon-set/icon-names.mjs';
31
31
  import {assertWithin} from '../fs/path-safety.mjs';
@@ -19,9 +19,9 @@
19
19
 
20
20
  import * as fs from 'node:fs';
21
21
  import * as path from 'node:path';
22
- import {createRequire} from 'node:module';
23
22
  import {findInstalledPackage} from '../fs/paths.mjs';
24
23
  import {inspectPackageExport} from './package-exports.mjs';
24
+ import {babelParser} from '../runtime/lazy-dependencies.mjs';
25
25
 
26
26
  const MODULE_EXTENSIONS = [
27
27
  '.mjs',
@@ -103,20 +103,6 @@ function moduleText(file) {
103
103
  return text;
104
104
  }
105
105
 
106
- const requireFromHere = createRequire(import.meta.url);
107
- /** @type {typeof import('@babel/parser') | undefined} */
108
- let parser;
109
-
110
- /**
111
- * @babel/parser, loaded on first use: a run that reads no module's exports
112
- * never loads it.
113
- * @returns {typeof import('@babel/parser')}
114
- */
115
- function babelParser() {
116
- parser ??= requireFromHere('@babel/parser');
117
- return /** @type {typeof import('@babel/parser')} */ (parser);
118
- }
119
-
120
106
  /** @param {string} file */
121
107
  function parseModule(file) {
122
108
  const stamp = fileStamp(file);
@@ -24,11 +24,10 @@
24
24
 
25
25
  import * as fs from 'node:fs';
26
26
  import * as path from 'node:path';
27
- import {createRequire} from 'node:module';
28
27
  import {readDocView} from '../doc-compiler/read.mjs';
29
28
  import {importDocModule} from '../doc-compiler/import.mjs';
30
29
  import {readStore, storedKey} from '../doc-compiler/stored-docs.mjs';
31
- import {CLI_ROOT, discoverExternalPackages} from '../fs/paths.mjs';
30
+ import {CLI_ROOT, discoverExternalPackages, realDir} from '../fs/paths.mjs';
32
31
  import {listDirEntries} from '../fs/listing-cache.mjs';
33
32
  import {CORE_PROVIDER_ID} from '../identity/providers.mjs';
34
33
  import {Project} from '../config/project.mjs';
@@ -40,8 +39,10 @@ import {
40
39
  writtenNames,
41
40
  TEMPLATE_COMPONENTS_FILE,
42
41
  } from './template-source-tree.mjs';
43
-
44
- const require = createRequire(import.meta.url);
42
+ import {persistedGather} from '../fs/gather-cache.mjs';
43
+ import {EXACT_CODEC} from '../fs/exact-codec.mjs';
44
+ import {hostedRun} from '../runtime/package-root.mjs';
45
+ import {babelParser} from '../runtime/lazy-dependencies.mjs';
45
46
 
46
47
  /** Identity used for core (built-in) templates in package-scoped listings. */
47
48
  const CORE_PACKAGE = CORE_PROVIDER_ID;
@@ -1070,19 +1071,6 @@ function expressionUse(source, open, close) {
1070
1071
  return null;
1071
1072
  }
1072
1073
 
1073
- /** @type {typeof import('@babel/parser') | undefined} */
1074
- let babelParser;
1075
-
1076
- /**
1077
- * The template source parser, loaded on first use so listing templates
1078
- * never pays for it.
1079
- * @returns {typeof import('@babel/parser')}
1080
- */
1081
- function templateSourceParser() {
1082
- babelParser ??= require('@babel/parser');
1083
- return /** @type {typeof import('@babel/parser')} */ (babelParser);
1084
- }
1085
-
1086
1074
  /**
1087
1075
  * The [start, end) range of every comment in `source`, or none when it does
1088
1076
  * not parse as TSX.
@@ -1091,7 +1079,7 @@ function templateSourceParser() {
1091
1079
  */
1092
1080
  function commentRanges(source) {
1093
1081
  try {
1094
- const {comments} = templateSourceParser().parse(source, {
1082
+ const {comments} = babelParser().parse(source, {
1095
1083
  sourceType: 'module',
1096
1084
  plugins: ['jsx', 'typescript'],
1097
1085
  });
@@ -1569,6 +1557,18 @@ export async function discoverAllUnresolved(cwd = process.cwd()) {
1569
1557
  );
1570
1558
  }
1571
1559
 
1560
+ /** Template discovery's records, kept exactly, with the shape they need. */
1561
+ const TEMPLATE_SOURCES_CODEC = {
1562
+ ...EXACT_CODEC,
1563
+ valid: (/** @type {any} */ value) =>
1564
+ value != null &&
1565
+ Array.isArray(value.templates) &&
1566
+ Array.isArray(value.errors) &&
1567
+ value.templates.every(
1568
+ (/** @type {any} */ t) => t != null && typeof t.name === 'string',
1569
+ ),
1570
+ };
1571
+
1572
1572
  /**
1573
1573
  * Resolve replacement declarations while retaining package-addressable entries
1574
1574
  * that are shadowed or rejected in the default discovery view.
@@ -1576,6 +1576,31 @@ export async function discoverAllUnresolved(cwd = process.cwd()) {
1576
1576
  * @returns {Promise<{templates: DiscoveredTemplate[], errors: Array<TemplateDiscoveryError | TemplateReplacementError>}>}
1577
1577
  */
1578
1578
  async function resolveAllSources(cwd = process.cwd()) {
1579
+ // A function of the files discovery reads, which the kept gather stamps:
1580
+ // a later run with every one unchanged reads the records back.
1581
+ return /** @type {Awaited<ReturnType<typeof resolveAllSourcesNow>>} */ (
1582
+ await persistedGather(
1583
+ // The folder, and the copy of the CLI that answers with the
1584
+ // integrations a host approves: outside a project the integrations
1585
+ // beside the answering copy load too.
1586
+ [
1587
+ 'template-sources',
1588
+ realDir(cwd),
1589
+ CLI_ROOT,
1590
+ hostedRun()?.integrations ?? [],
1591
+ ],
1592
+ () => resolveAllSourcesNow(cwd),
1593
+ TEMPLATE_SOURCES_CODEC,
1594
+ )
1595
+ );
1596
+ }
1597
+
1598
+ /**
1599
+ * resolveAllSources, worked out now.
1600
+ * @param {string} cwd
1601
+ * @returns {Promise<{templates: DiscoveredTemplate[], errors: Array<TemplateDiscoveryError | TemplateReplacementError>}>}
1602
+ */
1603
+ async function resolveAllSourcesNow(cwd) {
1579
1604
  const discovered = await discoverAllSources(cwd);
1580
1605
  const replacementErrors = discovered.errors.filter(
1581
1606
  error => error.replacementTarget != null,
@@ -2079,7 +2104,7 @@ function topLevelBindings(src) {
2079
2104
  const astryxImports = new Map();
2080
2105
  let program;
2081
2106
  try {
2082
- program = templateSourceParser().parse(src, {
2107
+ program = babelParser().parse(src, {
2083
2108
  sourceType: 'module',
2084
2109
  plugins: ['jsx', 'typescript'],
2085
2110
  errorRecovery: true,
@@ -11,9 +11,9 @@
11
11
  * the demo-content replacement in template-adapter, and by template-needs
12
12
  */
13
13
 
14
- import {createRequire} from 'node:module';
15
14
  import * as path from 'node:path';
16
15
  import {CLI_ROOT} from '../fs/paths.mjs';
16
+ import {babelCore, babelParser} from '../runtime/lazy-dependencies.mjs';
17
17
 
18
18
  /**
19
19
  * Where what parsing returns for each template Astryx ships is stored at
@@ -28,13 +28,6 @@ export const TEMPLATE_COMPONENTS_FILE = path.join(
28
28
  'components.generated.json',
29
29
  );
30
30
 
31
- const require = createRequire(import.meta.url);
32
-
33
- /** @type {typeof import('@babel/parser') | undefined} */
34
- let babelParser;
35
- /** @type {typeof import('@babel/core') | undefined} */
36
- let babelCore;
37
-
38
31
  /**
39
32
  * Parse template source as TSX. Babel loads on first use, so a command that
40
33
  * only lists templates never pays for it.
@@ -44,10 +37,7 @@ let babelCore;
44
37
  * @returns {any} The file node.
45
38
  */
46
39
  export function parseTemplateSource(source, {errorRecovery = false} = {}) {
47
- babelParser ??= /** @type {typeof import('@babel/parser')} */ (
48
- require('@babel/parser')
49
- );
50
- return babelParser.parse(source, {
40
+ return babelParser().parse(source, {
51
41
  sourceType: 'module',
52
42
  plugins: ['jsx', 'typescript'],
53
43
  errorRecovery,
@@ -79,10 +69,7 @@ export function programPath(file) {
79
69
  * @param {import('@babel/core').Visitor} visitor
80
70
  */
81
71
  export function traverseTemplateSource(file, visitor) {
82
- babelCore ??= /** @type {typeof import('@babel/core')} */ (
83
- require('@babel/core')
84
- );
85
- babelCore.traverse(file, visitor);
72
+ babelCore().traverse(file, visitor);
86
73
  }
87
74
 
88
75
  /**
@@ -30,9 +30,13 @@ export function themeImportPackage(theme: DiscoveredTheme): string;
30
30
  /**
31
31
  * Every module a theme source file imports or re-exports, as written.
32
32
  * @param {string} file
33
+ * @param {{withRequire?: boolean}} [options] `withRequire`: also
34
+ * `require('./x')` and TypeScript `import x = require('./x')`.
33
35
  * @returns {string[]}
34
36
  */
35
- export function themeFileImports(file: string): string[];
37
+ export function themeFileImports(file: string, options?: {
38
+ withRequire?: boolean;
39
+ }): string[];
36
40
  /**
37
41
  * How messages about a theme descriptor name it.
38
42
  * @param {string} docPath
@@ -17,7 +17,6 @@
17
17
  */
18
18
 
19
19
  import * as fs from 'node:fs';
20
- import {createRequire} from 'node:module';
21
20
  import * as path from 'node:path';
22
21
  import {lowerDoc} from '../doc-compiler/compile.mjs';
23
22
  import {packageSource} from '../doc-compiler/source.mjs';
@@ -27,37 +26,12 @@ import {formatCliCommand} from '../env/package-manager.mjs';
27
26
  import {LOCAL_THEME_ROOT} from '../config/theme-state.mjs';
28
27
  import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
29
28
  import {THEME_MODULE_EXTENSIONS} from './theme-module-extensions.mjs';
29
+ import {babelParser, jscodeshift} from '../runtime/lazy-dependencies.mjs';
30
30
 
31
31
  export const BUNDLED_THEME_PACKAGE = '@astryxdesign/cli';
32
32
  export const THEMES_DIR = path.join(CLI_ROOT, 'assets', 'templates', 'themes');
33
33
  export const THEME_DOC_SUFFIX = '.doc.mjs';
34
34
 
35
- const require = createRequire(import.meta.url);
36
-
37
- /** @type {typeof import('@babel/parser') | undefined} */
38
- let babelParser;
39
- /** @type {any} */
40
- let jscodeshiftApi;
41
-
42
- /**
43
- * The descriptor parser, loaded when the first descriptor is read.
44
- * @returns {typeof import('@babel/parser')}
45
- */
46
- function descriptorParser() {
47
- babelParser ??= require('@babel/parser');
48
- return /** @type {typeof import('@babel/parser')} */ (babelParser);
49
- }
50
-
51
- /**
52
- * The theme source parser, loaded when the first integration theme source is
53
- * checked. Bundled themes never need it.
54
- * @returns {any}
55
- */
56
- function sourceParser() {
57
- jscodeshiftApi ??= require('jscodeshift');
58
- return jscodeshiftApi;
59
- }
60
-
61
35
  /**
62
36
  * @typedef {object} DiscoveredTheme
63
37
  * @property {string} slug
@@ -169,9 +143,11 @@ class ThemeRuntimeExportError extends Error {}
169
143
  * Every string specifier a module imports, re-exports, or imports dynamically.
170
144
  * @param {string} file
171
145
  * @param {any} jscodeshift
146
+ * @param {{withRequire?: boolean}} [options] `withRequire`: also
147
+ * `require('./x')` and TypeScript `import x = require('./x')`
172
148
  * @returns {string[]}
173
149
  */
174
- function moduleSpecifiers(file, jscodeshift) {
150
+ function moduleSpecifiers(file, jscodeshift, {withRequire = false} = {}) {
175
151
  const parser = /\.(?:ts|tsx|mts|cts)$/u.test(file) ? 'tsx' : 'babel';
176
152
  const j = jscodeshift.withParser(parser);
177
153
  const root = j(fs.readFileSync(file, 'utf-8'));
@@ -207,6 +183,23 @@ function moduleSpecifiers(file, jscodeshift) {
207
183
  specifiers.push(importPath.node.source.value);
208
184
  }
209
185
  });
186
+ if (withRequire) {
187
+ // CommonJS `require('./x')` and TypeScript `import x = require('./x')`.
188
+ root.find(j.CallExpression).forEach((/** @type {any} */ callPath) => {
189
+ const {callee, arguments: args} = callPath.node;
190
+ if (
191
+ callee?.type === 'Identifier' &&
192
+ callee.name === 'require' &&
193
+ typeof args?.[0]?.value === 'string'
194
+ )
195
+ specifiers.push(args[0].value);
196
+ });
197
+ root.find(j.TSImportEqualsDeclaration).forEach((/** @type {any} */ p) => {
198
+ const ref = p.node.moduleReference;
199
+ const value = ref?.expression?.value ?? ref?.argument?.value;
200
+ if (typeof value === 'string') specifiers.push(value);
201
+ });
202
+ }
210
203
 
211
204
  return specifiers;
212
205
  }
@@ -214,10 +207,12 @@ function moduleSpecifiers(file, jscodeshift) {
214
207
  /**
215
208
  * Every module a theme source file imports or re-exports, as written.
216
209
  * @param {string} file
210
+ * @param {{withRequire?: boolean}} [options] `withRequire`: also
211
+ * `require('./x')` and TypeScript `import x = require('./x')`.
217
212
  * @returns {string[]}
218
213
  */
219
- export function themeFileImports(file) {
220
- return moduleSpecifiers(file, sourceParser());
214
+ export function themeFileImports(file, options = {}) {
215
+ return moduleSpecifiers(file, jscodeshift(), options);
221
216
  }
222
217
 
223
218
  /**
@@ -709,7 +704,7 @@ function readDescriptorSource(source, label, typeName, data = false) {
709
704
  /** @type {any} */
710
705
  let ast;
711
706
  try {
712
- ast = descriptorParser().parse(source, {
707
+ ast = babelParser().parse(source, {
713
708
  sourceType: 'module',
714
709
  tokens: true,
715
710
  createParenthesizedExpressions: true,
@@ -1514,11 +1509,11 @@ export function discoverThemeDirectory(
1514
1509
  )
1515
1510
  .filter(file => file !== docPath),
1516
1511
  );
1517
- const jscodeshift = sourceParser();
1512
+ const codeshift = jscodeshift();
1518
1513
  try {
1519
1514
  validateThemeModuleGraph(
1520
1515
  entryPath,
1521
- jscodeshift,
1516
+ codeshift,
1522
1517
  sourceDir,
1523
1518
  allowedFiles,
1524
1519
  owner,
@@ -1529,7 +1524,7 @@ export function discoverThemeDirectory(
1529
1524
  !moduleExportsName(
1530
1525
  entryPath,
1531
1526
  exportName,
1532
- jscodeshift,
1527
+ codeshift,
1533
1528
  sourceDir,
1534
1529
  allowedFiles,
1535
1530
  )
@@ -1660,7 +1655,7 @@ export function unreadThemeFolders(themeRoot, {packageDir, packageName} = {}) {
1660
1655
  for (const file of modules) {
1661
1656
  let specifiers;
1662
1657
  try {
1663
- specifiers = moduleSpecifiers(file, sourceParser());
1658
+ specifiers = moduleSpecifiers(file, jscodeshift());
1664
1659
  } catch {
1665
1660
  continue;
1666
1661
  }