@astryxdesign/cli 0.7.14 → 0.7.15-canary.g061b2fa

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 (164) hide show
  1. package/README.md +63 -63
  2. package/api/build/_adapter.d.mts +4 -0
  3. package/api/build/_adapter.mjs +28 -2
  4. package/api/build/blocks/blocks.d.mts +10 -1
  5. package/api/build/blocks/blocks.mjs +84 -7
  6. package/api/build/build.mjs +6 -1
  7. package/api/build/tree.d.mts +4 -0
  8. package/api/build/tree.mjs +9 -2
  9. package/api/doctor/_adapter.d.mts +3 -1
  10. package/api/doctor/_adapter.mjs +14 -1
  11. package/api/doctor/_authoring-audit.d.mts +42 -0
  12. package/api/doctor/_authoring-audit.mjs +106 -0
  13. package/api/doctor/doctor.d.mts +7 -0
  14. package/api/doctor/doctor.mjs +24 -39
  15. package/api/doctor/theme-checks.mjs +56 -21
  16. package/api/integration/validate-integration.mjs +17 -3
  17. package/api/integration/validateIntegration.doc.mjs +1 -1
  18. package/api/search/_adapter.mjs +124 -3
  19. package/api/search/_candidate-score.mjs +63 -9
  20. package/api/search/_entries.d.mts +10 -0
  21. package/api/search/_entries.mjs +68 -1
  22. package/api/search/_gather-templates.d.mts +11 -0
  23. package/api/search/_gather-templates.mjs +22 -2
  24. package/api/search/_words.mjs +26 -6
  25. package/api/swizzle/copy/copy.mjs +1 -1
  26. package/api/theme/_adapter.d.mts +17 -1
  27. package/api/theme/_adapter.mjs +185 -40
  28. package/api/theme/build/build.d.mts +29 -1
  29. package/api/theme/build/build.mjs +426 -65
  30. package/api/theme/eject/eject.mjs +10 -6
  31. package/api/theme/targets/targets.d.mts +8 -0
  32. package/api/theme/targets/targets.mjs +24 -4
  33. package/api/theme/template/template.mjs +48 -2
  34. package/api/theme/theme.type.d.mts +8 -0
  35. package/api/theme/theme.type.mjs +5 -1
  36. package/api/theme/themeBuild.doc.mjs +7 -4
  37. package/api/theme/themeEject.doc.mjs +3 -3
  38. package/api/theme/themeRemove.doc.mjs +5 -2
  39. package/api/theme/themeTargets.doc.mjs +1 -1
  40. package/api/theme/themeTemplate.doc.mjs +2 -2
  41. package/api/theme/themeUse.doc.mjs +8 -2
  42. package/api/theme-edit/_adapter.d.mts +46 -3
  43. package/api/theme-edit/_adapter.mjs +372 -109
  44. package/api/theme-edit/_check.d.mts +6 -3
  45. package/api/theme-edit/_check.mjs +665 -87
  46. package/api/theme-edit/_color.mjs +4 -2
  47. package/api/theme-edit/_compiler.d.mts +2 -1
  48. package/api/theme-edit/_compiler.mjs +27 -13
  49. package/api/theme-edit/_context.d.mts +2 -1
  50. package/api/theme-edit/_context.mjs +47 -8
  51. package/api/theme-edit/_edit.d.mts +9 -3
  52. package/api/theme-edit/_edit.mjs +484 -66
  53. package/api/theme-edit/_how-commands.d.mts +33 -7
  54. package/api/theme-edit/_how-commands.mjs +57 -45
  55. package/api/theme-edit/_how-question.d.mts +4 -0
  56. package/api/theme-edit/_how-question.mjs +1004 -223
  57. package/api/theme-edit/_how.mjs +96 -9
  58. package/api/theme-edit/_palette.d.mts +38 -13
  59. package/api/theme-edit/_palette.mjs +620 -223
  60. package/api/theme-edit/_parts.mjs +63 -7
  61. package/api/theme-edit/_png.d.mts +4 -4
  62. package/api/theme-edit/_png.mjs +4 -2
  63. package/api/theme-edit/_preview-app.d.mts +6 -0
  64. package/api/theme-edit/_preview-app.mjs +1194 -207
  65. package/api/theme-edit/_preview.mjs +2 -2
  66. package/api/theme-edit/_project.d.mts +12 -1
  67. package/api/theme-edit/_project.mjs +37 -6
  68. package/api/theme-edit/_settings.d.mts +104 -33
  69. package/api/theme-edit/_settings.mjs +818 -118
  70. package/api/theme-edit/_tables.d.mts +7 -0
  71. package/api/theme-edit/_tables.mjs +61 -0
  72. package/api/theme-edit/theme-edit.type.d.mts +11 -1
  73. package/api/theme-edit/theme-edit.type.mjs +4 -3
  74. package/api/theme-edit/themeEditHow.doc.mjs +6 -0
  75. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  76. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  77. package/api/theme-edit/themeEditPreviewList.doc.mjs +1 -1
  78. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +5 -1
  79. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  80. package/api/upgrade/_adapter.d.mts +1 -0
  81. package/assets/codemods/run-codemod.mjs +61 -0
  82. package/assets/codemods/runner.mjs +5 -4
  83. package/assets/codemods/transforms/v0.0.14/rename-status-variants.mjs +3 -0
  84. package/assets/codemods/transforms/v0.1.8/rename-avatar-size-scale.mjs +5 -1
  85. package/assets/codemods/transforms/v0.6.4/migrate-native-picker-to-presentation.mjs +3 -0
  86. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +4 -0
  87. package/assets/codemods/transforms/v0.7.0/rename-empty-search-results-text.mjs +17 -12
  88. package/assets/codemods/transforms/v0.7.2/migrate-list-edge-compensation-to-edge.mjs +3 -0
  89. package/assets/codemods/transforms/v0.7.5/migrate-codeblock-tokenizer-to-language.mjs +3 -0
  90. package/assets/codemods/transforms/v0.7.8/promote-tokenizer-presentation.mjs +3 -0
  91. package/assets/docs/tree/calendars.doc.mjs +1 -3
  92. package/assets/docs/tree/checks.doc.mjs +22 -1
  93. package/assets/docs.generated.json +2 -1
  94. package/assets/self-audit.generated.json +1 -0
  95. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.doc.mjs +14 -0
  96. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.tsx +31 -0
  97. package/assets/templates/components.generated.json +1 -1
  98. package/authoring/integration/integration.doc.mjs +8 -0
  99. package/authoring/integration/schema.d.mts +8 -0
  100. package/authoring/integration/schema.mjs +1 -0
  101. package/authoring/integration/type.ts +9 -0
  102. package/clients/cli/bin/astryx.mjs +19 -15
  103. package/clients/cli/build-id.generated.json +1 -0
  104. package/clients/cli/commands/ensure-core-built.mjs +12 -5
  105. package/clients/cli/commands/experimental/theme.mjs +178 -78
  106. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  107. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +1 -1
  108. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -2
  109. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  110. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  111. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  112. package/clients/cli/commands/experimental-theme-eject.doc.mjs +3 -3
  113. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  114. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  115. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +1 -1
  116. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +1 -1
  117. package/clients/cli/commands/experimental-theme-remove.doc.mjs +1 -1
  118. package/clients/cli/commands/experimental-theme-template.doc.mjs +4 -3
  119. package/clients/cli/commands/manifest.mjs +5 -2
  120. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  121. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  122. package/clients/cli/commands/theme-template.doc.mjs +1 -1
  123. package/clients/cli/commands/theme.mjs +374 -42
  124. package/clients/cli/index.mjs +9 -3
  125. package/clients/cli/lib/manifest.mjs +37 -0
  126. package/clients/cli/lib/resolve-theme.mjs +2 -1
  127. package/clients/cli/manifest.generated.json +1 -0
  128. package/foundation/config/theme-state.d.mts +16 -0
  129. package/foundation/config/theme-state.mjs +84 -13
  130. package/foundation/discovery/module-exports.mjs +16 -2
  131. package/foundation/discovery/stored-self-audit.d.mts +31 -0
  132. package/foundation/discovery/stored-self-audit.mjs +119 -0
  133. package/foundation/discovery/template-adapter.d.mts +48 -2
  134. package/foundation/discovery/template-adapter.mjs +51 -19
  135. package/foundation/discovery/template-needs.d.mts +19 -0
  136. package/foundation/discovery/template-needs.mjs +50 -16
  137. package/foundation/discovery/template-source-tree.d.mts +7 -0
  138. package/foundation/discovery/template-source-tree.mjs +15 -0
  139. package/foundation/discovery/theme-discovery.d.mts +2 -1
  140. package/foundation/discovery/theme-discovery.mjs +2 -8
  141. package/foundation/discovery/theme-module-extensions.d.mts +18 -0
  142. package/foundation/discovery/theme-module-extensions.mjs +30 -0
  143. package/foundation/doc-compiler/import.mjs +22 -15
  144. package/foundation/doc-compiler/stored-docs.d.mts +18 -12
  145. package/foundation/doc-compiler/stored-docs.mjs +52 -19
  146. package/foundation/env/private-dir.d.mts +17 -0
  147. package/foundation/env/private-dir.mjs +42 -0
  148. package/foundation/fs/gather-cache.d.mts +108 -0
  149. package/foundation/fs/gather-cache.mjs +741 -0
  150. package/foundation/fs/jiti-cache.d.mts +21 -0
  151. package/foundation/fs/jiti-cache.mjs +66 -0
  152. package/foundation/fs/module-loader.mjs +14 -3
  153. package/foundation/fs/read-record.d.mts +31 -1
  154. package/foundation/fs/read-record.mjs +100 -7
  155. package/foundation/integrations/integrations.d.mts +8 -0
  156. package/foundation/integrations/integrations.mjs +4 -0
  157. package/foundation/integrations/validate-contributions.d.mts +62 -1
  158. package/foundation/integrations/validate-contributions.mjs +476 -40
  159. package/foundation/release/cache.d.mts +11 -2
  160. package/foundation/release/cache.mjs +59 -10
  161. package/foundation/release/folder-lock.d.mts +21 -0
  162. package/foundation/release/folder-lock.mjs +171 -0
  163. package/foundation/response/response-types.doc.mjs +1 -1
  164. package/package.json +8 -6
