@astryxdesign/cli 0.7.3-canary.31e06f4 → 0.7.3-canary.641d08e

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 (122) hide show
  1. package/README.md +68 -68
  2. package/api/component/doc-links.test.mjs +2 -2
  3. package/api/docs/_adapter.d.mts +2 -2
  4. package/api/docs/_adapter.mjs +61 -18
  5. package/api/docs/docs-homes.test.mjs +127 -0
  6. package/api/docs/docs.doc.mjs +3 -3
  7. package/api/docs/docs.test.mjs +28 -18
  8. package/api/docs/integration-tree.test.mjs +20 -20
  9. package/api/docs/integrationDocs.test.mjs +1 -1
  10. package/api/docs/namespace-section.test.mjs +6 -6
  11. package/api/docs/reference-blocks.test.mjs +2 -2
  12. package/api/doctor/doctor.mjs +16 -12
  13. package/api/doctor/doctor.test.mjs +9 -9
  14. package/api/integration/_adapter.d.mts +8 -0
  15. package/api/integration/_adapter.mjs +21 -1
  16. package/api/integration/authoring-checks.test.mjs +220 -0
  17. package/api/integration/validate-integration.mjs +24 -0
  18. package/api/integration/validate-integration.type.d.mts +2 -1
  19. package/api/integration/validate-integration.type.mjs +2 -1
  20. package/api/integration/validateIntegration.doc.mjs +1 -1
  21. package/api/search/_candidate-score.mjs +1 -1
  22. package/api/search/_entries.mjs +57 -1
  23. package/api/search/_gather-docs.mjs +3 -3
  24. package/api/search/search.doc.mjs +1 -1
  25. package/api/search/search.test.mjs +14 -14
  26. package/assets/docs/author-a-theme.doc.mjs +2 -2
  27. package/assets/docs/tree/add-a-component.doc.mjs +1 -1
  28. package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
  29. package/assets/docs/tree/add-a-topic.doc.mjs +2 -2
  30. package/assets/docs/tree/agent-guidance.doc.mjs +1 -1
  31. package/assets/docs/tree/{integrations.doc.mjs → authoring.doc.mjs} +15 -6
  32. package/assets/docs/tree/block-template.doc.mjs +1 -1
  33. package/assets/docs/tree/build-the-template.doc.mjs +1 -1
  34. package/assets/docs/tree/building-blocks.doc.mjs +2 -2
  35. package/assets/docs/tree/check-the-props.doc.mjs +1 -1
  36. package/assets/docs/tree/check-your-docs.doc.mjs +2 -2
  37. package/assets/docs/tree/checks.doc.mjs +2 -2
  38. package/assets/docs/tree/cli-command.doc.mjs +1 -1
  39. package/assets/docs/tree/cli.doc.mjs +1 -1
  40. package/assets/docs/tree/codemods.doc.mjs +1 -1
  41. package/assets/docs/tree/component-family.doc.mjs +1 -1
  42. package/assets/docs/tree/component-imports.doc.mjs +1 -1
  43. package/assets/docs/tree/components.doc.mjs +1 -1
  44. package/assets/docs/tree/configuration.doc.mjs +1 -1
  45. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +1 -1
  46. package/assets/docs/tree/define-the-theme.doc.mjs +1 -1
  47. package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
  48. package/assets/docs/tree/docs.doc.mjs +1 -1
  49. package/assets/docs/tree/document-the-template.doc.mjs +1 -1
  50. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  51. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  52. package/assets/docs/tree/extend-or-replace.doc.mjs +1 -1
  53. package/assets/docs/{authoring.doc.mjs → tree/field-reference.doc.mjs} +4 -3
  54. package/assets/docs/tree/fonts-and-assets.doc.mjs +1 -1
  55. package/assets/docs/tree/generate-a-palette.doc.mjs +1 -1
  56. package/assets/docs/tree/global-integrations.doc.mjs +2 -2
  57. package/assets/docs/tree/grade-template-with-agent.doc.mjs +4 -4
  58. package/assets/docs/tree/help.doc.mjs +2 -2
  59. package/assets/docs/tree/links.doc.mjs +1 -1
  60. package/assets/docs/tree/package-and-test.doc.mjs +1 -1
  61. package/assets/docs/tree/page-template.doc.mjs +1 -1
  62. package/assets/docs/tree/publishing.doc.mjs +1 -1
  63. package/assets/docs/tree/quick-start.doc.mjs +1 -1
  64. package/assets/docs/tree/replace-a-core-component.doc.mjs +1 -1
  65. package/assets/docs/tree/replace-a-core-template.doc.mjs +1 -1
  66. package/assets/docs/tree/sections-and-placement.doc.mjs +1 -1
  67. package/assets/docs/tree/see-it-in-an-app.doc.mjs +1 -1
  68. package/assets/docs/tree/ship.doc.mjs +2 -2
  69. package/assets/docs/tree/short-and-findable.doc.mjs +1 -1
  70. package/assets/docs/tree/single-component.doc.mjs +1 -1
  71. package/assets/docs/tree/start-a-template.doc.mjs +1 -1
  72. package/assets/docs/tree/subcomponent.doc.mjs +1 -1
  73. package/assets/docs/tree/template-assets.doc.mjs +1 -1
  74. package/assets/docs/tree/template-doc-overview.doc.mjs +2 -2
  75. package/assets/docs/tree/template-fonts.doc.mjs +1 -1
  76. package/assets/docs/tree/template-grading-rubric.doc.mjs +1 -1
  77. package/assets/docs/tree/template-icons.doc.mjs +1 -1
  78. package/assets/docs/tree/template-images-media.doc.mjs +1 -1
  79. package/assets/docs/tree/template-styles.doc.mjs +1 -1
  80. package/assets/docs/tree/templates.doc.mjs +1 -1
  81. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  82. package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
  83. package/assets/docs/tree/themes.doc.mjs +1 -1
  84. package/assets/docs/tree/troubleshooting.doc.mjs +1 -1
  85. package/assets/docs/tree/upgrading.doc.mjs +1 -1
  86. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +1 -1
  87. package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
  88. package/assets/docs/tree/versioning.doc.mjs +1 -1
  89. package/assets/docs/tree/write-good-templates.doc.mjs +1 -1
  90. package/assets/docs/tree/write-the-template-file.doc.mjs +1 -1
  91. package/authoring/doctypes/schema/schema.doc.mjs +1 -1
  92. package/authoring/integration/integration.doc.mjs +1 -1
  93. package/authoring/integration/type.ts +1 -1
  94. package/clients/cli/commands/docs.doc.mjs +2 -2
  95. package/clients/cli/commands/docs.mjs +1 -1
  96. package/clients/cli/commands/docs.test.mjs +14 -14
  97. package/clients/cli/commands/integration-check.test.mjs +48 -0
  98. package/clients/cli/commands/integration.doc.mjs +1 -1
  99. package/foundation/agent-docs/agent-docs.mjs +2 -1
  100. package/foundation/agent-docs/agent-docs.test.mjs +5 -4
  101. package/foundation/discovery/authoring-self-docs.d.mts +7 -0
  102. package/foundation/discovery/authoring-self-docs.mjs +16 -4
  103. package/foundation/discovery/authoring-self-docs.test.mjs +15 -3
  104. package/foundation/discovery/authoring-surface.d.mts +1 -1
  105. package/foundation/discovery/authoring-surface.mjs +2 -2
  106. package/foundation/discovery/authoring-surface.test.mjs +1 -1
  107. package/foundation/discovery/cli-self-docs.d.mts +2 -1
  108. package/foundation/discovery/cli-self-docs.mjs +4 -3
  109. package/foundation/discovery/cli-self-docs.test.mjs +2 -2
  110. package/foundation/discovery/runner-allowlist.d.mts +2 -4
  111. package/foundation/discovery/runner-allowlist.mjs +2 -4
  112. package/foundation/discovery/runner-commands.d.mts +77 -0
  113. package/foundation/discovery/runner-commands.mjs +293 -0
  114. package/foundation/discovery/runner-commands.test.mjs +128 -0
  115. package/foundation/discovery/stray-pointers.test.mjs +1 -1
  116. package/foundation/doc-compiler/doc-loads.test.mjs +4 -0
  117. package/foundation/doc-compiler/split-topic-keywords.test.mjs +3 -2
  118. package/foundation/doc-compiler/tree.d.mts +32 -6
  119. package/foundation/doc-compiler/tree.mjs +77 -43
  120. package/foundation/doc-compiler/tree.test.mjs +7 -7
  121. package/foundation/response/response-types.doc.mjs +1 -1
  122. package/package.json +9 -9
