@astryxdesign/cli 0.7.3 → 0.7.4-canary.1a552f2

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 (244) hide show
  1. package/README.md +63 -63
  2. package/api/component/_adapter.mjs +4 -3
  3. package/api/component/component.mjs +13 -6
  4. package/api/component/doc-links.test.mjs +33 -0
  5. package/api/discover/_adapter.mjs +22 -10
  6. package/api/discover/_catalog-view.d.mts +2 -0
  7. package/api/discover/_catalog-view.mjs +1 -0
  8. package/api/discover/_catalog-view.test.mjs +1 -0
  9. package/api/discover/detail/detail.mjs +1 -0
  10. package/api/discover/detail/detail.test.mjs +3 -0
  11. package/api/discover/detail/doc/doc.mjs +5 -1
  12. package/api/discover/detail/doc/doc.test.mjs +1 -0
  13. package/api/discover/detail/item/item.mjs +2 -0
  14. package/api/discover/detail/item/item.test.mjs +2 -0
  15. package/api/discover/discover.doc.mjs +4 -4
  16. package/api/discover/discover.type.d.mts +20 -0
  17. package/api/discover/discover.type.mjs +5 -0
  18. package/api/discover/list/list.test.mjs +2 -0
  19. package/api/discover/search/search.d.mts +3 -2
  20. package/api/discover/search/search.mjs +26 -7
  21. package/api/discover/search/search.test.mjs +67 -0
  22. package/api/docs/_adapter.d.mts +80 -0
  23. package/api/docs/_adapter.mjs +261 -11
  24. package/api/docs/detail/detail.mjs +8 -0
  25. package/api/docs/docOverlays.test.mjs +2 -1
  26. package/api/docs/docs.d.mts +16 -6
  27. package/api/docs/docs.doc.mjs +1 -1
  28. package/api/docs/docs.mjs +55 -6
  29. package/api/docs/docs.type.d.mts +28 -2
  30. package/api/docs/docs.type.mjs +13 -3
  31. package/api/docs/guide-route-targets.test.mjs +1002 -0
  32. package/api/docs/index/index.mjs +9 -0
  33. package/api/docs/integration-tree.test.mjs +22 -0
  34. package/api/docs/integrationDocs.test.mjs +85 -0
  35. package/api/docs/list/list.mjs +7 -1
  36. package/api/docs/node/node.mjs +9 -1
  37. package/api/docs/reference-blocks.test.mjs +1 -1
  38. package/api/doctor/_adapter.d.mts +40 -0
  39. package/api/doctor/_adapter.mjs +171 -1
  40. package/api/doctor/built-checks.d.mts +85 -0
  41. package/api/doctor/built-checks.mjs +837 -0
  42. package/api/doctor/built-checks.test.mjs +531 -0
  43. package/api/doctor/doctor.d.mts +6 -5
  44. package/api/doctor/doctor.doc.mjs +11 -1
  45. package/api/doctor/doctor.mjs +18 -3
  46. package/api/doctor/doctor.type.d.mts +6 -0
  47. package/api/doctor/doctor.type.mjs +3 -0
  48. package/api/hook/list/list.d.mts +1 -1
  49. package/api/integration/_adapter.d.mts +11 -2
  50. package/api/integration/_adapter.mjs +30 -4
  51. package/api/integration/authoring-checks.mjs +29 -2
  52. package/api/integration/authoring-checks.test.mjs +131 -1
  53. package/api/integration/check/check.d.mts +4 -1
  54. package/api/integration/check/check.mjs +34 -4
  55. package/api/integration/check/check.test.mjs +49 -0
  56. package/api/integration/integration-check.type.d.mts +8 -0
  57. package/api/integration/integration-check.type.mjs +4 -1
  58. package/api/integration/integrationCheck.doc.mjs +8 -1
  59. package/api/integration/integrationDocConflicts.doc.mjs +1 -1
  60. package/api/integration/pack-check.mjs +71 -2
  61. package/api/integration/pack-check.test.mjs +77 -0
  62. package/api/integration/validate-integration-case-disk.test.mjs +2 -0
  63. package/api/integration/validate-integration-gaps.test.mjs +7 -4
  64. package/api/integration/validate-integration.mjs +96 -14
  65. package/api/integration/validate-integration.type.d.mts +10 -4
  66. package/api/integration/validate-integration.type.mjs +10 -5
  67. package/api/integration/validateIntegration.doc.mjs +1 -1
  68. package/api/search/_candidate-score.d.mts +3 -2
  69. package/api/search/_candidate-score.mjs +66 -3
  70. package/api/search/_gather-docs.mjs +12 -2
  71. package/api/search/_priority.mjs +2 -1
  72. package/api/search/_score.d.mts +2 -1
  73. package/api/search/_score.mjs +2 -2
  74. package/api/search/search.doc.mjs +1 -1
  75. package/api/search/search.family-members.test.mjs +124 -0
  76. package/api/search/search.mjs +28 -0
  77. package/api/theme/palette/generate/generate.d.mts +8 -0
  78. package/api/theme/palette/generate/generate.mjs +38 -11
  79. package/api/theme/palette/generate/generate.test.mjs +83 -4
  80. package/api/theme/palette/generate/generator.d.mts +6 -0
  81. package/api/theme/palette/generate/generator.mjs +6 -0
  82. package/api/theme/theme.type.d.mts +12 -1
  83. package/api/theme/theme.type.mjs +7 -1
  84. package/api/theme/themeBuild.doc.mjs +1 -1
  85. package/api/theme/themeList.doc.mjs +1 -1
  86. package/api/theme/themePaletteGenerate.doc.mjs +1 -1
  87. package/api/theme/themeTargets.doc.mjs +1 -1
  88. package/assets/discover/official-packages.json +16 -0
  89. package/assets/docs/author-a-theme.doc.mjs +15 -15
  90. package/assets/docs/browser-support.doc.mjs +1 -1
  91. package/assets/docs/getting-started.doc.mjs +16 -16
  92. package/assets/docs/is-my-code-good.doc.mjs +6 -6
  93. package/assets/docs/styling-overview.doc.mjs +5 -5
  94. package/assets/docs/theme.doc.mjs +5 -5
  95. package/assets/docs/tree/add-a-component.doc.mjs +1 -1
  96. package/assets/docs/tree/add-a-theme.doc.mjs +2 -2
  97. package/assets/docs/tree/add-a-topic.doc.mjs +6 -6
  98. package/assets/docs/tree/agent-guidance.doc.mjs +5 -5
  99. package/assets/docs/tree/authoring.doc.mjs +2 -2
  100. package/assets/docs/tree/block-template.doc.mjs +1 -1
  101. package/assets/docs/tree/check-the-props.doc.mjs +3 -3
  102. package/assets/docs/tree/check-your-docs.doc.mjs +6 -6
  103. package/assets/docs/tree/checks.doc.mjs +11 -11
  104. package/assets/docs/tree/ci-and-containers.doc.mjs +1 -1
  105. package/assets/docs/tree/cli-command.doc.mjs +6 -6
  106. package/assets/docs/tree/codemods.doc.mjs +7 -7
  107. package/assets/docs/tree/component-family.doc.mjs +1 -1
  108. package/assets/docs/tree/component-imports.doc.mjs +2 -2
  109. package/assets/docs/tree/component-lookups.doc.mjs +6 -5
  110. package/assets/docs/tree/computer-agent-guide.doc.mjs +7 -7
  111. package/assets/docs/tree/computer-integrations.doc.mjs +5 -5
  112. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +3 -3
  113. package/assets/docs/tree/define-the-theme.doc.mjs +1 -1
  114. package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
  115. package/assets/docs/tree/doc-syntax.doc.mjs +219 -0
  116. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  117. package/assets/docs/tree/extend-or-replace.doc.mjs +39 -7
  118. package/assets/docs/tree/font-setup.doc.mjs +1 -1
  119. package/assets/docs/tree/generate-a-palette.doc.mjs +1 -1
  120. package/assets/docs/tree/global-integrations.doc.mjs +11 -11
  121. package/assets/docs/tree/global.doc.mjs +2 -2
  122. package/assets/docs/tree/host-entry.doc.mjs +1 -1
  123. package/assets/docs/tree/install-once.doc.mjs +4 -4
  124. package/assets/docs/tree/layout.doc.mjs +1 -1
  125. package/assets/docs/tree/links.doc.mjs +2 -2
  126. package/assets/docs/tree/migration-setup.doc.mjs +15 -15
  127. package/assets/docs/tree/migration.doc.mjs +2 -1
  128. package/assets/docs/tree/other-libraries.doc.mjs +1 -1
  129. package/assets/docs/tree/page-template.doc.mjs +2 -2
  130. package/assets/docs/tree/problems.doc.mjs +8 -8
  131. package/assets/docs/tree/project-global-or-both.doc.mjs +2 -2
  132. package/assets/docs/tree/publishing.doc.mjs +7 -7
  133. package/assets/docs/tree/quick-start.doc.mjs +9 -9
  134. package/assets/docs/tree/reference.doc.mjs +2 -2
  135. package/assets/docs/tree/replace-a-core-component.doc.mjs +1 -1
  136. package/assets/docs/tree/replace-a-core-template.doc.mjs +4 -4
  137. package/assets/docs/tree/scaffold.doc.mjs +1 -1
  138. package/assets/docs/tree/sections-and-placement.doc.mjs +3 -3
  139. package/assets/docs/tree/see-it-in-an-app.doc.mjs +3 -3
  140. package/assets/docs/tree/short-and-findable.doc.mjs +3 -3
  141. package/assets/docs/tree/single-component.doc.mjs +4 -4
  142. package/assets/docs/tree/start-a-template.doc.mjs +5 -5
  143. package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
  144. package/assets/docs/tree/styling-libraries.doc.mjs +2 -1
  145. package/assets/docs/tree/styling.doc.mjs +1 -1
  146. package/assets/docs/tree/tailwind.doc.mjs +1 -1
  147. package/assets/docs/tree/test-in-an-app.doc.mjs +5 -5
  148. package/assets/docs/tree/test-template-in-app.doc.mjs +6 -6
  149. package/assets/docs/tree/tokens-and-setup.doc.mjs +1 -1
  150. package/assets/docs/tree/troubleshooting.doc.mjs +24 -20
  151. package/assets/docs/tree/uninstall.doc.mjs +2 -2
  152. package/assets/docs/tree/updates.doc.mjs +1 -1
  153. package/assets/docs/tree/upgrading.doc.mjs +3 -3
  154. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +1 -1
  155. package/assets/docs/tree/verify-packed-template.doc.mjs +3 -3
  156. package/assets/docs/tree/versioning.doc.mjs +4 -4
  157. package/assets/docs/tree/who-answers.doc.mjs +4 -4
  158. package/assets/docs/tree/write-the-template-file.doc.mjs +4 -4
  159. package/assets/docs/use-a-theme.doc.mjs +10 -10
  160. package/assets/docs/use-integrations.doc.mjs +1 -1
  161. package/assets/docs/working-with-ai.doc.mjs +15 -15
  162. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +1 -1
  163. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.doc.mjs +1 -1
  164. package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +54 -0
  165. package/assets/templates/themes/butter/butterPalettes.generated.receipt.json +1499 -0
  166. package/assets/templates/themes/butter/butterPalettes.generated.ts +550 -0
  167. package/assets/templates/themes/butter/butterTheme.ts +274 -234
  168. package/assets/templates/themes/butter/palette.config.json +484 -0
  169. package/assets/templates/themes/gothic/gothicPaletteRefs.generated.ts +41 -0
  170. package/assets/templates/themes/gothic/gothicPalettes.generated.receipt.json +979 -0
  171. package/assets/templates/themes/gothic/gothicPalettes.generated.ts +271 -0
  172. package/assets/templates/themes/gothic/gothicTheme.ts +130 -160
  173. package/assets/templates/themes/gothic/palette.config.json +371 -0
  174. package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +51 -0
  175. package/assets/templates/themes/matcha/matchaPalettes.generated.receipt.json +1502 -0
  176. package/assets/templates/themes/matcha/matchaPalettes.generated.ts +501 -0
  177. package/assets/templates/themes/matcha/matchaPalettes.ts +21 -0
  178. package/assets/templates/themes/matcha/matchaTheme.ts +164 -103
  179. package/assets/templates/themes/matcha/palette.config.json +448 -0
  180. package/assets/templates/themes/stone/palette.config.json +262 -0
  181. package/assets/templates/themes/stone/stonePaletteRefs.generated.ts +51 -0
  182. package/assets/templates/themes/stone/stonePalettes.generated.receipt.json +840 -0
  183. package/assets/templates/themes/stone/stonePalettes.generated.ts +501 -0
  184. package/assets/templates/themes/stone/stoneTheme.ts +190 -207
  185. package/assets/templates/themes/y2k/palette.config.json +166 -0
  186. package/assets/templates/themes/y2k/y2kPaletteRefs.generated.ts +54 -0
  187. package/assets/templates/themes/y2k/y2kPalettes.generated.receipt.json +872 -0
  188. package/assets/templates/themes/y2k/y2kPalettes.generated.ts +550 -0
  189. package/assets/templates/themes/y2k/y2kPalettes.ts +257 -0
  190. package/assets/templates/themes/y2k/y2kTheme.ts +164 -391
  191. package/authoring/codemod/codemod.doc.mjs +1 -1
  192. package/authoring/config/config.doc.mjs +4 -4
  193. package/authoring/debug/parse.d.mts +1 -1
  194. package/authoring/discover/discover.doc.mjs +1 -1
  195. package/authoring/doctypes/_schema.d.mts +4 -0
  196. package/authoring/doctypes/_schema.mjs +4 -0
  197. package/authoring/doctypes/reference/type.ts +14 -2
  198. package/authoring/gap-report/gap-report.doc.mjs +1 -1
  199. package/authoring/integration/integration.doc.mjs +6 -6
  200. package/clients/cli/commands/discover.mjs +1 -1
  201. package/clients/cli/commands/discover.sources.test.mjs +6 -0
  202. package/clients/cli/commands/docs.doc.mjs +1 -1
  203. package/clients/cli/commands/docs.mjs +27 -2
  204. package/clients/cli/commands/docs.test.mjs +1 -1
  205. package/clients/cli/commands/doctor-integration-docs.doc.mjs +1 -1
  206. package/clients/cli/commands/doctor.doc.mjs +13 -1
  207. package/clients/cli/commands/doctor.mjs +25 -9
  208. package/clients/cli/commands/integration-check.doc.mjs +11 -0
  209. package/clients/cli/commands/integration-check.test.mjs +36 -3
  210. package/clients/cli/commands/integration-pack.doc.mjs +1 -1
  211. package/clients/cli/commands/integration-verify.doc.mjs +1 -1
  212. package/clients/cli/commands/integration.mjs +15 -3
  213. package/clients/cli/commands/theme-targets.doc.mjs +1 -1
  214. package/clients/cli/commands/upgrade.doc.mjs +2 -2
  215. package/clients/cli/lib/define-command.mjs +5 -3
  216. package/foundation/agent-docs/agent-docs.d.mts +14 -2
  217. package/foundation/agent-docs/agent-docs.mjs +65 -4
  218. package/foundation/agent-docs/agent-docs.test.mjs +107 -0
  219. package/foundation/discovery/docs-discovery.d.mts +93 -2
  220. package/foundation/discovery/docs-discovery.mjs +237 -8
  221. package/foundation/discovery/runner-allowlist.d.mts +2 -0
  222. package/foundation/discovery/runner-allowlist.mjs +4 -0
  223. package/foundation/discovery/runner-commands.d.mts +29 -13
  224. package/foundation/discovery/runner-commands.mjs +253 -17
  225. package/foundation/discovery/stray-pointers.mjs +11 -5
  226. package/foundation/discovery/stray-pointers.test.mjs +6 -0
  227. package/foundation/doc-compiler/compile.mjs +8 -1
  228. package/foundation/doc-compiler/doc-loads.test.mjs +3 -0
  229. package/foundation/doc-compiler/doc-syntax.d.mts +194 -0
  230. package/foundation/doc-compiler/doc-syntax.mjs +739 -0
  231. package/foundation/doc-compiler/doc-syntax.test.mjs +383 -0
  232. package/foundation/doc-compiler/links.d.mts +13 -16
  233. package/foundation/doc-compiler/links.mjs +40 -7
  234. package/foundation/doc-compiler/tree.d.mts +5 -0
  235. package/foundation/doc-compiler/tree.mjs +9 -0
  236. package/foundation/env/package-manager.d.mts +18 -0
  237. package/foundation/env/package-manager.mjs +40 -0
  238. package/foundation/integrations/cli-requirement.d.mts +29 -0
  239. package/foundation/integrations/cli-requirement.mjs +47 -0
  240. package/foundation/integrations/cli-requirement.test.mjs +23 -2
  241. package/foundation/response/error-codes.test.mjs +9 -0
  242. package/foundation/response/response-types.doc.mjs +11 -11
  243. package/foundation/response/response-types.doc.test.mjs +29 -0
  244. package/package.json +9 -9
