@astryxdesign/cli 0.7.15 → 0.7.16-canary.g8514aae

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 (116) 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/validate-integration.mjs +1 -1
  8. package/api/theme/_adapter.d.mts +5 -2
  9. package/api/theme/_adapter.mjs +43 -12
  10. package/api/theme/build/build.d.mts +1 -21
  11. package/api/theme/build/build.mjs +149 -37
  12. package/api/theme/eject/eject.mjs +22 -1
  13. package/api/theme/template/template.d.mts +19 -0
  14. package/api/theme/template/template.mjs +91 -19
  15. package/api/theme/theme.type.d.mts +5 -1
  16. package/api/theme/theme.type.mjs +2 -1
  17. package/api/theme/themeBuild.doc.mjs +3 -3
  18. package/api/theme/themeEject.doc.mjs +1 -1
  19. package/api/theme/themeTemplate.doc.mjs +9 -3
  20. package/api/theme-edit/_adapter.d.mts +21 -0
  21. package/api/theme-edit/_adapter.mjs +176 -22
  22. package/api/theme-edit/_check.d.mts +7 -0
  23. package/api/theme-edit/_check.mjs +1050 -173
  24. package/api/theme-edit/_color.d.mts +2 -8
  25. package/api/theme-edit/_color.mjs +292 -219
  26. package/api/theme-edit/_compiler.d.mts +1 -0
  27. package/api/theme-edit/_compiler.mjs +20 -1
  28. package/api/theme-edit/_context.d.mts +21 -0
  29. package/api/theme-edit/_context.mjs +235 -12
  30. package/api/theme-edit/_css.d.mts +50 -1
  31. package/api/theme-edit/_css.mjs +310 -28
  32. package/api/theme-edit/_edit.d.mts +9 -0
  33. package/api/theme-edit/_edit.mjs +907 -123
  34. package/api/theme-edit/_how-commands.d.mts +11 -26
  35. package/api/theme-edit/_how-commands.mjs +30 -2
  36. package/api/theme-edit/_how-question.d.mts +7 -0
  37. package/api/theme-edit/_how-question.mjs +1304 -176
  38. package/api/theme-edit/_how.mjs +144 -30
  39. package/api/theme-edit/_palette.d.mts +6 -0
  40. package/api/theme-edit/_palette.mjs +164 -62
  41. package/api/theme-edit/_parts.d.mts +16 -0
  42. package/api/theme-edit/_parts.mjs +49 -1
  43. package/api/theme-edit/_png.mjs +19 -14
  44. package/api/theme-edit/_preview-app.mjs +514 -27
  45. package/api/theme-edit/_preview.d.mts +11 -0
  46. package/api/theme-edit/_preview.mjs +245 -24
  47. package/api/theme-edit/_project.d.mts +11 -0
  48. package/api/theme-edit/_project.mjs +35 -9
  49. package/api/theme-edit/_settings.d.mts +122 -5
  50. package/api/theme-edit/_settings.mjs +1363 -71
  51. package/api/theme-edit/_tables.d.mts +19 -0
  52. package/api/theme-edit/_tables.mjs +205 -149
  53. package/api/theme-edit/_vet.d.mts +42 -0
  54. package/api/theme-edit/_vet.mjs +144 -0
  55. package/api/theme-edit/theme-edit.type.d.mts +2 -1
  56. package/api/theme-edit/theme-edit.type.mjs +1 -1
  57. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  58. package/api/theme-edit/themeEditShow.doc.mjs +11 -3
  59. package/assets/docs/tree/single-component.doc.mjs +11 -2
  60. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  61. package/assets/docs.generated.json +2 -2
  62. package/assets/preview-app/next.config.mjs +7 -1
  63. package/assets/preview-app/src/app/globals.css +1 -0
  64. package/assets/self-audit.generated.json +1 -1
  65. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
  66. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
  67. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
  68. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
  69. package/assets/templates/components.generated.json +1 -1
  70. package/authoring/doctypes/_schema.d.mts +52 -5
  71. package/authoring/doctypes/_schema.mjs +62 -7
  72. package/authoring/doctypes/base/type.ts +10 -0
  73. package/authoring/doctypes/component/component.doc.mjs +1 -1
  74. package/authoring/doctypes/function/function.doc.mjs +6 -0
  75. package/authoring/doctypes/function/type.ts +3 -0
  76. package/authoring/doctypes/legacy.mjs +5 -1
  77. package/authoring/index.d.ts +1 -0
  78. package/clients/cli/build-id.generated.json +1 -1
  79. package/clients/cli/commands/doctor.doc.mjs +2 -2
  80. package/clients/cli/commands/experimental/theme.mjs +188 -37
  81. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  82. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +1 -1
  83. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +2 -2
  84. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  85. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  86. package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
  87. package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
  88. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  89. package/clients/cli/commands/theme-eject.doc.mjs +1 -1
  90. package/clients/cli/commands/theme-template.doc.mjs +3 -1
  91. package/clients/cli/commands/theme.mjs +89 -6
  92. package/clients/cli/formatters/index.mjs +7 -4
  93. package/clients/cli/lib/cli-error.mjs +9 -3
  94. package/clients/cli/lib/component-format.mjs +27 -9
  95. package/clients/cli/lib/json-shim.mjs +40 -2
  96. package/clients/cli/manifest.generated.json +1 -1
  97. package/foundation/config/project.mjs +50 -14
  98. package/foundation/discovery/theme-discovery.d.mts +5 -1
  99. package/foundation/discovery/theme-discovery.mjs +24 -3
  100. package/foundation/fs/gather-cache.d.mts +9 -2
  101. package/foundation/fs/gather-cache.mjs +36 -10
  102. package/foundation/fs/paths.d.mts +16 -0
  103. package/foundation/fs/paths.mjs +25 -0
  104. package/foundation/render/host-exit.d.mts +8 -0
  105. package/foundation/render/host-exit.mjs +11 -0
  106. package/foundation/render/page-host.mjs +56 -10
  107. package/foundation/render/page-worker.mjs +14 -0
  108. package/foundation/render/render-page.mjs +37 -2
  109. package/foundation/response/error-codes.d.mts +2 -1
  110. package/foundation/response/error-codes.d.ts +1 -0
  111. package/foundation/response/error-codes.doc.mjs +5 -0
  112. package/foundation/response/error-codes.mjs +3 -0
  113. package/foundation/response/json.d.mts +15 -0
  114. package/foundation/response/json.mjs +52 -1
  115. package/foundation/response/response-types.doc.mjs +1 -1
  116. package/package.json +7 -6
