@astryxdesign/cli 0.7.15 → 0.7.16-canary.g54dd64e

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 (123) hide show
  1. package/README.md +5 -3
  2. package/api/component/_adapter.d.mts +1 -1
  3. package/api/component/_adapter.mjs +28 -1
  4. package/api/doctor/_adapter.d.mts +17 -0
  5. package/api/doctor/_adapter.mjs +35 -0
  6. package/api/doctor/doctor.mjs +18 -4
  7. package/api/integration/pack-check.mjs +13 -7
  8. package/api/integration/validate-integration.mjs +1 -1
  9. package/api/theme/_adapter.d.mts +5 -2
  10. package/api/theme/_adapter.mjs +43 -12
  11. package/api/theme/build/build.d.mts +64 -27
  12. package/api/theme/build/build.mjs +341 -69
  13. package/api/theme/eject/eject.mjs +22 -1
  14. package/api/theme/template/template.d.mts +19 -0
  15. package/api/theme/template/template.mjs +102 -25
  16. package/api/theme/theme.type.d.mts +5 -1
  17. package/api/theme/theme.type.mjs +2 -1
  18. package/api/theme/themeBuild.doc.mjs +3 -3
  19. package/api/theme/themeEject.doc.mjs +1 -1
  20. package/api/theme/themeTemplate.doc.mjs +9 -3
  21. package/api/theme-edit/_adapter.d.mts +33 -0
  22. package/api/theme-edit/_adapter.mjs +208 -31
  23. package/api/theme-edit/_check.d.mts +38 -0
  24. package/api/theme-edit/_check.mjs +1326 -211
  25. package/api/theme-edit/_color.d.mts +2 -8
  26. package/api/theme-edit/_color.mjs +292 -219
  27. package/api/theme-edit/_compiler.d.mts +1 -0
  28. package/api/theme-edit/_compiler.mjs +20 -1
  29. package/api/theme-edit/_context.d.mts +29 -0
  30. package/api/theme-edit/_context.mjs +329 -30
  31. package/api/theme-edit/_css.d.mts +60 -1
  32. package/api/theme-edit/_css.mjs +404 -28
  33. package/api/theme-edit/_edit.d.mts +9 -0
  34. package/api/theme-edit/_edit.mjs +1025 -126
  35. package/api/theme-edit/_how-commands.d.mts +11 -26
  36. package/api/theme-edit/_how-commands.mjs +30 -2
  37. package/api/theme-edit/_how-question.d.mts +7 -0
  38. package/api/theme-edit/_how-question.mjs +1304 -176
  39. package/api/theme-edit/_how.mjs +144 -30
  40. package/api/theme-edit/_overview.mjs +2 -0
  41. package/api/theme-edit/_palette.d.mts +11 -0
  42. package/api/theme-edit/_palette.mjs +194 -65
  43. package/api/theme-edit/_parts.d.mts +16 -0
  44. package/api/theme-edit/_parts.mjs +59 -7
  45. package/api/theme-edit/_png.mjs +19 -14
  46. package/api/theme-edit/_preview-app.d.mts +3 -0
  47. package/api/theme-edit/_preview-app.mjs +843 -45
  48. package/api/theme-edit/_preview.d.mts +16 -3
  49. package/api/theme-edit/_preview.mjs +310 -29
  50. package/api/theme-edit/_project.d.mts +11 -0
  51. package/api/theme-edit/_project.mjs +46 -12
  52. package/api/theme-edit/_settings.d.mts +178 -5
  53. package/api/theme-edit/_settings.mjs +2035 -86
  54. package/api/theme-edit/_tables.d.mts +24 -0
  55. package/api/theme-edit/_tables.mjs +211 -149
  56. package/api/theme-edit/_vet.d.mts +42 -0
  57. package/api/theme-edit/_vet.mjs +144 -0
  58. package/api/theme-edit/theme-edit.type.d.mts +2 -1
  59. package/api/theme-edit/theme-edit.type.mjs +1 -1
  60. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  61. package/api/theme-edit/themeEditShow.doc.mjs +11 -3
  62. package/api/upgrade/registry/registry.mjs +6 -1
  63. package/assets/docs/tree/single-component.doc.mjs +11 -2
  64. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  65. package/assets/docs.generated.json +2 -2
  66. package/assets/preview-app/next.config.mjs +7 -1
  67. package/assets/preview-app/package.json +1 -0
  68. package/assets/preview-app/src/app/globals.css +1 -0
  69. package/assets/self-audit.generated.json +1 -1
  70. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
  71. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
  72. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
  73. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
  74. package/assets/templates/components.generated.json +1 -1
  75. package/assets/templates/pages/dashboard/page.tsx +1 -1
  76. package/authoring/doctypes/_schema.d.mts +52 -5
  77. package/authoring/doctypes/_schema.mjs +62 -7
  78. package/authoring/doctypes/base/type.ts +10 -0
  79. package/authoring/doctypes/component/component.doc.mjs +1 -1
  80. package/authoring/doctypes/function/function.doc.mjs +6 -0
  81. package/authoring/doctypes/function/type.ts +3 -0
  82. package/authoring/doctypes/legacy.mjs +5 -1
  83. package/authoring/index.d.ts +1 -0
  84. package/clients/cli/build-id.generated.json +1 -1
  85. package/clients/cli/commands/doctor.doc.mjs +2 -2
  86. package/clients/cli/commands/experimental/theme.mjs +190 -37
  87. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  88. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +1 -1
  89. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
  90. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  91. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  92. package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
  93. package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
  94. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  95. package/clients/cli/commands/theme-eject.doc.mjs +1 -1
  96. package/clients/cli/commands/theme-template.doc.mjs +3 -1
  97. package/clients/cli/commands/theme.mjs +168 -45
  98. package/clients/cli/formatters/index.mjs +7 -4
  99. package/clients/cli/lib/cli-error.mjs +9 -3
  100. package/clients/cli/lib/component-format.mjs +27 -9
  101. package/clients/cli/lib/json-shim.mjs +40 -2
  102. package/clients/cli/manifest.generated.json +1 -1
  103. package/clients/cli/run.mjs +49 -0
  104. package/foundation/config/project.mjs +50 -14
  105. package/foundation/discovery/theme-discovery.d.mts +5 -1
  106. package/foundation/discovery/theme-discovery.mjs +24 -3
  107. package/foundation/fs/gather-cache.d.mts +9 -2
  108. package/foundation/fs/gather-cache.mjs +79 -15
  109. package/foundation/fs/paths.d.mts +31 -0
  110. package/foundation/fs/paths.mjs +43 -10
  111. package/foundation/render/host-exit.d.mts +8 -0
  112. package/foundation/render/host-exit.mjs +11 -0
  113. package/foundation/render/page-host.mjs +56 -10
  114. package/foundation/render/page-worker.mjs +14 -0
  115. package/foundation/render/render-page.mjs +37 -2
  116. package/foundation/response/error-codes.d.mts +2 -1
  117. package/foundation/response/error-codes.d.ts +1 -0
  118. package/foundation/response/error-codes.doc.mjs +5 -0
  119. package/foundation/response/error-codes.mjs +3 -0
  120. package/foundation/response/json.d.mts +15 -0
  121. package/foundation/response/json.mjs +52 -1
  122. package/foundation/response/response-types.doc.mjs +1 -1
  123. 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;