@@ -0,0 +1,739 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The doc syntax for commands a reader runs: `{@command …}` for an
5
+ * Astryx command, `{@pm …}` for a package-manager step, `{@pm-only <manager>
6
+ * "<reason>" …}` for a step only one package manager can do, and `{@@…}` to
7
+ * show a token as written. Each token is rendered for the reader: an Astryx
8
+ * command with the reader's runner, a package-manager step in the reader's
9
+ * package manager.
10
+ *
11
+ * @input Doc text, the reader's CLI invocation and package manager.
12
+ * @output The text with each token rendered; each token's parse, for doctor;
13
+ * the token a plain-text command should be written as, for doctor's fix.
14
+ * @position foundation/doc-compiler — pure; the env lookup lives in
15
+ * foundation/env/package-manager.mjs.
16
+ */
17
+
18
+ /**
19
+ * The package managers a `{@pm}` token renders for. `yarn-classic` is Yarn 1;
20
+ * `yarn` is Yarn 2 and newer.
21
+ * @typedef {'npm' | 'pnpm' | 'yarn' | 'yarn-classic' | 'bun'} DocPackageManager
22
+ */
23
+
24
+ /** Every package manager a token renders for, in the order docs list them. */
25
+ export const DOC_PACKAGE_MANAGERS = /** @type {const} */ ([
26
+ 'npm',
27
+ 'pnpm',
28
+ 'yarn',
29
+ 'yarn-classic',
30
+ 'bun',
31
+ ]);
32
+
33
+ /** The binary each package manager runs as. */
34
+ const BINARY = {
35
+ npm: 'npm',
36
+ pnpm: 'pnpm',
37
+ yarn: 'yarn',
38
+ 'yarn-classic': 'yarn',
39
+ bun: 'bun',
40
+ };
41
+
42
+ /** The verbs `{@pm}` takes, each portable across every package manager. */
43
+ export const PM_VERBS = /** @type {const} */ ([
44
+ 'install',
45
+ 'remove',
46
+ 'update',
47
+ 'run',
48
+ 'exec',
49
+ 'dlx',
50
+ 'create',
51
+ ]);
52
+
53
+ /** The flags `{@pm}` takes, and the verbs each applies to. */
54
+ export const PM_FLAGS = {
55
+ '--dev': ['install'],
56
+ '--exact': ['install'],
57
+ '--global': ['install', 'remove'],
58
+ '--latest': ['update'],
59
+ };
60
+
61
+ /**
62
+ * A token: `{@name body}`, or one written with more `@`s, which shows with one
63
+ * `@` fewer (`{@@pm …}` shows as `{@pm …}`). The body may hold quoted text
64
+ * and balanced braces (`pkg set 'exports={}'`), but never a line break or a
65
+ * backtick: a token sits on one line of one code span or block, and one that
66
+ * does not is an unclosed opening doctor names.
67
+ */
68
+ const TOKEN =
69
+ /\{@(@*)([a-z][\w-]*)(?:[ \t]+((?:'[^'\n`]*'|"[^"\n`]*"|\{[^{}\n`]*\}|[^{}'"\n`])*?))?[ \t]*\}/g;
70
+
71
+ /** The token names this module renders (`{@link}` resolves elsewhere). */
72
+ const COMMAND_TOKENS = new Set(['command', 'pm', 'pm-only']);
73
+
74
+ /** A version range: anything after `@` that is not one exact version. */
75
+ const RANGE = /[\^~<>*| ]|(?:^|\.)x(?:\.|$)|^latest$|^next$/i;
76
+
77
+ /**
78
+ * Split a token body into words, keeping a double-quoted run as one word.
79
+ * @param {string} body
80
+ * @returns {string[]}
81
+ */
82
+ export function splitWords(body) {
83
+ const words = [];
84
+ const pattern = /"([^"]*)"|(\S+)/g;
85
+ for (const match of String(body).matchAll(pattern)) {
86
+ words.push(match[1] !== undefined ? `"${match[1]}"` : match[2]);
87
+ }
88
+ return words;
89
+ }
90
+
91
+ /**
92
+ * @typedef {object} PmStep
93
+ * @property {'pm'} kind
94
+ * @property {typeof PM_VERBS[number]} verb
95
+ * @property {string[]} args packages, a script or binary and its arguments
96
+ * @property {Set<string>} flags
97
+ */
98
+
99
+ /**
100
+ * @typedef {object} PmOnlyStep
101
+ * @property {'pm-only'} kind
102
+ * @property {DocPackageManager} manager
103
+ * @property {string} reason
104
+ * @property {string[]} args the command after the manager's name
105
+ */
106
+
107
+ /**
108
+ * @typedef {object} CliStep
109
+ * @property {'command'} kind
110
+ * @property {string[]} args the command after `astryx`
111
+ */
112
+
113
+ /**
114
+ * @typedef {PmStep | PmOnlyStep | CliStep} DocStep
115
+ */
116
+
117
+ /**
118
+ * Read one token. Returns the step, or `{error}` saying what is wrong and how
119
+ * to write it.
120
+ * @param {string} name the token name, `pm` in `{@pm install x}`
121
+ * @param {string} body what follows the name
122
+ * @returns {DocStep | {error: string}}
123
+ */
124
+ export function parseCommandToken(name, body = '') {
125
+ const words = splitWords(body);
126
+ if (name === 'command') {
127
+ if (words.length === 0) {
128
+ return {error: '`{@command}` needs a command: `{@command docs theme}`'};
129
+ }
130
+ if (words[0] === 'astryx') {
131
+ return {
132
+ error: `\`{@command}\` takes the command without \`astryx\`: \`{@command ${words.slice(1).join(' ')}}\``,
133
+ };
134
+ }
135
+ return {kind: 'command', args: words};
136
+ }
137
+ if (name === 'pm-only') {
138
+ const [manager, reason, ...args] = words;
139
+ if (!DOC_PACKAGE_MANAGERS.includes(/** @type {any} */ (manager))) {
140
+ return {
141
+ error: `\`{@pm-only}\` starts with the package manager, one of ${DOC_PACKAGE_MANAGERS.join(', ')}: \`{@pm-only npm "<why only npm>" pkg set name=x}\``,
142
+ };
143
+ }
144
+ if (!reason || !/^".+"$/.test(reason)) {
145
+ return {
146
+ error: `\`{@pm-only ${manager}}\` needs a reason in double quotes after the package manager, which readers see: \`{@pm-only ${manager} "<why only ${manager}>" ${args.join(' ') || '<command>'}}\``,
147
+ };
148
+ }
149
+ if (args.length === 0) {
150
+ return {error: `\`{@pm-only ${manager} ${reason}}\` needs a command`};
151
+ }
152
+ return {
153
+ kind: 'pm-only',
154
+ manager: /** @type {DocPackageManager} */ (manager),
155
+ reason: reason.slice(1, -1),
156
+ args,
157
+ };
158
+ }
159
+ if (name === 'pm') {
160
+ const [verb, ...rest] = words;
161
+ if (!PM_VERBS.includes(/** @type {any} */ (verb))) {
162
+ return {
163
+ error: `\`{@pm}\` starts with a verb, one of ${PM_VERBS.join(', ')}: \`{@pm install @acme/widgets}\``,
164
+ };
165
+ }
166
+ const flags = new Set();
167
+ const args = [];
168
+ for (const word of rest) {
169
+ if (
170
+ word.startsWith('--') &&
171
+ word in PM_FLAGS &&
172
+ verb !== 'run' &&
173
+ verb !== 'exec' &&
174
+ verb !== 'dlx'
175
+ ) {
176
+ const allowed = /** @type {Record<string, readonly string[]>} */ (
177
+ PM_FLAGS
178
+ )[word];
179
+ if (!allowed.includes(verb)) {
180
+ return {
181
+ error: `\`${word}\` applies to ${allowed.map(each => `\`{@pm ${each}}\``).join(' and ')}, not \`{@pm ${verb}}\``,
182
+ };
183
+ }
184
+ flags.add(word);
185
+ } else if (
186
+ word.startsWith('-') &&
187
+ verb !== 'run' &&
188
+ verb !== 'exec' &&
189
+ verb !== 'dlx' &&
190
+ verb !== 'create'
191
+ ) {
192
+ return {
193
+ error: `\`{@pm ${verb}}\` takes the flags ${
194
+ Object.keys(PM_FLAGS)
195
+ .filter(flag =>
196
+ /** @type {Record<string, readonly string[]>} */ (PM_FLAGS)[
197
+ flag
198
+ ].includes(verb),
199
+ )
200
+ .join(', ') || 'none'
201
+ }, not \`${word}\`, so it reads the same in every package manager`,
202
+ };
203
+ } else {
204
+ args.push(word);
205
+ }
206
+ }
207
+ if (['run', 'exec', 'dlx', 'create'].includes(verb) && args.length === 0) {
208
+ return {
209
+ error: `\`{@pm ${verb}}\` needs a name: \`{@pm ${verb} <name>}\``,
210
+ };
211
+ }
212
+ if (verb === 'remove' && args.length === 0) {
213
+ return {error: '`{@pm remove}` needs a package'};
214
+ }
215
+ if (flags.has('--exact')) {
216
+ const ranged = args.find(arg => {
217
+ const at = arg.lastIndexOf('@');
218
+ return at > 0 && RANGE.test(arg.slice(at + 1));
219
+ });
220
+ if (ranged) {
221
+ return {
222
+ error: `\`--exact\` with a range (\`${ranged}\`) keeps the range in every package manager but npm: write one version, or drop \`--exact\``,
223
+ };
224
+ }
225
+ }
226
+ if (flags.has('--latest') && args.length === 0) {
227
+ return {error: '`{@pm update --latest}` needs the packages to move'};
228
+ }
229
+ return {
230
+ kind: 'pm',
231
+ verb: /** @type {PmStep['verb']} */ (verb),
232
+ args,
233
+ flags,
234
+ };
235
+ }
236
+ return {error: `\`{@${name}}\` is not a doc token`};
237
+ }
238
+
239
+ /**
240
+ * The command a package-manager step runs in one package manager.
241
+ * @param {PmStep} step
242
+ * @param {DocPackageManager} pm
243
+ * @returns {string}
244
+ */
245
+ export function renderPmStep(step, pm) {
246
+ const {verb, args, flags} = step;
247
+ const join = (/** @type {Array<string | string[]>} */ ...parts) =>
248
+ parts.flat().filter(Boolean).join(' ');
249
+ const global = flags.has('--global');
250
+ // Yarn 2 and newer has no global install: npm, which every Node carries.
251
+ const manager = global && pm === 'yarn' ? 'npm' : pm;
252
+ switch (verb) {
253
+ case 'install': {
254
+ if (args.length === 0 && !global) return join(BINARY[manager], 'install');
255
+ const dev = flags.has('--dev') ? (manager === 'bun' ? '-d' : '-D') : '';
256
+ const exact = flags.has('--exact')
257
+ ? manager === 'bun'
258
+ ? '--exact'
259
+ : '-E'
260
+ : '';
261
+ if (global) {
262
+ return {
263
+ npm: join('npm install -g', exact, args),
264
+ pnpm: join('pnpm add -g', exact, args),
265
+ yarn: join('npm install -g', exact, args),
266
+ 'yarn-classic': join('yarn global add', exact, args),
267
+ bun: join('bun add -g', exact, args),
268
+ }[manager];
269
+ }
270
+ const add = manager === 'npm' ? 'npm install' : `${BINARY[manager]} add`;
271
+ return join(add, dev, exact, args);
272
+ }
273
+ case 'remove': {
274
+ if (global) {
275
+ return {
276
+ npm: join('npm uninstall -g', args),
277
+ pnpm: join('pnpm remove -g', args),
278
+ yarn: join('npm uninstall -g', args),
279
+ 'yarn-classic': join('yarn global remove', args),
280
+ bun: join('bun remove -g', args),
281
+ }[manager];
282
+ }
283
+ return join(
284
+ manager === 'npm' ? 'npm uninstall' : `${BINARY[manager]} remove`,
285
+ args,
286
+ );
287
+ }
288
+ case 'update': {
289
+ if (flags.has('--latest')) {
290
+ return {
291
+ npm: join(
292
+ 'npm install',
293
+ args.map(arg => `${arg}@latest`),
294
+ ),
295
+ pnpm: join('pnpm update --latest', args),
296
+ yarn: join('yarn up', args),
297
+ 'yarn-classic': join('yarn upgrade --latest', args),
298
+ bun: join('bun update --latest', args),
299
+ }[pm];
300
+ }
301
+ return {
302
+ npm: join('npm update', args),
303
+ pnpm: join('pnpm update', args),
304
+ // `yarn up` moves to the newest release; -R stays in the range.
305
+ yarn: join('yarn up -R', args),
306
+ 'yarn-classic': join('yarn upgrade', args),
307
+ bun: join('bun update', args),
308
+ }[pm];
309
+ }
310
+ case 'run': {
311
+ const [script, ...rest] = args;
312
+ // npm passes arguments to a script after `--`; pnpm and Yarn 2 pass a
313
+ // `--` on to the script itself, so only npm writes one.
314
+ if (pm === 'npm') {
315
+ return join('npm run', script, rest.length > 0 ? ['--', ...rest] : []);
316
+ }
317
+ return join(BINARY[pm], 'run', script, rest);
318
+ }
319
+ case 'exec':
320
+ return join(
321
+ {
322
+ npm: 'npx',
323
+ pnpm: 'pnpm exec',
324
+ yarn: 'yarn run',
325
+ 'yarn-classic': 'yarn run',
326
+ bun: 'bunx',
327
+ }[pm],
328
+ args,
329
+ );
330
+ case 'dlx':
331
+ return join(
332
+ {
333
+ npm: 'npx',
334
+ pnpm: 'pnpm dlx',
335
+ yarn: 'yarn dlx',
336
+ // Yarn 1 has no dlx: npx, which every Node carries.
337
+ 'yarn-classic': 'npx',
338
+ bun: 'bunx',
339
+ }[pm],
340
+ args,
341
+ );
342
+ case 'create': {
343
+ // npm passes flags to the initializer only after `--`.
344
+ const flagAt = args.findIndex(arg => arg.startsWith('-'));
345
+ if (pm === 'npm' && flagAt > 0) {
346
+ return join('npm create', [
347
+ ...args.slice(0, flagAt),
348
+ '--',
349
+ ...args.slice(flagAt),
350
+ ]);
351
+ }
352
+ return join(BINARY[pm], 'create', args);
353
+ }
354
+ default:
355
+ return join(BINARY[pm], verb, args);
356
+ }
357
+ }
358
+
359
+ /**
360
+ * @typedef {object} RenderOptions
361
+ * @property {string} stem how the reader runs the CLI, e.g. `pnpm exec astryx`
362
+ * @property {DocPackageManager} pm the reader's package manager
363
+ */
364
+
365
+ /**
366
+ * One step as the reader runs it, and, for a one-manager step, the line
367
+ * readers see beside it.
368
+ * @param {DocStep} step
369
+ * @param {RenderOptions} options
370
+ * @returns {{command: string, note: string | null}}
371
+ */
372
+ export function renderStep(step, {stem, pm}) {
373
+ if (step.kind === 'command') {
374
+ return {command: [stem, ...step.args].join(' '), note: null};
375
+ }
376
+ if (step.kind === 'pm-only') {
377
+ return {
378
+ command: [BINARY[step.manager], ...step.args].join(' '),
379
+ note: `only with ${managerName(step.manager)}: ${step.reason}`,
380
+ };
381
+ }
382
+ return {command: renderPmStep(step, pm), note: null};
383
+ }
384
+
385
+ /**
386
+ * A package manager's name as readers read it.
387
+ * @param {DocPackageManager} pm
388
+ * @returns {string}
389
+ */
390
+ export function managerName(pm) {
391
+ return {
392
+ npm: 'npm',
393
+ pnpm: 'pnpm',
394
+ yarn: 'Yarn',
395
+ 'yarn-classic': 'Yarn 1',
396
+ bun: 'Bun',
397
+ }[pm];
398
+ }
399
+
400
+ /** Code-fence languages whose lines are shell commands. */
401
+ const SHELL_LANGS = new Set(['', 'bash', 'sh', 'shell', 'zsh', 'console']);
402
+
403
+ /**
404
+ * Text with each `{@command}`, `{@pm}` and `{@pm-only}` token rendered for the
405
+ * reader, and each `{@@…}` shown as `{@…}`. A token in prose prints as a code
406
+ * span; in a code span or code block, as the bare command. A one-manager
407
+ * step's reason follows it: in prose right after the command, after a code
408
+ * span, in parentheses; in a shell block as a comment. A Markdown fence in
409
+ * prose is a code block in its language. A token that does not parse prints
410
+ * as written, and doctor names it.
411
+ * @param {string} text
412
+ * @param {RenderOptions} options
413
+ * @param {string | null} [lang] the code block's language when `text` is one
414
+ * block's body; omit for prose
415
+ * @returns {string}
416
+ */
417
+ export function renderCommandTokens(text, options, lang = null) {
418
+ const source = String(text);
419
+ if (!source.includes('{@')) return source;
420
+ const inBlock = lang != null;
421
+ const shell = inBlock && SHELL_LANGS.has(lang.toLowerCase());
422
+ /**
423
+ * @param {string} part
424
+ * @param {string[]} notes where a one-manager step's reason goes
425
+ * @param {boolean} wrap print a rendered command as a code span
426
+ */
427
+ const render = (part, notes, wrap) =>
428
+ part.replace(TOKEN, (whole, escaped, name, body = '') => {
429
+ if (escaped) return whole.replace('{@@', '{@');
430
+ if (!COMMAND_TOKENS.has(name)) return whole;
431
+ const step = parseCommandToken(name, body);
432
+ if ('error' in step) return whole;
433
+ const {command, note} = renderStep(step, options);
434
+ // In prose the reason follows its own command, inside the sentence.
435
+ if (note != null && wrap) return `\`${command}\` (${note})`;
436
+ if (note != null) notes.push(note);
437
+ return wrap ? `\`${command}\`` : command;
438
+ });
439
+ if (inBlock) {
440
+ return source
441
+ .split('\n')
442
+ .map(line => {
443
+ /** @type {string[]} */
444
+ const notes = [];
445
+ const out = render(line, notes, false);
446
+ if (notes.length === 0) return out;
447
+ return shell
448
+ ? `${out} # ${notes.join('; ')}`
449
+ : `${out} (${notes.join('; ')})`;
450
+ })
451
+ .join('\n');
452
+ }
453
+ /** @param {string} line */
454
+ const prose = line =>
455
+ line
456
+ .split(/(`[^`\n]*`)/)
457
+ .map(part => {
458
+ /** @type {string[]} */
459
+ const notes = [];
460
+ const isSpan =
461
+ part.length > 1 && part.startsWith('`') && part.endsWith('`');
462
+ const out = render(part, notes, !isSpan);
463
+ return notes.length === 0 ? out : `${out} (${notes.join('; ')})`;
464
+ })
465
+ .join('');
466
+ // A fence inside prose is a code block: its lines render as one, in the
467
+ // fence's language.
468
+ /** @type {{marker: string, lang: string} | null} */
469
+ let fence = null;
470
+ return source
471
+ .split('\n')
472
+ .map(line => {
473
+ const marker = /^\s*(`{3,}|~{3,})\s*([\w-]*)/.exec(line);
474
+ if (marker) {
475
+ if (fence == null) {
476
+ fence = {marker: marker[1], lang: marker[2].toLowerCase()};
477
+ } else if (marker[1].startsWith(fence.marker)) {
478
+ fence = null;
479
+ }
480
+ return line;
481
+ }
482
+ return fence == null
483
+ ? prose(line)
484
+ : renderCommandTokens(line, options, fence.lang);
485
+ })
486
+ .join('\n');
487
+ }
488
+
489
+ /**
490
+ * Every command token in a text, with its parse, for doctor.
491
+ * @param {string} text
492
+ * @returns {Array<{token: string, name: string, step: DocStep | {error: string}}>}
493
+ */
494
+ export function commandTokensIn(text) {
495
+ const found = [];
496
+ for (const match of String(text).matchAll(TOKEN)) {
497
+ if (match[1] || !COMMAND_TOKENS.has(match[2])) continue;
498
+ found.push({
499
+ token: match[0],
500
+ name: match[2],
501
+ step: parseCommandToken(match[2], match[3] ?? ''),
502
+ });
503
+ }
504
+ return found;
505
+ }
506
+
507
+ /** The token names a doc may write: these and `{@link}`. */
508
+ export const DOC_TOKEN_NAMES = new Set([...COMMAND_TOKENS, 'link']);
509
+
510
+ /**
511
+ * Every `{@name …}` whose name is not a doc token, for doctor.
512
+ * @param {string} text
513
+ * @returns {string[]}
514
+ */
515
+ export function unknownTokensIn(text) {
516
+ const found = [];
517
+ for (const match of String(text).matchAll(TOKEN)) {
518
+ if (!match[1] && !DOC_TOKEN_NAMES.has(match[2])) found.push(match[0]);
519
+ }
520
+ return found;
521
+ }
522
+
523
+ /**
524
+ * Each `{@command`, `{@pm` or `{@pm-only` opening (escaped with more `@`s
525
+ * too) that no whole token starts at: one with no closing brace, or with a
526
+ * quote it never closes. It prints as written, so doctor names it.
527
+ * @param {string} text
528
+ * @returns {string[]} the opening and the rest of its line
529
+ */
530
+ export function unclosedTokensIn(text) {
531
+ const source = String(text);
532
+ const starts = new Set(
533
+ [...source.matchAll(TOKEN)].map(
534
+ match => /** @type {number} */ (match.index),
535
+ ),
536
+ );
537
+ const found = [];
538
+ for (const match of source.matchAll(/\{@@*(?:command|pm-only|pm)\b/g)) {
539
+ const at = /** @type {number} */ (match.index);
540
+ if (starts.has(at)) continue;
541
+ const end = source.indexOf('\n', at);
542
+ found.push(source.slice(at, end < 0 ? undefined : end).slice(0, 80));
543
+ }
544
+ return found;
545
+ }
546
+
547
+ /** Package-manager binaries a plain-text command may start with. */
548
+ const PM_BINARIES = new Set(['npm', 'npx', 'pnpm', 'yarn', 'bun', 'bunx']);
549
+
550
+ /**
551
+ * The token a plain-text package-manager command should be written as, for
552
+ * doctor's fix: `{@pm …}` when every package manager can run it, `{@pm-only
553
+ * <manager> "<why>" …}` when only that one can, or null when the text is not
554
+ * a package-manager command. A runner in front of `astryx` is an Astryx
555
+ * command: `{@command …}`.
556
+ * @param {string} command one command, such as `npm install -D @acme/x`
557
+ * @returns {string | null}
558
+ */
559
+ export function tokenForCommand(command) {
560
+ const words = String(command).trim().split(/\s+/);
561
+ const [bin, ...rest] = words;
562
+ if (bin === 'astryx')
563
+ return `{@command ${rest.join(' ')}}`.replace(' }', '}');
564
+ if (!PM_BINARIES.has(bin)) return null;
565
+ // A runner in front of astryx.
566
+ const astryxAt = rest.indexOf('astryx');
567
+ const runnerWords =
568
+ bin === 'npx' || bin === 'bunx'
569
+ ? 0
570
+ : bin === 'pnpm' && (rest[0] === 'exec' || rest[0] === 'dlx')
571
+ ? 1
572
+ : bin === 'yarn' &&
573
+ (rest[0] === 'run' || rest[0] === 'dlx' || rest[0] === 'exec')
574
+ ? 1
575
+ : bin === 'npm' && (rest[0] === 'exec' || rest[0] === 'x')
576
+ ? 1
577
+ : bin === 'bun' && rest[0] === 'x'
578
+ ? 1
579
+ : 0;
580
+ if (
581
+ astryxAt === runnerWords ||
582
+ (astryxAt >= 0 &&
583
+ rest.slice(runnerWords, astryxAt).every(w => w.startsWith('-')))
584
+ ) {
585
+ const after = rest.slice(astryxAt + 1).filter(w => w !== '--');
586
+ return after.length > 0 ? `{@command ${after.join(' ')}}` : null;
587
+ }
588
+ if (bin === 'npm' && rest[0] === 'run' && rest[1] === 'astryx') {
589
+ const after = rest.slice(2).filter(w => w !== '--');
590
+ return after.length > 0 ? `{@command ${after.join(' ')}}` : null;
591
+ }
592
+ const flags = [];
593
+ const args = [];
594
+ const pmOnly = (/** @type {string} */ manager) =>
595
+ `{@pm-only ${manager} "<why only ${manager}>" ${rest.join(' ')}}`;
596
+ if (bin === 'npx' || bin === 'bunx') {
597
+ const plain = rest.filter(w => w !== '-y' && w !== '--yes');
598
+ // npx runs the project's own binary when it has one: an unscoped,
599
+ // unversioned name such as `tsc` is that, so it is `exec`. A scoped or
600
+ // versioned package, or `--yes`, downloads one: `dlx`.
601
+ const target = plain.find(w => !w.startsWith('-')) ?? '';
602
+ const downloads =
603
+ plain.length < rest.length ||
604
+ target.startsWith('@') ||
605
+ /^[^@]+@/.test(target);
606
+ return `{@pm ${downloads ? 'dlx' : 'exec'} ${plain.join(' ')}}`;
607
+ }
608
+ let verb = rest[0];
609
+ let tail = rest.slice(1);
610
+ if (bin === 'yarn' && verb === 'global') {
611
+ if (tail[0] === 'add' || tail[0] === 'remove') {
612
+ flags.push('--global');
613
+ verb = tail[0];
614
+ tail = tail.slice(1);
615
+ } else {
616
+ return pmOnly('yarn-classic');
617
+ }
618
+ }
619
+ if (verb === undefined) return '{@pm install}';
620
+ const VERBS = {
621
+ install: 'install',
622
+ i: 'install',
623
+ add: 'install',
624
+ uninstall: 'remove',
625
+ un: 'remove',
626
+ rm: 'remove',
627
+ remove: 'remove',
628
+ update: 'update',
629
+ up: 'update',
630
+ upgrade: 'update',
631
+ run: 'run',
632
+ 'run-script': 'run',
633
+ exec: 'exec',
634
+ dlx: 'dlx',
635
+ create: 'create',
636
+ x: 'exec',
637
+ };
638
+ const mapped = /** @type {Record<string, string>} */ (VERBS)[verb];
639
+ // npm's script shortcuts run a script every package manager can run.
640
+ if (
641
+ !mapped &&
642
+ bin === 'npm' &&
643
+ ['test', 'start', 'stop', 'restart'].includes(verb)
644
+ ) {
645
+ // The token puts npm's `--` back itself, as for `run`.
646
+ return `{@pm run ${[verb, ...tail.filter(word => word !== '--')].join(' ')}}`;
647
+ }
648
+ if (!mapped) {
649
+ // `pnpm build`, `yarn test`: a script run without `run`.
650
+ if (
651
+ bin !== 'npm' &&
652
+ bin !== 'bun' &&
653
+ /^[\w:.-]+$/.test(verb) &&
654
+ !NPM_TOOLS.has(verb)
655
+ ) {
656
+ return `{@pm run ${[verb, ...tail].join(' ')}}`;
657
+ }
658
+ return pmOnly(bin === 'yarn' ? 'yarn' : /** @type {string} */ (bin));
659
+ }
660
+ // A script, a binary or an initializer takes every word after it as its
661
+ // own, flags included; the token puts back any `--` npm needs.
662
+ const passesThrough = ['run', 'exec', 'dlx', 'create'].includes(mapped);
663
+ for (const word of tail) {
664
+ if (passesThrough) {
665
+ if (word !== '--') args.push(word);
666
+ continue;
667
+ }
668
+ if (['-D', '-d', '--save-dev', '--dev', '-DE', '-ED'].includes(word))
669
+ flags.push('--dev');
670
+ if (['-E', '--exact', '--save-exact', '-DE', '-ED'].includes(word))
671
+ flags.push('--exact');
672
+ if (['-g', '--global'].includes(word)) flags.push('--global');
673
+ if (word === '--latest' || word === '-L') flags.push('--latest');
674
+ if (!word.startsWith('-')) args.push(word);
675
+ }
676
+ const unique = [...new Set(flags)];
677
+ // A flag no other package manager shares: one manager only.
678
+ const known = new Set([
679
+ '-D',
680
+ '-d',
681
+ '--save-dev',
682
+ '--dev',
683
+ '-E',
684
+ '--exact',
685
+ '--save-exact',
686
+ '-g',
687
+ '--global',
688
+ '--latest',
689
+ '-L',
690
+ '-DE',
691
+ '-ED',
692
+ '--',
693
+ ]);
694
+ if (
695
+ mapped !== 'run' &&
696
+ mapped !== 'exec' &&
697
+ mapped !== 'dlx' &&
698
+ mapped !== 'create' &&
699
+ tail.some(w => w.startsWith('-') && !known.has(w))
700
+ ) {
701
+ return pmOnly(bin === 'yarn' ? 'yarn' : /** @type {string} */ (bin));
702
+ }
703
+ const token = `{@pm ${[mapped, ...args, ...unique].join(' ')}}`;
704
+ const parsed = parseCommandToken(
705
+ 'pm',
706
+ [mapped, ...args, ...unique].join(' '),
707
+ );
708
+ return 'error' in parsed
709
+ ? pmOnly(bin === 'yarn' ? 'yarn' : /** @type {string} */ (bin))
710
+ : token;
711
+ }
712
+
713
+ /** Commands only npm itself runs: `npm pkg`, `npm pack` and the rest. */
714
+ const NPM_TOOLS = new Set([
715
+ 'pkg',
716
+ 'pack',
717
+ 'publish',
718
+ 'version',
719
+ 'view',
720
+ 'info',
721
+ 'config',
722
+ 'prefix',
723
+ 'setup',
724
+ 'link',
725
+ 'why',
726
+ 'ls',
727
+ 'list',
728
+ 'outdated',
729
+ 'cache',
730
+ 'init',
731
+ 'dist-tag',
732
+ 'login',
733
+ 'whoami',
734
+ 'audit',
735
+ 'ci',
736
+ 'store',
737
+ 'global',
738
+ 'dedupe',
739
+ ]);