@@ -84,6 +84,7 @@ import {
84
84
  PathSafetyError,
85
85
  } from '../../../foundation/fs/path-safety.mjs';
86
86
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
87
+ import {editorThemeRecord} from '../../../foundation/config/theme-state.mjs';
87
88
  import {validatePrivateVars} from '../../../foundation/config/theme-private-vars.mjs';
88
89
  import {AstryxError} from '../../error.mjs';
89
90
  import {applyWrites} from '../../integration/add-helpers.mjs';
@@ -710,16 +711,21 @@ export function normalizeThemeBuildForCompare(content) {
710
711
  .join('\n');
711
712
  }
712
713
 
713
- /** @param {Array<{dest: string, content: string, allowMissingInCheck?: boolean}>} writes @param {string} cwd @returns {Array<{path: string, reason: 'missing' | 'outdated'}>} */
714
+ /**
715
+ * The outputs that are missing or differ from what the build would write. A
716
+ * missing `.css.d.ts` stylesheet type stub (a write marked `warnWhenMissing`)
717
+ * is not stale: projects that ignore it in version control run the check in
718
+ * CI, so the check warns about it instead (missingDeclarations). Every other
719
+ * missing output, the module's `.d.ts` too, is stale.
720
+ * @param {Array<{dest: string, content: string, warnWhenMissing?: boolean}>} writes @param {string} cwd @returns {Array<{path: string, reason: 'missing' | 'outdated'}>}
721
+ */
714
722
  function staleBuildOutputs(writes, cwd) {
715
723
  /** @type {Array<{path: string, reason: 'missing' | 'outdated'}>} */
716
724
  const stale = [];
717
725
  for (const write of writes) {
718
726
  const rel = path.relative(cwd, write.dest);
719
727
  if (!fs.existsSync(write.dest)) {
720
- if (!write.allowMissingInCheck) {
721
- stale.push({path: rel, reason: 'missing'});
722
- }
728
+ if (!write.warnWhenMissing) stale.push({path: rel, reason: 'missing'});
723
729
  continue;
724
730
  }
725
731
  const onDisk = fs.readFileSync(write.dest, 'utf8');
@@ -733,13 +739,42 @@ function staleBuildOutputs(writes, cwd) {
733
739
  return stale;
734
740
  }
735
741
 
736
- /** @param {Array<{dest: string, allowMissingInCheck?: boolean}>} writes @param {string} cwd */
742
+ /**
743
+ * Every output the build writes, from the same write list the build and the
744
+ * stale comparison use. A missing output makes the check stale, except the
745
+ * `.css.d.ts` stub, which it warns about (missingDeclarations).
746
+ * @param {Array<{dest: string}>} writes @param {string} cwd
747
+ */
737
748
  function checkedBuildOutputs(writes, cwd) {
749
+ return writes.map(write => path.relative(cwd, write.dest));
750
+ }
751
+
752
+ /**
753
+ * The `.css.d.ts` stylesheet type stubs the build writes that are missing,
754
+ * which the check names as checked and warns about in text mode without
755
+ * failing.
756
+ * @param {Array<{dest: string, warnWhenMissing?: boolean}>} writes @param {string} cwd
757
+ * @returns {string[]}
758
+ */
759
+ function missingDeclarations(writes, cwd) {
738
760
  return writes
739
- .filter(write => !write.allowMissingInCheck || fs.existsSync(write.dest))
761
+ .filter(write => write.warnWhenMissing && !fs.existsSync(write.dest))
740
762
  .map(write => path.relative(cwd, write.dest));
741
763
  }
742
764
 
765
+ /**
766
+ * The one text-mode warning line for missing stylesheet type stubs, or
767
+ * nothing when none is missing. The logger is silent under --json.
768
+ * @param {Array<{dest: string, warnWhenMissing?: boolean}>} writes @param {string} cwd
769
+ */
770
+ function warnMissingDeclarations(writes, cwd) {
771
+ const missing = missingDeclarations(writes, cwd);
772
+ if (missing.length === 0) return;
773
+ logger.warn(
774
+ ` Warning: stylesheet type declaration file${missing.length === 1 ? '' : 's'} not found: ${missing.join(', ')}. The check does not fail on ${missing.length === 1 ? 'it' : 'them'}; a build writes ${missing.length === 1 ? 'it' : 'them'}.`,
775
+ );
776
+ }
777
+
743
778
  /** Remedies that end a confinement error, by where the output path came from. */
744
779
  const OUT_REMEDY = 'Pass an --out path inside it.';
745
780
  const SOURCE_REMEDY =
@@ -1069,21 +1104,48 @@ function assertThemeSourceFile(filePath, cwd) {
1069
1104
  * @param {Array<{dest: string}>} writes
1070
1105
  * @param {string} filePath the theme source
1071
1106
  * @param {string} cwd
1072
- * @param {{claimed?: Map<string, string>, rerun?: string, refuseUnmarked?: boolean, elsewhere?: string}} [options]
1107
+ * @param {{claimed?: Map<string, string>, rerun?: (out: string | null) => string, refuseUnmarked?: boolean, elsewhere?: string, onEditorFolder?: (warning: string) => void}} [options]
1073
1108
  * `claimed`: outputs written earlier in this run, by the source that wrote
1074
- * them; `rerun`: this build's command, to run again once the file is gone;
1075
- * `elsewhere`: the option that moves the outputs
1109
+ * them; `rerun`: this build's command with another --out, or without one;
1110
+ * `elsewhere`: the option that moves the outputs; `onEditorFolder`: takes
1111
+ * the editor-folder warning instead of the logger (a watcher prints it once)
1076
1112
  */
1077
1113
  function assertOutputsReplaceable(
1078
1114
  writes,
1079
1115
  filePath,
1080
1116
  cwd,
1081
- {claimed, rerun, refuseUnmarked = false, elsewhere = '--out'} = {},
1117
+ {
1118
+ claimed,
1119
+ rerun,
1120
+ refuseUnmarked = false,
1121
+ elsewhere = '--out',
1122
+ onEditorFolder,
1123
+ } = {},
1082
1124
  ) {
1083
1125
  const rel = (/** @type {string} */ file) => path.relative(cwd, file);
1084
1126
  const sourceKey = outputKey(filePath);
1085
1127
  const seen = new Set();
1086
1128
  let warnedOtherSource = false;
1129
+ // The same build without --out, with its other options.
1130
+ const beside = {
1131
+ name: rerun
1132
+ ? rerun(null)
1133
+ : formatCliCommand(`theme build ${shellWord(rel(filePath))}`, cwd),
1134
+ reason: 'write the outputs beside this source',
1135
+ };
1136
+ // A folder the theme editor made holds that theme's outputs only: another
1137
+ // source's outputs there would sit beside its settings as if they were
1138
+ // its own. A source inside the folder builds there as always; one outside
1139
+ // it writes there too, with a warning.
1140
+ let editorFolder = false;
1141
+ if (refuseUnmarked && writes.length) {
1142
+ const dir = path.resolve(path.dirname(writes[0].dest));
1143
+ const record = editorThemeRecord(dir);
1144
+ const fromDir = path.relative(dir, path.resolve(filePath));
1145
+ // On Windows a source on another drive has an absolute relative path.
1146
+ const inside = !fromDir.startsWith('..') && !path.isAbsolute(fromDir);
1147
+ if (record && !inside) editorFolder = true;
1148
+ }
1087
1149
  for (const write of writes) {
1088
1150
  const key = outputKey(write.dest);
1089
1151
  if (key === sourceKey) {
@@ -1147,15 +1209,7 @@ function assertOutputsReplaceable(
1147
1209
  } else if (otherSource) {
1148
1210
  throw new AstryxError(
1149
1211
  `${rel(write.dest)} is the build output of another theme source, ${otherSource}, so this build does not replace it. Pick another ${elsewhere}.`,
1150
- [
1151
- {
1152
- name: formatCliCommand(
1153
- `theme build ${shellWord(rel(filePath))}`,
1154
- cwd,
1155
- ),
1156
- reason: 'write the outputs beside this source',
1157
- },
1158
- ],
1212
+ [beside],
1159
1213
  ERROR_CODES.ERR_FILE_EXISTS,
1160
1214
  );
1161
1215
  }
@@ -1166,20 +1220,70 @@ function assertOutputsReplaceable(
1166
1220
  stat.size > 0 &&
1167
1221
  !fileHead(write.dest).includes(GENERATED_MARKER)
1168
1222
  ) {
1223
+ // Never advice to delete it: it may be the person's file (a settings
1224
+ // file, app source). A path that takes every output instead.
1225
+ const free = freeOutPath(writes);
1169
1226
  throw new AstryxError(
1170
- `${rel(write.dest)} was written by an older CLI (before 0.6.7) or by hand, so the build does not replace it. Delete that file and run the same build again, or pick another ${elsewhere}.`,
1171
- rerun
1172
- ? [
1173
- {
1174
- name: rerun,
1175
- reason: 'run the same build again once the file is deleted',
1176
- },
1177
- ]
1178
- : undefined,
1227
+ `${rel(write.dest)} is a file this build did not write (it has no build header: your own file, another tool's, or an output from a CLI before 0.6.7), so the build does not replace it. Pick another ${elsewhere}.`,
1228
+ [
1229
+ ...(free && rerun
1230
+ ? [
1231
+ {
1232
+ name: rerun(rel(free)),
1233
+ reason: 'a path where no output of this build exists yet',
1234
+ },
1235
+ ]
1236
+ : []),
1237
+ beside,
1238
+ ],
1179
1239
  ERROR_CODES.ERR_FILE_EXISTS,
1180
1240
  );
1181
1241
  }
1182
1242
  }
1243
+ // Warned only once nothing refuses the build, so it names a write that
1244
+ // happens.
1245
+ if (editorFolder) {
1246
+ const dir = path.dirname(writes[0].dest);
1247
+ const warning = `Warning: ${rel(dir) || '.'} is a folder the theme editor made, and ${rel(filePath)} is outside it; the build writes there anyway. Build beside the source, or pick another ${elsewhere}, to keep the editor's folder to its own theme.`;
1248
+ if (onEditorFolder) onEditorFolder(warning);
1249
+ else logger.warn(warning);
1250
+ }
1251
+ }
1252
+
1253
+ /**
1254
+ * An --out path where no output of this build exists yet, or null. The
1255
+ * stylesheet and its `.css.d.ts` follow the name --out gives; the module and
1256
+ * its types are named after the theme, so a new name beside them (`brand-2.css`)
1257
+ * helps only when those are free, else a new folder (`brand-2/brand.css`).
1258
+ * @param {Array<{dest: string}>} writes the stylesheet first
1259
+ * @returns {string | null}
1260
+ */
1261
+ function freeOutPath(writes) {
1262
+ const css = writes[0]?.dest;
1263
+ if (!css) return null;
1264
+ const dir = path.dirname(css);
1265
+ const stem = path.basename(css).replace(/\.css$/i, '');
1266
+ /** Whether a write set (each output moved by `move`) touches any file. */
1267
+ const blocked = (/** @type {(dest: string) => string} */ move) =>
1268
+ writes.some(w => fs.existsSync(move(w.dest)));
1269
+ for (let n = 2; n < 100; n += 1) {
1270
+ const next = `${stem}-${n}`;
1271
+ // Only the stylesheet and its types follow --out's name.
1272
+ // The exact names a build with `--out <next>.css` writes, also when
1273
+ // --out did not end in .css (`o.txt` suggests `o.txt-2.css`).
1274
+ const renamed = (/** @type {string} */ dest) =>
1275
+ dest === css
1276
+ ? path.join(dir, `${next}.css`)
1277
+ : dest === `${css}.d.ts`
1278
+ ? path.join(dir, `${next}.css.d.ts`)
1279
+ : dest;
1280
+ if (!blocked(renamed)) return path.join(dir, `${next}.css`);
1281
+ const folder = (/** @type {string} */ dest) =>
1282
+ path.join(dir, next, path.basename(dest));
1283
+ if (!fs.existsSync(path.join(dir, next)) && !blocked(folder))
1284
+ return path.join(dir, next, path.basename(css));
1285
+ }
1286
+ return null;
1183
1287
  }
1184
1288
 
1185
1289
  /**
@@ -3504,13 +3608,14 @@ function validateHeadingTypeAugmentationSupport(themeDef) {
3504
3608
  * `logger` (silent by default).
3505
3609
  *
3506
3610
  * @param {string} file - Theme file path, resolved against `cwd`.
3507
- * @param {{out?: string, check?: boolean, iconsSpecifier?: string, __prepareFamily?: boolean, __familyLoader?: any, __familyInterception?: any, __onPrivateVarError?: () => void, __writeRoot?: string, __claimedOutputs?: Map<string, string>, __deferredWrites?: Array<Array<{dest: string, content: string}>>, __commandWords?: {group: string, build: string}}} [options] -
3611
+ * @param {{out?: string, check?: boolean, iconsSpecifier?: string, __prepareFamily?: boolean, __familyLoader?: any, __familyInterception?: any, __onPrivateVarError?: () => void, __writeRoot?: string, __claimedOutputs?: Map<string, string>, __deferredWrites?: Array<Array<{dest: string, content: string}>>, __commandWords?: {group: string, build: string}, __onEditorFolder?: (warning: string) => void}} [options] -
3508
3612
  * `out` overrides the output CSS path, or names the folder for it; `check` compares against on-disk outputs
3509
3613
  * instead of writing. `__claimedOutputs` records the outputs earlier builds
3510
3614
  * in one run wrote, so a later theme cannot replace them; with
3511
3615
  * `__deferredWrites` the build adds its outputs to that list instead of
3512
3616
  * writing them, for writeDeferredBuildOutputs. `__writeRoot` replaces `cwd` as the directory every
3513
- * output must stay inside, for a CLI-internal scratch build. `iconsSpecifier` overrides the icon registry import
3617
+ * output must stay inside, for a CLI-internal scratch build. `__onEditorFolder`
3618
+ * takes the editor-folder warning instead of the logger. `iconsSpecifier` overrides the icon registry import
3514
3619
  * specifier in the generated module (e.g. `./icons.mjs`); when omitted, the
3515
3620
  * direct registry specifiers from the theme source are emitted unchanged.
3516
3621
  * @param {{cwd?: string, projectCore?: boolean}} [ctx] `projectCore`: only the
@@ -4209,7 +4314,7 @@ async function themeBuildInternal(
4209
4314
 
4210
4315
  const writes = [
4211
4316
  {dest: outPath, content: cssContent},
4212
- {dest: cssDtsPath, content: cssDtsContent, allowMissingInCheck: true},
4317
+ {dest: cssDtsPath, content: cssDtsContent, warnWhenMissing: true},
4213
4318
  {dest: jsPath, content: jsContent},
4214
4319
  {dest: dtsPath, content: dtsContent},
4215
4320
  ];
@@ -4223,7 +4328,7 @@ async function themeBuildInternal(
4223
4328
  if (!options.check) {
4224
4329
  confineOutputs(writes, writeRoot, options.out ? OUT_REMEDY : SOURCE_REMEDY);
4225
4330
  // prettier-ignore
4226
- const rerun = formatCliCommand(['theme', 'build', path.relative(cwd, filePath), ...(options.out ? ['--out', options.out] : []), ...(options.iconsSpecifier ? ['--icons-specifier', options.iconsSpecifier] : [])].map(shellWord).join(' '), cwd);
4331
+ const rerun = (/** @type {string | null} */ out) => formatCliCommand(['theme', 'build', path.relative(cwd, filePath), ...(out == null ? [] : ['--out', out]), ...(options.iconsSpecifier ? ['--icons-specifier', options.iconsSpecifier] : [])].map(shellWord).join(' '), cwd);
4227
4332
  // A header-less file at the default paths beside the source is an output
4228
4333
  // of a CLI before 0.6.7, which the build has always replaced. A path that
4229
4334
  // --out names may hold anything, so there only build outputs are replaced.
@@ -4231,6 +4336,7 @@ async function themeBuildInternal(
4231
4336
  claimed: options.__claimedOutputs,
4232
4337
  rerun,
4233
4338
  refuseUnmarked: Boolean(options.out),
4339
+ onEditorFolder: options.__onEditorFolder,
4234
4340
  });
4235
4341
  }
4236
4342
 
@@ -4263,15 +4369,18 @@ async function themeBuildInternal(
4263
4369
  `\n Rebuild with: ${rebuildLine(rebuildCommand, options.__commandWords)}`,
4264
4370
  );
4265
4371
  }