@@ -1320,6 +1320,28 @@ export class Project {
1320
1320
  return integration?.issuesUrl;
1321
1321
  }
1322
1322
 
1323
+ /**
1324
+ * The passes that find every integration issue: template and component
1325
+ * replacement validity (over Core plus every integration), then each
1326
+ * integration's own checks.
1327
+ */
1328
+ async #runIssuePasses() {
1329
+ // Template replacement validity depends on the combined Core +
1330
+ // integration catalog. Resolve it first so issue results never depend
1331
+ // on which discovery method the caller happened to invoke earlier. Only
1332
+ // an integration's own templates can replace one, so with none, Core's
1333
+ // templates stay unread.
1334
+ if (this.#loadedIntegrations.some(hasTemplatesRoot)) {
1335
+ await this.templates();
1336
+ }
1337
+ // Component replacement validity likewise depends on Core plus every
1338
+ // integration's components.
1339
+ await this.componentReplacements();
1340
+ for (const integration of this.#loadedIntegrations) {
1341
+ await this.#collectIssues(integration);
1342
+ }
1343
+ }
1344
+
1323
1345
  /**
1324
1346
  * The accumulated integration issues (deduped by package, code, message).
1325
1347
  * When called directly, also validates any configured integration not yet
@@ -1332,20 +1354,34 @@ export class Project {
1332
1354
  // templates and components declare no replacement. A project with no
1333
1355
  // integration skips them.
1334
1356
  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
- }
1357
+ // A function of the files the passes read and of the issues already
1358
+ // found (which the key holds): the kept gather stamps the files, so a
1359
+ // later run with every one unchanged reads the whole list back and
1360
+ // skips the passes, discovery included.
1361
+ const before = this.#issues.map(i => ({...i}));
1362
+ const all = await persistedGather(
1363
+ [
1364
+ 'project-issues',
1365
+ this.#cwd,
1366
+ // The Core the replacement pass reads: a carried Core for everyday
1367
+ // commands, the project's own for doctor.
1368
+ this.#core,
1369
+ this.coreDir() ?? null,
1370
+ this.#loadedIntegrations.map(
1371
+ integration => integration.__packageDir ?? integration.name ?? null,
1372
+ ),
1373
+ Boolean(this.#config.experimental?.iconSet),
1374
+ before,
1375
+ ],
1376
+ async () => {
1377
+ await this.#runIssuePasses();
1378
+ return this.#issues.map(i => ({...i}));
1379
+ },
1380
+ ISSUES_CODEC,
1381
+ );
1382
+ this.#issues = all.map((/** @type {ProjectIntegrationIssue} */ i) => ({
1383
+ ...i,
1384
+ }));
1349
1385
  }