@@ -7,7 +7,7 @@
7
7
  * @input authoring/index.d.ts and the type modules it re-exports, parsed with
8
8
  * jscodeshift and never imported or type-checked (consumer installs have no
9
9
  * TypeScript); the self-docs on disk and in AUTHORING_SELF_DOCS; and the
10
- * section keys `astryx docs authoring --index` lists.
10
+ * section keys `astryx docs authoring/field-reference --index` lists.
11
11
  * @output {@link tracePublicAuthoringTypes}: each exported type and the module
12
12
  * that declares it. {@link auditAuthoringSurface}: each module whose exported
13
13
  * types a reader cannot reach and why, each export that cannot be traced,
@@ -424,7 +424,7 @@ function declaredIn(t, folder, name) {
424
424
  * document something public: a public type declared beside it, and, when its
425
425
  * title is the name of a type declared beside it, that type.
426
426
  * @param {{root?: string, sources?: string[], topicKeys?: Set<string> | null}} [options]
427
- * `topicKeys` are the keys `astryx docs authoring --index` lists; null skips
427
+ * `topicKeys` are the keys `astryx docs authoring/field-reference --index` lists; null skips
428
428
  * that comparison.
429
429
  * @returns {Promise<{
430
430
  * types: number,
@@ -86,7 +86,7 @@ describe('the public authoring surface of this repo', () => {
86
86
  it(
87
87
  'audits clean: every exported type has a readable doc and every self-doc documents one',
88
88
  async () => {
89
- const index = await docs('authoring', undefined, {index: true});
89
+ const index = await docs('authoring/field-reference', undefined, {index: true});
90
90
  const topicKeys = new Set(
91
91
  index.data.sections.map((/** @type {any} */ s) => s.id),
92
92
  );
@@ -102,7 +102,8 @@ export const CLI_SELF_DOC_DIRS: string[];
102
102
  /**
103
103
  * Every namespace a CLI doc may declare, and what reads it. The docs tree
104
104
  * adopts `cli/commands` (under `cli/commands`) and `cli/api` (under
105
- * `cli/api/<kind>s`); `astryx docs authoring` reads `authoring` from its list.
105
+ * `cli/api/<kind>s`); the field reference (`astryx docs authoring/field-reference`)
106
+ * reads `authoring` from its list.
106
107
  * @type {Record<string, {reader: 'tree' | 'authoring'}>}
107
108
  */
108
109
  export const CLI_DOC_NAMESPACES: Record<string, {
@@ -41,7 +41,8 @@ const NAMESPACED_KINDS = new Set(['command', 'function', 'schema', 'enum']);
41
41
  /**
42
42
  * Every namespace a CLI doc may declare, and what reads it. The docs tree
43
43
  * adopts `cli/commands` (under `cli/commands`) and `cli/api` (under
44
- * `cli/api/<kind>s`); `astryx docs authoring` reads `authoring` from its list.
44
+ * `cli/api/<kind>s`); the field reference (`astryx docs authoring/field-reference`)
45
+ * reads `authoring` from its list.
45
46
  * @type {Record<string, {reader: 'tree' | 'authoring'}>}
46
47
  */
47
48
  export const CLI_DOC_NAMESPACES = {
@@ -492,13 +493,13 @@ export async function auditCliSelfDocs({
492
493
  else {
493
494
  misfiled.push({
494
495
  source,
495
- message: `${source} has namespace "authoring", but \`${formatCliCommand('docs authoring')}\` does not list it`,
496
+ message: `${source} has namespace "authoring", but \`${formatCliCommand('docs authoring/field-reference')}\` does not list it`,
496
497
  });
497
498
  }
498
499
  } else if (inAuthoring) {
499
500
  misfiled.push({
500
501
  source,
501
- message: `${source} is read in \`${formatCliCommand('docs authoring')}\`, but its namespace is "${namespace}"`,
502
+ message: `${source} is read in \`${formatCliCommand('docs authoring/field-reference')}\`, but its namespace is "${namespace}"`,
502
503
  });
503
504
  } else {
504
505
  treeDocs.push(doc);
@@ -146,8 +146,8 @@ describe('auditCliSelfDocs', () => {
146
146
  });
147
147
  const result = await audit(root, ['y/y.doc.mjs', 'z/z.doc.mjs']);
148
148
  expect(result.misfiled.map(entry => entry.message)).toEqual([
149
- `authoring/x/x.doc.mjs has namespace "authoring", but \`${formatCliCommand('docs authoring')}\` does not list it`,
150
- `authoring/y/y.doc.mjs is read in \`${formatCliCommand('docs authoring')}\`, but its namespace is "cli/api"`,
149
+ `authoring/x/x.doc.mjs has namespace "authoring", but \`${formatCliCommand('docs authoring/field-reference')}\` does not list it`,
150
+ `authoring/y/y.doc.mjs is read in \`${formatCliCommand('docs authoring/field-reference')}\`, but its namespace is "cli/api"`,
151
151
  ]);
152
152
  expect(result.authoring).toBe(1);
153
153
  expect(result.tree).toBe(0);
@@ -14,10 +14,8 @@
14
14
  * command, except in `runnerExamples`, where `npx @astryxdesign/cli …` and a
15
15
  * project's runner may (still never the unscoped name through npx).
16
16
  *
17
- * TODO(doctor-runner-check): the Doctor lane's integration-docs runner check
18
- * (foundation/discovery/runner-commands.mjs) becomes the single rule; then the
19
- * repo test runs that check over Core's and the CLI's own docs with this
20
- * allowlist.
17
+ * The matcher is runnerCommandsIn (foundation/discovery/runner-commands.mjs),
18
+ * the rule `doctor integration validate` holds integrations to.
21
19
  *
22
20
  * @input nothing
23
21
  * @output the allowlist
@@ -13,10 +13,8 @@
13
13
  * command, except in `runnerExamples`, where `npx @astryxdesign/cli …` and a
14
14
  * project's runner may (still never the unscoped name through npx).
15
15
  *
16
- * TODO(doctor-runner-check): the Doctor lane's integration-docs runner check
17
- * (foundation/discovery/runner-commands.mjs) becomes the single rule; then the
18
- * repo test runs that check over Core's and the CLI's own docs with this
19
- * allowlist.
16
+ * The matcher is runnerCommandsIn (foundation/discovery/runner-commands.mjs),
17
+ * the rule `doctor integration validate` holds integrations to.
20
18
  *
21
19
  * @input nothing
22
20
  * @output the allowlist
@@ -0,0 +1,77 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * Each runner command in a text, as written, with the bare command to write
6
+ * instead, in order.
7
+ *
8
+ * @param {string} text
9
+ * @returns {{written: string, bare: string, download: boolean}[]}
10
+ */
11
+ export function runnerCommandsIn(text: string): {
12
+ written: string;
13
+ bare: string;
14
+ download: boolean;
15
+ }[];
16
+ /**
17
+ * An Astryx command a topic writes with a package runner in front.
18
+ *
19
+ * @typedef {object} RunnerCommand
20
+ * @property {string} topic
21
+ * @property {string | null} section the title of the section it is in
22
+ * @property {string} file the doc file
23
+ * @property {number | null} line the file line that holds it, when found
24
+ * @property {string} command the command as written
25
+ * @property {string} message the bare command to write instead
26
+ */
27
+ /**
28
+ * Runner commands in a package's topics.
29
+ *
30
+ * @param {{records: {name: string, path: string}[], guides?: {name: string, ref?: {topicFile?: string}}[]}} discovered
31
+ * the package's docs, as discoverIntegrationDocs returns them
32
+ * @returns {Promise<RunnerCommand[]>}
33
+ */
34
+ export function findRunnerCommands(discovered: {
35
+ records: {
36
+ name: string;
37
+ path: string;
38
+ }[];
39
+ guides?: {
40
+ name: string;
41
+ ref?: {
42
+ topicFile?: string;
43
+ };
44
+ }[];
45
+ }): Promise<RunnerCommand[]>;
46
+ /**
47
+ * The CLI's top-level commands, the words that make a project runner's
48
+ * `astryx <word>` a command; runner-commands.test.mjs keeps the list equal to
49
+ * the command docs.
50
+ */
51
+ export const CLI_COMMANDS: readonly string[];
52
+ /**
53
+ * An Astryx command a topic writes with a package runner in front.
54
+ */
55
+ export type RunnerCommand = {
56
+ topic: string;
57
+ /**
58
+ * the title of the section it is in
59
+ */
60
+ section: string | null;
61
+ /**
62
+ * the doc file
63
+ */
64
+ file: string;
65
+ /**
66
+ * the file line that holds it, when found
67
+ */
68
+ line: number | null;
69
+ /**
70
+ * the command as written
71
+ */
72
+ command: string;
73
+ /**
74
+ * the bare command to write instead
75
+ */
76
+ message: string;
77
+ };
@@ -0,0 +1,293 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * Runner commands in docs: an Astryx command written with a package runner in
5
+ * front. Docs write the bare `astryx <command>` and the CLI shows it with the
6
+ * reader's runner, so a written runner is wrong for every reader on another
7
+ * package manager.
8
+ *
9
+ * - A download runner (`npx`, `pnpm dlx`, `yarn dlx`, `bunx`, `bun x`,
10
+ * `npm exec`) with the unscoped name `astryx`, with or without an
11
+ * `@<version>` after it, always counts, even with nothing after it: it
12
+ * fetches an unrelated package of that name.
13
+ * - A project runner (`pnpm exec`, `pnpm`, `yarn`) counts when one of the
14
+ * CLI's commands follows on the same line (`pnpm exec astryx docs`). Named
15
+ * alone, or before a word that is not a command (`yarn astryx runs the
16
+ * copy…`), it is left alone.
17
+ * - A runner and what follows it are read within one line; flags between a
18
+ * download runner and the name are read past.
19
+ * - The scoped package (`npx @astryxdesign/cli init`) never counts: it is the
20
+ * one form that runs before the CLI is installed, for install steps.
21
+ * - Other tools' commands (`npx prettier`) never count.
22
+ */
23
+
24
+ import fs from 'node:fs';
25
+
26
+ import {loadTopicModule} from './docs-discovery.mjs';
27
+
28
+ /**
29
+ * @param {string} file
30
+ * @returns {Promise<any>} the doc, or null for one that does not load (the
31
+ * load problem is reported elsewhere)
32
+ */
33
+ async function loadQuietly(file) {
34
+ try {
35
+ return await loadTopicModule(file);
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
40
+
41
+ const DOWNLOAD_RUNNERS = [
42
+ 'pnpm dlx',
43
+ 'yarn dlx',
44
+ 'npm exec --',
45
+ 'npm exec',
46
+ 'npx',
47
+ 'bunx',
48
+ 'bun x',
49
+ ];
50
+ const PROJECT_RUNNERS = ['pnpm exec', 'pnpm', 'yarn'];
51
+
52
+ /**
53
+ * The CLI's top-level commands, the words that make a project runner's
54
+ * `astryx <word>` a command; runner-commands.test.mjs keeps the list equal to
55
+ * the command docs.
56
+ */
57
+ export const CLI_COMMANDS = Object.freeze([
58
+ 'blog',
59
+ 'build',
60
+ 'component',
61
+ 'discover',
62
+ 'docs',
63
+ 'doctor',
64
+ 'gap-report',
65
+ 'help',
66
+ 'hook',
67
+ 'init',
68
+ 'integration',
69
+ 'manifest',
70
+ 'search',
71
+ 'swizzle',
72
+ 'template',
73
+ 'theme',
74
+ 'upgrade',
75
+ ]);
76
+
77
+ /** Blanks within one line. */
78
+ const BLANK = '[ \\t]+';
79
+
80
+ /** @param {readonly string[]} words */
81
+ const alternatives = words =>
82
+ words.map(each => each.replace(/ /g, BLANK)).join('|');
83
+
84
+ /** After `astryx`: the name ends here (a sentence's period may follow). */
85
+ const NAME_END = '(?![\\w@/-]|\\.\\w)';
86
+
87
+ const DOWNLOAD_COMMAND = new RegExp(
88
+ `(?<![\\w@/.-])(?:${alternatives(DOWNLOAD_RUNNERS)})(?:${BLANK}--?[a-z][\\w-]*)*${BLANK}astryx(?:@[\\w^~-]+(?:\\.[\\w^~-]+)*)?${NAME_END}(?:${BLANK}(${alternatives(CLI_COMMANDS)})(?![\\w-]))?`,
89
+ 'g',
90
+ );
91
+ const PROJECT_COMMAND = new RegExp(
92
+ `(?<![\\w@/.-])(?:${alternatives(PROJECT_RUNNERS)})${BLANK}astryx${BLANK}(${alternatives(CLI_COMMANDS)})(?![\\w-])`,
93
+ 'g',
94
+ );
95
+
96
+ /**
97
+ * Each runner command in a text, as written, with the bare command to write
98
+ * instead, in order.
99
+ *
100
+ * @param {string} text
101
+ * @returns {{written: string, bare: string, download: boolean}[]}
102
+ */
103
+ export function runnerCommandsIn(text) {
104
+ const found = [
105
+ ...[...String(text).matchAll(DOWNLOAD_COMMAND)].map(match => ({
106
+ match,
107
+ download: true,
108
+ })),
109
+ ...[...String(text).matchAll(PROJECT_COMMAND)].map(match => ({
110
+ match,
111
+ download: false,
112
+ })),
113
+ ];
114
+ /** @type {{at: number, written: string, bare: string, download: boolean}[]} */
115
+ const unique = [];
116
+ for (const {match, download} of found) {
117
+ if (unique.some(each => each.at === match.index)) continue;
118
+ unique.push({
119
+ at: match.index,
120
+ written: match[0],
121
+ bare: match[1] ? `astryx ${match[1]}` : 'astryx',
122
+ download,
123
+ });
124
+ }
125
+ return unique
126
+ .sort((a, b) => a.at - b.at)
127
+ .map(({written, bare, download}) => ({written, bare, download}));
128
+ }
129
+
130
+ /**
131
+ * Every string in a doc, with the title of the section it is in. Code counts:
132
+ * most commands sit in code blocks.
133
+ *
134
+ * @param {unknown} doc
135
+ * @returns {{text: string, section: string | null}[]}
136
+ */
137
+ function allStrings(doc) {
138
+ /** @type {{text: string, section: string | null}[]} */
139
+ const strings = [];
140
+ // A doc that refers back to itself is walked once.
141
+ const visited = new WeakSet();
142
+ /**
143
+ * @param {unknown} value
144
+ * @param {string | null} section
145
+ */
146
+ const walk = (value, section) => {
147
+ if (typeof value === 'string') {
148
+ strings.push({text: value, section});
149
+ } else if (value != null && typeof value === 'object') {
150
+ if (visited.has(value)) return;
151
+ visited.add(value);
152
+ if (Array.isArray(value)) {
153
+ for (const item of value) walk(item, section);
154
+ return;
155
+ }
156
+ const record = /** @type {Record<string, unknown>} */ (value);
157
+ for (const [key, field] of Object.entries(record)) {
158
+ if (key === 'sections' && Array.isArray(field)) {
159
+ for (const each of field) {
160
+ walk(each, typeof each?.title === 'string' ? each.title : section);
161
+ }
162
+ } else {
163
+ walk(field, section);
164
+ }
165
+ }
166
+ }
167
+ };
168
+ walk(doc, null);
169
+ return strings;
170
+ }
171
+
172
+ /**
173
+ * Where a file next holds the text, at or after a cursor (a 0-based line and
174
+ * column), else the first place it holds it, else null. A doc's strings are
175
+ * read in file order, so moving the cursor past each match puts a command the
176
+ * topic writes twice on its own line.
177
+ *
178
+ * @param {string[]} lines
179
+ * @param {string} written
180
+ * @param {{line: number, column: number}} cursor
181
+ * @returns {{line: number, column: number} | null}
182
+ */
183
+ function locate(lines, written, cursor) {
184
+ for (let at = cursor.line; at < lines.length; at++) {
185
+ const column = lines[at].indexOf(
186
+ written,
187
+ at === cursor.line ? cursor.column : 0,
188
+ );
189
+ if (column >= 0) return {line: at, column};
190
+ }
191
+ const first = lines.findIndex(line => line.includes(written));
192
+ return first < 0
193
+ ? null
194
+ : {line: first, column: lines[first].indexOf(written)};
195
+ }
196
+
197
+ /**
198
+ * An Astryx command a topic writes with a package runner in front.
199
+ *
200
+ * @typedef {object} RunnerCommand
201
+ * @property {string} topic
202
+ * @property {string | null} section the title of the section it is in
203
+ * @property {string} file the doc file
204
+ * @property {number | null} line the file line that holds it, when found
205
+ * @property {string} command the command as written
206
+ * @property {string} message the bare command to write instead
207
+ */
208
+
209
+ /**
210
+ * Runner commands in a package's topics.
211
+ *
212
+ * @param {{records: {name: string, path: string}[], guides?: {name: string, ref?: {topicFile?: string}}[]}} discovered
213
+ * the package's docs, as discoverIntegrationDocs returns them
214
+ * @returns {Promise<RunnerCommand[]>}
215
+ */
216
+ export async function findRunnerCommands(discovered) {
217
+ const topics = [
218
+ ...discovered.records.map(record => ({
219
+ name: record.name,
220
+ file: record.path,
221
+ })),
222
+ ...(discovered.guides ?? []).map(guide => ({
223
+ name: guide.name,
224
+ file: guide.ref?.topicFile,
225
+ })),
226
+ ];
227
+ /** @type {RunnerCommand[]} */
228
+ const found = [];
229
+ for (const {name, file} of topics) {
230
+ const doc = file ? await loadQuietly(file) : null;
231
+ if (!doc || !file) continue;
232
+ /** @type {string[]} */
233
+ let lines = [];
234
+ try {
235
+ lines = fs.readFileSync(file, 'utf8').split('\n');
236
+ } catch {
237
+ // The doc loaded, so only the line number is lost.
238
+ }
239
+ let cursor = {line: 0, column: 0};
240
+ for (const {text, section} of allStrings(doc)) {
241
+ const commands = runnerCommandsIn(text);
242
+ if (commands.length === 0) continue;
243
+ // A one-line string's own literal places it, wherever the walk reached
244
+ // it (a section in a constant above the doc object, say); else the
245
+ // cursor search, in file order.
246
+ const forms = [`'${text}'`, `"${text}"`, JSON.stringify(text)];
247
+ const holds = (/** @type {string} */ line) =>
248
+ forms.some(form => line.includes(form));
249
+ // On the cursor's own line, only a copy at or after the cursor counts:
250
+ // the copy just matched still sits before it.
251
+ const after = text.includes('\n')
252
+ ? -1
253
+ : lines.findIndex((line, at) =>
254
+ at > cursor.line
255
+ ? holds(line)
256
+ : at === cursor.line &&
257
+ forms.some(form => line.indexOf(form, cursor.column) >= 0),
258
+ );
259
+ const literal =
260
+ after >= 0 || text.includes('\n') ? after : lines.findIndex(holds);
261
+ const column = after >= 0 && after === cursor.line ? cursor.column : 0;
262
+ let from = literal >= 0 ? {line: literal, column} : cursor;
263
+ for (const {written, bare, download} of commands) {
264
+ const place = locate(lines, written, from);
265
+ if (place) {
266
+ from = {line: place.line, column: place.column + written.length};
267
+ if (
268
+ place.line > cursor.line ||
269
+ (place.line === cursor.line && place.column >= cursor.column)
270
+ ) {
271
+ cursor = from;
272
+ }
273
+ }
274
+ const line = place ? place.line + 1 : null;
275
+ const where = section ? ` (section "${section}")` : '';
276
+ found.push({
277
+ topic: name,
278
+ section,
279
+ file,
280
+ line,
281
+ command: written,
282
+ message:
283
+ `"${name}"${where} writes \`${written}\`${line == null ? '' : ` (line ${line})`}. ` +
284
+ `Write \`${bare}\`; the CLI shows it with your project's runner` +
285
+ (download
286
+ ? ', and a download runner with the bare name fetches an unrelated package called astryx.'
287
+ : '.'),
288
+ });
289
+ }
290
+ }
291
+ }
292
+ return found;
293
+ }
@@ -0,0 +1,128 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import {fileURLToPath} from 'node:url';
7
+ import {describe, expect, it} from 'vitest';
8
+
9
+ import {
10
+ CLI_COMMANDS,
11
+ findRunnerCommands,
12
+ runnerCommandsIn,
13
+ } from './runner-commands.mjs';
14
+
15
+ const COMMAND_DOCS = path.resolve(
16
+ path.dirname(fileURLToPath(import.meta.url)),
17
+ '../../clients/cli/commands',
18
+ );
19
+
20
+ describe('runnerCommandsIn', () => {
21
+ it('finds an Astryx command after a project runner, with the bare command', () => {
22
+ expect(runnerCommandsIn('Run `pnpm exec astryx docs theme`.')).toEqual([
23
+ {written: 'pnpm exec astryx docs', bare: 'astryx docs', download: false},
24
+ ]);
25
+ expect(runnerCommandsIn('yarn astryx component Button')).toEqual([
26
+ {
27
+ written: 'yarn astryx component',
28
+ bare: 'astryx component',
29
+ download: false,
30
+ },
31
+ ]);
32
+ });
33
+
34
+ it('always flags the unscoped name after a download runner, even alone', () => {
35
+ expect(runnerCommandsIn('npx astryx doctor; or `bunx astryx`')).toEqual([
36
+ {written: 'npx astryx doctor', bare: 'astryx doctor', download: true},
37
+ {written: 'bunx astryx', bare: 'astryx', download: true},
38
+ ]);
39
+ });
40
+
41
+ it('leaves the scoped package, a project runner named alone, and other tools', () => {
42
+ expect(
43
+ runnerCommandsIn(
44
+ 'Install with `npx @astryxdesign/cli init`. Run it through Yarn (`yarn astryx`). Format with `npx prettier --write`. `npx astryx-tools init`.',
45
+ ),
46
+ ).toEqual([]);
47
+ });
48
+
49
+ it('flags `bun x`, a versioned name, and a name that ends a sentence', () => {
50
+ expect(
51
+ runnerCommandsIn(
52
+ 'Run `bun x astryx doctor`, `npx astryx@latest init` or `npx astryx@0.7.1 docs`. Or just npx astryx.',
53
+ ),
54
+ ).toEqual([
55
+ {written: 'bun x astryx doctor', bare: 'astryx doctor', download: true},
56
+ {
57
+ written: 'npx astryx@latest init',
58
+ bare: 'astryx init',
59
+ download: true,
60
+ },
61
+ {written: 'npx astryx@0.7.1 docs', bare: 'astryx docs', download: true},
62
+ {written: 'npx astryx', bare: 'astryx', download: true},
63
+ ]);
64
+ expect(
65
+ runnerCommandsIn('Edit astryx.config, not npx astryx.config.'),
66
+ ).toEqual([]);
67
+ });
68
+
69
+ it('reads a runner and its command within one line, and needs a CLI command after a project runner', () => {
70
+ expect(
71
+ runnerCommandsIn(
72
+ 'Through Yarn, yarn astryx runs the copy in node_modules.\n```\npnpm astryx\nyarn astryx\n```',
73
+ ),
74
+ ).toEqual([]);
75
+ expect(runnerCommandsIn('npx astryx runs an unrelated package')).toEqual([
76
+ {written: 'npx astryx', bare: 'astryx', download: true},
77
+ ]);
78
+ expect(runnerCommandsIn('pnpm\nastryx docs')).toEqual([]);
79
+ expect(
80
+ runnerCommandsIn('Run `npx -y astryx doctor` or `npx --yes astryx`.'),
81
+ ).toEqual([
82
+ {written: 'npx -y astryx doctor', bare: 'astryx doctor', download: true},
83
+ {written: 'npx --yes astryx', bare: 'astryx', download: true},
84
+ ]);
85
+ });
86
+ });
87
+
88
+ describe('findRunnerCommands', () => {
89
+ it('reads a topic that refers back to itself once, without crashing', async () => {
90
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'runner-self-'));
91
+ const file = path.join(dir, 'acme-setup.doc.mjs');
92
+ fs.writeFileSync(
93
+ file,
94
+ [
95
+ 'const section = {',
96
+ " title: 'Overview',",
97
+ " content: [{type: 'prose', text: 'Run `yarn astryx docs` first.'}],",
98
+ '};',
99
+ "const doc = {type: 'generic', name: 'acme-setup', sections: [section]};",
100
+ 'section.self = section;',
101
+ 'doc.again = doc;',
102
+ 'export default doc;',
103
+ '',
104
+ ].join('\n'),
105
+ );
106
+
107
+ const found = await findRunnerCommands({
108
+ records: [{name: 'acme-setup', path: file}],
109
+ });
110
+
111
+ expect(
112
+ found.map(({command, section, line}) => ({command, section, line})),
113
+ ).toEqual([{command: 'yarn astryx docs', section: 'Overview', line: 3}]);
114
+ });
115
+ });
116
+
117
+ describe('CLI_COMMANDS', () => {
118
+ it('names every top-level command the CLI documents, and nothing else', () => {
119
+ const documented = fs
120
+ .readdirSync(COMMAND_DOCS)
121
+ .filter(file => file.endsWith('.doc.mjs'))
122
+ .map(file => file.slice(0, -'.doc.mjs'.length))
123
+ .filter(name => !name.includes('-') || name === 'gap-report');
124
+ expect([...CLI_COMMANDS].sort()).toEqual(
125
+ [...new Set([...documented, 'help'])].sort(),
126
+ );
127
+ });
128
+ });
@@ -16,7 +16,7 @@ describe('pointersIn', () => {
16
16
  it('leaves a walkthrough alone: more words, a flag, or a route', () => {
17
17
  expect(
18
18
  pointersIn(
19
- 'Run `astryx docs theme overview`, `astryx docs --json`, `astryx docs cli/integrations`, or `astryx docs`.',
19
+ 'Run `astryx docs theme overview`, `astryx docs --json`, `astryx docs authoring/quick-start`, or `astryx docs`.',
20
20
  ),
21
21
  ).toEqual([]);
22
22
  });
@@ -276,6 +276,10 @@ const RUNNERS = {
276
276
  runs: 'a contributed topic, for its catalog fields: name, title, description, category, and relationships',
277
277
  sites: ['importDocModule'],
278
278
  },
279
+ 'foundation/discovery/runner-commands.mjs': {
280
+ runs: "the package's topics, to find each Astryx command written with a package runner",
281
+ sites: ['loadTopicModule'],
282
+ },
279
283
  'foundation/discovery/stray-pointers.mjs': {
280
284
  runs: "the CLI's own guides, for their names, to tell which written `astryx docs <name>` points at a doc that exists",
281
285
  sites: ['builtinGuideFiles'],
@@ -3,7 +3,8 @@
3
3
  /**
4
4
  * @file A topic split into a namespace of guides indexes each term once, a
5
5
  * policy of the docs split itself (no spec clause states it). Below the CLI's own
6
- * root namespaces (every one but `cli`, its command and API reference), a
6
+ * root namespaces (every one but `cli`, its command and API reference, and
7
+ * `authoring`, the integration guides `cli` held before this rule), a
7
8
  * guide's search keywords are its own: none repeats a keyword its namespace,
8
9
  * a sibling, or a guide of another split topic declares. A term several
9
10
  * guides share belongs to their namespace, or to none.
@@ -45,7 +46,7 @@ describe('the keywords of a split topic', () => {
45
46
  return doc?.name;
46
47
  };
47
48
  const roots = [...docs.values()]
48
- .filter(doc => doc.type === 'namespace' && !parentOf(doc) && doc.name !== 'cli')
49
+ .filter(doc => doc.type === 'namespace' && !parentOf(doc) && !['cli', 'authoring'].includes(doc.name))
49
50
  .map(doc => doc.name);
50
51
  expect(roots).toContain('layout');
51
52