@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
@@ -85,6 +85,8 @@ export function makeCompiler(project, parts) {
85
85
  }
86
86
  /** @param {string} tok */
87
87
  const typeOf = tok => TYPE[tok] ?? 'other';
88
+ /** Whether Core defines the token. @param {string} tok */
89
+ const isToken = tok => Object.hasOwn(TYPE, tok);
88
90
 
89
91
  /** @param {string} p */
90
92
  const groupOfProp = p =>
@@ -276,7 +278,23 @@ export function makeCompiler(project, parts) {
276
278
  void fed;
277
279
  return null;
278
280
  }
279
- const reads = g ? partReads(target, g) : [];
281
+ // Only a token of the same kind takes the value: a weight never goes to
282
+ // a line height, a line height never to a weight.
283
+ // Font tokens share a type across kinds (a weight and a leading are
284
+ // both numbers), so the kind is read from the name.
285
+ const kindOf = (/** @type {string} */ t) =>
286
+ /family/.test(t)
287
+ ? 'family'
288
+ : /weight/.test(t)
289
+ ? 'weight'
290
+ : /leading|line-height/.test(t)
291
+ ? 'leading'
292
+ : /tracking|letter-spacing/.test(t)
293
+ ? 'tracking'
294
+ : typeOf(t);
295
+ const reads = g
296
+ ? partReads(target, g).filter(r => kindOf(r.token) === kindOf(token))
297
+ : [];
280
298
  const v = quoteCommandArgument(value);
281
299
  /** @type {string[]} */
282
300
  const run = [];