1350
1386
  // Return a stable copy so callers can't mutate internal state.
1351
1387
  return this.#issues.map(i => ({...i}));
@@ -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
@@ -169,9 +169,11 @@ class ThemeRuntimeExportError extends Error {}
169
169
  * Every string specifier a module imports, re-exports, or imports dynamically.
170
170
  * @param {string} file
171
171
  * @param {any} jscodeshift
172
+ * @param {{withRequire?: boolean}} [options] `withRequire`: also
173
+ * `require('./x')` and TypeScript `import x = require('./x')`
172
174
  * @returns {string[]}
173
175
  */
174
- function moduleSpecifiers(file, jscodeshift) {
176
+ function moduleSpecifiers(file, jscodeshift, {withRequire = false} = {}) {
175
177
  const parser = /\.(?:ts|tsx|mts|cts)$/u.test(file) ? 'tsx' : 'babel';
176
178
  const j = jscodeshift.withParser(parser);
177
179
  const root = j(fs.readFileSync(file, 'utf-8'));
@@ -207,6 +209,23 @@ function moduleSpecifiers(file, jscodeshift) {
207
209
  specifiers.push(importPath.node.source.value);
208
210
  }
209
211
  });
212
+ if (withRequire) {
213
+ // CommonJS `require('./x')` and TypeScript `import x = require('./x')`.
214
+ root.find(j.CallExpression).forEach((/** @type {any} */ callPath) => {
215
+ const {callee, arguments: args} = callPath.node;
216
+ if (
217
+ callee?.type === 'Identifier' &&
218
+ callee.name === 'require' &&
219
+ typeof args?.[0]?.value === 'string'
220
+ )
221
+ specifiers.push(args[0].value);
222
+ });
223
+ root.find(j.TSImportEqualsDeclaration).forEach((/** @type {any} */ p) => {
224
+ const ref = p.node.moduleReference;
225
+ const value = ref?.expression?.value ?? ref?.argument?.value;
226
+ if (typeof value === 'string') specifiers.push(value);
227
+ });
228
+ }
210
229
 