4266
- return {
4372
+ warnMissingDeclarations(writes, cwd);
4373
+ return /** @type {import('../theme.type.mjs').ThemeBuildCheckResponse} */ ({
4267
4374
  type: 'theme.build.check',
4268
4375
  data: {
4269
4376
  name: themeDef.name,
4270
4377
  upToDate,
4271
4378
  stale,
4272
4379
  checked: checkedBuildOutputs(writes, cwd),
4380
+ // The code a stale check exits 1 with, for --json callers.
4381
+ ...(upToDate ? {} : {code: ERROR_CODES.ERR_THEME_STALE}),
4273
4382
  },
4274
- };
4383
+ });
4275
4384
  }
4276
4385
 
4277
4386
  // A batch publishes every theme's outputs after all of them are built, so
@@ -4871,7 +4980,7 @@ export async function themeBuildFamily(
4871
4980
  },
4872
4981
  {
4873
4982
  dest: cssDtsPath,
4874
- allowMissingInCheck: true,
4983
+ warnWhenMissing: true,
4875
4984
  content:
4876
4985
  generatedHeader(headerSource, 'ts', buildCommand, root.versions) +
4877
4986
  'export {};\n',
@@ -4931,15 +5040,18 @@ export async function themeBuildFamily(
4931
5040
  `\n Rebuild with: ${rebuildLine(rebuildCommand, options.__commandWords)}`,
4932
5041
  );
4933
5042
  }
