@astryxdesign/cli 0.7.17 → 0.7.18-canary.gb660bba

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 (215) hide show
  1. package/README.md +1 -1
  2. package/api/discover/_adapter.d.mts +7 -5
  3. package/api/discover/_adapter.mjs +12 -11
  4. package/api/doctor/_adapter.d.mts +11 -62
  5. package/api/doctor/_adapter.mjs +242 -472
  6. package/api/doctor/doctor.d.mts +1 -1
  7. package/api/doctor/doctor.mjs +1 -1
  8. package/api/doctor/theme-checks.mjs +37 -45
  9. package/api/error.d.mts +21 -0
  10. package/api/error.mjs +89 -0
  11. package/api/hook/list/list.d.mts +1 -1
  12. package/api/icon/icon.doc.mjs +1 -1
  13. package/api/icon/icon.type.mjs +1 -1
  14. package/api/icon/search/search.d.mts +4 -1
  15. package/api/icon/search/search.mjs +41 -4
  16. package/api/render/_adapter.mjs +233 -40
  17. package/api/render/render.type.d.mts +1 -1
  18. package/api/render/render.type.mjs +1 -1
  19. package/api/search/ORDERING.md +2 -2
  20. package/api/search/_candidate-score.mjs +32 -8
  21. package/api/search/_cards.json +1 -1
  22. package/api/search/_compound.d.mts +0 -1
  23. package/api/search/_compound.mjs +93 -2
  24. package/api/search/_lexical.mjs +177 -17
  25. package/api/search/_order.d.mts +3 -1
  26. package/api/search/_order.mjs +16 -5
  27. package/api/search/_result.d.mts +9 -0
  28. package/api/search/_result.mjs +10 -0
  29. package/api/search/_score.mjs +21 -3
  30. package/api/swizzle/_moduleGraph.d.mts +8 -0
  31. package/api/swizzle/_moduleGraph.mjs +61 -3
  32. package/api/template/copy/copy.mjs +5 -3
  33. package/api/theme/_adapter.mjs +48 -12
  34. package/api/theme/build/build.d.mts +4 -0
  35. package/api/theme/build/build.mjs +69 -18
  36. package/api/theme/build/style-keys.mjs +4 -5
  37. package/api/theme/build/theme-object.mjs +5 -0
  38. package/api/theme/palette/generate/color.d.mts +47 -0
  39. package/api/theme/palette/generate/color.mjs +58 -1
  40. package/api/theme/palette/generate/derive-look.d.mts +293 -0
  41. package/api/theme/palette/generate/derive-look.mjs +1362 -0
  42. package/api/theme/palette/generate/editor-look.d.mts +62 -0
  43. package/api/theme/palette/generate/editor-look.mjs +550 -0
  44. package/api/theme/palette/generate/mapping.d.mts +94 -9
  45. package/api/theme/palette/generate/mapping.mjs +94 -22
  46. package/api/theme-edit/_adapter.mjs +10 -1
  47. package/api/theme-edit/_catalog.d.mts +13 -13
  48. package/api/theme-edit/_catalog.mjs +38 -16
  49. package/api/theme-edit/_check.d.mts +82 -6
  50. package/api/theme-edit/_check.mjs +1270 -238
  51. package/api/theme-edit/_color.d.mts +74 -0
  52. package/api/theme-edit/_color.mjs +168 -3
  53. package/api/theme-edit/_compiler.d.mts +3 -5
  54. package/api/theme-edit/_compiler.mjs +122 -23
  55. package/api/theme-edit/_context.d.mts +46 -5
  56. package/api/theme-edit/_context.mjs +97 -49
  57. package/api/theme-edit/_css.d.mts +11 -0
  58. package/api/theme-edit/_css.mjs +40 -0
  59. package/api/theme-edit/_edit.d.mts +13 -0
  60. package/api/theme-edit/_edit.mjs +1450 -128
  61. package/api/theme-edit/_how-commands.d.mts +12 -0
  62. package/api/theme-edit/_how-commands.mjs +96 -14
  63. package/api/theme-edit/_how-question.d.mts +73 -29
  64. package/api/theme-edit/_how-question.mjs +8233 -1207
  65. package/api/theme-edit/_how-sides.d.mts +22 -0
  66. package/api/theme-edit/_how-sides.mjs +173 -0
  67. package/api/theme-edit/_how-steps.d.mts +42 -0
  68. package/api/theme-edit/_how-steps.mjs +208 -0
  69. package/api/theme-edit/_how-values.d.mts +29 -0
  70. package/api/theme-edit/_how-values.mjs +111 -0
  71. package/api/theme-edit/_how-words.d.mts +15 -0
  72. package/api/theme-edit/_how-words.mjs +239 -0
  73. package/api/theme-edit/_how.d.mts +1 -1
  74. package/api/theme-edit/_how.mjs +412 -37
  75. package/api/theme-edit/_missed.d.mts +2 -1
  76. package/api/theme-edit/_missed.mjs +125 -13
  77. package/api/theme-edit/_normal.d.mts +5 -3
  78. package/api/theme-edit/_normal.mjs +54 -14
  79. package/api/theme-edit/_palette.d.mts +204 -4
  80. package/api/theme-edit/_palette.mjs +901 -25
  81. package/api/theme-edit/_part-pieces.d.mts +54 -0
  82. package/api/theme-edit/_part-pieces.mjs +179 -0
  83. package/api/theme-edit/_part-words.mjs +33 -5
  84. package/api/theme-edit/_parts.d.mts +133 -0
  85. package/api/theme-edit/_parts.mjs +599 -10
  86. package/api/theme-edit/_picture-traits.d.mts +259 -0
  87. package/api/theme-edit/_picture-traits.mjs +1030 -0
  88. package/api/theme-edit/_preview.d.mts +84 -4
  89. package/api/theme-edit/_preview.mjs +260 -24
  90. package/api/theme-edit/_project.d.mts +14 -4
  91. package/api/theme-edit/_project.mjs +95 -26
  92. package/api/theme-edit/_set-line.d.mts +29 -0
  93. package/api/theme-edit/_set-line.mjs +100 -0
  94. package/api/theme-edit/_settings.d.mts +625 -28
  95. package/api/theme-edit/_settings.mjs +4587 -880
  96. package/api/theme-edit/_tables.d.mts +25 -1
  97. package/api/theme-edit/_tables.mjs +115 -11
  98. package/api/theme-edit/_text-looks.d.mts +78 -0
  99. package/api/theme-edit/_text-looks.mjs +376 -0
  100. package/api/theme-edit/_vet.d.mts +3 -26
  101. package/api/theme-edit/_vet.mjs +5 -89
  102. package/api/theme-edit/how/how.d.mts +1 -1
  103. package/api/theme-edit/how/how.mjs +1 -1
  104. package/api/theme-edit/theme-edit.type.d.mts +54 -1
  105. package/api/theme-edit/theme-edit.type.mjs +17 -2
  106. package/api/theme-edit/themeEditHow.doc.mjs +3 -3
  107. package/api/theme-edit/themeEditNew.doc.mjs +4 -0
  108. package/api/theme-edit/themeEditSet.doc.mjs +6 -2
  109. package/api/theme-edit/themeEditShow.doc.mjs +4 -0
  110. package/api/theme-edit/themeEditUndo.doc.mjs +4 -0
  111. package/assets/component-list.generated.json +1 -1
  112. package/assets/docs/author-a-theme.doc.mjs +7 -4
  113. package/assets/docs/tree/export-pages.doc.mjs +1 -1
  114. package/assets/docs/tree/icon-sets.doc.mjs +1 -1
  115. package/assets/docs/use-a-theme.doc.mjs +89 -17
  116. package/assets/docs-routes.generated.json +1 -1
  117. package/assets/docs.generated.json +2 -2
  118. package/assets/icons/definitions/article.doc.mjs +9 -0
  119. package/assets/icons/template-icons.json +5 -0
  120. package/assets/search-catalog.generated.json +2 -2
  121. package/assets/self-audit.generated.json +1 -1
  122. package/assets/templates/blocks/components/Citation/CitationInlineText.doc.mjs +1 -1
  123. package/assets/templates/blocks/components/Citation/CitationInlineText.tsx +3 -1
  124. package/assets/templates/blocks/components/Citation/CitationShowcase.doc.mjs +1 -1
  125. package/assets/templates/blocks/components/Citation/CitationShowcase.tsx +3 -1
  126. package/assets/templates/blocks/components/Citation/CitationSourceList.doc.mjs +1 -1
  127. package/assets/templates/blocks/components/Citation/CitationSourceList.tsx +7 -3
  128. package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.doc.mjs +1 -1
  129. package/assets/templates/blocks/components/Markdown/MarkdownCitedContent.tsx +5 -2
  130. package/assets/templates/components.generated.json +1 -1
  131. package/assets/templates/pages/ide/page.tsx +5 -10
  132. package/assets/templates/pages/payment-form/page.tsx +16 -60
  133. package/assets/templates/themes/neutral/neutralTheme.ts +4 -3
  134. package/authoring/debug/parse.d.mts +1 -1
  135. package/authoring/doctypes/_schema.d.mts +13 -13
  136. package/authoring/gap-report/parse.d.mts +1 -1
  137. package/clients/cli/build-id.generated.json +1 -1
  138. package/clients/cli/commands/ensure-core-built.mjs +17 -10
  139. package/clients/cli/commands/experimental/theme.mjs +46 -6
  140. package/clients/cli/commands/experimental-icon.doc.mjs +1 -1
  141. package/clients/cli/commands/experimental-theme-compile.doc.mjs +1 -1
  142. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +11 -3
  143. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +3 -3
  144. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  145. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  146. package/clients/cli/commands/hook.mjs +7 -0
  147. package/clients/cli/commands/theme-build.doc.mjs +1 -1
  148. package/clients/cli/index.mjs +6 -2
  149. package/clients/cli/lib/hook-format.mjs +41 -1
  150. package/clients/cli/lib/json-shim.mjs +34 -14
  151. package/clients/cli/lib/other-cli-words.mjs +7 -0
  152. package/clients/cli/lib/unknown-command.mjs +345 -46
  153. package/clients/cli/manifest.generated.json +1 -1
  154. package/foundation/agent-docs/agent-docs.d.mts +11 -2
  155. package/foundation/agent-docs/agent-docs.mjs +51 -8
  156. package/foundation/config/project.d.mts +11 -1
  157. package/foundation/config/project.mjs +11 -2
  158. package/foundation/discovery/component-replacement.d.mts +5 -8
  159. package/foundation/discovery/component-replacement.mjs +12 -14
  160. package/foundation/discovery/core-augmentation-points.d.mts +35 -0
  161. package/foundation/discovery/core-augmentation-points.mjs +410 -9
  162. package/foundation/discovery/core-elements.d.mts +6 -0
  163. package/foundation/discovery/core-elements.mjs +10 -2
  164. package/foundation/discovery/core-prop-types.d.mts +5 -0
  165. package/foundation/discovery/core-prop-types.mjs +10 -3
  166. package/foundation/discovery/core-props.d.mts +8 -0
  167. package/foundation/discovery/core-props.mjs +29 -13
  168. package/foundation/discovery/official-list.d.mts +17 -0
  169. package/foundation/discovery/official-list.mjs +77 -0
  170. package/foundation/discovery/unknown-core-props.d.mts +130 -0
  171. package/foundation/discovery/unknown-core-props.mjs +601 -0
  172. package/foundation/discovery/written-prop-values.d.mts +35 -0
  173. package/foundation/discovery/written-prop-values.mjs +258 -0
  174. package/foundation/fonts/fontsource-families.generated.d.mts +5 -0
  175. package/foundation/fonts/fontsource-families.generated.mjs +2110 -0
  176. package/foundation/fs/package-name.d.mts +10 -0
  177. package/foundation/fs/package-name.mjs +37 -0
  178. package/foundation/fs/project-source-files.d.mts +27 -0
  179. package/foundation/fs/project-source-files.mjs +115 -0
  180. package/foundation/fs/project-sources.d.mts +25 -0
  181. package/foundation/fs/project-sources.mjs +83 -0
  182. package/foundation/fs/read-record.d.mts +18 -0
  183. package/foundation/fs/read-record.mjs +36 -2
  184. package/foundation/integrations/integration-warnings.d.mts +39 -7
  185. package/foundation/integrations/integration-warnings.mjs +141 -19
  186. package/foundation/integrations/missing-dependency.d.mts +11 -0
  187. package/foundation/integrations/missing-dependency.mjs +20 -5
  188. package/foundation/release/folder-lock.d.mts +5 -2
  189. package/foundation/release/folder-lock.mjs +9 -3
  190. package/foundation/release/notices.mjs +2 -8
  191. package/foundation/render/cut-short.d.mts +8 -2
  192. package/foundation/render/cut-short.mjs +50 -5
  193. package/foundation/render/drawn-contrast.d.mts +38 -0
  194. package/foundation/render/drawn-contrast.mjs +237 -0
  195. package/foundation/render/file-header.mjs +4 -0
  196. package/foundation/render/layers.d.mts +2 -1
  197. package/foundation/render/layers.mjs +22 -1
  198. package/foundation/render/package-resolve-hooks.d.mts +38 -9
  199. package/foundation/render/package-resolve-hooks.mjs +124 -32
  200. package/foundation/render/page-worker.mjs +95 -23
  201. package/foundation/render/part-boxes.d.mts +27 -0
  202. package/foundation/render/part-boxes.mjs +45 -0
  203. package/foundation/render/picture-chrome.mjs +36 -4
  204. package/foundation/render/picture-playwright.mjs +26 -2
  205. package/foundation/render/picture-window.d.mts +29 -2
  206. package/foundation/render/picture-window.mjs +99 -1
  207. package/foundation/render/render-page.d.mts +39 -6
  208. package/foundation/render/render-page.mjs +26 -10
  209. package/foundation/render/ts-paths.mjs +2 -1
  210. package/foundation/response/error-codes.doc.mjs +1 -1
  211. package/foundation/response/error-codes.mjs +1 -1
  212. package/foundation/response/response-types.doc.mjs +1 -1
  213. package/foundation/text/written-string.d.mts +4 -1
  214. package/foundation/text/written-string.mjs +4 -1
  215. package/package.json +8 -7