211
230
  return specifiers;
212
231
  }
@@ -214,10 +233,12 @@ function moduleSpecifiers(file, jscodeshift) {
214
233
  /**
215
234
  * Every module a theme source file imports or re-exports, as written.
216
235
  * @param {string} file
236
+ * @param {{withRequire?: boolean}} [options] `withRequire`: also
237
+ * `require('./x')` and TypeScript `import x = require('./x')`.
217
238
  * @returns {string[]}
218
239
  */
219
- export function themeFileImports(file) {
220
- return moduleSpecifiers(file, sourceParser());
240
+ export function themeFileImports(file, options = {}) {
241
+ return moduleSpecifiers(file, sourceParser(), options);
221
242
  }
222
243
 
223
244
  /**
@@ -2,7 +2,9 @@
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
4
  /**
5
- * The running recorded run's reads and variables, for tests: null outside one.
5
+ * The running recorded run's reads and variables, or null outside one: for
6
+ * tests, and for a caller that keeps a value only in a recorded run (doctor's
7
+ * kept checks).
6
8
  * @returns {{reads: Map<string, string>, env: Map<string, string | null>} | null}
7
9
  */
8
10
  export function recordedRun(): {
@@ -39,9 +41,14 @@ export function recordRunFor<T>(argv: string[], fn: () => Promise<T>): Promise<T
39
41
  * tests; `recordRunFor` decides which runs are recorded.
40
42
  * @template T
41
43
  * @param {() => Promise<T>} fn
44
+ * @param {{armedThroughout?: boolean}} [options] - `armedThroughout`: record
45
+ * modules and variables until the run ends, not only until each gather
46
+ * ends, for a run whose later gathers read modules loaded in between.
42
47
  * @returns {Promise<T>}
43
48
  */
44
- export function recordRun<T>(fn: () => Promise<T>): Promise<T>;
49
+ export function recordRun<T>(fn: () => Promise<T>, { armedThroughout }?: {
50
+ armedThroughout?: boolean;
51
+ }): Promise<T>;
45
52
  /**
46
53
  * The gather `load` returns, or the one a run with the same key stored while
47
54
  * every input it read is unchanged (see the file header).
@@ -13,9 +13,12 @@
13
13
  * gathers fresh and stores that.
14
14
  * @position Used by search's adapter (api/search/_adapter.mjs) for the
15
15
  * catalog `search` and `build` rank, and by the project
16
- * (foundation/config/project.mjs) for each integration's contribution-check
17
- * results, which `component`, `template`, `theme`, `discover` and `search`
18
- * report. Gathers are kept only once the folder exists: the first such run
16
+ * (foundation/config/project.mjs) for its integration issues: each
17
+ * integration's contribution-check results and the whole list (with the
18
+ * template and component replacement passes), which `component`,
19
+ * `template`, `theme`, `discover` and `search` report, and by `doctor`
20
+ * (api/doctor/_adapter.mjs) for its file-only docs checks, in a run that
21
+ * records modules and variables to its end. Gathers are kept only once the folder exists: the first such run
19
22
  * on a machine just makes it, so a one-off run pays nothing to store one. Only a published CLI stores anything:
20
23
  * its build id (clients/cli/build-id.generated.json, written at `prepack`)
21
24
  * names its exact code, so a checkout, whose code may change between runs
@@ -67,6 +70,8 @@ const KEPT_COMMANDS = new Set([
67
70
  'template',
68
71
  'theme',
69
72
  'discover',
73
+ // The project check alone (no subcommand): its docs checks are kept.
74
+ 'doctor',
70
75
  ]);
71
76
 
72
77
  /** Where `prepack` writes the id of the package's exact contents. */
@@ -91,13 +96,16 @@ const UNSET = null;
91
96
  * their states, modules it loaded with theirs, and environment variables with
92
97
  * their values. Files and folders are recorded for the whole run; modules and
93
98
  * variables only while the run is armed: from its start to the end of its
94
- * first gather, and during each later gather. Null outside a recorded run.
95
- * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void} | null}
99
+ * first gather, and during each later gather, or, in a run armed throughout
100
+ * (`doctor`), to its end. Null outside a recorded run.
101
+ * @type {{reads: Map<string, string>, env: Map<string, string | null>, arm: () => boolean, disarm: () => void, armedThroughout: boolean, realEnv: NodeJS.ProcessEnv} | null}
96
102
  */
97
103
  let run = null;
98
104
 
99
105
  /**
100
- * The running recorded run's reads and variables, for tests: null outside one.
106
+ * The running recorded run's reads and variables, or null outside one: for
107
+ * tests, and for a caller that keeps a value only in a recorded run (doctor's
108
+ * kept checks).
101
109
  * @returns {{reads: Map<string, string>, env: Map<string, string | null>} | null}
102
110
  */
103
111
  export function recordedRun() {
@@ -225,6 +233,8 @@ export async function recordRunFor(argv, fn) {
225
233
  if (
226
234
  !command ||
227
235
  !KEPT_COMMANDS.has(command) ||
236
+ (command === 'doctor' &&
237
+ argv.filter(arg => !arg.startsWith('-')).length > 1) ||
228
238
  runsUnderPnp() ||
229
239
  currentUid() == null ||
230
240
  withoutRecording(buildId) == null ||
@@ -240,6 +250,10 @@ export async function recordRunFor(argv, fn) {
240
250
  // it, so a one-off run never pays to store one.
241
251
  const dir = cacheDirPath();
242
252
  if (!withoutRecording(() => fs.existsSync(dir))) {
253
+ // Once the per-user cache folder exists doctor uses it; it never makes
254
+ // it, so a doctor alone changes nothing on the machine (nor its own
255
+ // cache row).
256
+ if (command === 'doctor') return fn();
243
257
  try {
244
258
  withoutRecording(() => fs.mkdirSync(dir, {recursive: true, mode: 0o700}));
245
259
  } catch {
@@ -250,7 +264,10 @@ export async function recordRunFor(argv, fn) {
250
264
  // A folder the cache would refuse (not the user's own, open to others, a
251
265
  // link) leaves the run exactly as without the cache: no hook is installed.
252
266
  if (withoutRecording(gatherCacheDir) == null) return fn();
253
- return recordRun(fn);
267
+ // doctor's kept checks read docs modules that load between its gathers, so
268
+ // its run records modules and variables to the end, not only during each
269
+ // gather.
270
+ return recordRun(fn, {armedThroughout: command === 'doctor'});
254
271
  }
255
272
 
256
273
  /**
@@ -258,9 +275,12 @@ export async function recordRunFor(argv, fn) {
258
275
  * tests; `recordRunFor` decides which runs are recorded.
259
276
  * @template T
260
277
  * @param {() => Promise<T>} fn
278
+ * @param {{armedThroughout?: boolean}} [options] - `armedThroughout`: record
279
+ * modules and variables until the run ends, not only until each gather
280
+ * ends, for a run whose later gathers read modules loaded in between.
261
281
  * @returns {Promise<T>}
262
282
  */
263
- export async function recordRun(fn) {
283
+ export async function recordRun(fn, {armedThroughout = false} = {}) {
264
284
  const registerHooks = /** @type {any} */ (nodeModule).registerHooks;
265
285
  if (typeof registerHooks !== 'function' || run != null) return fn();
266
286
  /** @type {Map<string, string>} */
@@ -276,17 +296,31 @@ export async function recordRun(fn) {
276
296
  env.set(name, envState(realEnv[name]));
277
297
  }
278
298
  };
299
+ // Asking whether a variable is set, as `in` does and as `Object.keys` does
300
+ // for every name it lists, reads no value: only whether it is set counts.
301
+ // Every read of a value goes through `get` (spread, Object.entries,
302
+ // Object.assign, JSON.stringify included). Known gap: a descriptor's
303
+ // `value` (Object.getOwnPropertyDescriptor(s) on process.env) is a value
304
+ // read with no `get`, so it counts only as set. Nothing the CLI, Core, its
305
+ // dependencies, jiti's bundled babel or the integrations we test reads the
306
+ // environment that way.
307
+ /** @param {string | symbol} name */
308
+ const notePresence = name => {
309
+ if (typeof name === 'string' && !env.has(PRESENCE + name)) {
310
+ env.set(PRESENCE + name, presenceState(realEnv, name));
311
+ }
312
+ };
279
313
  const proxy = new Proxy(realEnv, {
280
314
  get(target, name) {
281
315
  noteEnv(name);
282
316
  return Reflect.get(target, name);
283
317
  },
284
318
  has(target, name) {
285
- noteEnv(name);
319
+ notePresence(name);
286
320
  return Reflect.has(target, name);
287
321
  },
288
322
  getOwnPropertyDescriptor(target, name) {
289
- noteEnv(name);
323
+ notePresence(name);
290
324
  return Reflect.getOwnPropertyDescriptor(target, name);
291
325
  },
292
326
  ownKeys(target) {
@@ -346,7 +380,7 @@ export async function recordRun(fn) {
346
380
  }
347
381
  };
348
382
  if (!arm()) return fn();
349
- run = {reads, env, arm, disarm};
383
+ run = {reads, env, arm, disarm, armedThroughout, realEnv};
350
384
  try {
351
385
  return await recordReads(reads, fn, {exact: true});
352
386
  } finally {
@@ -407,6 +441,19 @@ function lookupStamp(folder) {
407
441
  }
408
442
  }
409
443
 
444
+ /** The stamp name prefix for whether a variable is set, not its value. */
445
+ const PRESENCE = '\0set:';
446
+
447
+ /**
448
+ * Whether `name` is set in `env`, as stored.
449
+ * @param {NodeJS.ProcessEnv} env
450
+ * @param {string} name
451
+ * @returns {string | null}
452
+ */
453
+ function presenceState(env, name) {
454
+ return typeof env[name] === 'string' ? 'set' : UNSET;
455
+ }
456
+
410
457
  /**
411
458
  * How an environment variable the run read is kept: a digest of its value,
412
459
  * never the value (it can be a token), or UNSET when it was not set.
@@ -471,10 +518,13 @@ function entryKey(key, id) {
471
518
  * @returns {string | null} why not, or null when all hold
472
519
  */
473
520
  function staleReason(stamps, env) {
521
+ // The environment as it is, never through the run's recording proxy:
522
+ // checking an entry is no read the run's own gathers depend on.
523
+ const real = run?.realEnv ?? process.env;
474
524
  for (const [name, value] of env) {
475
- const current = envState(
476
- name === '\0names' ? namesOf(process.env) : process.env[name],
477
- );
525
+ const current = name.startsWith(PRESENCE)
526
+ ? presenceState(real, name.slice(PRESENCE.length))
527
+ : envState(name === '\0names' ? namesOf(real) : real[name]);
478
528
  if (current !== value) {
479
529
  return name === '\0names'
480
530
  ? 'the environment has other variables'
@@ -509,7 +559,7 @@ export async function persistedGather(key, load, codec, keep = () => true) {
509
559
  try {
510
560
  return await gatherAt(key, load, codec, keep);
511
561
  } finally {
512
- if (--gatherDepth === 0) run?.disarm();
562
+ if (--gatherDepth === 0 && !run?.armedThroughout) run?.disarm();
513
563
  }
514
564
  }
515
565
 
@@ -556,6 +606,14 @@ async function gatherAt(key, load, codec, keep) {
556
606
  if (stale == null) {
557
607
  const value = codec.decode(stored.value);
558
608
  recordStates(stored.stamps);
609
+ // A gather around this one depends on what this one read, the
610
+ // environment included: a hit hands its variables on, as a fresh
611
+ // gather's reads would be.
612
+ if (run) {
613
+ for (const [name, value] of stored.env) {
614
+ if (!run.env.has(name)) run.env.set(name, value);
615
+ }
616
+ }
559
617
  gatherCode = cliStates(stored.stamps);
560
618
  lastOutcome = {hit: true, reason: 'unchanged'};
561
619
  try {
@@ -761,6 +819,12 @@ export function runsUnderPnp() {
761
819
  * `pnp`: whether this run is under Yarn Plug'n'Play's loader (runsUnderPnp)
762
820
  */
763
821
  export function gatherCacheSummary() {
822
+ // The cache's own bookkeeping, never an input of what it keeps.
823
+ return withoutRecording(summarize);
824
+ }
825
+
826
+ /** @returns {{dir: string, bytes: number, entries: number, pnp: boolean} | null} */
827
+ function summarize() {
764
828
  if (buildId() == null) return null;
765
829
  const dir = gatherCacheDir();
766
830
  if (dir == null) return null;
@@ -60,6 +60,12 @@ export function findCarriedCoreDir(startDir: string, { fetch }?: {
60
60
  * @returns {string | null}
61
61
  */
62
62
  export function findProjectCoreDir(startDir?: string): string | null;
63
+ /**
64
+ * The home folder, resolved through symlinks, or null when there is none.
65
+ *
66
+ * @returns {string|null}
67
+ */
68
+ export function homeFolder(): string | null;
63
69
  /**
64
70
  * The project root: the directory of the nearest package.json at or above
65
71
  * `startDir`, or null when there is none.
@@ -68,6 +74,22 @@ export function findProjectCoreDir(startDir?: string): string | null;
68
74
  * @returns {string|null}
69
75
  */
70
76
  export function findPackageRoot(startDir: string): string | null;
77
+ /**
78
+ * A path a command writes in a project, read the same way by every theme
79
+ * command: from the working folder when given, from the project root (the
80
+ * nearest folder with a package.json, else the working folder) when not,
81
+ * and with a backslash as a folder separator on every platform.
82
+ * @param {string} cwd
83
+ * @param {string | undefined} typed the path as given, or undefined
84
+ * @param {string} fallback the path from the project root when none is given
85
+ * @returns {{projectDir: string, typed: string | undefined, relative: string}}
86
+ * `typed`: the path as given with `/`; `relative`: from the project root
87
+ */
88
+ export function projectWritePath(cwd: string, typed: string | undefined, fallback: string): {
89
+ projectDir: string;
90
+ typed: string | undefined;
91
+ relative: string;
92
+ };
71
93
  /**
72
94
  * The Astryx project `startDir` belongs to: the nearest folder at or above it
73
95
  * whose package.json declares an Astryx package (`@astryxdesign/*`, or the
@@ -89,6 +111,15 @@ export function findAstryxProject(startDir: string): string | null;
89
111
  * @returns {string}
90
112
  */
91
113
  export function findWorkspaceRoot(startDir: string): string;
114
+ /**
115
+ * The nearest directory at or above `startDir` that declares a workspace,
116
+ * by a `pnpm-workspace.yaml` or a package.json `workspaces` field; null
117
+ * outside a workspace.
118
+ *
119
+ * @param {string} startDir
120
+ * @returns {string|null}
121
+ */
122
+ export function findDeclaredWorkspaceRoot(startDir: string): string | null;
92
123
  /**
93
124
  * The nearest `node_modules/<name>` at or above `startDir`, found the way
94
125
  * Node's resolver finds a package, and the folder whose node_modules holds it.
@@ -282,7 +282,7 @@ function realDir(p) {
282
282
  *
283
283
  * @returns {string|null}
284
284
  */
285
- function homeFolder() {
285
+ export function homeFolder() {
286
286
  try {
287
287
  const home = os.homedir();
288
288
  return home ? realDir(home) : null;
@@ -339,6 +339,31 @@ export function findPackageRoot(startDir) {
339
339
  return null;
340
340
  }
341
341
 
342
+ /**
343
+ * A path a command writes in a project, read the same way by every theme
344
+ * command: from the working folder when given, from the project root (the
345
+ * nearest folder with a package.json, else the working folder) when not,
346
+ * and with a backslash as a folder separator on every platform.
347
+ * @param {string} cwd
348
+ * @param {string | undefined} typed the path as given, or undefined
349
+ * @param {string} fallback the path from the project root when none is given
350
+ * @returns {{projectDir: string, typed: string | undefined, relative: string}}
351
+ * `typed`: the path as given with `/`; `relative`: from the project root
352
+ */
353
+ export function projectWritePath(cwd, typed, fallback) {
354
+ const projectDir = findPackageRoot(cwd) ?? path.resolve(cwd);
355
+ const given = typed == null ? undefined : String(typed).replace(/\\/g, '/');
356
+ const absolute =
357
+ given == null ? path.join(projectDir, fallback) : path.resolve(cwd, given);
358
+ return {
359
+ projectDir,
360
+ typed: given,
361
+ relative: path.isAbsolute(given ?? '')
362
+ ? /** @type {string} */ (given)
363
+ : path.relative(projectDir, absolute) || '.',
364
+ };
365
+ }
366
+
342
367
  /**
343
368
  * The Astryx project `startDir` belongs to: the nearest folder at or above it
344
369
  * whose package.json declares an Astryx package (`@astryxdesign/*`, or the
@@ -374,8 +399,22 @@ export function findAstryxProject(startDir) {
374
399
  * @returns {string}
375
400
  */
376
401
  export function findWorkspaceRoot(startDir) {
377
- /** @type {string|null} */
378
- let nearestPackage = null;
402
+ return (
403
+ findDeclaredWorkspaceRoot(startDir) ??
404
+ findPackageRoot(startDir) ??
405
+ path.resolve(startDir)
406
+ );
407
+ }
408
+
409
+ /**
410
+ * The nearest directory at or above `startDir` that declares a workspace,
411
+ * by a `pnpm-workspace.yaml` or a package.json `workspaces` field; null
412
+ * outside a workspace.
413
+ *
414
+ * @param {string} startDir
415
+ * @returns {string|null}
416
+ */
417
+ export function findDeclaredWorkspaceRoot(startDir) {
379
418
  for (const dir of foldersUpFrom(startDir)) {
380
419
  if (
381
420
  fs.existsSync(path.join(dir, 'pnpm-workspace.yaml')) ||
@@ -383,14 +422,8 @@ export function findWorkspaceRoot(startDir) {
383
422
  ) {
384
423
  return dir;
385
424
  }
386
- if (
387
- nearestPackage == null &&
388
- fs.existsSync(path.join(dir, 'package.json'))
389
- ) {
390
- nearestPackage = dir;
391
- }
392
425
  }
393
- return nearestPackage ?? path.resolve(startDir);
426
+ return null;
394
427
  }
395
428
 
396
429
  /**
@@ -0,0 +1,8 @@
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
+ export namespace HOST_EXIT {
5
+ let disconnected: number;
6
+ let timeout: number;
7
+ let noChannel: number;
8
+ }
@@ -0,0 +1,11 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The exit codes of the render's host process (page-host.mjs) for each
5
+ * way it ends without sending an answer. render-page.mjs reads them to say
6
+ * why.
7
+ * @position foundation/render — imported by page-host.mjs and render-page.mjs
8
+ */
9
+
10
+ /** Exit codes for each way the host ends without sending an answer. */
11
+ export const HOST_EXIT = {disconnected: 70, timeout: 71, noChannel: 72};