4934
- return {
5043
+ warnMissingDeclarations(writes, cwd);
5044
+ return /** @type {import('../theme.type.mjs').ThemeBuildCheckResponse} */ ({
4935
5045
  type: 'theme.build.check',
4936
5046
  data: {
4937
5047
  name: root.theme.name,
4938
5048
  upToDate,
4939
5049
  stale,
4940
5050
  checked: checkedBuildOutputs(writes, cwd),
5051
+ // The code a stale check exits 1 with, for --json callers.
5052
+ ...(upToDate ? {} : {code: ERROR_CODES.ERR_THEME_STALE}),
4941
5053
  },
4942
- };
5054
+ });
4943
5055
  }
4944
5056
  writeBuildOutputs(writes);
4945
5057
  for (const output of Object.values(outputs)) logger.log(`[ok] ${output}`);
@@ -7,7 +7,12 @@
7
7
 
8
8
  import * as path from 'node:path';
9
9
  import {AstryxError} from '../../error.mjs';
10
+ import {logger} from '../../logger.mjs';
10
11
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
12
+ import {
13
+ formatCliCommand,
14
+ quoteCommandArgument,
15
+ } from '../../../foundation/env/package-manager.mjs';
11
16
  import {
12
17
  ejectThemeFiles,
13
18
  findTheme,
@@ -54,12 +59,28 @@ export async function themeEject(slug, options = {}) {
54
59
  );
55
60
  }