@@ -539,6 +557,7 @@ export function makeCompiler(project, parts) {
539
557
  return {
540
558
  MAP,
541
559
  typeOf,
560
+ isToken,
542
561
  propsReading,
543
562
  feeds,
544
563
  fmtFeeds,
@@ -18,6 +18,7 @@ export function context(cwd: string): Promise<{
18
18
  C: {
19
19
  MAP: Record<string, import("./_parts.mjs").PartRecord>;
20
20
  typeOf: (tok: string) => string;
21
+ isToken: (tok: string) => boolean;
21
22
  propsReading: (tok: string) => string[];
22
23
  feeds: (target: string, token: string) => Array<{
23
24
  part: string;
@@ -63,7 +64,28 @@ export function context(cwd: string): Promise<{
63
64
  parts: import("./_parts.mjs").PartData;
64
65
  unchecked: string | null;
65
66
  uncheckedCode: string | null;
67
+ derived: Record<string, {
68
+ property: string;
69
+ vars?: string[];
70
+ replaces?: boolean;
71
+ expand?: string;
72
+ }[]>;
66
73
  }>;
74
+ /**
75
+ * The app a folder belongs to: the nearest folder at or above it whose
76
+ * package.json lists @astryxdesign/core (dependencies, devDependencies or
77
+ * peerDependencies), never the home folder; null when there is none.
78
+ * @param {string} cwd
79
+ * @returns {string | null}
80
+ */
81
+ export function coreApp(cwd: string): string | null;
82
+ /**
83
+ * Why the components did not render, with the likely cause: two copies of
84
+ * React (the app's and the one Core resolves) break every hook.
85
+ * @param {import('./_project.mjs').ThemeProject} project
86
+ * @param {string} message
87
+ */
88
+ export function renderCause(project: import("./_project.mjs").ThemeProject, message: string): string;
67
89
  /**
68
90
  * Pictures of HTML files, on request.
69
91
  * @param {import('./_project.mjs').ThemeProject} project
@@ -111,6 +133,13 @@ export function previewAfter(ctx: import("./_settings.mjs").Ctx, slug: string, d
111
133
  cause: string;
112
134
  } | null;
113
135
  }>;
136
+ /**
137
+ * Record in a freshly made full sheet the build it shows.
138
+ * @param {string} file
139
+ * @param {string} slug
140
+ * @param {string} cwd
141
+ */
142
+ export function stampSheet(file: string, slug: string, cwd: string): void;
114
143
  /**
115
144
  * The section of the sheet, in lower case, or undefined; a refusal for one
116
145
  * that is not a section.
@@ -8,14 +8,25 @@
8
8
  * @position api/theme-edit — imported only by the adapter's operation helpers
9
9
  */
10
10
 
11
+ import {createHash} from 'node:crypto';
11
12
  import * as fs from 'node:fs';
12
13
  import {createRequire} from 'node:module';
13
14
  import * as path from 'node:path';
15
+ import {pathToFileURL} from 'node:url';
14
16
  import {AstryxError} from '../error.mjs';
15
- import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
17
+ import {
18
+ findDeclaredWorkspaceRoot,
19
+ findNodeModulesPackage,
20
+ homeFolder,
21
+ } from '../../foundation/fs/paths.mjs';
22
+ import {
23
+ detectPackageManager,
24
+ quoteCommandArgument,
25
+ } from '../../foundation/env/package-manager.mjs';
16
26
  import {
17
27
  CLOSEST_OPEN_FONT,
18
28
  FONT_ROLES,
29
+ FONTS,
19
30
  OPEN_FONTS,
20
31
  isDesktopFamily,
21
32
  oneSystemOf,
@@ -33,6 +44,7 @@ import {
33
44
  nearest,
34
45
  refuse,
35
46
  themeFiles,
47
+ refuseThemesNotFolder,
36
48
  } from './_settings.mjs';
37
49
  import {SHOWCASE_GROUPS, renderPreview, screenshot} from './_preview.mjs';
38
50
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
@@ -45,10 +57,13 @@ const E = ERROR_CODES;
45
57
  * @param {string} cwd
46
58
  */
47
59
  export function requireProject(cwd) {
48
- if (!isOutsideProject(cwd)) return;
49
- const pkg = path.join(cwd, 'package.json');
50
- // Yarn Plug'n'Play has no node_modules for the editor to read Core from.
51
- if (fs.existsSync(path.join(cwd, '.pnp.cjs')))
60
+ // One rule: the app is the nearest folder at or above cwd whose
61
+ // package.json lists @astryxdesign/core.
62
+ const app = coreApp(cwd);
63
+ if (!app) refuseNoApp(cwd);
64
+ // Yarn Plug'n'Play has no node_modules for the editor to read Core from:
65
+ // its .pnp.cjs sits at the app or at the workspace root above it.
66
+ if (pnpAbove(app))
52
67
  throw new AstryxError(
53
68
  "This app uses Yarn Plug'n'Play, which astryx experimental theme does not support: it reads the installed Core from node_modules. Set `nodeLinker: node-modules` in .yarnrc.yml and run yarn install. Nothing was written.",
54
69
  [
@@ -59,21 +74,11 @@ export function requireProject(cwd) {
59
74
  ],
60
75
  E.ERR_PROJECT_REQUIRED,
61
76
  );
62
- // A package.json with no Core: an app (or a workspace root) that needs it.
63
- if (fs.existsSync(pkg))
64
- throw new AstryxError(
65
- `@astryxdesign/core is not installed in this app (package.json here does not use it). astryx experimental theme checks every change against the installed Core: install it, or run the command from the package that uses it. Nothing was written.`,
66
- [{name: 'npm install @astryxdesign/core', reason: 'do this first'}],
67
- E.ERR_PROJECT_REQUIRED,
68
- );
77
+ if (findNodeModulesPackage(app, '@astryxdesign/core')) return;
78
+ // The app lists Core but it is not installed.
69
79
  throw new AstryxError(
70
- 'No package.json found. astryx experimental theme works inside an app project, which it writes the theme into and whose installed Core it checks every change against. Nothing was written.',
71
- [
72
- {
73
- name: 'npm init -y',
74
- reason: 'make a package.json here, then install @astryxdesign/core',
75
- },
76
- ],
80
+ `${path.relative(path.resolve(cwd), app) || 'This app'}'s package.json lists @astryxdesign/core, but it is not installed. astryx experimental theme checks every change against the installed Core: install the app's packages, then run the command again. Nothing was written.`,
81
+ [{name: installAll(app), reason: 'do this first, in the app'}],
77
82
  E.ERR_PROJECT_REQUIRED,
78
83
  );
79
84
  }
@@ -86,6 +91,7 @@ export function requireProject(cwd) {
86
91
  */
87
92
  export async function context(cwd) {
88
93
  requireProject(cwd);
94
+ refuseThemesNotFolder(cwd);
89
95
  const project = await openProject(cwd);
90
96
  /** @type {import('./_parts.mjs').PartData} */
91
97
  let parts;
@@ -114,22 +120,221 @@ export async function context(cwd) {
114
120
  parts,
115
121
  unchecked,
116
122
  uncheckedCode,
123
+ derived: await derivedVars(project.coreDir),
117
124
  };
118
125
  }
119
126
 
127
+ /**
128
+ * Core's registry of the variables a part's plain border-radius or padding
129
+ * also writes (a card's inner corners), or empty on a Core without it.
130
+ * @param {string} coreDir
131
+ * @returns {Promise<Record<string, Array<{property: string, vars?: string[], replaces?: boolean, expand?: string}>>>}
132
+ */
133
+ async function derivedVars(coreDir) {
134
+ const file = path.join(coreDir, 'dist', 'theme', 'derivedVarRegistry.js');
135
+ if (!fs.existsSync(file)) return {};
136
+ try {
137
+ const mod = await import(pathToFileURL(file).href);
138
+ return mod.derivedVarRegistry ?? {};
139
+ } catch {
140
+ return {};
141
+ }
142
+ }
143
+
120
144
  /** @param {unknown} e */
121
145
  const isRenderFailure = e =>
122
146
  e instanceof AstryxError &&
123
147
  (e.code === E.ERR_RENDER_FAILED ||
124
148
  e.code === E.ERR_RENDER_MISSING_DEPENDENCY);
125
149
 
150
+ /**
151
+ * The app a folder belongs to: the nearest folder at or above it whose
152
+ * package.json lists @astryxdesign/core (dependencies, devDependencies or
153
+ * peerDependencies), never the home folder; null when there is none.
154
+ * @param {string} cwd
155
+ * @returns {string | null}
156
+ */
157
+ export function coreApp(cwd) {
158
+ const home = homeFolder();
159
+ let dir = path.resolve(cwd);
160
+ // An installed package is never the app: resume above the outermost
161
+ // node_modules.
162
+ const nm = dir.split(path.sep).indexOf('node_modules');
163
+ if (nm > 0) dir = dir.split(path.sep).slice(0, nm).join(path.sep) || path.sep;
164
+ // A repository below the home folder is its own project: the home folder
165
+ // is the app only when the search did not come up out of another repo.
166
+ let crossedRepo = false;
167
+ for (;;) {
168
+ const listsCore = usesCore(readJson(path.join(dir, 'package.json')));
169
+ if (home && dir === home) return listsCore && !crossedRepo ? dir : null;
170
+ if (listsCore) return dir;
171
+ if (fs.existsSync(path.join(dir, '.git'))) crossedRepo = true;
172
+ const up = path.dirname(dir);
173
+ if (up === dir) return null;
174
+ dir = up;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Whether a Yarn Plug'n'Play loader (.pnp.cjs) is at the app folder or above
180
+ * it, up to its workspace root (or, outside a workspace, the repository
181
+ * root).
182
+ * @param {string} app
183
+ */
184
+ function pnpAbove(app) {
185
+ const root = findDeclaredWorkspaceRoot(app);
186
+ for (let dir = app; ; dir = path.dirname(dir)) {
187
+ if (fs.existsSync(path.join(dir, '.pnp.cjs'))) return true;
188
+ if (dir === root || (!root && fs.existsSync(path.join(dir, '.git'))))
189
+ return false;
190
+ if (path.dirname(dir) === dir) return false;
191
+ }
192
+ }
193
+
194
+ /**
195
+ * The command that installs an app's packages, in its package manager.
196
+ * @param {string} dir
197
+ */
198
+ function installAll(dir) {
199
+ const pm = detectPackageManager(dir);
200
+ return pm === 'npm' || pm === 'npx' ? 'npm install' : `${pm} install`;
201
+ }
202
+
203
+ /**
204
+ * Refuse a folder no app above lists Core for: say where Core resolves
205
+ * from (when it does) and, inside a workspace, name the folders below its
206
+ * root whose package.json lists Core, each with the `cd` to run from.
207
+ * @param {string} cwd
208
+ * @returns {never}
209
+ */
210
+ function refuseNoApp(cwd) {
211
+ const start = path.resolve(cwd);
212
+ // A repository inside a home-folder app (a submodule, a vendored clone) is
213
+ // its own project: name the app above instead of claiming there is none.
214
+ const home = homeFolder();
215
+ if (
216
+ home &&
217
+ !path.relative(home, start).startsWith('..') &&
218
+ usesCore(readJson(path.join(home, 'package.json')))
219
+ ) {
220
+ const rel = path.relative(start, home) || '.';
221
+ throw new AstryxError(
222
+ `This folder is inside its own repository, below the app at ${rel} (your home folder): the theme editor does not reach out of a repository into an app around it. Run the command from the app. Nothing was written.`,
223
+ [
224
+ {
225
+ name: `cd ${quoteCommandArgument(rel)}`,
226
+ reason: 'run the command from the app',
227
+ },
228
+ ],
229
+ E.ERR_PROJECT_REQUIRED,
230
+ );
231
+ }
232
+ // Where Core resolves from: the nearest node_modules above that holds it.
233
+ const resolved =
234
+ findNodeModulesPackage(start, '@astryxdesign/core')?.packageDir ?? null;
235
+ const root = findDeclaredWorkspaceRoot(start);
236
+ const where = resolved
237
+ ? ` @astryxdesign/core resolves from ${path.relative(start, resolved) || '.'}, but the theme editor needs the app's package.json to list it.`
238
+ : '';
239
+ if (root) {
240
+ const apps = appsBelow(root)
241
+ .map(a => path.relative(start, a) || '.')
242
+ .sort();
243
+ throw new AstryxError(
244
+ `${path.relative(start, root) || 'This folder'} is a workspace root${root === start ? '' : ` (${path.relative(root, start)} is not an app that uses @astryxdesign/core)`}, not an app: a theme belongs in an app whose package.json lists @astryxdesign/core.${where}${apps.length ? ` The workspace's apps that use it: ${apps.join(', ')}.` : ''} Run the command from the app that uses it. Nothing was written.`,
245
+ apps.slice(0, 4).map(a => ({
246
+ name: `cd ${quoteCommandArgument(a.startsWith('-') ? `./${a}` : a)}`,
247
+ reason: 'run the command from this app',
248
+ })),
249
+ E.ERR_PROJECT_REQUIRED,
250
+ );
251
+ }
252
+ if (fs.existsSync(path.join(start, 'package.json')) || resolved)
253
+ throw new AstryxError(
254
+ `${fs.existsSync(path.join(start, 'package.json')) ? '@astryxdesign/core is not installed in this app (package.json here does not use it).' : 'No package.json at or above this folder lists @astryxdesign/core.'}${where} astryx experimental theme checks every change against the installed Core: install it, or run the command from the package that uses it. Nothing was written.`,
255
+ [
256
+ {
257
+ name: installLine(start, ['@astryxdesign/core']),
258
+ reason: 'do this first',
259
+ },
260
+ ],
261
+ E.ERR_PROJECT_REQUIRED,
262
+ );
263
+ throw new AstryxError(
264
+ 'No package.json found. astryx experimental theme works inside an app project, which it writes the theme into and whose installed Core it checks every change against. Nothing was written.',
265
+ [
266
+ {
267
+ name: 'npm init -y',
268
+ reason: 'make a package.json here, then install @astryxdesign/core',
269
+ },
270
+ ],
271
+ E.ERR_PROJECT_REQUIRED,
272
+ );
273
+ }
274
+
275
+ /**
276
+ * The folders below a workspace root, up to three levels deep (node_modules
277
+ * and dot folders skipped), whose package.json lists Core.
278
+ * @param {string} root
279
+ * @returns {string[]}
280
+ */
281
+ function appsBelow(root) {
282
+ /** @type {string[]} */
283
+ const out = [];
284
+ /** @param {string} dir @param {number} depth */
285
+ const walk = (dir, depth) => {
286
+ if (depth > 6) return;
287
+ /** @type {fs.Dirent[]} */
288
+ let entries;
289
+ try {
290
+ entries = fs.readdirSync(dir, {withFileTypes: true});
291
+ } catch {
292
+ return;
293
+ }
294
+ for (const e of entries) {
295
+ if (
296
+ !e.isDirectory() ||
297
+ e.name === 'node_modules' ||
298
+ e.name.startsWith('.')
299
+ )
300
+ continue;
301
+ const sub = path.join(dir, e.name);
302
+ if (usesCore(readJson(path.join(sub, 'package.json')))) out.push(sub);
303
+ walk(sub, depth + 1);
304
+ }
305
+ };
306
+ walk(root, 1);
307
+ return out;
308
+ }
309
+
310
+ /** @param {string} file */
311
+ function readJson(file) {
312
+ try {
313
+ return JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, ''));
314
+ } catch {
315
+ return null;
316
+ }
317
+ }
318
+
319
+ /** Whether a package.json lists @astryxdesign/core itself. */
320
+ function usesCore(/** @type {any} */ json) {
321
+ if (!json || typeof json !== 'object') return false;
322
+ return ['dependencies', 'devDependencies', 'peerDependencies'].some(
323
+ k =>
324
+ json[k] != null &&
325
+ typeof json[k] === 'object' &&
326
+ !Array.isArray(json[k]) &&
327
+ Object.prototype.hasOwnProperty.call(json[k], '@astryxdesign/core'),
328
+ );
329
+ }
330
+
126
331
  /**
127
332
  * Why the components did not render, with the likely cause: two copies of
128
333
  * React (the app's and the one Core resolves) break every hook.
129
334
  * @param {import('./_project.mjs').ThemeProject} project
130
335
  * @param {string} message
131
336
  */
132
- function renderCause(project, message) {
337
+ export function renderCause(project, message) {
133
338
  /** @param {(id: string) => string} resolve */
134
339
  const reactOf = resolve => {
135
340
  try {
@@ -141,10 +346,15 @@ function renderCause(project, message) {
141
346
  const app = reactOf(project.req.resolve);
142
347
  let core = null;
143
348
  try {
144
- core = reactOf(
145
- createRequire(project.req.resolve('@astryxdesign/core/package.json'))
146
- .resolve,
147
- );
349
+ // Core's exports keep its package.json private: its folder is found by
350
+ // walking up from the entry the app resolves.
351
+ let dir = path.dirname(project.req.resolve('@astryxdesign/core'));
352
+ while (!fs.existsSync(path.join(dir, 'package.json'))) {
353
+ const up = path.dirname(dir);
354
+ if (up === dir) throw new Error('no package.json');
355
+ dir = up;
356
+ }
357
+ core = reactOf(createRequire(path.join(dir, 'package.json')).resolve);
148
358
  } catch {
149
359
  // No Core package file: nothing to compare.
150
360
  }
@@ -219,6 +429,18 @@ export function fontNotes(ctx, data) {
219
429
  : `${family} is not installed, so it does not load: the previews and the app show the next font in the stack. Install it: ${installLine(ctx.project.cwd, [pkg])}. Then the preview imports it on the next set or show, and the app imports it once: import '${pkg}'; Until then, the closest font that needs no install is ${near}: ${P} set ${data.name} font ${near}`,
220
430
  );
221
431
  }
432
+ // A family the editor's own presets lead with, where the preset gets no
433
+ // note (Comic Sans MS, of comic), is no more a font to load typed by name.
434
+ const presetFamily = (/** @type {string} */ family) => {
435
+ const f = family.replace(/["']/g, '').trim().toLowerCase();
436
+ // A one-system font keeps its note, preset or not.
437
+ if (oneSystemOf(family.replace(/["']/g, '').trim())) return false;
438
+ return Object.entries(FONTS).some(
439
+ ([k, [first]]) =>
440
+ (SYSTEM_FONTS.includes(k) || k === 'comic' || k === 'rounded') &&
441
+ first.replace(/["']/g, '').trim().toLowerCase() === f,
442
+ );
443
+ };
222
444
  for (const s of data.settings) {
223
445
  if (
224
446
  s.thing !== 'font' ||
@@ -228,7 +450,8 @@ export function fontNotes(ctx, data) {
228
450
  fontOf(s.value).pkg ||
229
451
  packages.some(x => x.of === s) ||
230
452
  s.value === 'rounded' ||
231
- s.value === 'comic'
453
+ s.value === 'comic' ||
454
+ presetFamily(fontOf(s.value).family)
232
455
  )
233
456
  continue;
234
457
  const family = fontOf(s.value).family.replace(/["']/g, '').toLowerCase();
@@ -314,8 +537,17 @@ export function styledChanges(before, after) {
314
537
  `typography ${role}: ${
315
538
  x
316
539
  ? Object.entries(x)
317
- .map(([k, v]) => `${k} ${v}`)
318
- .join(', ')
540
+ .map(
541
+ ([k, v]) =>
542
+ `${k} ${
543
+ v && typeof v === 'object'
544
+ ? Object.entries(v)
545
+ .map(([a, b]) => `${a}: ${b}`)
546
+ .join(', ')
547
+ : v
548
+ }`,
549
+ )
550
+ .join('; ')
319
551
  : '(removed)'
320
552
  }`,
321
553
  );
@@ -356,7 +588,7 @@ export async function previewAfter(ctx, slug, data, png) {
356
588
  const f = themeFiles(ctx.project.cwd, slug);
357
589
  // The sheet of every component is made by show only: after a change it
358
590
  // says, at its top, that it shows the theme as it was.
359
- const sheetStale = markSheetStale(f.sheet, slug);
591
+ const sheetStale = markSheetStale(f.sheet, slug, ctx.project.cwd);
360
592
  const shots =
361
593
  png && preview
362
594
  ? await pictures(
@@ -380,19 +612,86 @@ export async function previewAfter(ctx, slug, data, png) {
380
612
 
381
613
  const STALE_MARK = '<!-- astryx: sheet older than the theme -->';
382
614
 
615
+ /** The build a full sheet was made from, recorded in the sheet. */
616
+ const SHEET_STAMP = /<!-- astryx: sheet of build ([0-9a-f]{16}) -->/;
617
+
618
+ /** @param {string} s */
619
+ const escapeForRegExp = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
620
+
621
+ /**
622
+ * A hash of a theme's built stylesheet and module, or null when either is
623
+ * missing.
624
+ * @param {string} cwd
625
+ * @param {string} slug
626
+ */
627
+ function buildHash(cwd, slug) {
628
+ const f = themeFiles(cwd, slug);
629
+ try {
630
+ return createHash('sha256')
631
+ .update(fs.readFileSync(f.css))
632
+ .update('\0')
633
+ .update(fs.readFileSync(f.js))
634
+ .digest('hex')
635
+ .slice(0, 16);
636
+ } catch {
637
+ return null;
638
+ }
639
+ }
640
+
641
+ /**
642
+ * Record in a freshly made full sheet the build it shows.
643
+ * @param {string} file
644
+ * @param {string} slug
645
+ * @param {string} cwd
646
+ */
647
+ export function stampSheet(file, slug, cwd) {
648
+ const hash = buildHash(cwd, slug);
649
+ if (!hash) return;
650
+ try {
651
+ const html = fs.readFileSync(file, 'utf8').replace(SHEET_STAMP, '');
652
+ // At the end: a comment before the doctype would put the page in
653
+ // quirks mode.
654
+ writeWhole(
655
+ file,
656
+ `${html.trimEnd()}\n<!-- astryx: sheet of build ${hash} -->\n`,
657
+ );
658
+ } catch (e) {
659
+ if (/** @type {any} */ (e)?.lockLost) throw e;
660
+ }
661
+ }
662
+
383
663
  /**
384
664
  * Put a notice at the top of a sheet the last change made out of date
385
- * (once). Returns whether there is such a sheet.
665
+ * (once), or take it off a sheet made from the build there is now.
666
+ * Returns whether the sheet is out of date.
386
667
  * @param {string} file
387
668
  * @param {string} slug
669
+ * @param {string} cwd
388
670
  */
389
- function markSheetStale(file, slug) {
671
+ function markSheetStale(file, slug, cwd) {
390
672
  let html;
391
673
  try {
392
674
  html = fs.readFileSync(file, 'utf8');
393
675
  } catch {
394
676
  return false;
395
677
  }
678
+ // A sheet made from the build there is now (a change, then its undo)
679
+ // shows the theme as it is: no banner, and the result says nothing.
680
+ const made = SHEET_STAMP.exec(html)?.[1];
681
+ if (made && made === buildHash(cwd, slug)) {
682
+ if (html.includes(STALE_MARK)) {
683
+ const clean = html.replace(
684
+ new RegExp(`${escapeForRegExp(STALE_MARK)}<div[^>]*>[\\s\\S]*?</div>`),
685
+ '',
686
+ );
687
+ try {
688
+ writeWhole(file, clean);
689
+ } catch (e) {
690
+ if (/** @type {any} */ (e)?.lockLost) throw e;
691
+ }
692
+ }
693
+ return false;
694
+ }
396
695
  if (html.includes(STALE_MARK)) return true;
397
696
  const banner = `${STALE_MARK}<div style="position:sticky;top:0;z-index:9999;padding:8px 12px;background:#fff3cd;color:#3d2c00;font:14px system-ui,sans-serif;border-bottom:1px solid #e0c870">This sheet shows ${slug} as it was before the last change. Run ${P} show ${slug} to make it again.</div>`;
398
697
  const at = html.search(/<body[^>]*>/i);
@@ -8,6 +8,29 @@
8
8
  * @returns {ReadonlySet<string>}
9
9
  */
10
10
  export function cssProperties(): ReadonlySet<string>;
11
+ /**
12
+ * Every longhand a shorthand sets, through shorthands inside it (border
13
+ * sets border-color, so border-top-color), as MDN's data lists them; empty
14
+ * for a property that is no shorthand.
15
+ * @param {string} prop
16
+ * @returns {Set<string>}
17
+ */
18
+ export function longhandsOf(prop: string): Set<string>;
19
+ /**
20
+ * Whether setting `a` sets `b` too: `b` is one of `a`'s longhands, or a
21
+ * shorthand whose every longhand `a` sets (border covers border-top).
22
+ * overflow-wrap is no part of overflow, nor transform-origin of transform.
23
+ * @param {string} a
24
+ * @param {string} b
25
+ */
26
+ export function coversProperty(a: string, b: string): boolean;
27
+ /**
28
+ * The value as it reads when each variable is unset: every `var(--x, f)`
29
+ * replaced by its fallback `f`; null when no var() has one.
30
+ * @param {string} value
31
+ * @returns {string | null}
32
+ */
33
+ export function withFallbacks(value: string): string | null;
11
34
  /**
12
35
  * Whether a value matches a property's grammar, as css-tree reads it. A
13
36
  * var() or env() can hold anything, so the value matches when some
@@ -29,7 +52,8 @@ export function matchType(type: string, value: string): any | null;
29
52
  /**
30
53
  * Whether a value that matches a property's grammar is still below a range
31
54
  * the specs set and css-tree's data does not carry: a `<line-width>` (a
32
- * border or outline width) and a duration are never negative.
55
+ * border or outline width) and a duration are never negative, nor is any
56
+ * number the current specifications' grammar ranges at 0 or more.
33
57
  * @param {string} property
34
58
  * @param {string} value
35
59
  */
@@ -51,6 +75,12 @@ export function numbersIn(ast: any): Array<{
51
75
  * @returns {string[]}
52
76
  */
53
77
  export function keywordsOf(prop: string): string[];
78
+ /**
79
+ * The CSS generic font families (serif, math, ui-rounded …), as the
80
+ * grammar's `<generic-family>` type lists them; non-standard ones left out.
81
+ * @returns {string[]}
82
+ */
83
+ export function genericFamilies(): string[];
54
84
  /**
55
85
  * A value the property takes, to put in a suggestion: its initial value
56
86
  * from MDN's data (a shorthand's first longhand's).
@@ -58,6 +88,25 @@ export function keywordsOf(prop: string): string[];
58
88
  * @returns {string | null}
59
89
  */
60
90
  export function initialOf(prop: string): string | null;
91
+ /**
92
+ * One CSS number, percentage or dimension, read by css-tree's tokenizer as
93
+ * the browser reads it (`1.5e0`, `+1.5`, `15E-1`, `1.5EM`; never `50.%`),
94
+ * or null for anything else.
95
+ * @param {string} value
96
+ * @returns {{value: number, unit: string} | null}
97
+ */
98
+ export function cssNumber(value: string): {
99
+ value: number;
100
+ unit: string;
101
+ } | null;
102
+ /**
103
+ * A value split at `sep` outside brackets: `a(b, c), d` by `,` is
104
+ * [`a(b, c)`, ` d`]. A space separator also splits runs of spaces.
105
+ * @param {string} v
106
+ * @param {',' | ' ' | '/'} sep
107
+ * @returns {string[]}
108
+ */
109
+ export function splitTop(v: string, sep: "," | " " | "/"): string[];
61
110
  /**
62
111
  * The parts of a CSS `font` value, by the longhand each sets, as written:
63
112
  * `italic small-caps 700 1rem/1 Arial` gives font-style, font-variant-caps,
@@ -79,6 +128,16 @@ export function grammarText(syntax: any): any;
79
128
  * @param {string} prop
80
129
  */
81
130
  export function takesPercentage(prop: string): boolean;
131
+ /**
132
+ * A font family list with its missing commas put back, read with css-tree:
133
+ * within one comma-separated entry, a quoted family next to another quoted
134
+ * family or a generic one (`"Open Sans" serif`) is two families. Returns the
135
+ * list as CSS writes it (`"Open Sans", serif`), or null when nothing is
136
+ * missing, the value does not parse, or a family would hold a quote.
137
+ * @param {string} value
138
+ * @returns {string | null}
139
+ */
140
+ export function familyListMeant(value: string): string | null;
82
141
  export function propertyGrammar(prop: string): any | null;
83
142
  /** The math functions, by name, for a check of whether a value holds one. */
84
143
  export const MATH_FUNCTION: RegExp;