@@ -29,6 +29,7 @@ import {
29
29
  import {resolveImportPath} from '../../foundation/discovery/component-discovery.mjs';
30
30
  import {detectPackageManager} from '../../foundation/env/package-manager.mjs';
31
31
  import {
32
+ findDeclaredWorkspaceRoot,
32
33
  findInstalledPackage,
33
34
  findProjectCoreDir,
34
35
  } from '../../foundation/fs/paths.mjs';
@@ -44,7 +45,8 @@ import {installGuidanceFor} from '../../foundation/env/cli-command.mjs';
44
45
  * @property {string} coreDir - The folder of the installed Core package.
45
46
  * @property {string} coreVersion
46
47
  * @property {string} coreKey - The Core version plus a fingerprint of its stylesheet.
47
- * @property {string} cacheDir - node_modules/.cache/astryx-theme/<coreKey>
48
+ * @property {string | null} cacheDir - node_modules/.cache/astryx-theme/<coreKey>;
49
+ * null under Yarn Plug'n'Play, which has no node_modules to keep it in
48
50
  * @property {any} core - Core's theme module.
49
51
  * @property {NodeJS.Require} req - Resolves packages from the project.
50
52
  * @property {ProjectTarget[]} targets
@@ -87,11 +89,13 @@ const DOCS_VERSION = 10;
87
89
  * of the node_modules that holds the Core the command reads (the
88
90
  * project's own, or a workspace root's). The locks and the part-data cache
89
91
  * both live here, so one command never finds two answers. Null without
90
- * Core.
92
+ * Core, and null under Yarn Plug'n'Play: there Core resolves into a
93
+ * read-only package archive, which is no node_modules.
91
94
  * @param {string} dir
92
95
  * @returns {string | null}
93
96
  */
94
97
  export function themeCacheRoot(dir) {
98
+ if (plugAndPlayAbove(dir)) return null;
95
99
  const core = findProjectCoreDir(dir);
96
100
  if (!core) return null;
97
101
  // Core read from source (a fork of the design system, packages/core) is
@@ -107,6 +111,41 @@ export function themeCacheRoot(dir) {
107
111
  return own ? path.join(own, 'node_modules', '.cache', 'astryx-theme') : null;
108
112
  }
109
113
 
114
+ /**
115
+ * Whether a Yarn Plug'n'Play loader (.pnp.cjs) is at `dir` or above it, up
116
+ * to its workspace root (or, outside a workspace, the repository root).
117
+ * @param {string} dir
118
+ */
119
+ function plugAndPlayAbove(dir) {
120
+ const root = findDeclaredWorkspaceRoot(dir);
121
+ for (let d = path.resolve(dir); ; d = path.dirname(d)) {
122
+ if (fs.existsSync(path.join(d, '.pnp.cjs'))) return true;
123
+ if (d === root || (!root && fs.existsSync(path.join(d, '.git'))))
124
+ return false;
125
+ if (path.dirname(d) === d) return false;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Refuse a Yarn Plug'n'Play app: it has no node_modules for the editor to
131
+ * read Core from, or to keep its locks and cache in. Its .pnp.cjs sits at
132
+ * the app or at the workspace root above it.
133
+ * @param {string} dir the app folder, or a folder in it
134
+ */
135
+ export function refusePlugAndPlay(dir) {
136
+ if (!plugAndPlayAbove(dir)) return;
137
+ throw new AstryxError(
138
+ "This app uses Yarn Plug'n'Play, which astryx experimental theme does not support: it reads the installed Core from node_modules. Set `nodeLinker: node-modules` in .yarnrc.yml and run yarn install. Nothing was written.",
139
+ [
140
+ {
141
+ name: 'yarn config set nodeLinker node-modules',
142
+ reason: 'do this first, then yarn install',
143
+ },
144
+ ],
145
+ ERROR_CODES.ERR_PROJECT_REQUIRED,
146
+ );
147
+ }
148
+
110
149
  /**
111
150
  * The nearest folder at or above `dir` that holds node_modules.
112
151
  * @param {string} dir
@@ -362,23 +401,27 @@ export async function openProject(cwd) {
362
401
  }
363
402
  const coreKey = `${coreVersion}-${fingerprint}`;
364
403
  const core = await import(pathToFileURL(themeFile).href);
365
- const cacheDir = path.join(
366
- themeCacheRoot(key) ??
367
- path.join(
368
- nodeModulesRoot(key) ?? key,
369
- 'node_modules',
370
- '.cache',
371
- 'astryx-theme',
372
- ),
373
- coreKey,
374
- );
404
+ // A Plug'n'Play app is read (a preview app with no theme reads it), but
405
+ // nothing is cached: it has no node_modules to keep the cache in.
406
+ const cacheDir = plugAndPlayAbove(key)
407
+ ? null
408
+ : path.join(
409
+ themeCacheRoot(key) ??
410
+ path.join(
411
+ nodeModulesRoot(key) ?? key,
412
+ 'node_modules',
413
+ '.cache',
414
+ 'astryx-theme',
415
+ ),
416
+ coreKey,
417
+ );
375
418
  // Reading every component doc takes a second; it is fixed per Core install.
376
- const docsFile = path.join(cacheDir, 'docs.json');
419
+ const docsFile = cacheDir && path.join(cacheDir, 'docs.json');
377
420
  /** @type {{version: number, targets: import('../../foundation/discovery/theming-targets.mjs').ThemingTarget[], components: ThemeProject['components'], vars: ThemeProject['vars']} | null} */
378
421
  let docs = null;
379
422
  try {
380
- const cached = JSON.parse(fs.readFileSync(docsFile, 'utf8'));
381
- if (cached.version === DOCS_VERSION) docs = cached;
423
+ const cached = docsFile && JSON.parse(fs.readFileSync(docsFile, 'utf8'));
424
+ if (cached?.version === DOCS_VERSION) docs = cached;
382
425
  } catch {
383
426
  // Not cached yet.
384
427
  }
@@ -391,12 +434,13 @@ export async function openProject(cwd) {
391
434
  vars: await collectThemingVars(path.join(coreDir, 'src')),
392
435
  components: await readComponents(coreDir),
393
436
  }));
394
- try {
395
- fs.mkdirSync(cacheDir, {recursive: true});
396
- fs.writeFileSync(docsFile, JSON.stringify(docs));
397
- } catch {
398
- // A read-only node_modules: read the docs again next time.
399
- }
437
+ if (cacheDir && docsFile)
438
+ try {
439
+ fs.mkdirSync(cacheDir, {recursive: true});
440
+ fs.writeFileSync(docsFile, JSON.stringify(docs));
441
+ } catch {
442
+ // A read-only node_modules: read the docs again next time.
443
+ }
400
444
  }
401
445
  const {components, vars} = docs;
402
446
  // The value Core writes for each state a target lists: a state that names
@@ -456,13 +500,30 @@ function readInputFields(themeDir) {
456
500
  */
457
501
  export function importPathOf(project, name) {
458
502
  const spec =
459
- project.components[name]?.importPath ??
460
- oncePerListing(`importPath ${project.coreDir} ${name}`, () =>
461
- resolveImportPath(project.coreDir, name),
462
- );
503
+ project.components[name]?.importPath ?? coreImportPath(project, name);
463
504
  return spec && spec !== '@astryxdesign/core' ? spec : null;
464
505
  }
465
506
 
507
+ /**
508
+ * Where a component's own module is in the installed Core: the same for as
509
+ * long as that Core is installed (its key names its version and files), so
510
+ * it is found once in a process for each Core and name.
511
+ * @param {ThemeProject} project
512
+ * @param {string} name
513
+ * @returns {string | null | undefined}
514
+ */
515
+ function coreImportPath(project, name) {
516
+ const key = `${project.coreKey}\0${project.coreDir}\0${name}`;
517
+ if (CORE_IMPORT_PATHS.has(key)) return CORE_IMPORT_PATHS.get(key);
518
+ const spec = oncePerListing(`importPath ${project.coreDir} ${name}`, () =>
519
+ resolveImportPath(project.coreDir, name),
520
+ );
521
+ if (project.coreKey) CORE_IMPORT_PATHS.set(key, spec);
522
+ return spec;
523
+ }
524
+ /** @type {Map<string, string | null | undefined>} */
525
+ const CORE_IMPORT_PATHS = new Map();
526
+
466
527
  /**
467
528
  * The built module of a base's theme package, resolved the way the theme
468
529
  * resolves it: from the project (an open project, or its folder, relative
@@ -550,7 +611,7 @@ export function baseThemeFile(project, like) {
550
611
  * Resolved tokens of the base theme in both modes.
551
612
  * @param {ThemeProject} project
552
613
  * @param {string | undefined} like
553
- * @returns {Promise<{light: Record<string, string>, dark: Record<string, string>, components: Record<string, any>}>}
614
+ * @returns {Promise<{light: Record<string, string>, dark: Record<string, string>, components: Record<string, any>, onDark: Record<string, string>}>}
554
615
  */
555
616
  export async function baseResolved(project, like) {
556
617
  const {core} = project;
@@ -569,6 +630,14 @@ export async function baseResolved(project, like) {
569
630
  // The base theme's own part styles (neutral's pale destructive button),
570
631
  // which a one-mode setting keeps in the other mode.
571
632
  components: theme.components ?? {},
633
+ // The tokens the base's dark media rule (`[data-astryx-media="dark"]`:
634
+ // the lightbox, what sits on the overlay scrim) replaces, with their
635
+ // values there; Core's own when the base sets none.
636
+ onDark: Object.fromEntries(
637
+ Object.entries(theme.__onDark?.tokens ?? {}).filter(
638
+ ([k, v]) => k.startsWith('--') && typeof v === 'string',
639
+ ),
640
+ ),
572
641
  };
573
642
  return out;
574
643
  }
@@ -0,0 +1,29 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * Split a printed command into its arguments, as a POSIX shell reads them:
6
+ * single quotes (with `'\''` for a quote inside), double quotes and
7
+ * backslashes. `null` when the quotes do not close.
8
+ * @param {string} line
9
+ * @returns {string[] | null}
10
+ */
11
+ export function splitCommand(line: string): string[] | null;
12
+ /**
13
+ * Read a suggested `set <slug> <thing> <value> [flags]` line; `null` when it
14
+ * is not one this check can read (another command or theme, a placeholder,
15
+ * an unknown flag): such a line is kept as it is.
16
+ * @param {string} name
17
+ * @param {string} slug
18
+ */
19
+ export function readSetLine(name: string, slug: string): {
20
+ thing: string;
21
+ value: string;
22
+ flags: {
23
+ on?: string;
24
+ when?: string;
25
+ state?: string;
26
+ mode?: string;
27
+ only?: boolean;
28
+ };
29
+ } | null;
@@ -0,0 +1,100 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file How a printed `set` line reads: its arguments as a shell splits
5
+ * them, and its setting (thing, value and scope flags). Pure text reading,
6
+ * shared by the checks of offered lines.
7
+ *
8
+ * @position api/theme-edit — imported by _check.mjs and _vet.mjs
9
+ */
10
+
11
+ import {P} from './_tables.mjs';
12
+
13
+ /**
14
+ * Split a printed command into its arguments, as a POSIX shell reads them:
15
+ * single quotes (with `'\''` for a quote inside), double quotes and
16
+ * backslashes. `null` when the quotes do not close.
17
+ * @param {string} line
18
+ * @returns {string[] | null}
19
+ */
20
+ export function splitCommand(line) {
21
+ /** @type {string[]} */
22
+ const out = [];
23
+ let cur = '';
24
+ let has = false;
25
+ for (let i = 0; i < line.length; i += 1) {
26
+ const c = line[i];
27
+ if (c === "'") {
28
+ const end = line.indexOf("'", i + 1);
29
+ if (end < 0) return null;
30
+ cur += line.slice(i + 1, end);
31
+ has = true;
32
+ i = end;
33
+ } else if (c === '"') {
34
+ let j = i + 1;
35
+ for (; j < line.length && line[j] !== '"'; j += 1) {
36
+ if (line[j] === '\\' && j + 1 < line.length) j += 1;
37
+ cur += line[j];
38
+ }
39
+ if (j >= line.length) return null;
40
+ has = true;
41
+ i = j;
42
+ } else if (c === '\\' && i + 1 < line.length) {
43
+ cur += line[i + 1];
44
+ has = true;
45
+ i += 1;
46
+ } else if (/\s/.test(c)) {
47
+ if (has) out.push(cur);
48
+ cur = '';
49
+ has = false;
50
+ } else {
51
+ cur += c;
52
+ has = true;
53
+ }
54
+ }
55
+ if (has) out.push(cur);
56
+ return out;
57
+ }
58
+
59
+ const VALUE_FLAGS = new Set(['on', 'when', 'state', 'mode']);
60
+
61
+ /**
62
+ * Read a suggested `set <slug> <thing> <value> [flags]` line; `null` when it
63
+ * is not one this check can read (another command or theme, a placeholder,
64
+ * an unknown flag): such a line is kept as it is.
65
+ * @param {string} name
66
+ * @param {string} slug
67
+ */
68
+ export function readSetLine(name, slug) {
69
+ const prefix = `${P} set ${slug} `;
70
+ if (!name.startsWith(prefix)) return null;
71
+ const rest = name.slice(prefix.length);
72
+ // A placeholder (`<a color>`, `…`) is a pattern, not a command to run.
73
+ if (/<[^>]*>|…/.test(rest)) return null;
74
+ const args = splitCommand(rest);
75
+ if (!args || !args.length) return null;
76
+ /** @type {string[]} */
77
+ const pos = [];
78
+ /** @type {{on?: string, when?: string, state?: string, mode?: string, only?: boolean}} */
79
+ const flags = {};
80
+ for (let i = 0; i < args.length; i += 1) {
81
+ const a = args[i];
82
+ const m = /^--([a-z]+)(?:=(.*))?$/.exec(a);
83
+ // A token (`--color-accent`) is the thing, not a flag.
84
+ if (m && (VALUE_FLAGS.has(m[1]) || m[1] === 'only') && pos.length) {
85
+ if (m[1] === 'only') {
86
+ flags.only = true;
87
+ continue;
88
+ }
89
+ const v = m[2] ?? args[(i += 1)];
90
+ if (v == null) return null;
91
+ /** @type {any} */ (flags)[m[1]] = v;
92
+ } else if (m && pos.length) {
93
+ // Some other flag (`--png`): not part of the setting.
94
+ if (m[1] === 'png') continue;
95
+ return null;
96
+ } else pos.push(a);
97
+ }
98
+ if (pos.length > 2 || !pos.length) return null;
99
+ return {thing: pos[0], value: pos[1] ?? '', flags};
100
+ }