56
61
 
57
- const {outputDir, files} = ejectThemeFiles(match, {
62
+ const {outputDir, files, editorFolder} = ejectThemeFiles(match, {
58
63
  cwd,
59
64
  targetPath,
60
65
  overwrite,
61
66
  takenSlugs: await availableSlugs(cwd),
62
67
  });
68
+ // A folder the theme editor made holds that theme only, but a subfolder of
69
+ // it was always writable, so the fork is written there with one warning.
70
+ if (editorFolder != null) {
71
+ const fork = formatCliCommand(
72
+ [
73
+ 'theme eject',
74
+ match.slug,
75
+ ...(packageName
76
+ ? ['--package', quoteCommandArgument(packageName)]
77
+ : []),
78
+ ].join(' '),
79
+ cwd,
80
+ );
81
+ const warning = `Warning: ${editorFolder} is a folder the theme editor made, and the fork in ${outputDir} is inside it; eject writes there anyway. Fork into the project's local themes folder (${fork}), or pick a path outside ${editorFolder}, to keep the editor's folder to its own theme.`;
82
+ logger.warn(warning);
83
+ }
63
84
  const {missingPackages, installCommand} = forkNeeds(outputDir, files, cwd);
64
85
  return {
65
86
  type: 'theme.eject',
@@ -15,7 +15,26 @@ export function themeTemplate(options?: {
15
15
  overwrite?: boolean;
16
16
  cwd?: string;
17
17
  }): import("../theme.type.mjs").ThemeTemplateResponse;
18
+ /**
19
+ * The template write behind `themeTemplate`, with the experimental editor's
20
+ * one difference: with `editorFilesOnly`, in a folder the theme editor made
21
+ * only the editor's own files (`<folder>.*`) are protected, so an earlier
22
+ * template there is the person's. Internal: the experimental `template`
23
+ * command reaches it through `editorThemeTemplate` (api/theme-edit), and it
24
+ * is not part of the public API.
25
+ * @internal
26
+ * @param {{targetPath?: string, overwrite?: boolean, cwd?: string}} options
27
+ * @param {boolean} editorFilesOnly
28
+ * @returns {import('../theme.type.mjs').ThemeTemplateResponse}
29
+ */
30
+ export function writeThemeTemplate(options: {
31
+ targetPath?: string;
32
+ overwrite?: boolean;
33
+ cwd?: string;
34
+ }, editorFilesOnly: boolean): import("../theme.type.mjs").ThemeTemplateResponse;
18
35
  /** The annotated `defineTheme` reference that ships with the CLI. */
19
36
  export const THEME_TEMPLATE_SRC: string;
37
+ /** The warning the CLI prints when it writes the template outside a project. */
38
+ export const OUTSIDE_PROJECT_WARNING: "Warning: no package.json is in this folder or above it, so the template is written here. Run it inside a project, or run `npm init -y` here first, to write it at a project root.";
20
39
  /** Where it lands when the caller does not say. */
21
40
  export const THEME_TEMPLATE_DEFAULT_PATH: "theme.template.ts";
@@ -15,8 +15,11 @@
15
15
 
16
16
  import * as fs from 'node:fs';
17
17
  import * as path from 'node:path';
18
- import {CLI_ROOT} from '../../../foundation/fs/paths.mjs';
19
- import {assertWithin, PathSafetyError} from '../../../foundation/fs/path-safety.mjs';
18
+ import {CLI_ROOT, projectWritePath} from '../../../foundation/fs/paths.mjs';
19
+ import {
20
+ assertWithin,
21
+ PathSafetyError,
22
+ } from '../../../foundation/fs/path-safety.mjs';
20
23
  import {stripCopyrightHeader} from '../../../foundation/text/copyright-header.mjs';
21
24
  import {formatCliCommand} from '../../../foundation/env/package-manager.mjs';
22
25
  import {fsErrorMessage} from '../../../foundation/fs/fs-error.mjs';
@@ -25,7 +28,15 @@ import {AstryxError} from '../../error.mjs';
25
28
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
26
29
 
27
30
  /** The annotated `defineTheme` reference that ships with the CLI. */
28
- export const THEME_TEMPLATE_SRC = path.join(CLI_ROOT, 'assets', 'theme.template.ts');
31
+ export const THEME_TEMPLATE_SRC = path.join(
32
+ CLI_ROOT,
33
+ 'assets',
34
+ 'theme.template.ts',
35
+ );
36
+
37
+ /** The warning the CLI prints when it writes the template outside a project. */
38
+ export const OUTSIDE_PROJECT_WARNING =
39
+ 'Warning: no package.json is in this folder or above it, so the template is written here. Run it inside a project, or run `npm init -y` here first, to write it at a project root.';
29
40
 
30
41
  /** Where it lands when the caller does not say. */
31
42
  export const THEME_TEMPLATE_DEFAULT_PATH = 'theme.template.ts';
@@ -72,6 +83,22 @@ function isReplaceableThemeFile(file) {
72
83
  * @returns {import('../theme.type.mjs').ThemeTemplateResponse}
73
84
  */
74
85
  export function themeTemplate(options = {}) {
86
+ return writeThemeTemplate(options, false);
87
+ }
88
+
89
+ /**
90
+ * The template write behind `themeTemplate`, with the experimental editor's
91
+ * one difference: with `editorFilesOnly`, in a folder the theme editor made
92
+ * only the editor's own files (`<folder>.*`) are protected, so an earlier
93
+ * template there is the person's. Internal: the experimental `template`
94
+ * command reaches it through `editorThemeTemplate` (api/theme-edit), and it
95
+ * is not part of the public API.
96
+ * @internal
97
+ * @param {{targetPath?: string, overwrite?: boolean, cwd?: string}} options
98
+ * @param {boolean} editorFilesOnly
99
+ * @returns {import('../theme.type.mjs').ThemeTemplateResponse}
100
+ */
101
+ export function writeThemeTemplate(options, editorFilesOnly) {
75
102
  const {
76
103
  targetPath = THEME_TEMPLATE_DEFAULT_PATH,
77
104
  overwrite = false,
@@ -100,20 +127,33 @@ export function themeTemplate(options = {}) {
100
127
  );
101
128
  }
102
129
 
130
+ // The project the folder belongs to bounds the path, as for other theme
131
+ // commands: a path given is read from the current folder, and no path
132
+ // means the project root.
133
+ // With no package.json at or above the folder it writes here, and the CLI
134
+ // prints OUTSIDE_PROJECT_WARNING.
135
+ // Read as every theme command reads a path it writes; a backslash
136
+ // separates folders on every platform.
137
+ const {projectDir, relative: fromRoot} = projectWritePath(
138
+ cwd,
139
+ options.targetPath == null ? undefined : String(targetPath),
140
+ THEME_TEMPLATE_DEFAULT_PATH,
141
+ );
103
142
  let resolved;
104
143
  try {
105
- resolved = assertWithin(targetPath, cwd, {label: 'theme template path'});
144
+ resolved = assertWithin(fromRoot, projectDir, {
145
+ label: 'theme template path',
146
+ });
106
147
  } catch (err) {
107
148
  if (err instanceof PathSafetyError) {
108
149
  throw new AstryxError(
109
- err.message,
150
+ // The path as typed, not as read from the project root.
151
+ err.message.replace(`"${fromRoot}"`, `"${targetPath}"`),
110
152
  [
111
153
  {
112
- name: formatCliCommand(
113
- `theme template ${THEME_TEMPLATE_DEFAULT_PATH}`,
114
- cwd,
115
- ),
116
- reason: 'write the template in this project',
154
+ // No path: the template at the project root.
155
+ name: formatCliCommand('theme template', cwd),
156
+ reason: 'write the template at the project root',
117
157
  },
118
158
  ],
119
159
  ERROR_CODES.ERR_PATH_TRAVERSAL,
@@ -153,10 +193,39 @@ export function themeTemplate(options = {}) {
153
193
  );
154
194
  }
155
195
 
156
- const editorRecord =
157
- fs.existsSync(resolved) && overwrite
158
- ? editorThemeRecord(path.dirname(resolved))
159
- : null;
196
+ // The folder as it is on disk (`themes/T` is `themes/t` on a
197
+ // case-insensitive disk, or through a link).
198
+ const realDir = fs.existsSync(resolved)
199
+ ? fs.realpathSync(path.dirname(resolved))
200
+ : path.dirname(resolved);
201
+ const record = fs.existsSync(resolved) ? editorThemeRecord(realDir) : null;
202
+ // The editor's own files are `<folder>.*` beside its settings; with
203
+ // editorFilesOnly any other file there (an earlier template) is the
204
+ // person's.
205
+ // On a case-insensitive disk `T.theme.ts` is `t.theme.ts`: names are
206
+ // compared in any case, and an existing file by what it is on disk.
207
+ const folder = path.basename(realDir).toLowerCase();
208
+ const sameFile = (/** @type {string} */ other) => {
209
+ try {
210
+ const a = fs.statSync(resolved);
211
+ const b = fs.statSync(other);
212
+ return a.ino === b.ino && a.dev === b.dev;
213
+ } catch {
214
+ return false;
215
+ }
216
+ };
217
+ const editorFile =
218
+ record != null &&
219
+ (!editorFilesOnly ||
220
+ path.basename(resolved).toLowerCase().startsWith(`${folder}.`) ||
221
+ fs
222
+ .readdirSync(realDir)
223
+ .some(
224
+ f =>
225
+ f.toLowerCase().startsWith(`${folder}.`) &&
226
+ sameFile(path.join(realDir, f)),
227
+ ));
228
+ const editorRecord = overwrite && editorFile ? record : null;
160
229
  if (editorRecord) {
161
230
  throw new AstryxError(
162
231
  `${relative} belongs to a theme the theme editor made (${editorRecord} is beside it). Replacing it would cut the theme off from its settings, so --overwrite does not replace it.`,
@@ -196,9 +265,7 @@ export function themeTemplate(options = {}) {
196
265
  if (fs.existsSync(resolved) && !overwrite) {
197
266
  // Whether --overwrite would replace this file, and if not, a path that
198
267
  // takes the template, so the hint never names a command that refuses.
199
- const overwritable =
200
- !editorThemeRecord(path.dirname(resolved)) &&
201
- isReplaceableThemeFile(resolved);
268
+ const overwritable = !editorFile && isReplaceableThemeFile(resolved);
202
269
  return {
203
270
  type: 'theme.template',
204
271
  data: {
@@ -212,7 +279,9 @@ export function themeTemplate(options = {}) {
212
279
  }
213
280
 
214
281
  // Our repo header has no business in someone else's source tree.
215
- const contents = stripCopyrightHeader(fs.readFileSync(THEME_TEMPLATE_SRC, 'utf-8'));
282
+ const contents = stripCopyrightHeader(
283
+ fs.readFileSync(THEME_TEMPLATE_SRC, 'utf-8'),
284
+ );
216
285
  try {
217
286
  fs.mkdirSync(path.dirname(resolved), {recursive: true});
218
287
  fs.writeFileSync(resolved, contents);
@@ -223,7 +292,10 @@ export function themeTemplate(options = {}) {
223
292
  throw new AstryxError(message, undefined, ERROR_CODES.ERR_WRITE_FAILED);
224
293
  }
225
294
 
226
- return {type: 'theme.template', data: {path: relative, written: true, reason: null}};
295
+ return {
296
+ type: 'theme.template',
297
+ data: {path: relative, written: true, reason: null},
298
+ };
227
299
  }
228
300
 
229
301
  /** @param {string} file */
@@ -32,6 +32,9 @@ export type ThemeBuildResponse = {
32
32
  */
33
33
  export type ThemeBuildCheckResponse = {
34
34
  type: "theme.build.check";
35
+ /**
36
+ * `code` is set when the outputs are stale or missing (exit 1).
37
+ */
35
38
  data: {
36
39
  name: string;
37
40
  upToDate: boolean;
@@ -40,6 +43,7 @@ export type ThemeBuildCheckResponse = {
40
43
  reason: "missing" | "outdated";
41
44
  }>;
42
45
  checked: string[];
46
+ code?: "ERR_THEME_STALE";
43
47
  };
44
48
  };
45
49
  /**
@@ -281,7 +285,7 @@ export type TonalPaletteFamilyInput = {
281
285
  * `neutral` derives the ramp from
282
286
  * `neutralProfile` instead of the seed hue; defaults to `chromatic`.
283
287
  */
284
- kind?: "neutral" | "chromatic" | undefined;
288
+ kind?: "chromatic" | "neutral" | undefined;
285
289
  /**
286
290
  * Colors pinned at specific stops.
287
291
  */
@@ -38,7 +38,8 @@
38
38
  * astryx --json theme build <file> --check
39
39
  * @typedef {object} ThemeBuildCheckResponse
40
40
  * @property {'theme.build.check'} type
41
- * @property {{name: string, upToDate: boolean, stale: Array<{path: string, reason: 'missing' | 'outdated'}>, checked: string[]}} data
41
+ * @property {{name: string, upToDate: boolean, stale: Array<{path: string, reason: 'missing' | 'outdated'}>, checked: string[], code?: 'ERR_THEME_STALE'}} data
42
+ * `code` is set when the outputs are stale or missing (exit 1).
42
43
  */
43
44
 
44
45
  /**
@@ -58,7 +58,7 @@ export const doc = {
58
58
  name: 'options.out',
59
59
  type: 'string',
60
60
  description:
61
- 'Override the output CSS path, relative or absolute; a build refuses a path outside cwd, and check mode refuses only a relative path that escapes cwd. The .js, .d.ts and any .variants.d.ts are written in the same directory, named after the theme (<name>.js), not after the CSS file. An existing folder, or a path that ends in a separator, names the folder: the CSS is written there as <name>.css. A file path should end in .css; another name still builds, with a warning, and the import line it prints names the file as written.',
61
+ 'Override the output CSS path, relative or absolute; a build refuses a path outside cwd, and check mode refuses only a relative path that escapes cwd. The .js, .d.ts and any .variants.d.ts are written in the same directory, named after the theme (<name>.js), not after the CSS file. An existing folder, or a path that ends in a separator, names the folder: the CSS is written there as <name>.css. A file path should end in .css; another name still builds, with a warning, and the import line it prints names the file as written. A path in a folder the theme editor made (folder <slug>/ holds <slug>.settings.json) takes the outputs of a source outside that folder too, and the CLI warns; to keep the editor folder to its own theme, build beside the source, or pick an out path outside it.',
62
62
  },
63
63
  {
64
64
  name: 'options.check',
@@ -90,7 +90,7 @@ export const doc = {
90
90
  {
91
91
  type: 'theme.build.check',
92
92
  description:
93
- 'The {check: true} receipt: theme name, an upToDate flag, the stale outputs (each {path, reason: "missing" | "outdated"}), and the full list of checked paths. Writes nothing. Resolves to null, like a normal build, when the theme produces no CSS.',
93
+ 'The {check: true} receipt: theme name, an upToDate flag, the stale outputs (each {path, reason: "missing" | "outdated"}), the full list of checked paths, and, when stale, code ERR_THEME_STALE. Writes nothing. Resolves to null, like a normal build, when the theme produces no CSS.',
94
94
  },
95
95
  ],
96
96
  throws: [
@@ -108,7 +108,7 @@ export const doc = {
108
108
  },
109
109
  {
110
110
  code: 'ERR_FILE_EXISTS',
111
- when: 'a path that out names holds a non-empty file without the generated header, such as package.json, app source, or an output from a CLI before 0.6.7; nothing is written. Delete the file and run the same build again (the suggestion), or pick another out path. At the default paths beside the source, and at family output paths, a header-less file is an output of a CLI before 0.6.7 and is replaced',
111
+ when: 'a path that out names holds a non-empty file without the generated header, such as package.json, app source, or an output from a CLI before 0.6.7; nothing is written. The suggestions name a free out path beside it and the build beside the source; the file is left as it is. At the default paths beside the source, and at family output paths, a header-less file is an output of a CLI before 0.6.7 and is replaced',
112
112
  },
113
113
  {
114
114
  code: 'ERR_INVALID_ARGUMENT',
@@ -13,7 +13,7 @@ export const doc = {
13
13
  displayName: 'themeEject()',
14
14
  summary: 'Eject an available theme into a project as editable source.',
15
15
  description:
16
- "Resolves a theme from the CLI bundle or an installed integration and ejects it into the consumer project: a bundled theme's source files, or an integration theme's complete directory with its descriptor. Writes are staged and existing files require explicit overwrite. Duplicate slugs fail closed until the caller selects an owner package.",
16
+ "Resolves a theme from the CLI bundle or an installed integration and ejects it into the consumer project: a bundled theme's source files, or an integration theme's complete directory with its descriptor. Writes are staged and existing files require explicit overwrite. A target inside a folder the theme editor made (a subfolder of <slug>/ that holds <slug>.settings.json) is written, and the CLI warns. Duplicate slugs fail closed until the caller selects an owner package.",
17
17
  importPath: '@astryxdesign/cli/api',
18
18
  signature:
19
19
  'themeEject(slug: string, options?: {targetPath?: string, overwrite?: boolean, cwd?: string, package?: string}): Promise<ThemeEjectResponse>',