@astryxdesign/cli 0.7.3-canary.cbe384c → 0.7.3-canary.f94e962
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.
- package/README.md +63 -63
- package/api/component/component.mjs +16 -2
- package/api/component/component.test.mjs +15 -0
- package/api/doctor/doctor.test.mjs +28 -3
- package/api/integration/_adapter.d.mts +8 -0
- package/api/integration/_adapter.mjs +21 -1
- package/api/integration/authoring-checks.test.mjs +220 -0
- package/api/integration/validate-integration.d.mts +2 -1
- package/api/integration/validate-integration.mjs +30 -5
- package/api/integration/validate-integration.type.d.mts +2 -1
- package/api/integration/validate-integration.type.mjs +2 -1
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/search/_entries.mjs +57 -1
- package/api/upgrade/_adapter.d.mts +57 -3
- package/api/upgrade/_adapter.mjs +149 -27
- package/api/upgrade/registry/registry.d.mts +10 -4
- package/api/upgrade/registry/registry.mjs +33 -16
- package/api/upgrade/registry/registry.test.mjs +85 -0
- package/api/upgrade/run/run.d.mts +4 -2
- package/api/upgrade/run/run.mjs +13 -2
- package/api/upgrade/run/run.test.mjs +417 -3
- package/api/upgrade/upgrade.doc.mjs +10 -5
- package/api/upgrade/upgrade.mjs +13 -2
- package/api/upgrade/upgrade.type.d.mts +5 -3
- package/api/upgrade/upgrade.type.mjs +8 -6
- package/assets/docs/tree/add-a-topic.doc.mjs +1 -1
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/templates/themes/butter/butterTheme.ts +4 -2
- package/assets/templates/themes/stone/stoneTheme.ts +4 -2
- package/assets/templates/themes/y2k/y2kTheme.ts +4 -2
- package/clients/cli/commands/integration-check.test.mjs +48 -0
- package/clients/cli/commands/upgrade.doc.mjs +4 -2
- package/clients/cli/commands/upgrade.mjs +9 -2
- package/clients/cli/commands/upgrade.subfolder-path.test.mjs +97 -0
- package/foundation/config/project.mjs +5 -20
- package/foundation/config/project.test.mjs +30 -0
- package/foundation/discovery/runner-allowlist.d.mts +2 -4
- package/foundation/discovery/runner-allowlist.mjs +2 -4
- package/foundation/discovery/runner-commands.d.mts +77 -0
- package/foundation/discovery/runner-commands.mjs +293 -0
- package/foundation/discovery/runner-commands.test.mjs +128 -0
- package/foundation/discovery/template-needs.mjs +1 -44
- package/foundation/doc-compiler/doc-loads.test.mjs +4 -0
- package/foundation/fs/paths.d.mts +70 -8
- package/foundation/fs/paths.mjs +526 -35
- package/foundation/fs/paths.test.mjs +430 -54
- package/foundation/integrations/autolink.d.mts +2 -1
- package/foundation/integrations/autolink.mjs +4 -13
- package/foundation/integrations/integrations.d.mts +11 -0
- package/foundation/integrations/integrations.mjs +21 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/package.json +9 -9
|
@@ -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
|
+
});
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
import * as fs from 'node:fs';
|
|
19
19
|
import * as path from 'node:path';
|
|
20
20
|
import module from 'node:module';
|
|
21
|
-
import {CLI_ROOT} from '../fs/paths.mjs';
|
|
21
|
+
import {CLI_ROOT, findWorkspaceRoot} from '../fs/paths.mjs';
|
|
22
22
|
import {detectStylingSystem} from '../agent-docs/agent-docs.mjs';
|
|
23
23
|
import {
|
|
24
24
|
decidePackageManagerIn,
|
|
@@ -71,49 +71,6 @@ const ADD_VERB = {
|
|
|
71
71
|
npm: 'npm install',
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
-
/**
|
|
75
|
-
* Whether a package.json declares a workspace (`workspaces` field).
|
|
76
|
-
* @param {string} dir
|
|
77
|
-
* @returns {{exists: boolean, declaresWorkspace: boolean}}
|
|
78
|
-
*/
|
|
79
|
-
function readPackageJson(dir) {
|
|
80
|
-
const pkgPath = path.join(dir, 'package.json');
|
|
81
|
-
if (!fs.existsSync(pkgPath)) return {exists: false, declaresWorkspace: false};
|
|
82
|
-
try {
|
|
83
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
84
|
-
return {exists: true, declaresWorkspace: pkg.workspaces != null};
|
|
85
|
-
} catch {
|
|
86
|
-
return {exists: true, declaresWorkspace: false};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* The workspace root (AST-048 FR15): the nearest directory at or above the
|
|
92
|
-
* target that declares a workspace, by `pnpm-workspace.yaml` or a package.json
|
|
93
|
-
* `workspaces` field; outside a workspace, the nearest package.json's
|
|
94
|
-
* directory; with no package.json, the target itself.
|
|
95
|
-
* @param {string} target
|
|
96
|
-
* @returns {string}
|
|
97
|
-
*/
|
|
98
|
-
function findWorkspaceRoot(target) {
|
|
99
|
-
let dir = path.resolve(target);
|
|
100
|
-
/** @type {string | null} */
|
|
101
|
-
let nearestPackage = null;
|
|
102
|
-
for (;;) {
|
|
103
|
-
const pkg = readPackageJson(dir);
|
|
104
|
-
if (
|
|
105
|
-
fs.existsSync(path.join(dir, 'pnpm-workspace.yaml')) ||
|
|
106
|
-
pkg.declaresWorkspace
|
|
107
|
-
) {
|
|
108
|
-
return dir;
|
|
109
|
-
}
|
|
110
|
-
if (pkg.exists && nearestPackage == null) nearestPackage = dir;
|
|
111
|
-
const parent = path.dirname(dir);
|
|
112
|
-
if (parent === dir) return nearestPackage ?? path.resolve(target);
|
|
113
|
-
dir = parent;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
74
|
/**
|
|
118
75
|
* The project's package manager for an install command (AST-048 FR17, FR19):
|
|
119
76
|
* the nearest directory from the target up to the workspace root that has a
|
|
@@ -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'],
|
|
@@ -51,17 +51,58 @@ export function findCarriedCoreDir(startDir: string, { fetch }?: {
|
|
|
51
51
|
fetch?: boolean;
|
|
52
52
|
}): string | null;
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* released version used, else from the folder of the package.json that makes
|
|
59
|
-
* the folder part of a project, however deep the folder is.
|
|
54
|
+
* The project's own @astryxdesign/core, never a carried one: found from the
|
|
55
|
+
* folder (see {@link findNearbyCoreDir}), else from the folder of the
|
|
56
|
+
* package.json that makes the folder part of a project, however deep the
|
|
57
|
+
* folder is.
|
|
60
58
|
*
|
|
61
59
|
* @param {string} [startDir]
|
|
62
60
|
* @returns {string | null}
|
|
63
61
|
*/
|
|
64
62
|
export function findProjectCoreDir(startDir?: string): string | null;
|
|
63
|
+
/**
|
|
64
|
+
* The project root: the directory of the nearest package.json at or above
|
|
65
|
+
* `startDir`, or null when there is none.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} startDir
|
|
68
|
+
* @returns {string|null}
|
|
69
|
+
*/
|
|
70
|
+
export function findPackageRoot(startDir: string): string | null;
|
|
71
|
+
/**
|
|
72
|
+
* The Astryx project `startDir` belongs to: the nearest folder at or above it
|
|
73
|
+
* whose package.json declares an Astryx package (`@astryxdesign/*`, or the
|
|
74
|
+
* legacy `@xds/*`), or that a workspace whose root declares one lists as a
|
|
75
|
+
* member. The search stops at a repository root, and it ends at the home
|
|
76
|
+
* folder without counting it. Returns null when no folder qualifies.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} startDir
|
|
79
|
+
* @returns {string|null}
|
|
80
|
+
*/
|
|
81
|
+
export function findAstryxProject(startDir: string): string | null;
|
|
82
|
+
/**
|
|
83
|
+
* The workspace root (`spec:AST-048` FR15): the nearest directory at or above
|
|
84
|
+
* `startDir` that declares a workspace, by a `pnpm-workspace.yaml` or a
|
|
85
|
+
* package.json `workspaces` field; outside a workspace, the directory of the
|
|
86
|
+
* nearest package.json; with no package.json, `startDir` itself.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} startDir
|
|
89
|
+
* @returns {string}
|
|
90
|
+
*/
|
|
91
|
+
export function findWorkspaceRoot(startDir: string): string;
|
|
92
|
+
/**
|
|
93
|
+
* The nearest `node_modules/<name>` at or above `startDir`, found the way
|
|
94
|
+
* Node's resolver finds a package, and the folder whose node_modules holds it.
|
|
95
|
+
* It searches every folder up to the filesystem root and does not ask
|
|
96
|
+
* Plug'n'Play.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} startDir
|
|
99
|
+
* @param {string} name bare package name; callers reject path segments first
|
|
100
|
+
* @returns {{packageDir: string, hostDir: string}|null}
|
|
101
|
+
*/
|
|
102
|
+
export function findNodeModulesPackage(startDir: string, name: string): {
|
|
103
|
+
packageDir: string;
|
|
104
|
+
hostDir: string;
|
|
105
|
+
} | null;
|
|
65
106
|
/**
|
|
66
107
|
* Locate a package the project at `startDir` actually installed.
|
|
67
108
|
*
|
|
@@ -73,14 +114,35 @@ export function findProjectCoreDir(startDir?: string): string | null;
|
|
|
73
114
|
* Reading package.json off disk also sidesteps packages that don't export it,
|
|
74
115
|
* so the version is always available to range-check.
|
|
75
116
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
117
|
+
* Searches the node_modules of `startDir` and the five folders above it, as
|
|
118
|
+
* released versions did, then on up to the workspace root of the Astryx
|
|
119
|
+
* project it belongs to. Then it asks PnP, which covers the Yarn default
|
|
120
|
+
* layout where that chain does not exist.
|
|
78
121
|
*
|
|
79
122
|
* @param {string} startDir
|
|
80
123
|
* @param {string} name
|
|
81
124
|
* @returns {string|null} the package directory, or null when not installed
|
|
82
125
|
*/
|
|
83
126
|
export function findInstalledPackage(startDir: string, name: string): string | null;
|
|
127
|
+
/**
|
|
128
|
+
* The first of `names` the Astryx project of `startDir` installed, for a
|
|
129
|
+
* command that writes to the project (`upgrade`).
|
|
130
|
+
*
|
|
131
|
+
* It searches the node_modules of the project root, nearest name first. For
|
|
132
|
+
* a member its workspace lists, it then searches each folder above the
|
|
133
|
+
* project up to the workspace root, where the workspace installs the packages
|
|
134
|
+
* its members share; otherwise the search stops at the project root. Then it
|
|
135
|
+
* asks Plug'n'Play, in name order. With no Astryx project it looks only in the
|
|
136
|
+
* node_modules of `startDir` itself, as released versions did.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} startDir
|
|
139
|
+
* @param {readonly string[]} names in order of preference
|
|
140
|
+
* @returns {{name: string, packageDir: string}|null}
|
|
141
|
+
*/
|
|
142
|
+
export function findProjectPackageOf(startDir: string, names: readonly string[]): {
|
|
143
|
+
name: string;
|
|
144
|
+
packageDir: string;
|
|
145
|
+
} | null;
|
|
84
146
|
/**
|
|
85
147
|
* Find the monorepo root by looking for the root package.json
|
|
86
148
|
* that has workspaces defined.
|