@astryxdesign/cli 0.7.10 → 0.7.11-canary.84a2254

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 (65) hide show
  1. package/api/build/blocks/blocks.weights.json +1 -1
  2. package/api/build/build.doc.mjs +1 -1
  3. package/api/build/kit/signals.mjs +9 -3
  4. package/api/build/kit/start.d.mts +19 -0
  5. package/api/build/kit/start.mjs +49 -2
  6. package/api/build/kit/weights.d.mts +12 -0
  7. package/api/build/kit/weights.mjs +52 -16
  8. package/api/docs/_adapter.mjs +2 -1
  9. package/api/doctor/_adapter.d.mts +28 -1
  10. package/api/doctor/_adapter.mjs +39 -2
  11. package/api/doctor/doctor.doc.mjs +1 -1
  12. package/api/doctor/doctor.mjs +9 -2
  13. package/api/doctor/theme-checks.d.mts +44 -3
  14. package/api/doctor/theme-checks.mjs +273 -6
  15. package/api/hook/hook.mjs +13 -0
  16. package/api/integration/pack-check.d.mts +8 -6
  17. package/api/integration/pack-check.mjs +16 -6
  18. package/api/render/_adapter.mjs +47 -1
  19. package/api/render/render.doc.mjs +2 -1
  20. package/api/search/_gather-components.mjs +2 -1
  21. package/api/swizzle/copy/copy.mjs +5 -1
  22. package/api/theme/build/build.mjs +15 -4
  23. package/assets/docs/author-a-theme.doc.mjs +2 -0
  24. package/assets/docs/getting-started.doc.mjs +20 -0
  25. package/assets/docs/tree/building-blocks.doc.mjs +1 -0
  26. package/assets/docs/tree/global-integrations.doc.mjs +1 -0
  27. package/assets/docs/tree/help.doc.mjs +1 -1
  28. package/assets/docs/tree/quick-start.doc.mjs +1 -0
  29. package/assets/docs/tree/render-pages.doc.mjs +1 -1
  30. package/assets/docs/tree/ship.doc.mjs +1 -1
  31. package/assets/docs/tree/tokens-color.doc.mjs +2 -2
  32. package/assets/templates/blocks/components/Table/TableLeadingMarkSelection.doc.mjs +15 -0
  33. package/assets/templates/blocks/components/Table/TableLeadingMarkSelection.tsx +134 -0
  34. package/authoring/doctypes/_schema.d.mts +1 -1
  35. package/clients/cli/commands/build.doc.mjs +3 -2
  36. package/clients/cli/commands/build.mjs +34 -5
  37. package/clients/cli/index.mjs +36 -7
  38. package/foundation/discovery/authoring-self-docs.mjs +8 -3
  39. package/foundation/discovery/cli-self-docs.mjs +2 -1
  40. package/foundation/discovery/component-discovery.mjs +16 -0
  41. package/foundation/discovery/docs-discovery.mjs +6 -4
  42. package/foundation/discovery/icon-definition-discovery.mjs +3 -3
  43. package/foundation/discovery/module-exports.d.mts +91 -4
  44. package/foundation/discovery/module-exports.mjs +316 -42
  45. package/foundation/discovery/template-adapter.mjs +5 -7
  46. package/foundation/discovery/theme-discovery.mjs +8 -10
  47. package/foundation/doc-compiler/tree.mjs +2 -2
  48. package/foundation/env/package-manager.d.mts +12 -4
  49. package/foundation/env/package-manager.mjs +15 -6
  50. package/foundation/fs/listing-cache.d.mts +3 -1
  51. package/foundation/fs/listing-cache.mjs +63 -11
  52. package/foundation/fs/paths.mjs +2 -1
  53. package/foundation/release/notices.d.mts +16 -0
  54. package/foundation/release/notices.mjs +27 -6
  55. package/foundation/render/embedded-imports.d.mts +7 -0
  56. package/foundation/render/embedded-imports.mjs +1 -1
  57. package/foundation/render/page-worker.mjs +109 -4
  58. package/foundation/render/render-page.d.mts +16 -1
  59. package/foundation/render/render-page.mjs +25 -18
  60. package/foundation/render/served-packages.d.mts +33 -0
  61. package/foundation/render/served-packages.mjs +75 -0
  62. package/foundation/render/single-copy-hooks.d.mts +18 -0
  63. package/foundation/render/single-copy-hooks.mjs +44 -0
  64. package/foundation/text/string-utils.mjs +25 -25
  65. package/package.json +6 -6
