@astryxdesign/cli 0.7.15-canary.gea16e8d → 0.7.15

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 (134) hide show
  1. package/CHANGELOG.md +353 -3
  2. package/README.md +63 -63
  3. package/api/build/_adapter.d.mts +4 -0
  4. package/api/build/_adapter.mjs +28 -2
  5. package/api/build/blocks/blocks.d.mts +10 -1
  6. package/api/build/blocks/blocks.mjs +84 -7
  7. package/api/build/build.mjs +6 -1
  8. package/api/build/tree.d.mts +4 -0
  9. package/api/build/tree.mjs +9 -2
  10. package/api/doctor/_adapter.d.mts +2 -1
  11. package/api/doctor/_adapter.mjs +6 -1
  12. package/api/doctor/doctor.mjs +2 -2
  13. package/api/doctor/theme-checks.mjs +56 -21
  14. package/api/integration/add-helpers.mjs +5 -0
  15. package/api/integration/add-theme.mjs +15 -110
  16. package/api/integration/validate-integration.mjs +17 -3
  17. package/api/integration/validateIntegration.doc.mjs +1 -1
  18. package/api/theme/_adapter.d.mts +37 -5
  19. package/api/theme/_adapter.mjs +363 -50
  20. package/api/theme/_fork.d.mts +90 -0
  21. package/api/theme/_fork.mjs +246 -0
  22. package/api/theme/build/build.d.mts +25 -1
  23. package/api/theme/build/build.mjs +352 -41
  24. package/api/theme/eject/eject.mjs +36 -15
  25. package/api/theme/list/list.mjs +3 -6
  26. package/api/theme/palette/generate/generator.d.mts +13 -0
  27. package/api/theme/palette/generate/generator.mjs +32 -2
  28. package/api/theme/targets/targets.mjs +10 -3
  29. package/api/theme/template/template.mjs +48 -2
  30. package/api/theme/theme.type.d.mts +8 -0
  31. package/api/theme/theme.type.mjs +5 -1
  32. package/api/theme/themeBuild.doc.mjs +2 -2
  33. package/api/theme/themeRemove.doc.mjs +5 -2
  34. package/api/theme/themeTemplate.doc.mjs +2 -2
  35. package/api/theme/themeUse.doc.mjs +8 -2
  36. package/api/theme-edit/_adapter.mjs +157 -36
  37. package/api/theme-edit/_catalog.d.mts +1 -6
  38. package/api/theme-edit/_catalog.mjs +5 -268
  39. package/api/theme-edit/_check.d.mts +3 -1
  40. package/api/theme-edit/_check.mjs +733 -308
  41. package/api/theme-edit/_color.d.mts +19 -0
  42. package/api/theme-edit/_color.mjs +277 -33
  43. package/api/theme-edit/_compiler.d.mts +1 -0
  44. package/api/theme-edit/_compiler.mjs +25 -0
  45. package/api/theme-edit/_context.d.mts +2 -0
  46. package/api/theme-edit/_context.mjs +83 -9
  47. package/api/theme-edit/_css.d.mts +84 -0
  48. package/api/theme-edit/_css.mjs +700 -0
  49. package/api/theme-edit/_edit.mjs +370 -85
  50. package/api/theme-edit/_how-commands.d.mts +5 -0
  51. package/api/theme-edit/_how-commands.mjs +20 -13
  52. package/api/theme-edit/_how-question.d.mts +29 -0
  53. package/api/theme-edit/_how-question.mjs +1573 -182
  54. package/api/theme-edit/_how.mjs +306 -22
  55. package/api/theme-edit/_overview.mjs +2 -7
  56. package/api/theme-edit/_palette.d.mts +27 -5
  57. package/api/theme-edit/_palette.mjs +231 -97
  58. package/api/theme-edit/_parts.d.mts +42 -10
  59. package/api/theme-edit/_parts.mjs +224 -69
  60. package/api/theme-edit/_preview-app.d.mts +8 -0
  61. package/api/theme-edit/_preview-app.mjs +549 -96
  62. package/api/theme-edit/_preview.d.mts +7 -16
  63. package/api/theme-edit/_preview.mjs +77 -84
  64. package/api/theme-edit/_project.d.mts +20 -1
  65. package/api/theme-edit/_project.mjs +43 -13
  66. package/api/theme-edit/_settings.d.mts +96 -20
  67. package/api/theme-edit/_settings.mjs +646 -141
  68. package/api/theme-edit/_tables.d.mts +18 -12
  69. package/api/theme-edit/_tables.mjs +144 -147
  70. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  71. package/api/theme-edit/theme-edit.type.mjs +3 -1
  72. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  73. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  74. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  75. package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
  76. package/api/theme-edit/themeEditSet.doc.mjs +1 -1
  77. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  78. package/assets/docs/tree/checks.doc.mjs +22 -1
  79. package/assets/docs/tree/publishing.doc.mjs +1 -0
  80. package/assets/docs.generated.json +2 -2
  81. package/assets/self-audit.generated.json +1 -1
  82. package/authoring/integration/integration.doc.mjs +8 -0
  83. package/authoring/integration/schema.d.mts +8 -0
  84. package/authoring/integration/schema.mjs +1 -0
  85. package/authoring/integration/type.ts +9 -0
  86. package/clients/cli/build-id.generated.json +1 -1
  87. package/clients/cli/commands/experimental/index.mjs +10 -10
  88. package/clients/cli/commands/experimental/theme.mjs +22 -3
  89. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  90. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
  91. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  92. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  93. package/clients/cli/commands/integration.mjs +8 -0
  94. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  95. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  96. package/clients/cli/commands/theme-template.doc.mjs +1 -1
  97. package/clients/cli/commands/theme.mjs +325 -48
  98. package/clients/cli/index.mjs +18 -3
  99. package/clients/cli/lib/cli-error.mjs +118 -3
  100. package/clients/cli/manifest.generated.json +1 -1
  101. package/foundation/config/project.mjs +34 -8
  102. package/foundation/config/theme-state.d.mts +16 -0
  103. package/foundation/config/theme-state.mjs +84 -13
  104. package/foundation/discovery/template-needs.d.mts +13 -0
  105. package/foundation/discovery/template-needs.mjs +30 -15
  106. package/foundation/discovery/theme-discovery.mjs +14 -2
  107. package/foundation/discovery/theme-imports.d.mts +16 -0
  108. package/foundation/discovery/theme-imports.mjs +30 -2
  109. package/foundation/doc-compiler/import.mjs +10 -4
  110. package/foundation/fs/gather-cache.mjs +82 -23
  111. package/foundation/fs/jiti-cache.d.mts +21 -0
  112. package/foundation/fs/jiti-cache.mjs +66 -0
  113. package/foundation/fs/module-loader.mjs +7 -2
  114. package/foundation/integrations/autolink.d.mts +1 -24
  115. package/foundation/integrations/autolink.mjs +7 -60
  116. package/foundation/integrations/declared-dependencies.d.mts +27 -0
  117. package/foundation/integrations/declared-dependencies.mjs +73 -0
  118. package/foundation/integrations/integrations.d.mts +8 -0
  119. package/foundation/integrations/integrations.mjs +4 -0
  120. package/foundation/integrations/may-have-integrations.d.mts +8 -0
  121. package/foundation/integrations/may-have-integrations.mjs +47 -0
  122. package/foundation/integrations/validate-contributions.d.mts +62 -1
  123. package/foundation/integrations/validate-contributions.mjs +452 -29
  124. package/foundation/release/cache.d.mts +21 -2
  125. package/foundation/release/cache.mjs +48 -8
  126. package/foundation/release/folder-lock.d.mts +7 -0
  127. package/foundation/release/folder-lock.mjs +60 -26
  128. package/foundation/release/lock-lease.d.mts +2 -1
  129. package/foundation/release/lock-lease.mjs +7 -2
  130. package/foundation/response/error-codes.doc.mjs +1 -1
  131. package/foundation/response/response-types.doc.mjs +5 -5
  132. package/package.json +6 -6
  133. package/api/theme-edit/_css-properties.d.mts +0 -10
  134. package/api/theme-edit/_css-properties.mjs +0 -556