@@ -10,9 +10,11 @@
10
10
  */
11
11
 
12
12
  import * as fs from 'node:fs';
13
+ import * as path from 'node:path';
13
14
  import {AstryxError} from '../error.mjs';
14
15
  import {
15
16
  AUTO_SPREAD,
17
+ CORNERS,
16
18
  DIALS,
17
19
  FAMILIES,
18
20
  KEY_TOKENS,
@@ -26,6 +28,7 @@ import {
26
28
  commitTheme,
27
29
  compileTheme,
28
30
  cornerPx,
31
+ radiusAt,
29
32
  contrastProblems,
30
33
  describe,
31
34
  keyOf,
@@ -35,7 +38,6 @@ import {
35
38
  nearest,
36
39
  partDefault,
37
40
  partsOf,
38
- q,
39
41
  undoArgs,
40
42
  withoutBadValues,
41
43
  NAME_RULE,
@@ -48,19 +50,23 @@ import {
48
50
  themeFiles,
49
51
  buildTheme,
50
52
  staleBuild,
53
+ EDITOR_SUFFIXES,
54
+ themeSource,
55
+ themeSourceProblem,
51
56
  } from './_settings.mjs';
52
- import {checkSetting, exampleOf, storedTokenProblem} from './_check.mjs';
57
+ import {checkSetting, exampleOf, storedProblems} from './_check.mjs';
53
58
  import {MODAL} from './_compiler.mjs';
54
59
  import {
55
60
  PALETTE_DIALS,
56
61
  VIBRANCY_DEFAULT,
57
62
  paletteFiles,
58
63
  paletteMapping,
64
+ paletteDialOf,
59
65
  paletteNotes,
60
66
  } from './_palette.mjs';
61
67
  import {renderPreview, renderSheet} from './_preview.mjs';
62
68
  import {refreshPreviewRoute} from './_preview-app.mjs';
63
- import {baseResolved} from './_project.mjs';
69
+ import {baseResolved, installLine} from './_project.mjs';
64
70
  import {
65
71
  requireProject,
66
72
  context,
@@ -71,12 +77,63 @@ import {
71
77
  checkSection,
72
78
  } from './_context.mjs';
73
79
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
80
+ import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
74
81
 
75
82
  const E = ERROR_CODES;
76
83
 
77
84
  /**
78
85
  * @typedef {import('./theme-edit.type.mjs').ThemeEditChangeOptions} ChangeOptions
79
86
  */
87
+ /** The marker a `new` writes first and removes last. */
88
+ const NEW_MARKER = '.astryx-new.json';
89
+
90
+ /** Files a system or a tool puts in any folder; a folder of only these is empty. */
91
+ const FOLDER_JUNK = new Set(['.DS_Store', '.gitkeep', 'Thumbs.db']);
92
+
93
+ /**
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.
99
+ * @param {string} dir
100
+ * @param {string} slug
101
+ * @returns {string[] | null}
102
+ */
103
+ function editorLeftovers(dir, slug) {
104
+ if (!fs.existsSync(path.join(dir, NEW_MARKER))) return null;
105
+ /** @type {string[]} */
106
+ const files = [];
107
+ const named = (/** @type {string} */ n) =>
108
+ n === NEW_MARKER ||
109
+ EDITOR_SUFFIXES.some(x => n === `${slug}.${x}`) ||
110
+ /^(preview(-[a-z]+)?\.(html|png)|sheet(-[a-z-]+)?\.(html|png)|palette\.config\.json)$/.test(
111
+ n,
112
+ ) ||
113
+ // The editor's temp names: `.<name>.astryx-<pid>-<rand>.tmp`, and the
114
+ // build's `.<name>.tmp-<id>`.
115
+ /^\..+\.(astryx-\d+-[a-z0-9]+\.tmp|tmp-[\w-]+)$/.test(n);
116
+ for (const e of fs.readdirSync(dir, {withFileTypes: true})) {
117
+ const p = path.join(dir, e.name);
118
+ if (FOLDER_JUNK.has(e.name)) continue;
119
+ 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
+ }
124
+ continue;
125
+ }
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);
133
+ }
134
+ return files;
135
+ }
136
+
80
137
  /**
81
138
  * Start a theme in themes/<name>/, optionally on a built-in theme, with any
82
139
  * dial settings applied in the same build. Each setting is checked as `set`
@@ -87,7 +144,9 @@ const E = ERROR_CODES;
87
144
  * @returns {Promise<import('./theme-edit.type.mjs').ThemeEditNewResponse>}
88
145
  */
89
146
  export async function themeEditNew(name, options = {}) {
90
- const {cwd = process.cwd(), like, png = false, settings = {}} = options;
147
+ const {cwd = process.cwd(), png = false, settings = {}} = options;
148
+ // `--like none` builds on no base, as `set <theme> base none` does.
149
+ const like = options.like === 'none' ? undefined : options.like;
91
150
  requireProject(cwd);
92
151
  if (!slugOk(name)) {
93
152
  const fixed =
@@ -99,12 +158,22 @@ export async function themeEditNew(name, options = {}) {
99
158
  .replace(/^[^a-z]+/, '')
100
159
  .replace(/-+$/, '')
101
160
  .slice(0, 64) || 'my-theme';
161
+ // The name as typed may be a theme that exists in another case.
162
+ const exists = fs.existsSync(themeFiles(cwd, fixed).settings);
102
163
  refuse(
103
164
  E.ERR_INVALID_ARGUMENT,
104
- `"${String(name ?? '').slice(0, 80)}" is not a theme name (${NAME_RULE}).`,
105
- {run: [`${P} new ${fixed}`]},
165
+ `"${String(name ?? '').slice(0, 80)}" is not a theme name (${NAME_RULE})${exists ? `; ${fixed} is a theme here already` : ''}.`,
166
+ {run: exists ? [`${P} show ${fixed}`] : [`${P} new ${fixed}`]},
106
167
  );
107
168
  }
169
+ // A built-in theme's name would collide with the theme of that name the
170
+ // app may also use.
171
+ if (LIKE.includes(name))
172
+ refuse(
173
+ E.ERR_FILE_EXISTS,
174
+ `${name} is a built-in theme; an editor theme needs a name of its own. To start from ${name}, build on it.`,
175
+ {run: [`${P} new my-${name} --like ${name}`]},
176
+ );
108
177
  const f = themeFiles(cwd, name);
109
178
  // A link or a file where the theme goes is never written through or
110
179
  // replaced.
@@ -124,7 +193,16 @@ export async function themeEditNew(name, options = {}) {
124
193
  refuse(E.ERR_FILE_EXISTS, `Theme "${name}" already exists.`, {
125
194
  run: [`${P} show ${name}`, `${P} set ${name} base neutral`],
126
195
  });
127
- if (fs.existsSync(f.dir) && fs.readdirSync(f.dir).length)
196
+ // A `new` stopped part way (killed, out of memory) leaves its own marker
197
+ // beside the files it wrote: only such a folder is cleared, and only
198
+ // after every argument is checked. Anything else in the folder, or a
199
+ // folder without the marker, is never touched.
200
+ const left = fs.existsSync(f.dir) ? editorLeftovers(f.dir, name) : null;
201
+ const holdsOther =
202
+ fs.existsSync(f.dir) &&
203
+ !left &&
204
+ fs.readdirSync(f.dir).some(n => !FOLDER_JUNK.has(n));
205
+ if (holdsOther)
128
206
  refuse(
129
207
  E.ERR_FILE_EXISTS,
130
208
  `themes/${name}/ already holds files this command did not make; choose another name.`,
@@ -147,12 +225,55 @@ export async function themeEditNew(name, options = {}) {
147
225
  data.like,
148
226
  );
149
227
  applyNewSettings(ctx, name, data, settings);
150
- const r = await commitTheme(ctx, name, data, {history: false});
228
+ /** @type {string[]} */
229
+ 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);
238
+ leftNotes.push(
239
+ `cleared ${shownLeft.length} file${shownLeft.length === 1 ? '' : 's'} a stopped \`new\` left in themes/${name}/ (${shownLeft
240
+ .slice(0, 4)
241
+ .map(p => path.relative(f.dir, p))
242
+ .join(', ')}${shownLeft.length > 4 ? ', …' : ''}), then made the theme`,
243
+ );
244
+ }
245
+ // The marker goes first and comes off last: a `new` stopped between
246
+ // leaves a folder the next `new` can prove it made.
247
+ const dirMade = !fs.existsSync(f.dir);
248
+ fs.mkdirSync(f.dir, {recursive: true});
249
+ const marker = path.join(f.dir, NEW_MARKER);
250
+ fs.writeFileSync(
251
+ marker,
252
+ JSON.stringify({pid: process.pid, started: Date.now()}),
253
+ );
254
+ /** @type {Awaited<ReturnType<typeof commitTheme>>} */
255
+ let r;
256
+ try {
257
+ r = await commitTheme(ctx, name, data, {history: false});
258
+ } catch (e) {
259
+ fs.rmSync(marker, {force: true});
260
+ if (dirMade)
261
+ try {
262
+ fs.rmdirSync(f.dir);
263
+ } catch {
264
+ // It holds something else now.
265
+ }
266
+ throw e;
267
+ }
268
+ fs.rmSync(marker, {force: true});
151
269
  const p = await previewAndRoute(ctx, name, data, png);
152
270
  /** @type {string[]} */
153
- const notes = data.settings.some(s => s.thing === 'font')
154
- ? fontNotes(ctx, data)
155
- : [];
271
+ const notes = [
272
+ ...leftNotes,
273
+ ...(data.settings.some(s => s.thing === 'font')
274
+ ? fontNotes(ctx, data)
275
+ : []),
276
+ ];
156
277
  return {
157
278
  type: 'themeEdit.new',
158
279
  data: {
@@ -225,7 +346,11 @@ function applyNewSettings(ctx, slug, data, settings) {
225
346
  refuse(
226
347
  E.ERR_INVALID_ARGUMENT,
227
348
  `--${thing} was given two values for the same thing (${data.settings[same].value}, ${s.value}); give one.`,
228
- {run: [`${P} new ${slug} --${thing} ${q(s.value)}`]},
349
+ {
350
+ run: [
351
+ `${P} new ${slug} --${thing} ${quoteCommandArgument(s.value)}`,
352
+ ],
353
+ },
229
354
  );
230
355
  data.settings.push(s);
231
356
  }
@@ -245,7 +370,7 @@ function asNewRefusal(e, slug, settings) {
245
370
  const flags = Object.entries(settings)
246
371
  .filter(([, v]) => v != null)
247
372
  .flatMap(([k, v]) => (Array.isArray(v) ? v : [v]).map(x => [k, x]))
248
- .map(([k, v]) => `--${k} ${q(v)}`);
373
+ .map(([k, v]) => `--${k} ${quoteCommandArgument(String(v))}`);
249
374
  const setPrefix = `${P} set ${slug} `;
250
375
  e.suggestions = (e.suggestions ?? []).map(s => {
251
376
  if (s.name === `${P} set --help`) return {...s, name: `${P} new --help`};
@@ -317,18 +442,30 @@ export async function themeEditSet(theme, thing, value, options = {}) {
317
442
  * @param {string} [skip]
318
443
  */
319
444
  function refuseStoredTokens(ctx, slug, data, skip) {
320
- const bad = storedTokenProblem(ctx, slug, data, skip);
321
- if (bad)
322
- refuse(
323
- E.ERR_THEME_INVALID,
324
- `themes/${slug}/${slug}.settings.json: ${describe(bad.setting)} holds a value the editor no longer accepts (${bad.why}).`,
325
- {
326
- run: [
327
- `${P} undo ${slug} ${undoArgs(bad.setting)}`,
328
- `${P} set ${slug} ${bad.setting.thing} ${q(exampleOf(ctx.C.typeOf(bad.setting.thing), bad.setting.thing))}`,
329
- ],
330
- },
331
- );
445
+ const bad = storedProblems(ctx, slug, data, skip);
446
+ if (!bad.length) return;
447
+ const one = bad.length === 1 ? bad[0] : null;
448
+ refuse(
449
+ E.ERR_THEME_INVALID,
450
+ `themes/${slug}/${slug}.settings.json: ${bad
451
+ .map(
452
+ b =>
453
+ `${describe(b.setting)} ${b.scope ? 'applies where the editor does not accept it' : 'holds a value the editor no longer accepts'} (${b.why})`,
454
+ )
455
+ .join('; ')}.`,
456
+ {
457
+ // Each undo runs while the others are still saved: an undo of one
458
+ // setting is never refused for another.
459
+ run: [
460
+ ...bad.slice(0, 5).map(b => `${P} undo ${slug} ${undoArgs(b.setting)}`),
461
+ ...(one && !one.scope
462
+ ? [
463
+ `${P} set ${slug} ${one.setting.thing} ${quoteCommandArgument(exampleOf(ctx.C.typeOf(one.setting.thing), one.setting.thing))}`,
464
+ ]
465
+ : []),
466
+ ],
467
+ },
468
+ );
332
469
  }
333
470
 
334
471
  /**
@@ -363,9 +500,37 @@ function extremeSteps(res) {
363
500
  async function change(sub, slug, thing, value, options) {
364
501
  const {cwd = process.cwd(), png = false} = options;
365
502
  requireProject(cwd);
366
- const data = loadTheme(cwd, slug, thing);
503
+ // The setting this command changes or removes may be the bad one; any
504
+ // other bad setting still refuses (or is left out when this fixes one).
505
+ const target = /** @type {Partial<import('./_settings.mjs').Setting>} */ ({
506
+ thing,
507
+ on: options.on,
508
+ when: options.when,
509
+ state: options.state,
510
+ mode: options.mode,
511
+ });
512
+ const data = loadTheme(cwd, slug, target);
513
+ /** @type {string[]} */
514
+ const fieldNotes = [];
515
+ if (sub === 'set' && (thing === 'base' || thing === 'mode')) {
516
+ // The other field, when it is damaged too, goes back to its default,
517
+ // so the theme loads again.
518
+ if (
519
+ thing !== 'mode' &&
520
+ !['both', 'light-only', 'dark-only'].includes(data.mode)
521
+ ) {
522
+ data.mode = 'both';
523
+ fieldNotes.push('mode was missing or damaged, so it is both again');
524
+ }
525
+ if (thing !== 'base' && data.like != null && !LIKE.includes(data.like)) {
526
+ delete data.like;
527
+ fieldNotes.push('base was not a built-in theme, so it is none again');
528
+ }
529
+ }
367
530
  const ctx = await context(cwd);
368
- refuseStoredTokens(ctx, slug, data, thing);
531
+ // An undo of one setting only removes it, so it never waits on the
532
+ // others being fixed.
533
+ if (sub !== 'undo') refuseStoredTokens(ctx, slug, data, thing);
369
534
  const flags = {
370
535
  on: options.on,
371
536
  when: options.when,
@@ -409,8 +574,33 @@ async function change(sub, slug, thing, value, options) {
409
574
  }
410
575
  // undo <thing> <value> removes the setting only when that is its value.
411
576
  if (sub === 'undo' && value != null && value !== '') {
577
+ // A word that names a part with a setting of this thing meant --on.
578
+ const onPart = data.settings.find(
579
+ x => x.thing === s.thing && x.on === value.trim() && !s.on,
580
+ );
581
+ if (onPart)
582
+ refuse(
583
+ E.ERR_THEME_NOT_SET,
584
+ `${value.trim()} is a part: ${describe(onPart)} is set; name the part with --on.`,
585
+ {run: [`${P} undo ${slug} ${undoArgs(onPart)}`]},
586
+ );
412
587
  const held = data.settings.find(x => keyOf(x) === keyOf(s));
413
- if (held && held.value !== value.trim())
588
+ // `headings weight|tracking` names a half, not a value; a value is
589
+ // compared as `set` reads it (case, spacing and quotes aside).
590
+ const norm = (/** @type {string} */ x) =>
591
+ x
592
+ .trim()
593
+ .toLowerCase()
594
+ .replace(/\s+/g, ' ')
595
+ .replace(/^["']|["']$/g, '');
596
+ const half =
597
+ s.thing === 'headings' && ['weight', 'tracking'].includes(norm(value));
598
+ if (
599
+ held &&
600
+ !half &&
601
+ norm(held.value) !== norm(value) &&
602
+ norm(held.value) !== norm(s.value ?? '')
603
+ )
414
604
  refuse(
415
605
  E.ERR_THEME_NOT_SET,
416
606
  `The theme sets ${describe(held)}, not ${value}.`,
@@ -446,6 +636,7 @@ async function change(sub, slug, thing, value, options) {
446
636
  ...(r.unchanged
447
637
  ? [`no change: ${s.thing} is already ${s.value}`]
448
638
  : []),
639
+ ...fieldNotes,
449
640
  ...r.notes,
450
641
  `your ${data.settings.length} settings are kept`,
451
642
  ...otherModeNotes(data),
@@ -516,7 +707,19 @@ async function change(sub, slug, thing, value, options) {
516
707
  data.settings.splice(v, 1)[0]?.value
517
708
  );
518
709
  }
519
- } else if (prev && JSON.stringify(prev) === JSON.stringify(s)) {
710
+ } else if (
711
+ prev &&
712
+ JSON.stringify(prev) === JSON.stringify(s) &&
713
+ // A later setting that covers the same parts hides it: set again, it
714
+ // moves last and wins.
715
+ !data.settings
716
+ .slice(prevIdx + 1)
717
+ .some(
718
+ x =>
719
+ x.thing === s.thing &&
720
+ partsOf(ctx, x).some(t => partsOf(ctx, s).includes(t)),
721
+ )
722
+ ) {
520
723
  // The same setting again: nothing moves.
521
724
  } else {
522
725
  // A changed setting goes last, so it wins over what came before it.
@@ -543,7 +746,24 @@ async function change(sub, slug, thing, value, options) {
543
746
  x.on === s.on &&
544
747
  x.when === s.when &&
545
748
  x.state === s.state &&
546
- x.thing.startsWith(`${s.thing}-`),
749
+ x.thing.startsWith(`${s.thing}-`) &&
750
+ // Not part of border: its radius, collapse and spacing.
751
+ !/^border-(radius|collapse|spacing)$|^border-[a-z]+-radius$/.test(
752
+ x.thing,
753
+ ),
754
+ )
755
+ : [];
756
+ // A group setting covers part settings made before it for its members.
757
+ const covers =
758
+ sub === 'set' && s.source === 'dial' && s.on && FAMILIES[s.on]
759
+ ? data.settings.filter(
760
+ x =>
761
+ x !== s &&
762
+ x.thing === s.thing &&
763
+ x.on &&
764
+ FAMILIES[/** @type {string} */ (s.on)].includes(x.on) &&
765
+ (x.when ?? '') === (s.when ?? '') &&
766
+ (x.state ?? '') === (s.state ?? ''),
547
767
  )
548
768
  : [];
549
769
  const r = await commitTheme(ctx, slug, data, {history: true});
@@ -553,9 +773,31 @@ async function change(sub, slug, thing, value, options) {
553
773
  sub === 'set' && s.source === 'dial' && !s.on
554
774
  ? data.settings.filter(x => x.thing === s.thing && x.on).map(describe)
555
775
  : [];
776
+ // The exact property a dial on a part competes with there.
777
+ const DIAL_PROPERTY = /** @type {Record<string, RegExp>} */ ({
778
+ corners: /radius$/,
779
+ spacing: /^(padding|gap)(-|$)/,
780
+ });
781
+ const exactOver =
782
+ sub === 'set' && s.source === 'dial' && s.on && DIAL_PROPERTY[s.thing]
783
+ ? (data.settings.find(
784
+ x =>
785
+ x.source === 'property' &&
786
+ DIAL_PROPERTY[s.thing].test(x.thing) &&
787
+ x.on &&
788
+ partsOf(ctx, x).some(t => partsOf(ctx, s).includes(t)) &&
789
+ (x.when ?? '') === (s.when ?? '') &&
790
+ (x.state ?? '') === (s.state ?? ''),
791
+ ) ?? null)
792
+ : null;
556
793
  /** @type {string[]} */
557
794
  const notes = [
558
795
  ...(covered.length ? [`replaces ${covered.map(describe).join(', ')}`] : []),
796
+ ...(covers.length
797
+ ? [
798
+ `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`,
799
+ ]
800
+ : []),
559
801
  // Phones keep the base theme's reading sizes (a coarse pointer below
560
802
  // the lg width), so a size change says where it applies.
561
803
  ...(sub === 'set' &&
@@ -570,8 +812,17 @@ async function change(sub, slug, thing, value, options) {
570
812
  ...(sub === 'set' && ['text-size', 'type'].includes(s.thing)
571
813
  ? extremeSteps(r.res.light)
572
814
  : []),
815
+ // An exact property on the same part wins over a dial there.
816
+ ...(exactOver
817
+ ? [
818
+ `${describe(exactOver)} is an exact property and wins over ${s.thing} there, so this shows only after \`${P} undo ${slug} ${undoArgs(exactOver)}\``,
819
+ ]
820
+ : []),
573
821
  // An accepted change that the theme already showed says so.
574
822
  ...(sub === 'set' &&
823
+ !exactOver &&
824
+ // With the last build unreadable, nothing was compared.
825
+ !r.notes.some(n => n.includes('could not be read')) &&
575
826
  !r.unchanged &&
576
827
  !r.changed.length &&
577
828
  !styled.length &&
@@ -609,6 +860,27 @@ async function change(sub, slug, thing, value, options) {
609
860
  modesOf(data),
610
861
  ),
611
862
  );
863
+ // An exact color the mapping reads (a fill, a tint, text on a fill)
864
+ // changes what it picks around it: say what, as the dial does.
865
+ const dialOfToken =
866
+ s.source === 'token' && !s.on && !s.when && !s.state
867
+ ? paletteDialOf(s.thing)
868
+ : null;
869
+ if (dialOfToken && sub === 'set') {
870
+ const pal = paletteMapping(
871
+ cwd,
872
+ slug,
873
+ data,
874
+ base,
875
+ ctx.project.core.tokenDefaults,
876
+ );
877
+ if (pal?.families.some(x => x.dial === dialOfToken))
878
+ notes.push(
879
+ ...paletteNotes(pal, dialOfToken, slug, modesOf(data)).filter(
880
+ n => !n.startsWith('the generated colors are in '),
881
+ ),
882
+ );
883
+ }
612
884
  if (PALETTE_DIALS.includes(s.thing) && sub === 'undo')
613
885
  notes.push(
614
886
  `the ${s.thing} color is no longer generated, and the tokens it fed go back to the base theme's values`,
@@ -628,8 +900,8 @@ async function change(sub, slug, thing, value, options) {
628
900
  notes.push(
629
901
  `the page background stays the base theme's${pages.length ? ` (${pages.join(', ')})` : ''}${
630
902
  modes.includes('light')
631
- ? `; for a white or off-white page: ${P} set ${slug} --color-background-body "#ffffff"${modes.length > 1 ? ' --mode light' : ''}`
632
- : `; for another page color: ${P} set ${slug} --color-background-body "<color>"`
903
+ ? `; for a white or off-white page: ${P} set ${slug} --color-background-body ${quoteCommandArgument('#ffffff')}${modes.length > 1 ? ' --mode light' : ''}`
904
+ : `; for another page color: ${P} set ${slug} --color-background-body ${quoteCommandArgument('#f8fafc')}`
633
905
  }`,
634
906
  );
635
907
  }
@@ -650,7 +922,18 @@ async function change(sub, slug, thing, value, options) {
650
922
  notes.push(
651
923
  'text below the base size keeps its standard sizes, so small text stays readable',
652
924
  );
653
- const p = await previewAndRoute(ctx, slug, data, png);
925
+ // A change that changed nothing writes nothing, the preview included.
926
+ const f0 = themeFiles(cwd, slug);
927
+ const p =
928
+ r.unchanged && !png && fs.existsSync(f0.preview)
929
+ ? {
930
+ preview: shown(cwd, f0.preview),
931
+ pictures: [],
932
+ pictureProblem: null,
933
+ notChecked: null,
934
+ previewStale: [],
935
+ }
936
+ : await previewAndRoute(ctx, slug, data, png);
654
937
  return {
655
938
  type: sub === 'set' ? 'themeEdit.set' : 'themeEdit.undo',
656
939
  data: {
@@ -664,7 +947,10 @@ async function change(sub, slug, thing, value, options) {
664
947
  ...(sub === 'undo' && prev
665
948
  ? {removed: prev, now: nowApplies(ctx, data, prev, base)}
666
949
  : {}),
667
- replaced: sub === 'set' ? prev : null,
950
+ // A plain value that replaced per-mode ones names the first of them;
951
+ // the notes name them all.
952
+ replaced:
953
+ sub === 'set' ? (prev ?? covered[0] ?? longhands[0] ?? null) : null,
668
954
  // A whole-theme dial names the value it replaced, as base and mode do.
669
955
  was:
670
956
  sub === 'set' && prev && prev.source === 'dial' && !prev.on
@@ -713,7 +999,10 @@ function unsetColorNotes(data, base, slug) {
713
999
  if (!unset.length) return [];
714
1000
  return [
715
1001
  `${unset.join(', ')} ${unset.length > 1 ? 'are' : 'is'} not set by this theme, so ${unset.length > 1 ? 'they keep' : 'it keeps'} the base theme's colors at any vibrancy${unset.includes('info') ? ', and so do surfaces tinted with info, such as colored cards' : ''}. To include ${unset.length > 1 ? 'them' : 'it'}: ${unset
716
- .map(d => `${P} set ${slug} ${d} "${base.light[word[d]] ?? '<color>'}"`)
1002
+ .map(
1003
+ d =>
1004
+ `${P} set ${slug} ${d} ${quoteCommandArgument(base.light[word[d]] ?? '#1f2937')}`,
1005
+ )
717
1006
  .join('; ')}`,
718
1007
  ];
719
1008
  }
@@ -728,18 +1017,47 @@ function unsetColorNotes(data, base, slug) {
728
1017
  */
729
1018
  export async function themeEditShow(theme, options = {}) {
730
1019
  const {cwd = process.cwd(), png = false} = options;
1020
+ // Without the theme lock (it cannot be taken), show writes nothing: it
1021
+ // reports the last build as it is.
1022
+ const readOnly = Boolean(/** @type {any} */ (options).readOnly);
731
1023
  requireProject(cwd);
732
1024
  const section = checkSection(options.section, theme);
733
1025
  const data = loadTheme(cwd, theme);
734
1026
  const ctx = await context(cwd);
735
1027
  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)
1031
+ refuse(
1032
+ E.ERR_RENDER_FAILED,
1033
+ `${theme} cannot be shown: ${ctx.unchecked} Nothing was written.`,
1034
+ {
1035
+ // Only a missing React is fixed by installing it; two copies or a
1036
+ // component that throws are named in the message.
1037
+ run:
1038
+ ctx.uncheckedCode === 'ERR_RENDER_MISSING_DEPENDENCY'
1039
+ ? [installLine(cwd, ['react', 'react-dom'])]
1040
+ : [`${P} show ${theme}`],
1041
+ },
1042
+ );
736
1043
  const f = themeFiles(cwd, theme);
737
1044
  // A build that does not match the settings (a command stopped mid-build,
738
1045
  // a hand edit, another CLI) is made again from the settings, and said.
739
1046
  const stale = staleBuild(cwd, theme, data);
740
- if (stale) await buildTheme(ctx, theme, data);
741
- const res = await resolvedTokens(ctx, theme);
742
- const problems = await contrastProblems(ctx, data, res);
1047
+ const brokenSource = themeSourceProblem(cwd, theme);
1048
+ if (stale && !brokenSource && !readOnly) await buildTheme(ctx, theme, data);
1049
+ // A built module that cannot be read (damaged by hand) is built again.
1050
+ /** @type {string | null} */
1051
+ let unreadable = null;
1052
+ const res = await resolvedTokens(ctx, theme).catch(async e => {
1053
+ if (e instanceof AstryxError || readOnly) throw e;
1054
+ unreadable = `${theme}.js could not be read (${e instanceof Error ? e.message.split('\n')[0] : String(e)}), so it was built again from the settings`;
1055
+ await buildTheme(ctx, theme, data);
1056
+ return resolvedTokens(ctx, theme);
1057
+ });
1058
+ const checked = await contrastProblems(ctx, data, res);
1059
+ // What cannot be measured is a note, not a problem.
1060
+ const problems = checked.filter(x => !x.startsWith('not checked'));
743
1061
  const pal = paletteMapping(
744
1062
  cwd,
745
1063
  theme,
@@ -747,20 +1065,31 @@ export async function themeEditShow(theme, options = {}) {
747
1065
  await baseResolved(ctx.project, data.like),
748
1066
  ctx.project.core.tokenDefaults,
749
1067
  );
750
- const preview = await renderPreview(ctx, theme, data);
751
- const previewStale = await keepPreviewRoute(ctx, theme);
752
- const sheet = await renderSheet(ctx, theme, data, {group: section});
1068
+ const preview = readOnly
1069
+ ? shown(cwd, f.preview)
1070
+ : await renderPreview(ctx, theme, data);
1071
+ const previewStale = readOnly ? [] : await keepPreviewRoute(ctx, theme);
1072
+ const sheet = readOnly
1073
+ ? {file: shown(cwd, f.sheet), components: 0, skipped: []}
1074
+ : await renderSheet(ctx, theme, data, {group: section});
753
1075
  const modes = modesOf(data);
754
- const shots = png
755
- ? await pictures(
756
- ctx.project,
757
- [
758
- {html: f.preview, png: f.previewPng},
759
- {html: f.sheet, png: f.sheetPng},
760
- ],
761
- modes,
762
- )
763
- : {files: [], problem: null};
1076
+ const shots =
1077
+ png && !readOnly
1078
+ ? await pictures(
1079
+ ctx.project,
1080
+ [
1081
+ {html: f.preview, png: f.previewPng},
1082
+ // A section is photographed from its own sheet.
1083
+ section
1084
+ ? {
1085
+ html: path.join(f.dir, `sheet-${section}.html`),
1086
+ png: path.join(f.dir, `sheet-${section}.png`),
1087
+ }
1088
+ : {html: f.sheet, png: f.sheetPng},
1089
+ ],
1090
+ modes,
1091
+ )
1092
+ : {files: [], problem: null};
764
1093
  return {
765
1094
  type: 'themeEdit.show',
766
1095
  data: {
@@ -772,11 +1101,22 @@ export async function themeEditShow(theme, options = {}) {
772
1101
  return {...s, parts: partsOf(ctx, s), ...(masked ? {masked} : {})};
773
1102
  }),
774
1103
  notes: [
775
- ...(stale
1104
+ ...(readOnly
776
1105
  ? [
777
- `the build did not match ${theme}.settings.json (${stale}), so it was built again from the settings`,
1106
+ `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`,
1107
+ ]
1108
+ : stale
1109
+ ? [
1110
+ `the build did not match ${theme}.settings.json (${stale}), so it was built again from the settings`,
1111
+ ]
1112
+ : []),
1113
+ ...(unreadable ? [unreadable] : []),
1114
+ ...(brokenSource
1115
+ ? [
1116
+ `${brokenSource}, so settings cannot take effect; put back the import, extends and name lines in ${theme}.theme.ts`,
778
1117
  ]
779
1118
  : []),
1119
+ ...checked.filter(x => x.startsWith('not checked')),
780
1120
  ...fontNotes(ctx, data),
781
1121
  ],
782
1122
  // A size, number or font is the same in both modes: one value.
@@ -788,7 +1128,12 @@ export async function themeEditShow(theme, options = {}) {
788
1128
  : (res[modes[0]][k] ?? null),
789
1129
  ]),
790
1130
  ),
791
- partValues: partValues(ctx, data, res, modes[0]),
1131
+ partValues: partValues(
1132
+ Object.assign(ctx, {base: await baseResolved(ctx.project, data.like)}),
1133
+ data,
1134
+ res,
1135
+ modes[0],
1136
+ ),
792
1137
  ...colorsReport(pal, data),
793
1138
  problems,
794
1139
  preview,
@@ -881,12 +1226,16 @@ function otherModeNotes(data) {
881
1226
  * @returns {string[]}
882
1227
  */
883
1228
  function partValues(ctx, data, res, mode) {
1229
+ const masked = maskedSettings(data);
884
1230
  return data.settings
885
1231
  .filter(
886
1232
  s =>
887
1233
  s.source === 'dial' && s.on && ['corners', 'spacing'].includes(s.thing),
888
1234
  )
889
1235
  .map(s => {
1236
+ // A setting a later group setting covers shows that one's value.
1237
+ const hidden = masked.get(s);
1238
+ if (hidden) return `${describe(s)} -> not shown: ${hidden}`;
890
1239
  const parts = partsOf(ctx, s);
891
1240
  const listed = `${parts.slice(0, 4).join(', ')}${parts.length > 4 ? `, and ${parts.length - 4} more` : ''}`;
892
1241
  if (s.thing === 'corners') {
@@ -919,7 +1268,21 @@ function partValues(ctx, data, res, mode) {
919
1268
  `${token === 'unknown' ? 'unknown' : `${token} ${res[mode][token] ?? 'unset'}`}${byToken.size > 1 ? ` for ${ts.join(', ')}` : ''}`,
920
1269
  )
921
1270
  .join('; ');
922
- return `${describe(s)} -> border-radius ${px >= 9999 ? 'fully round' : `${px}px`} on ${listed} (whole theme: ${whole})`;
1271
+ // What the build writes on each part: a rung by the radius token
1272
+ // the part reads, an exact size as given.
1273
+ const rung = CORNERS.some(([n]) => n === s.value) || px >= 1000;
1274
+ const built = [...byToken]
1275
+ .map(([token, ts]) => {
1276
+ const v =
1277
+ rung && token !== 'unknown' && ctx.base
1278
+ ? radiusAt(ctx.base, s.value, token)
1279
+ : px >= 1000
1280
+ ? 9999
1281
+ : px;
1282
+ return `${v >= 9999 ? 'fully round' : `${v}px`}${byToken.size > 1 ? ` on ${ts.join(', ')}` : ''}`;
1283
+ })
1284
+ .join('; ');
1285
+ return `${describe(s)} -> border-radius ${built}${byToken.size > 1 ? '' : ` on ${listed}`} (whole theme: ${whole})`;
923
1286
  }
924
1287
  const f = SPACING.find(([n]) => n === s.value)?.[1] ?? 1;
925
1288
  return `${describe(s)} -> spacing x${f} inside ${listed} (whole theme: --spacing-4 ${res[mode]['--spacing-4'] ?? 'unset'})`;
@@ -1013,16 +1376,29 @@ function colorFiles(slug) {
1013
1376
  */
1014
1377
  export async function themeEditUndo(theme, options = {}) {
1015
1378
  const {cwd = process.cwd(), png = false, thing} = options;
1016
- // base and mode are not settings in the list: undoing one sets it back to
1017
- // its default.
1018
- if (thing === 'base' || thing === 'mode')
1019
- return change(
1020
- 'set',
1021
- theme,
1022
- thing,
1023
- thing === 'base' ? 'none' : 'both',
1024
- options,
1025
- );
1379
+ // base and mode are not settings in the list: undoing one goes back to
1380
+ // what the theme was made with (the oldest step the history keeps).
1381
+ if (thing === 'base' || thing === 'mode') {
1382
+ requireProject(cwd);
1383
+ const now = loadTheme(cwd, theme, thing);
1384
+ const [first] = readHistory(themeFiles(cwd, theme).history).steps;
1385
+ /** @type {any} */
1386
+ let made = now;
1387
+ try {
1388
+ if (first) made = JSON.parse(first.replace(/^\uFEFF/, ''));
1389
+ } catch {
1390
+ made = now;
1391
+ }
1392
+ const of = (/** @type {any} */ d) =>
1393
+ thing === 'base' ? (d?.like ?? 'none') : (d?.mode ?? 'both');
1394
+ if (of(made) === of(now))
1395
+ refuse(
1396
+ E.ERR_THEME_NOT_SET,
1397
+ `${thing} was never changed: it is ${of(now)}, as ${theme} was made.`,
1398
+ {run: [`${P} show ${theme}`]},
1399
+ );
1400
+ return change('set', theme, thing, of(made), options);
1401
+ }
1026
1402
  if (thing != null)
1027
1403
  return change('undo', theme, thing, options.value, options);
1028
1404
  requireProject(cwd);
@@ -1082,6 +1458,27 @@ export async function themeEditUndo(theme, options = {}) {
1082
1458
  `Nothing to undo: ${theme} has no earlier change.`,
1083
1459
  {run: [`${P} show ${theme}`]},
1084
1460
  );
1461
+ // A top step equal to the settings now (left by an undo stopped between
1462
+ // its two writes) changes nothing: skip it.
1463
+ const nowText = fs.readFileSync(f.settings, 'utf8');
1464
+ const same = (/** @type {string} */ a, /** @type {string} */ b) => {
1465
+ try {
1466
+ return (
1467
+ JSON.stringify({...JSON.parse(a.replace(/^\uFEFF/, '')), lifted: 0}) ===
1468
+ JSON.stringify({...JSON.parse(b.replace(/^\uFEFF/, '')), lifted: 0})
1469
+ );
1470
+ } catch {
1471
+ return false;
1472
+ }
1473
+ };
1474
+ while (history.length && same(history[history.length - 1], nowText))
1475
+ history.pop();
1476
+ if (!history.length)
1477
+ refuse(
1478
+ E.ERR_THEME_NOTHING_TO_UNDO,
1479
+ `Nothing to undo: ${theme} has no earlier change (the last step equals the settings now).`,
1480
+ {run: [`${P} show ${theme}`]},
1481
+ );
1085
1482
  const ctx = await context(cwd);
1086
1483
  const prevText = /** @type {string} */ (history.pop());
1087
1484
  let prev = /** @type {import('./_settings.mjs').ThemeData} */ (
@@ -1110,7 +1507,10 @@ export async function themeEditUndo(theme, options = {}) {
1110
1507
  leftOut = repaired.dropped;
1111
1508
  }
1112
1509
  // The popped history is written with the change, inside its rollback.
1113
- await commitTheme(ctx, theme, prev, {history: false, historyAfter: history});
1510
+ const committed = await commitTheme(ctx, theme, prev, {
1511
+ history: false,
1512
+ historyAfter: history,
1513
+ });
1114
1514
  /** @type {string[]} */
1115
1515
  const undone = current
1116
1516
  ? []
@@ -1132,6 +1532,8 @@ export async function themeEditUndo(theme, options = {}) {
1132
1532
  if (current)
1133
1533
  for (const [k, s] of now)
1134
1534
  if (!was.has(k)) undone.push(`put back ${describe(s)}`);
1535
+ // What the build kept, such as a color file edited by hand.
1536
+ undone.push(...committed.notes);
1135
1537
  if (current && (current.like ?? 'none') !== (prev.like ?? 'none'))
1136
1538
  undone.push(
1137
1539
  `base ${current.like ?? 'none'} is back to ${prev.like ?? 'none'}`,
@@ -1147,7 +1549,23 @@ export async function themeEditUndo(theme, options = {}) {
1147
1549
  undone,
1148
1550
  settings: prev.settings.length,
1149
1551
  stepsLeft: history.length,
1552
+ // What the step back changed, as a change reports it.
1553
+ changedTokens: committed.changed,
1554
+ problems: committed.problems,
1150
1555
  ...p,
1151
1556
  },
1152
1557
  };
1153
1558
  }
1559
+
1560
+ /**
1561
+ * Why an editor theme's build is not current, or null: the editor's own
1562
+ * staleness check (the settings it was built from, the CLI and packages it
1563
+ * was built with), for `compile --check`.
1564
+ * @param {string} cwd
1565
+ * @param {string} slug
1566
+ * @returns {Promise<string | null>}
1567
+ */
1568
+ export async function editorStale(cwd, slug) {
1569
+ const data = loadTheme(cwd, slug);
1570
+ return staleBuild(cwd, slug, data) || null;
1571
+ }