@@ -28,6 +28,7 @@ import {createRequire} from 'node:module';
28
28
  import {readDocView} from '../doc-compiler/read.mjs';
29
29
  import {importDocModule} from '../doc-compiler/import.mjs';
30
30
  import {CLI_ROOT, discoverExternalPackages} from '../fs/paths.mjs';
31
+ import {listDirEntries} from '../fs/listing-cache.mjs';
31
32
  import {CORE_PROVIDER_ID} from '../identity/providers.mjs';
32
33
  import {Project} from '../config/project.mjs';
33
34
  import {
@@ -1115,8 +1116,7 @@ export function listTemplates() {
1115
1116
  const all = [];
1116
1117
  if (fs.existsSync(PAGES_DIR)) {
1117
1118
  all.push(
1118
- ...fs
1119
- .readdirSync(PAGES_DIR, {withFileTypes: true})
1119
+ ...listDirEntries(PAGES_DIR)
1120
1120
  .filter(e => e.isDirectory())
1121
1121
  .map(e => e.name),
1122
1122
  );
@@ -1133,7 +1133,7 @@ function findDocFiles(dir, pattern) {
1133
1133
  /** @type {string[]} */
1134
1134
  const results = [];
1135
1135
  if (!fs.existsSync(dir)) return results;
1136
- for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
1136
+ for (const entry of listDirEntries(dir)) {
1137
1137
  const full = path.join(dir, entry.name);
1138
1138
  if (entry.isDirectory()) {
1139
1139
  results.push(...findDocFiles(full, pattern));
@@ -1160,9 +1160,7 @@ export function findPageDocFile(dirPath) {
1160
1160
 
1161
1161
  async function discoverPages() {
1162
1162
  if (!fs.existsSync(PAGES_DIR)) return [];
1163
- const dirs = fs
1164
- .readdirSync(PAGES_DIR, {withFileTypes: true})
1165
- .filter(e => e.isDirectory());
1163
+ const dirs = listDirEntries(PAGES_DIR).filter(e => e.isDirectory());
1166
1164
 
1167
1165
  /** @type {DiscoveredTemplate[]} */
1168
1166
  const templates = [];
@@ -1602,7 +1600,7 @@ function findIntegrationDocFiles(root) {
1602
1600
  if (!fs.existsSync(root)) return results;
1603
1601
  /** @param {string} dir */
1604
1602
  const walk = dir => {
1605
- for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
1603
+ for (const entry of listDirEntries(dir)) {
1606
1604
  const full = path.join(dir, entry.name);
1607
1605
  if (entry.isDirectory()) {
1608
1606
  walk(full);
@@ -22,6 +22,7 @@ import * as path from 'node:path';
22
22
  import {lowerDoc} from '../doc-compiler/compile.mjs';
23
23
  import {packageSource} from '../doc-compiler/source.mjs';
24
24
  import {CLI_ROOT} from '../fs/paths.mjs';
25
+ import {listDirEntries} from '../fs/listing-cache.mjs';
25
26
  import {formatCliCommand} from '../env/package-manager.mjs';
26
27
  import {LOCAL_THEME_ROOT} from '../config/theme-state.mjs';
27
28
  import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
@@ -1187,7 +1188,7 @@ function listThemeFolder(folder) {
1187
1188
  const symlinks = [];
1188
1189
  /** @param {string} directory */
1189
1190
  function walk(directory) {
1190
- for (const entry of fs.readdirSync(directory, {withFileTypes: true})) {
1191
+ for (const entry of listDirEntries(directory)) {
1191
1192
  if (isIgnoredThemeEntry(entry.name)) continue;
1192
1193
  const full = path.join(directory, entry.name);
1193
1194
  const relative = path.relative(folder, full).split(path.sep).join('/');
@@ -1291,8 +1292,7 @@ export function discoverUnmigratedThemeCopies(
1291
1292
  ) {
1292
1293
  const root = path.resolve(projectDir, owner);
1293
1294
  if (!fs.existsSync(root) || !fs.statSync(root).isDirectory()) return [];
1294
- return fs
1295
- .readdirSync(root, {withFileTypes: true})
1295
+ return listDirEntries(root)
1296
1296
  .filter(entry => entry.isDirectory() && !isIgnoredThemeEntry(entry.name))
1297
1297
  .map(entry => unmigratedThemeCopy(path.join(root, entry.name)))
1298
1298
  .filter(copy => copy !== null)
@@ -1369,9 +1369,9 @@ export function discoverThemeDirectory(
1369
1369
  /** @type {DiscoveredTheme[]} */
1370
1370
  const themes = [];
1371
1371
  const slugs = new Set();
1372
- const rootEntries = fs
1373
- .readdirSync(themeRoot, {withFileTypes: true})
1374
- .filter(entry => !isIgnoredThemeEntry(entry.name));
1372
+ const rootEntries = listDirEntries(themeRoot).filter(
1373
+ entry => !isIgnoredThemeEntry(entry.name),
1374
+ );
1375
1375
  const rootDescriptor = rootEntries.find(
1376
1376
  entry => entry.isFile() && entry.name.endsWith(THEME_DOC_SUFFIX),
1377
1377
  );
@@ -1614,8 +1614,7 @@ export function unreadThemeFolders(themeRoot, {packageDir, packageName} = {}) {
1614
1614
  if (!fs.existsSync(themeRoot) || !fs.statSync(themeRoot).isDirectory()) {
1615
1615
  return [];
1616
1616
  }
1617
- const folders = fs
1618
- .readdirSync(themeRoot, {withFileTypes: true})
1617
+ const folders = listDirEntries(themeRoot)
1619
1618
  .filter(entry => entry.isDirectory() && !isIgnoredThemeEntry(entry.name))
1620
1619
  .map(entry => path.join(themeRoot, entry.name))
1621
1620
  .sort();
@@ -1630,8 +1629,7 @@ export function unreadThemeFolders(themeRoot, {packageDir, packageName} = {}) {
1630
1629
  if (unread.length === 0) return [];
1631
1630
 
1632
1631
  const modules = [
1633
- ...fs
1634
- .readdirSync(themeRoot, {withFileTypes: true})
1632
+ ...listDirEntries(themeRoot)
1635
1633
  .filter(
1636
1634
  entry =>
1637
1635
  entry.isFile() &&
@@ -20,6 +20,7 @@
20
20
  import * as fs from 'node:fs';
21
21
  import * as path from 'node:path';
22
22
  import {CLI_ROOT} from '../fs/paths.mjs';
23
+ import {listDir} from '../fs/listing-cache.mjs';
23
24
  import {loadCliSelfDocs} from '../discovery/cli-self-docs.mjs';
24
25
  import {astryxIconDefinitions} from '../discovery/icon-definition-discovery.mjs';
25
26
  import {routeSegment} from '../discovery/docs-section-key.mjs';
@@ -812,8 +813,7 @@ function adoptionsFor(doc, declared, routeOf) {
812
813
  */
813
814
  export function treeDocFiles(dir = TREE_DOCS_DIR) {
814
815
  if (!fs.existsSync(dir)) return [];
815
- return fs
816
- .readdirSync(dir)
816
+ return listDir(dir)
817
817
  .filter(file => /^[\w-]+\.doc\.mjs$/u.test(file))
818
818
  .sort(byText)
819
819
  .map(file => path.join(dir, file));
@@ -159,16 +159,24 @@ export function cliBinResolves(targetDir?: string): boolean;
159
159
  *
160
160
  * With `installed`, the line follows a step that installs the project's own
161
161
  * CLI (an update notice, the hint that matches a project's CLI to its Core),
162
- * so the guess is that CLI: `<run-prefix> astryx`. A declared `cliCommand`
163
- * and a host's door still come first.
162
+ * so the guess is that CLI: `<run-prefix> astryx`. With `release`, the line
163
+ * runs in another project's folder than the run's, so the guess names that
164
+ * project's release instead of the one that answered: its own CLI when it
165
+ * installs one and the bin resolves there, otherwise the package runner
166
+ * pinned to that release. A declared `cliCommand` and a host's door still
167
+ * come first.
164
168
  *
165
169
  * @param {string} [targetDir] the project the printed command runs in;
166
170
  * defaults to the working directory
167
- * @param {{installed?: boolean}} [options]
171
+ * @param {{installed?: boolean, release?: {version: string, own: boolean} | null}} [options]
168
172
  * @returns {string}
169
173
  */
170
- export function getCliInvocation(targetDir?: string, { installed }?: {
174
+ export function getCliInvocation(targetDir?: string, { installed, release }?: {
171
175
  installed?: boolean;
176
+ release?: {
177
+ version: string;
178
+ own: boolean;
179
+ } | null;
172
180
  }): string;
173
181
  /**
174
182
  * The package-manager guess alone, without any integration's `cliCommand` or
@@ -509,21 +509,30 @@ export function cliBinResolves(targetDir = process.cwd()) {
509
509
  *
510
510
  * With `installed`, the line follows a step that installs the project's own
511
511
  * CLI (an update notice, the hint that matches a project's CLI to its Core),
512
- * so the guess is that CLI: `<run-prefix> astryx`. A declared `cliCommand`
513
- * and a host's door still come first.
512
+ * so the guess is that CLI: `<run-prefix> astryx`. With `release`, the line
513
+ * runs in another project's folder than the run's, so the guess names that
514
+ * project's release instead of the one that answered: its own CLI when it
515
+ * installs one and the bin resolves there, otherwise the package runner
516
+ * pinned to that release. A declared `cliCommand` and a host's door still
517
+ * come first.
514
518
  *
515
519
  * @param {string} [targetDir] the project the printed command runs in;
516
520
  * defaults to the working directory
517
- * @param {{installed?: boolean}} [options]
521
+ * @param {{installed?: boolean, release?: {version: string, own: boolean} | null}} [options]
518
522
  * @returns {string}
519
523
  */
520
- export function getCliInvocation(targetDir, {installed = false} = {}) {
524
+ export function getCliInvocation(
525
+ targetDir,
526
+ {installed = false, release = null} = {},
527
+ ) {
521
528
  const declared = declaredCliCommand(targetDir);
522
529
  if (declared.command) return declared.command;
523
530
  const guess = () =>
524
- installed
531
+ installed || (release?.own && cliBinResolves(targetDir))
525
532
  ? `${getRunPrefix(targetDir)} ${CLI_BIN}`
526
- : guessCliInvocation(targetDir);
533
+ : release
534
+ ? `${getDlxPrefix(targetDir)} ${CLI_PACKAGE}@${release.version}`
535
+ : guessCliInvocation(targetDir);
527
536
  if (declared.conflict) return guess();
528
537
  return cliDoor() ?? guess();
529
538
  }
@@ -25,7 +25,9 @@ export function oncePerListing<T>(key: string, compute: () => T): T;
25
25
  export function listingCacheActive(): boolean;
26
26
  /**
27
27
  * `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
28
- * caller that sorts it cannot reorder another caller's listing.
28
+ * caller that sorts it cannot reorder another caller's listing. The names come
29
+ * from the same read as {@link listDirEntries}, so a folder listed both ways
30
+ * is read once.
29
31
  * @param {string} dir
30
32
  * @returns {string[]}
31
33
  */
@@ -2,23 +2,27 @@
2
2
 
3
3
  /**
4
4
  * @file One directory listing per path for the duration of one read-only
5
- * operation.
5
+ * operation, kept for later operations while the folder is unchanged.
6
6
  *
7
7
  * @input A read-only operation (`withListingCache(fn)`), and directory paths
8
8
  * its discovery helpers list or probe.
9
9
  * @output The same answers `fs.readdirSync` / `fs.existsSync` give, read from
10
10
  * disk once per path while the operation runs, and `oncePerListing`, a
11
- * value computed once per operation.
12
- * @position Used by the name resolvers in foundation/discovery and by
13
- * `existsCaseExact`. A search or build resolves ~160 components and their
14
- * hooks, and each resolution walks the same source tree again; inside one
15
- * operation the tree does not change, so each directory is read once.
16
- * Outside `withListingCache` every helper reads the disk directly, exactly
17
- * as before, so long-lived callers never see a stale listing.
11
+ * value computed once per operation. A later operation in the same process
12
+ * reuses a listing while its folder's stamp is unchanged.
13
+ * @position Used by the name resolvers and doc walkers in
14
+ * foundation/discovery and by `existsCaseExact`. A search or build resolves
15
+ * ~160 components and their hooks, and each resolution walks the same
16
+ * source tree again. Inside one operation the tree does not change, so each
17
+ * directory is read once. Outside `withListingCache` every helper reads the
18
+ * disk directly, exactly as before.
18
19
  */
19
20
 
20
21
  import {AsyncLocalStorage} from 'node:async_hooks';
21
22
  import * as fs from 'node:fs';
23
+ import * as path from 'node:path';
24
+ import {performance} from 'node:perf_hooks';
25
+ import {fileStamp, recordContentRead} from './read-record.mjs';
22
26
 
23
27
  /**
24
28
  * @typedef {{
@@ -33,6 +37,24 @@ import * as fs from 'node:fs';
33
37
  /** @type {AsyncLocalStorage<ListingCache>} */
34
38
  const scope = new AsyncLocalStorage();
35
39
 
40
+ /**
41
+ * Listings kept for later operations in this process, by folder. Each holds
42
+ * the folder's stamp (`fileStamp`: size, times and inode) and when this
43
+ * process first saw it. Once the stamp has held for {@link SETTLE_MS}, it
44
+ * holds the entries too. Adding, removing or renaming an entry changes the
45
+ * stamp.
46
+ * @type {Map<string, {stamp: string, since: number, entries: fs.Dirent[] | null}>}
47
+ */
48
+ const kept = new Map();
49
+
50
+ /**
51
+ * How long a folder's stamp must hold, by this process's clock, before its
52
+ * listing is kept. A file system can store times as coarsely as 2 s. So a
53
+ * change within one tick of the last can leave the stamp as it was. After the
54
+ * stamp has held this long, a new change falls in a later tick.
55
+ */
56
+ const SETTLE_MS = 2000;
57
+
36
58
  /**
37
59
  * Run `fn` with a listing cache. A nested call shares the outer cache. Only
38
60
  * wrap operations that do not change the files they list.
@@ -79,7 +101,9 @@ export function listingCacheActive() {
79
101
 
80
102
  /**
81
103
  * `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
82
- * caller that sorts it cannot reorder another caller's listing.
104
+ * caller that sorts it cannot reorder another caller's listing. The names come
105
+ * from the same read as {@link listDirEntries}, so a folder listed both ways
106
+ * is read once.
83
107
  * @param {string} dir
84
108
  * @returns {string[]}
85
109
  */
@@ -88,7 +112,7 @@ export function listDir(dir) {
88
112
  if (!cache) return fs.readdirSync(dir);
89
113
  let names = cache.names.get(dir);
90
114
  if (!names) {
91
- names = fs.readdirSync(dir);
115
+ names = listDirEntries(dir).map(entry => entry.name);
92
116
  cache.names.set(dir, names);
93
117
  }
94
118
  return names.slice();
@@ -104,12 +128,40 @@ export function listDirEntries(dir) {
104
128
  if (!cache) return fs.readdirSync(dir, {withFileTypes: true});
105
129
  let entries = cache.entries.get(dir);
106
130
  if (!entries) {
107
- entries = fs.readdirSync(dir, {withFileTypes: true});
131
+ entries = keptOrRead(dir);
108
132
  cache.entries.set(dir, entries);
109
133
  }
110
134
  return entries.slice();
111
135
  }
112
136
 
137
+ /**
138
+ * `dir`'s entries for an operation. They are the kept listing while the
139
+ * folder's stamp is the one it was kept with. Otherwise they are a new read,
140
+ * kept once the stamp has held for {@link SETTLE_MS}.
141
+ * @param {string} dir
142
+ * @returns {fs.Dirent[]}
143
+ */
144
+ function keptOrRead(dir) {
145
+ const folder = path.resolve(dir);
146
+ // A reuse check (read-record.mjs) records the folder's state before the
147
+ // stamp below is taken. So a change between the two shows as a change.
148
+ recordContentRead(folder);
149
+ const before = performance.now();
150
+ const stamp = fileStamp(folder);
151
+ const held = kept.get(folder);
152
+ if (held?.entries && held.stamp === stamp) return held.entries;
153
+ const entries = fs.readdirSync(dir, {withFileTypes: true});
154
+ if (held?.stamp !== stamp) {
155
+ // Taken after the stamp, so the stamp's times are no later than this.
156
+ kept.set(folder, {stamp, since: performance.now(), entries: null});
157
+ } else if (before - held.since >= SETTLE_MS) {
158
+ // Read after the stamp held for the settle time: a later change gets a
159
+ // new stamp.
160
+ kept.set(folder, {stamp, since: held.since, entries});
161
+ }
162
+ return entries;
163
+ }
164
+
113
165
  /**
114
166
  * The entry named exactly `name` in `dir`'s listing, or null when the folder
115
167
  * holds no such name or cannot be read. Inside an operation, a name the
@@ -16,6 +16,7 @@ import {fileURLToPath} from 'node:url';
16
16
  import {
17
17
  findEntry,
18
18
  listDir,
19
+ listDirEntries,
19
20
  listingCacheActive,
20
21
  pathExists,
21
22
  } from './listing-cache.mjs';
@@ -811,7 +812,7 @@ export function discoverExternalPackages(startDir = process.cwd()) {
811
812
  /** @param {string} searchDir */
812
813
  const scanDir = searchDir => {
813
814
  if (!fs.existsSync(searchDir)) return;
814
- const entries = fs.readdirSync(searchDir, {withFileTypes: true});
815
+ const entries = listDirEntries(searchDir);
815
816
 
816
817
  for (const entry of entries) {
817
818
  if (!entry.isDirectory()) continue;
@@ -132,6 +132,17 @@ export function installFolder(cwd: string): {
132
132
  dir: string;
133
133
  moved: boolean;
134
134
  } | null;
135
+ /**
136
+ * The release of the project at `dir`, for a command that runs in its folder
137
+ * from a folder below it: its own CLI's version when it installs one, else
138
+ * the Core it records; null when it records neither.
139
+ * @param {string} dir
140
+ * @returns {{version: string, own: boolean} | null}
141
+ */
142
+ export function projectRelease(dir: string): {
143
+ version: string;
144
+ own: boolean;
145
+ } | null;
135
146
  /**
136
147
  * The command that adds the CLI at Core's version, for a run in `cwd` whose
137
148
  * Core is `core`, with the project's package manager in the project root.
@@ -255,6 +266,11 @@ export function noticeLines({ cwd, self, choice, json, stateDir, cacheDir, now,
255
266
  }): string[];
256
267
  /** A notice prints at most once in this long. */
257
268
  export const NOTICE_INTERVAL_MS: number;
269
+ /**
270
+ * The key prefix of the once-a-day setup reminder. It is no update notice, so
271
+ * no skip is recorded for it and doctor's update line never names it.
272
+ */
273
+ export const SETUP_REMINDER_KEY: "setup:";
258
274
  /**
259
275
  * What to tell a run in a folder inside a project whose package.json is not
260
276
  * the project's: run from the project's folder, since adding there would make
@@ -81,6 +81,12 @@ const FORGET_AFTER_MS = 30 * 24 * 60 * 60_000;
81
81
  */
82
82
  const LATEST = '@astryxdesign/cli@latest';
83
83
 
84
+ /**
85
+ * The key prefix of the once-a-day setup reminder. It is no update notice, so
86
+ * no skip is recorded for it and doctor's update line never names it.
87
+ */
88
+ export const SETUP_REMINDER_KEY = 'setup:';
89
+
84
90
  /**
85
91
  * Whether notice `key` may print now; when it may, its time is recorded
86
92
  * first, and a time that cannot be recorded keeps it from printing.
@@ -104,7 +110,9 @@ export function takeNotice(
104
110
  const shown = state.notifiedAt ?? {};
105
111
  const last = shown[key];
106
112
  if (typeof last === 'number' && now - last < NOTICE_INTERVAL_MS) {
107
- const skips = withSkip(state.noticeSkips, key, 'already-shown', now);
113
+ const skips = key.startsWith(SETUP_REMINDER_KEY)
114
+ ? null
115
+ : withSkip(state.noticeSkips, key, 'already-shown', now);
108
116
  if (skips) writeGlobalState({...state, noticeSkips: skips}, stateDir);
109
117
  return /** @type {const} */ ('already-shown');
110
118
  }
@@ -198,6 +206,7 @@ export function lastNoticeSkip({stateDir = userConfigDir()} = {}) {
198
206
  readGlobalState(stateDir).noticeSkips ?? {},
199
207
  )) {
200
208
  if (
209
+ !key.startsWith(SETUP_REMINDER_KEY) &&
201
210
  skip &&
202
211
  typeof skip.at === 'number' &&
203
212
  (!newest || skip.at > newest.at)
@@ -459,6 +468,19 @@ export function installFolder(cwd) {
459
468
  export const RUN_FROM_PROJECT =
460
469
  "Run this from the Astryx project's folder, or declare an Astryx package in this folder's package.json to make it a project of its own.";
461
470
 
471
+ /**
472
+ * The release of the project at `dir`, for a command that runs in its folder
473
+ * from a folder below it: its own CLI's version when it installs one, else
474
+ * the Core it records; null when it records neither.
475
+ * @param {string} dir
476
+ * @returns {{version: string, own: boolean} | null}
477
+ */
478
+ export function projectRelease(dir) {
479
+ const facts = readReleaseFacts(dir);
480
+ const version = facts.cli?.version ?? coreVersionOf(facts);
481
+ return version ? {version, own: Boolean(facts.cli)} : null;
482
+ }
483
+
462
484
  /**
463
485
  * The command that adds the CLI at Core's version, for a run in `cwd` whose
464
486
  * Core is `core`, with the project's package manager in the project root.
@@ -534,11 +556,10 @@ export function projectLag({cwd, current: given, stateDir = userConfigDir()}) {
534
556
  const target = installFolder(cwd);
535
557
  if (target == null) return null;
536
558
  const root = target.moved ? target.dir : (projectRootOf(facts) ?? cwd);
537
- // From a folder inside the project, the project's own release: its CLI's,
538
- // else its Core's, so the project gets one notice and one --from.
539
- const projectFacts = target.moved ? readReleaseFacts(root) : null;
540
- const current = projectFacts
541
- ? (projectFacts.cli?.version ?? coreVersionOf(projectFacts) ?? given)
559
+ // From a folder inside the project, the project's own release, as the
560
+ // setup reminder names it, so the project gets one notice and one --from.
561
+ const current = target.moved
562
+ ? (projectRelease(root)?.version ?? given)
542
563
  : given;
543
564
  if (!isBehind(current, latest.version)) return null;
544
565
  const newest = latest.version;
@@ -1,6 +1,13 @@
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
+ * What a stylesheet's `@import` rules load, read with postcss so comments
6
+ * are ignored.
7
+ * @param {string} css
8
+ * @returns {string[]}
9
+ */
10
+ export function cssImports(css: string): string[];
4
11
  /**
5
12
  * The string specifiers one module imports.
6
13
  * @param {string} source
@@ -26,7 +26,7 @@ import {isAssetPath} from './assets.mjs';
26
26
  * @param {string} css
27
27
  * @returns {string[]}
28
28
  */
29
- function cssImports(css) {
29
+ export function cssImports(css) {
30
30
  /** @type {string[]} */
31
31
  const out = [];
32
32
  try {
@@ -8,7 +8,11 @@
8
8
  * It runs in a worker thread so a page that never finishes rendering can be
9
9
  * stopped, and so nothing the page prints reaches the CLI's own output.
10
10
  *
11
- * @input workerData {entry, root}
11
+ * Outside a project, the Astryx packages the page's folder lacks resolve
12
+ * from the package set the CLI runs from (served-packages.mjs), and React and
13
+ * StyleX always come from that set, so the page loads one copy of each.
14
+ *
15
+ * @input workerData {entry, root, served}
12
16
  * @output one message: {ok: true, markup, css, files, texts, logs} or
13
17
  * {ok: false, error: {code, message, detail?}}
14
18
  * @position foundation/render — started by render-page.mjs
@@ -16,17 +20,23 @@
16
20
 
17
21
  import * as fs from 'node:fs';
18
22
  import * as path from 'node:path';
19
- import {createRequire} from 'node:module';
23
+ import Module, {createRequire, register} from 'node:module';
24
+ import {pathToFileURL} from 'node:url';
20
25
  import {format} from 'node:util';
21
26
  import {parentPort, workerData} from 'node:worker_threads';
22
27
  import {transformSync} from '@babel/core';
23
28
  import {createJiti} from 'jiti';
24
29
  import {assetDataUri, isAssetPath} from './assets.mjs';
30
+ import {SINGLE_COPY, isServedPackage, packageOf} from './served-packages.mjs';
31
+ import {findNodeModulesPackage} from '../fs/paths.mjs';
25
32
 
26
33
  /** @typedef {{level: string, text: string}} PageLog */
27
34
 
28
- /** @type {{entry: string, root: string}} */
35
+ /** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null}} */
29
36
  const {entry, root} = workerData;
37
+
38
+ /** The package set, for a page outside a project whose folder has no Core of its own. */
39
+ const served = workerData.served && findNodeModulesPackage(path.dirname(entry), '@astryxdesign/core') === null ? workerData.served : null;
30
40
  const ROOT_PREFIX = root.endsWith(path.sep) ? root : root + path.sep;
31
41
 
32
42
  /** @type {PageLog[]} */
@@ -348,6 +358,96 @@ const resolveAlias = spec => {
348
358
  return spec;
349
359
  };
350
360
 
361
+ /** Packages the page took from the package set, by name: their folders. */
362
+ /** @type {Map<string, string>} */
363
+ const fromSet = new Map();
364
+
365
+ /**
366
+ * The folder of the package that holds `file`.
367
+ * @param {string} file
368
+ * @param {string} name
369
+ * @returns {string}
370
+ */
371
+ function packageDirOf(file, name) {
372
+ for (let d = path.dirname(file); path.dirname(d) !== d; d = path.dirname(d)) {
373
+ try {
374
+ if (JSON.parse(fs.readFileSync(path.join(d, 'package.json'), 'utf8')).name === name) return d;
375
+ } catch {
376
+ // No package.json here, or one that does not parse: look further up.
377
+ }
378
+ }
379
+ return path.dirname(file);
380
+ }
381
+
382
+ const moduleApi = /** @type {any} */ (Module);
383
+
384
+ /** A module at the served Core's folder: what resolves as the Core would. */
385
+ const servedParent = served ? new moduleApi(path.join(served.dir, 'package.json')) : null;
386
+ if (servedParent && served) {
387
+ servedParent.filename = servedParent.id;
388
+ servedParent.paths = moduleApi._nodeModulePaths(served.dir);
389
+ }
390
+
391
+ /**
392
+ * When the page's folder has no copy of a served package, take the one the
393
+ * package set holds. Node's resolver keeps each package's `exports`.
394
+ */
395
+ function serveFromSet() {
396
+ if (!served || !servedParent) return;
397
+ const resolveFilename = moduleApi._resolveFilename;
398
+ moduleApi._resolveFilename = function (/** @type {string} */ request, /** @type {any[]} */ ...rest) {
399
+ // React, React DOM and StyleX come from the set for every module, a
400
+ // library in the page's folder included, so the page runs on one copy.
401
+ if (!request.startsWith('.') && !path.isAbsolute(request) && packageOf(request) in SINGLE_COPY_ALIASES) {
402
+ try {
403
+ return resolveFilename.call(this, request, servedParent, false);
404
+ } catch {
405
+ // The set has no such file: the importer's own copy serves.
406
+ }
407
+ }
408
+ try {
409
+ return resolveFilename.call(this, request, ...rest);
410
+ } catch (e) {
411
+ const name = /** @type {any} */ (e)?.code === 'MODULE_NOT_FOUND' && !request.startsWith('.') && !path.isAbsolute(request)
412
+ ? packageOf(request)
413
+ : null;
414
+ if (!name || !isServedPackage(name, served.integrations)) throw e;
415
+ const file = resolveFilename.call(this, request, servedParent, false);
416
+ if (!fromSet.has(name)) fromSet.set(name, packageDirOf(file, name));
417
+ return file;
418
+ }
419
+ };
420
+ }
421
+
422
+ /**
423
+ * React and StyleX keep state per copy, and the packages from the set load
424
+ * the set's copy. So outside a project the page's own modules load that copy
425
+ * too, even when its folder holds another one.
426
+ * @returns {Record<string, string>}
427
+ */
428
+ function singleCopyAliases() {
429
+ /** @type {Record<string, string>} */
430
+ const alias = {};
431
+ if (!servedParent) return alias;
432
+ for (const name of SINGLE_COPY) {
433
+ try {
434
+ alias[name] = path.dirname(moduleApi._resolveFilename(`${name}/package.json`, servedParent, false));
435
+ } catch {
436
+ // The set has no copy: the page's own copy, if any, serves.
437
+ }
438
+ }
439
+ return alias;
440
+ }
441
+ const SINGLE_COPY_ALIASES = singleCopyAliases();
442
+ serveFromSet();
443
+ // ES modules follow the same rule through resolve hooks (CommonJS goes
444
+ // through serveFromSet).
445
+ if (servedParent && Object.keys(SINGLE_COPY_ALIASES).length > 0) {
446
+ register(new URL('./single-copy-hooks.mjs', import.meta.url), {
447
+ data: {parent: pathToFileURL(servedParent.filename).href, names: Object.keys(SINGLE_COPY_ALIASES)},
448
+ });
449
+ }
450
+
351
451
  const plain = createJiti(entry, {fsCache: false, jsx: {runtime: 'automatic'}});
352
452
 
353
453
  const jiti = createJiti(entry, {
@@ -356,7 +456,7 @@ const jiti = createJiti(entry, {
356
456
  extensions: ['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx', '.mts', '.cts', '.json'],
357
457
  moduleCache: true,
358
458
  interopDefault: true,
359
- alias: ALIASES,
459
+ alias: {...SINGLE_COPY_ALIASES, ...ALIASES},
360
460
  jsx: {runtime: 'automatic'},
361
461
  transform(opts) {
362
462
  const filename = opts.filename ?? '';
@@ -495,6 +595,10 @@ try {
495
595
  classify(e);
496
596
  throw null;
497
597
  }
598
+ // The render always runs on React; StyleX only when the page uses it.
599
+ for (const [name, dir] of Object.entries(SINGLE_COPY_ALIASES)) {
600
+ if (name !== '@stylexjs/stylex' || packages.has(name)) fromSet.set(name, dir);
601
+ }
498
602
  /** @type {string} */
499
603
  let css = '';
500
604
  if (stylexRules.length > 0) {
@@ -508,6 +612,7 @@ try {
508
612
  packages: [...packages],
509
613
  packageCss,
510
614
  skippedCss,
615
+ fromSet: [...fromSet].map(([name, dir]) => ({name, dir})),
511
616
  assets: [...assetsInlined],
512
617
  files: localFiles,
513
618
  texts,