@@ -17,6 +17,7 @@ import {
17
17
  CORNERS,
18
18
  DIALS,
19
19
  FAMILIES,
20
+ FONT_ROLES,
20
21
  KEY_TOKENS,
21
22
  LIKE,
22
23
  P,
@@ -29,6 +30,7 @@ import {
29
30
  compileTheme,
30
31
  cornerPx,
31
32
  radiusAt,
33
+ radiusTokenOf,
32
34
  contrastProblems,
33
35
  describe,
34
36
  keyOf,
@@ -36,7 +38,6 @@ import {
36
38
  maskedSettings,
37
39
  modesOf,
38
40
  nearest,
39
- partDefault,
40
41
  partsOf,
41
42
  undoArgs,
42
43
  withoutBadValues,
@@ -51,6 +52,12 @@ import {
51
52
  buildTheme,
52
53
  staleBuild,
53
54
  EDITOR_SUFFIXES,
55
+ madeByEditor,
56
+ NEW_MARKER,
57
+ clearNewMarker,
58
+ refuseThemeSource,
59
+ settingsShapeOk,
60
+ slugFor,
54
61
  themeSource,
55
62
  themeSourceProblem,
56
63
  } from './_settings.mjs';
@@ -63,10 +70,14 @@ import {
63
70
  paletteMapping,
64
71
  paletteDialOf,
65
72
  paletteNotes,
73
+ lockHeld,
74
+ refuseIfLockLost,
75
+ writeWhole,
66
76
  } from './_palette.mjs';
67
77
  import {renderPreview, renderSheet} from './_preview.mjs';
68
78
  import {refreshPreviewRoute} from './_preview-app.mjs';
69
79
  import {baseResolved, installLine} from './_project.mjs';
80
+ import {reactMissing} from './_parts.mjs';
70
81
  import {
71
82
  requireProject,
72
83
  context,
@@ -85,25 +96,38 @@ const E = ERROR_CODES;
85
96
  * @typedef {import('./theme-edit.type.mjs').ThemeEditChangeOptions} ChangeOptions
86
97
  */
87
98
  /** The marker a `new` writes first and removes last. */
88
- const NEW_MARKER = '.astryx-new.json';
89
99
 
90
100
  /** Files a system or a tool puts in any folder; a folder of only these is empty. */
91
101
  const FOLDER_JUNK = new Set(['.DS_Store', '.gitkeep', 'Thumbs.db']);
92
102
 
93
103
  /**
94
- * What a stopped `new` left, when the folder holds its marker and only
95
- * files the editor writes for that name (its generated files, the source
96
- * exactly as it writes it, its pages, colors and temp files); null when
97
- * there is no marker or anything else is there. System files (.DS_Store)
98
- * stay.
104
+ * A folder the editor made but that has no settings: a stopped `new` left
105
+ * its marker, or the settings were removed beside the editor's generated
106
+ * module. `clear` is what `new` makes again (the files the editor writes
107
+ * for that name, the source only exactly as it writes it); `keep` is every
108
+ * other file, which is never touched. Null when the editor did not make
109
+ * the folder. System files (.DS_Store) are in neither.
99
110
  * @param {string} dir
100
111
  * @param {string} slug
101
- * @returns {string[] | null}
112
+ * @param {boolean} [damaged] its settings are there but not valid JSON
113
+ * @returns {{clear: string[], keep: string[]} | null}
102
114
  */
103
- function editorLeftovers(dir, slug) {
104
- if (!fs.existsSync(path.join(dir, NEW_MARKER))) return null;
115
+ function editorLeftovers(dir, slug, damaged = false) {
116
+ // The editor made this folder: a stopped `new` left its marker, the
117
+ // generated settings module (which carries the editor's header) is there
118
+ // without its settings, or its settings are there but damaged (kept
119
+ // aside by `new`, so neither cleared nor kept here).
120
+ const marked = fs.existsSync(path.join(dir, NEW_MARKER));
121
+ if (
122
+ !marked &&
123
+ !damaged &&
124
+ !madeByEditor(path.join(dir, `${slug}.settings.ts`))
125
+ )
126
+ return null;
105
127
  /** @type {string[]} */
106
- const files = [];
128
+ const clear = [];
129
+ /** @type {string[]} */
130
+ const keep = [];
107
131
  const named = (/** @type {string} */ n) =>
108
132
  n === NEW_MARKER ||
109
133
  EDITOR_SUFFIXES.some(x => n === `${slug}.${x}`) ||
@@ -116,22 +140,41 @@ function editorLeftovers(dir, slug) {
116
140
  for (const e of fs.readdirSync(dir, {withFileTypes: true})) {
117
141
  const p = path.join(dir, e.name);
118
142
  if (FOLDER_JUNK.has(e.name)) continue;
143
+ if (damaged && e.name === `${slug}.settings.json`) continue;
119
144
  if (e.isDirectory() && e.name === 'tokens') {
120
- for (const t of fs.readdirSync(p, {withFileTypes: true})) {
121
- if (!t.isFile() || !t.name.startsWith(`${slug}.palette`)) return null;
122
- files.push(path.join(p, t.name));
123
- }
145
+ for (const t of fs.readdirSync(p, {withFileTypes: true}))
146
+ (t.isFile() && t.name.startsWith(`${slug}.palette`)
147
+ ? clear
148
+ : keep
149
+ ).push(path.join(p, t.name));
124
150
  continue;
125
151
  }
126
- if (!e.isFile()) return null;
127
- if (e.name === `${slug}.theme.ts`) {
128
- // Only the source exactly as the editor writes it, never one changed
129
- // by hand.
130
- if (fs.readFileSync(p, 'utf8') !== themeSource(slug)) return null;
131
- } else if (!named(e.name)) return null;
132
- files.push(p);
152
+ // The source as the editor writes it is made again; one changed by hand
153
+ // is the user's, and stays. The history stays too when the settings were
154
+ // removed, so `undo` can step back to them.
155
+ const mine =
156
+ e.isFile() &&
157
+ (e.name === `${slug}.theme.ts`
158
+ ? fs.readFileSync(p, 'utf8') === themeSource(slug)
159
+ : // A `new` writes no history: one here is from later commands,
160
+ // and always stays.
161
+ e.name === `${slug}.history.json`
162
+ ? false
163
+ : named(e.name));
164
+ (mine ? clear : keep).push(p);
165
+ }
166
+ return {clear, keep};
167
+ }
168
+
169
+ /** Whether a settings file parses, in the shape the editor reads. @param {string} file */
170
+ function readableJson(file) {
171
+ try {
172
+ return settingsShapeOk(
173
+ JSON.parse(fs.readFileSync(file, 'utf8').replace(/^\uFEFF/, '')),
174
+ );
175
+ } catch {
176
+ return false;
133
177
  }
134
- return files;
135
178
  }
136
179
 
137
180
  /**
@@ -149,15 +192,7 @@ export async function themeEditNew(name, options = {}) {
149
192
  const like = options.like === 'none' ? undefined : options.like;
150
193
  requireProject(cwd);
151
194
  if (!slugOk(name)) {
152
- const fixed =
153
- String(name ?? '')
154
- .normalize('NFKD')
155
- .replace(/[\u0300-\u036f]/g, '')
156
- .toLowerCase()
157
- .replace(/[^a-z0-9-]+/g, '-')
158
- .replace(/^[^a-z]+/, '')
159
- .replace(/-+$/, '')
160
- .slice(0, 64) || 'my-theme';
195
+ const fixed = slugFor(name);
161
196
  // The name as typed may be a theme that exists in another case.
162
197
  const exists = fs.existsSync(themeFiles(cwd, fixed).settings);
163
198
  refuse(
@@ -177,19 +212,26 @@ export async function themeEditNew(name, options = {}) {
177
212
  const f = themeFiles(cwd, name);
178
213
  // A link or a file where the theme goes is never written through or
179
214
  // replaced.
180
- for (const [p, what] of /** @type {const} */ ([
181
- [f.root, 'themes/'],
182
- [f.dir, `themes/${name}`],
183
- ])) {
184
- const st = fs.lstatSync(p, {throwIfNoEntry: false});
185
- if (st && (st.isSymbolicLink() || !st.isDirectory()))
186
- refuse(
187
- E.ERR_FILE_EXISTS,
188
- `${what} is ${st.isSymbolicLink() ? 'a link' : 'a file'}, so a theme is not made there.`,
189
- {run: [`${P} new ${name}-copy`]},
190
- );
191
- }
192
- if (fs.existsSync(f.settings))
215
+ // themes/ itself in the way blocks every name: say so, and how to fix it.
216
+ const rootSt = fs.lstatSync(f.root, {throwIfNoEntry: false});
217
+ if (rootSt && (rootSt.isSymbolicLink() || !rootSt.isDirectory()))
218
+ refuse(
219
+ E.ERR_FILE_EXISTS,
220
+ `themes in this app is ${rootSt.isSymbolicLink() ? `a link (to ${fs.readlinkSync(f.root)})` : 'a file'}, not a folder, so no editor theme can be made: every theme goes in themes/<name>/. Rename or move it, then run new again.`,
221
+ // A look that changes nothing: the themes the app can use now.
222
+ {run: ['astryx theme list']},
223
+ );
224
+ const dirSt = fs.lstatSync(f.dir, {throwIfNoEntry: false});
225
+ if (dirSt && (dirSt.isSymbolicLink() || !dirSt.isDirectory()))
226
+ refuse(
227
+ E.ERR_FILE_EXISTS,
228
+ `themes/${name} is ${dirSt.isSymbolicLink() ? 'a link' : 'a file'}, so a theme is not made there.`,
229
+ {run: [`${P} new ${name}-copy`]},
230
+ );
231
+ // Settings that are not valid JSON are kept aside, and the theme is
232
+ // made again beside them.
233
+ const damaged = fs.existsSync(f.settings) && !readableJson(f.settings);
234
+ if (fs.existsSync(f.settings) && !damaged)
193
235
  refuse(E.ERR_FILE_EXISTS, `Theme "${name}" already exists.`, {
194
236
  run: [`${P} show ${name}`, `${P} set ${name} base neutral`],
195
237
  });
@@ -197,7 +239,9 @@ export async function themeEditNew(name, options = {}) {
197
239
  // beside the files it wrote: only such a folder is cleared, and only
198
240
  // after every argument is checked. Anything else in the folder, or a
199
241
  // folder without the marker, is never touched.
200
- const left = fs.existsSync(f.dir) ? editorLeftovers(f.dir, name) : null;
242
+ const left = fs.existsSync(f.dir)
243
+ ? editorLeftovers(f.dir, name, damaged)
244
+ : null;
201
245
  const holdsOther =
202
246
  fs.existsSync(f.dir) &&
203
247
  !left &&
@@ -224,38 +268,68 @@ export async function themeEditNew(name, options = {}) {
224
268
  ctx.project,
225
269
  data.like,
226
270
  );
271
+ // Every check comes before the first change to the folder: the settings,
272
+ // and a source of the user's own kept from before (it must still extend
273
+ // the settings, or the build fails after the folder was cleared).
227
274
  applyNewSettings(ctx, name, data, settings);
275
+ if (left?.keep.some(p => path.basename(p) === `${name}.theme.ts`))
276
+ refuseThemeSource(cwd, name);
277
+ /** @type {string[]} */
278
+ const keptNotes = [];
228
279
  /** @type {string[]} */
229
280
  const leftNotes = [];
230
- if (left && left.length) {
231
- for (const p of left) fs.rmSync(p, {force: true});
232
- try {
233
- fs.rmdirSync(path.join(f.dir, 'tokens'));
234
- } catch {
235
- // Not there, or it holds something else.
236
- }
237
- const shownLeft = left.filter(p => path.basename(p) !== NEW_MARKER);
281
+ const ownKept = (left?.keep ?? []).filter(
282
+ p => path.basename(p) !== `${name}.history.json`,
283
+ );
284
+ if (ownKept.length)
238
285
  leftNotes.push(
239
- `cleared ${shownLeft.length} file${shownLeft.length === 1 ? '' : 's'} a stopped \`new\` left in themes/${name}/ (${shownLeft
286
+ `kept your own files in themes/${name}/: ${ownKept
240
287
  .slice(0, 4)
241
288
  .map(p => path.relative(f.dir, p))
242
- .join(', ')}${shownLeft.length > 4 ? ', …' : ''}), then made the theme`,
289
+ .join(', ')}${ownKept.length > 4 ? ', …' : ''}`,
290
+ );
291
+ if (left?.keep.some(p => path.basename(p) === `${name}.history.json`))
292
+ leftNotes.push(
293
+ `the earlier settings are kept in ${name}.history.json: \`${P} undo ${name}\` steps back to them`,
243
294
  );
244
- }
245
295
  // The marker goes first and comes off last: a `new` stopped between
246
- // leaves a folder the next `new` can prove it made.
296
+ // leaves a folder the next `new` can prove it made. The damaged settings
297
+ // are moved aside after it, and back on a failure.
247
298
  const dirMade = !fs.existsSync(f.dir);
248
299
  fs.mkdirSync(f.dir, {recursive: true});
249
300
  const marker = path.join(f.dir, NEW_MARKER);
250
- fs.writeFileSync(
251
- marker,
252
- JSON.stringify({pid: process.pid, started: Date.now()}),
301
+ // Fenced like every write under the theme lock.
302
+ writeWhole(marker, JSON.stringify({pid: process.pid, started: Date.now()}));
303
+ /** @type {string | null} */
304
+ let aside = null;
305
+ if (damaged) {
306
+ let n = 1;
307
+ const at = (/** @type {number} */ k) =>
308
+ path.join(f.dir, `${name}.settings.damaged${k > 1 ? `-${k}` : ''}.json`);
309
+ while (fs.existsSync(at(n))) n++;
310
+ aside = at(n);
311
+ refuseIfLockLost();
312
+ fs.renameSync(f.settings, aside);
313
+ keptNotes.push(
314
+ `${name}.settings.json was damaged: it is kept as ${path.basename(aside)}, and the theme was made again`,
315
+ );
316
+ }
317
+ // A file this build writes again is newer than this moment.
318
+ const before = new Map(
319
+ (left?.clear ?? []).map(p => [
320
+ p,
321
+ fs.statSync(p, {throwIfNoEntry: false})?.mtimeMs ?? null,
322
+ ]),
253
323
  );
254
324
  /** @type {Awaited<ReturnType<typeof commitTheme>>} */
255
325
  let r;
256
326
  try {
257
327
  r = await commitTheme(ctx, name, data, {history: false});
258
328
  } catch (e) {
329
+ // A command that lost its lock owns none of these files now: it puts
330
+ // nothing back and removes nothing.
331
+ if (/** @type {any} */ (e)?.lockLost || !lockHeld()) refuseIfLockLost();
332
+ if (aside && !fs.existsSync(f.settings)) fs.renameSync(aside, f.settings);
259
333
  fs.rmSync(marker, {force: true});
260
334
  if (dirMade)
261
335
  try {
@@ -265,10 +339,49 @@ export async function themeEditNew(name, options = {}) {
265
339
  }
266
340
  throw e;
267
341
  }
342
+ // Only after the theme is made: the editor's old files this build did not
343
+ // write again go (old pictures, a stopped run's temp files).
344
+ if (left && left.clear.length) {
345
+ // The build's own outputs are its now; the rest (old pictures, a
346
+ // stopped run's temp files and history) goes when this build did not
347
+ // write it again.
348
+ const outputs = [
349
+ 'css',
350
+ 'js',
351
+ 'd.ts',
352
+ 'css.d.ts',
353
+ 'settings.ts',
354
+ // The source exactly as the editor writes it, which the build keeps.
355
+ 'theme.ts',
356
+ ].map(x => `${name}.${x}`);
357
+ const gone = left.clear.filter(p => {
358
+ if (path.basename(p) === NEW_MARKER) return false;
359
+ if (outputs.includes(path.basename(p))) return false;
360
+ const now = fs.statSync(p, {throwIfNoEntry: false})?.mtimeMs;
361
+ return now != null && now === before.get(p);
362
+ });
363
+ refuseIfLockLost();
364
+ for (const p of gone) fs.rmSync(p, {force: true});
365
+ try {
366
+ fs.rmdirSync(path.join(f.dir, 'tokens'));
367
+ } catch {
368
+ // Not there, or it holds something else.
369
+ }
370
+ const stopped = left.clear.some(p => path.basename(p) === NEW_MARKER);
371
+ const shownLeft = left.clear.filter(p => path.basename(p) !== NEW_MARKER);
372
+ leftNotes.unshift(
373
+ `made the theme again over ${shownLeft.length} file${shownLeft.length === 1 ? '' : 's'} ${stopped ? 'a stopped `new` left' : 'the editor had left'} in themes/${name}/ (${shownLeft
374
+ .slice(0, 4)
375
+ .map(p => path.relative(f.dir, p))
376
+ .join(', ')}${shownLeft.length > 4 ? ', …' : ''})`,
377
+ );
378
+ }
379
+ refuseIfLockLost();
268
380
  fs.rmSync(marker, {force: true});
269
381
  const p = await previewAndRoute(ctx, name, data, png);
270
382
  /** @type {string[]} */
271
383
  const notes = [
384
+ ...keptNotes,
272
385
  ...leftNotes,
273
386
  ...(data.settings.some(s => s.thing === 'font')
274
387
  ? fontNotes(ctx, data)
@@ -569,7 +682,36 @@ async function change(sub, slug, thing, value, options) {
569
682
  (x.mode ?? null) === (flags.mode ?? null),
570
683
  )
571
684
  : null;
572
- if (!stored) throw e;
685
+ if (!stored) {
686
+ // An undo is told what it can undo, never a `set`: the settings of
687
+ // what it named (or of the dial its refusal names), else the last
688
+ // step.
689
+ if (sub === 'undo' && e instanceof AstryxError) {
690
+ const named = new Set([
691
+ thing,
692
+ ...(e.suggestions ?? [])
693
+ .map(x => new RegExp(` set ${slug} (\\S+)`).exec(x.name)?.[1])
694
+ .filter(Boolean),
695
+ ]);
696
+ const matches = data.settings.filter(x => named.has(x.thing));
697
+ const steps = readHistory(themeFiles(cwd, slug).history).steps.length;
698
+ throw new AstryxError(
699
+ e.message,
700
+ matches.length
701
+ ? matches.slice(0, 5).map(x => ({
702
+ name: `${P} undo ${slug} ${undoArgs(x)}`,
703
+ reason: 'do this instead',
704
+ }))
705
+ : [
706
+ steps
707
+ ? {name: `${P} undo ${slug}`, reason: 'step back one change'}
708
+ : {name: `${P} show ${slug}`, reason: 'see what it sets'},
709
+ ],
710
+ e.code,
711
+ );
712
+ }
713
+ throw e;
714
+ }
573
715
  s = stored;
574
716
  }
575
717
  // undo <thing> <value> removes the setting only when that is its value.
@@ -638,7 +780,11 @@ async function change(sub, slug, thing, value, options) {
638
780
  : []),
639
781
  ...fieldNotes,
640
782
  ...r.notes,
641
- `your ${data.settings.length} settings are kept`,
783
+ ...(data.settings.length
784
+ ? [
785
+ `your ${data.settings.length} setting${data.settings.length === 1 ? ' is' : 's are'} kept`,
786
+ ]
787
+ : []),
642
788
  ...otherModeNotes(data),
643
789
  // The colors are made again for the new base or mode: their
644
790
  // shade notes follow.
@@ -769,9 +915,46 @@ async function change(sub, slug, thing, value, options) {
769
915
  const r = await commitTheme(ctx, slug, data, {history: true});
770
916
  const styled = styledChanges(compiledBefore, compileTheme(ctx, data, base));
771
917
  const parts = partsOf(ctx, s);
918
+ // Exact whole-theme tokens that win over what this dial writes: the dial
919
+ // changes them only in a theme without them.
920
+ /** @param {import('./_settings.mjs').Setting[]} list */
921
+ const dialsOnly = list => ({
922
+ ...data,
923
+ settings: list.filter(x => x.source === 'dial'),
924
+ });
925
+ /** @type {import('./_settings.mjs').Setting[]} */
926
+ let exactWins = [];
927
+ if (sub === 'set' && s.source === 'dial' && !s.on) {
928
+ const withDial = compileTheme(ctx, dialsOnly(data.settings), base);
929
+ const without = compileTheme(
930
+ ctx,
931
+ dialsOnly(data.settings.filter(x => x !== s)),
932
+ base,
933
+ );
934
+ const differs = (/** @type {unknown} */ a, /** @type {unknown} */ b) =>
935
+ JSON.stringify(a) !== JSON.stringify(b);
936
+ const touched = new Set(
937
+ [
938
+ ...Object.keys(withDial.tokens ?? {}),
939
+ ...Object.keys(without.tokens ?? {}),
940
+ ].filter(k => differs(withDial.tokens?.[k], without.tokens?.[k])),
941
+ );
942
+ for (const r of FONT_ROLES)
943
+ if (differs(withDial.typography?.[r], without.typography?.[r]))
944
+ touched.add(`--font-family-${r}`);
945
+ if (differs(withDial.typography?.scale, without.typography?.scale))
946
+ for (const k of Object.keys(ctx.project.core.tokenDefaults ?? {}))
947
+ if (/^--font-size-/.test(k)) touched.add(k);
948
+ exactWins = data.settings.filter(
949
+ x => x.source === 'token' && !x.on && !x.when && touched.has(x.thing),
950
+ );
951
+ }
772
952
  const kept =
773
953
  sub === 'set' && s.source === 'dial' && !s.on
774
- ? data.settings.filter(x => x.thing === s.thing && x.on).map(describe)
954
+ ? [
955
+ ...data.settings.filter(x => x.thing === s.thing && x.on),
956
+ ...exactWins,
957
+ ].map(describe)
775
958
  : [];
776
959
  // The exact property a dial on a part competes with there.
777
960
  const DIAL_PROPERTY = /** @type {Record<string, RegExp>} */ ({
@@ -793,6 +976,11 @@ async function change(sub, slug, thing, value, options) {
793
976
  /** @type {string[]} */
794
977
  const notes = [
795
978
  ...(covered.length ? [`replaces ${covered.map(describe).join(', ')}`] : []),
979
+ ...(exactWins.length
980
+ ? [
981
+ `keeps your exact ${exactWins.map(describe).join(', ')}: an exact setting wins over a dial, so ${exactWins.length > 1 ? 'those do' : 'that does'} not change`,
982
+ ]
983
+ : []),
796
984
  ...(covers.length
797
985
  ? [
798
986
  `comes after ${covers.map(describe).join(', ')}, so ${covers.length > 1 ? 'those parts show' : `${covers[0].on} shows`} this value now; set ${covers.length > 1 ? 'one' : 'it'} again to make it win`,
@@ -1025,9 +1213,17 @@ export async function themeEditShow(theme, options = {}) {
1025
1213
  const data = loadTheme(cwd, theme);
1026
1214
  const ctx = await context(cwd);
1027
1215
  refuseStoredTokens(ctx, theme, data);
1028
- // show renders its pages with the project's React: when that cannot
1029
- // render, nothing is built or written, and the result says how to fix it.
1030
- if (ctx.unchecked && !readOnly)
1216
+ // Without React, show still lists the settings and builds the theme, as
1217
+ // set does; only the pages, pictures and the checks that render are
1218
+ // skipped, and the result says how to fix it.
1219
+ const noReact =
1220
+ !readOnly &&
1221
+ (reactMissing(cwd).length > 0 ||
1222
+ ctx.uncheckedCode === 'ERR_RENDER_MISSING_DEPENDENCY');
1223
+ const noPages = readOnly || noReact;
1224
+ // Any other render failure (two copies of React, a component that
1225
+ // throws) is refused, and nothing is written.
1226
+ if (ctx.unchecked && !readOnly && !noReact)
1031
1227
  refuse(
1032
1228
  E.ERR_RENDER_FAILED,
1033
1229
  `${theme} cannot be shown: ${ctx.unchecked} Nothing was written.`,
@@ -1037,7 +1233,9 @@ export async function themeEditShow(theme, options = {}) {
1037
1233
  run:
1038
1234
  ctx.uncheckedCode === 'ERR_RENDER_MISSING_DEPENDENCY'
1039
1235
  ? [installLine(cwd, ['react', 'react-dom'])]
1040
- : [`${P} show ${theme}`],
1236
+ : // Two copies of React and other install problems are named by
1237
+ // doctor; the same show would fail the same way.
1238
+ ['astryx doctor'],
1041
1239
  },
1042
1240
  );
1043
1241
  const f = themeFiles(cwd, theme);
@@ -1046,6 +1244,8 @@ export async function themeEditShow(theme, options = {}) {
1046
1244
  const stale = staleBuild(cwd, theme, data);
1047
1245
  const brokenSource = themeSourceProblem(cwd, theme);
1048
1246
  if (stale && !brokenSource && !readOnly) await buildTheme(ctx, theme, data);
1247
+ // The settings loaded whole: a `new` marker beside them is stale.
1248
+ if (!readOnly) clearNewMarker(f.dir);
1049
1249
  // A built module that cannot be read (damaged by hand) is built again.
1050
1250
  /** @type {string | null} */
1051
1251
  let unreadable = null;
@@ -1065,16 +1265,16 @@ export async function themeEditShow(theme, options = {}) {
1065
1265
  await baseResolved(ctx.project, data.like),
1066
1266
  ctx.project.core.tokenDefaults,
1067
1267
  );
1068
- const preview = readOnly
1268
+ const preview = noPages
1069
1269
  ? shown(cwd, f.preview)
1070
1270
  : await renderPreview(ctx, theme, data);
1071
- const previewStale = readOnly ? [] : await keepPreviewRoute(ctx, theme);
1072
- const sheet = readOnly
1271
+ const previewStale = noPages ? [] : await keepPreviewRoute(ctx, theme);
1272
+ const sheet = noPages
1073
1273
  ? {file: shown(cwd, f.sheet), components: 0, skipped: []}
1074
1274
  : await renderSheet(ctx, theme, data, {group: section});
1075
1275
  const modes = modesOf(data);
1076
1276
  const shots =
1077
- png && !readOnly
1277
+ png && !noPages
1078
1278
  ? await pictures(
1079
1279
  ctx.project,
1080
1280
  [
@@ -1101,6 +1301,11 @@ export async function themeEditShow(theme, options = {}) {
1101
1301
  return {...s, parts: partsOf(ctx, s), ...(masked ? {masked} : {})};
1102
1302
  }),
1103
1303
  notes: [
1304
+ ...(noReact
1305
+ ? [
1306
+ `react and react-dom cannot be found from this app, so the pages and pictures were not made again and the checks that render components were skipped; install them (${installLine(cwd, ['react', 'react-dom'])}), then run show again`,
1307
+ ]
1308
+ : []),
1104
1309
  ...(readOnly
1105
1310
  ? [
1106
1311
  `the theme lock could not be taken, so nothing was written: this is the last build as it is${stale ? ` (it does not match the settings: ${stale})` : ''}, and the pages were not made again`,
@@ -1177,6 +1382,56 @@ function nowApplies(ctx, data, removed, base) {
1177
1382
  : describe(d);
1178
1383
  }
1179
1384
  }
1385
+ // What still reaches where the removed setting applied: on a part, the
1386
+ // same thing set plainly on it, on its group, or for the whole theme; an
1387
+ // exact property on the part that this dial would have competed with.
1388
+ if (removed.on) {
1389
+ const fam = familyOf(removed.on);
1390
+ const reaches = [...data.settings]
1391
+ .reverse()
1392
+ .find(
1393
+ x =>
1394
+ x.thing === removed.thing &&
1395
+ x !== removed &&
1396
+ (!x.mode || x.mode === removed.mode) &&
1397
+ ((x.on === removed.on && !x.when && !x.state) ||
1398
+ (fam && x.on === fam && !x.when && !x.state)),
1399
+ );
1400
+ if (reaches) return describe(reaches);
1401
+ const DIAL_PROPERTY = /** @type {Record<string, RegExp>} */ ({
1402
+ corners: /radius$/,
1403
+ spacing: /^(padding|gap)(-|$)/,
1404
+ });
1405
+ const exact =
1406
+ removed.source === 'dial' && DIAL_PROPERTY[removed.thing]
1407
+ ? data.settings.find(
1408
+ x =>
1409
+ x.source === 'property' &&
1410
+ x.on === removed.on &&
1411
+ DIAL_PROPERTY[removed.thing].test(x.thing),
1412
+ )
1413
+ : null;
1414
+ if (exact) return describe(exact);
1415
+ // A removed exact property leaves the dial that writes it: on the part,
1416
+ // its group, or the whole theme.
1417
+ if (removed.source === 'property') {
1418
+ const dial = Object.entries(DIAL_PROPERTY).find(([, re]) =>
1419
+ re.test(removed.thing),
1420
+ )?.[0];
1421
+ const fromDial = dial
1422
+ ? [...data.settings]
1423
+ .reverse()
1424
+ .find(
1425
+ x =>
1426
+ x.thing === dial &&
1427
+ (x.on === removed.on || (fam && x.on === fam) || !x.on) &&
1428
+ (x.when ?? '') === (removed.when ?? '') &&
1429
+ (x.state ?? '') === (removed.state ?? ''),
1430
+ )
1431
+ : undefined;
1432
+ if (fromDial) return describe(fromDial);
1433
+ }
1434
+ }
1180
1435
  const whole =
1181
1436
  removed.on || removed.when || removed.state || removed.mode
1182
1437
  ? [...data.settings]
@@ -1236,6 +1491,20 @@ function partValues(ctx, data, res, mode) {
1236
1491
  // A setting a later group setting covers shows that one's value.
1237
1492
  const hidden = masked.get(s);
1238
1493
  if (hidden) return `${describe(s)} -> not shown: ${hidden}`;
1494
+ // An exact property on the part wins over the dial there.
1495
+ const exact = data.settings.find(
1496
+ x =>
1497
+ x.source === 'property' &&
1498
+ x.on &&
1499
+ (s.thing === 'corners'
1500
+ ? x.thing === 'border-radius'
1501
+ : /^(padding|gap)(-|$)/.test(x.thing)) &&
1502
+ partsOf(ctx, x).some(t => partsOf(ctx, s).includes(t)) &&
1503
+ (x.when ?? '') === (s.when ?? '') &&
1504
+ (x.state ?? '') === (s.state ?? ''),
1505
+ );
1506
+ if (exact)
1507
+ return `${describe(s)} -> not shown: ${describe(exact)} is an exact property and wins there`;
1239
1508
  const parts = partsOf(ctx, s);
1240
1509
  const listed = `${parts.slice(0, 4).join(', ')}${parts.length > 4 ? `, and ${parts.length - 4} more` : ''}`;
1241
1510
  if (s.thing === 'corners') {
@@ -1249,17 +1518,7 @@ function partValues(ctx, data, res, mode) {
1249
1518
  /** @type {Map<string, string[]>} */
1250
1519
  const byToken = new Map();
1251
1520
  for (const t of parts) {
1252
- const token =
1253
- /var\((--[\w-]+)\)/.exec(
1254
- partDefault(ctx, t, 'borderRadius', undefined) ?? '',
1255
- )?.[1] ??
1256
- // Every variant reads it (none is the resting one).
1257
- ctx.C.partReads(t, 'corners').find(x =>
1258
- x.props.includes('border-radius'),
1259
- )?.token ??
1260
- (['containers', 'menus'].includes(familyOf(t) ?? '')
1261
- ? '--radius-container'
1262
- : 'unknown');
1521
+ const token = radiusTokenOf(ctx, t);
1263
1522
  byToken.set(token, [...(byToken.get(token) ?? []), t]);
1264
1523
  }
1265
1524
  const whole = [...byToken]
@@ -1464,8 +1723,16 @@ export async function themeEditUndo(theme, options = {}) {
1464
1723
  const same = (/** @type {string} */ a, /** @type {string} */ b) => {
1465
1724
  try {
1466
1725
  return (
1467
- JSON.stringify({...JSON.parse(a.replace(/^\uFEFF/, '')), lifted: 0}) ===
1468
- JSON.stringify({...JSON.parse(b.replace(/^\uFEFF/, '')), lifted: 0})
1726
+ JSON.stringify({
1727
+ ...JSON.parse(a.replace(/^\uFEFF/, '')),
1728
+ lifted: 0,
1729
+ built: 0,
1730
+ }) ===
1731
+ JSON.stringify({
1732
+ ...JSON.parse(b.replace(/^\uFEFF/, '')),
1733
+ lifted: 0,
1734
+ built: 0,
1735
+ })
1469
1736
  );
1470
1737
  } catch {
1471
1738
  return false;
@@ -1486,6 +1753,23 @@ export async function themeEditUndo(theme, options = {}) {
1486
1753
  );
1487
1754
  // A step always restores into this theme, whatever name it holds.
1488
1755
  prev.name = theme;
1756
+ // What the step's build showed is restored with it, not saved as a
1757
+ // setting.
1758
+ const built = /** @type {any} */ (prev).built;
1759
+ delete (/** @type {any} */ (prev).built);
1760
+ const radii =
1761
+ built?.radii && typeof built.radii === 'object'
1762
+ ? /** @type {Record<string, string>} */ (
1763
+ Object.fromEntries(
1764
+ Object.entries(built.radii).filter(
1765
+ ([k, v]) =>
1766
+ /^--radius-(element|inner|container|page)$/.test(k) &&
1767
+ typeof v === 'string' &&
1768
+ /^\d+(\.\d+)?px$/.test(v),
1769
+ ),
1770
+ )
1771
+ )
1772
+ : null;
1489
1773
  // The step it goes back to is checked like the settings file itself. A
1490
1774
  // value the editor no longer accepts is left out of that step (with a
1491
1775
  // note); a step whose shape is broken is refused.
@@ -1510,6 +1794,7 @@ export async function themeEditUndo(theme, options = {}) {
1510
1794
  const committed = await commitTheme(ctx, theme, prev, {
1511
1795
  history: false,
1512
1796
  historyAfter: history,
1797
+ radii,
1513
1798
  });
1514
1799
  /** @type {string[]} */
1515
1800
  const undone = current