@astryxdesign/cli 0.7.2-canary.621784f → 0.7.2-canary.64e1dde
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 +12 -12
- package/api/discover/_adapter.d.mts +119 -12
- package/api/discover/_adapter.mjs +449 -10
- package/api/discover/_adapter.test.mjs +599 -17
- package/api/discover/_catalog-view.d.mts +16 -5
- package/api/discover/_catalog-view.mjs +32 -8
- package/api/discover/discover.d.mts +11 -0
- package/api/discover/discover.doc.mjs +7 -6
- package/api/discover/discover.mjs +107 -19
- package/api/discover/discover.type.d.mts +18 -7
- package/api/discover/discover.type.mjs +15 -7
- package/api/discover/list/list.d.mts +4 -3
- package/api/discover/list/list.mjs +17 -10
- package/api/doctor/doctor.mjs +27 -3
- package/api/doctor/doctor.test.mjs +43 -0
- package/api/doctor/theme-checks.test.mjs +136 -0
- package/api/gap-report/gap-report.d.mts +3 -0
- package/api/gap-report/gap-report.doc.mjs +11 -3
- package/api/gap-report/gap-report.mjs +152 -48
- package/api/gap-report/gap-report.test.mjs +233 -0
- package/api/gap-report/gap-report.type.d.mts +41 -0
- package/api/gap-report/gap-report.type.mjs +18 -0
- package/api/integration/_adapter.d.mts +10 -0
- package/api/integration/_adapter.mjs +13 -0
- package/api/integration/authoring-checks.mjs +14 -3
- package/api/integration/authoring-checks.test.mjs +326 -0
- package/api/integration/authoring-checks.type.d.mts +50 -0
- package/api/integration/authoring-checks.type.mjs +21 -1
- package/api/integration/integrationComponentConflicts.doc.mjs +9 -3
- package/api/integration/integrationDocConflicts.doc.mjs +17 -5
- package/api/integration/pack-check.mjs +292 -7
- package/api/integration/pack-check.test.mjs +341 -0
- package/api/integration/validateIntegration.doc.mjs +3 -1
- package/api/search/search.d.mts +72 -12
- package/api/search/search.domain-priority.test.mjs +108 -18
- package/api/search/search.mjs +501 -37
- package/api/search/search.test.mjs +139 -0
- package/api/theme/build/build.family.test.mjs +54 -6
- package/api/theme/build/build.mjs +110 -13
- package/api/theme/themeBuild.doc.mjs +4 -4
- package/api/upgrade/_adapter.d.mts +40 -2
- package/api/upgrade/_adapter.mjs +117 -4
- package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
- package/api/upgrade/run/run.mjs +30 -1
- package/api/upgrade/status/status.d.mts +21 -5
- package/api/upgrade/status/status.mjs +45 -8
- package/api/upgrade/upgrade.doc.mjs +5 -3
- package/api/upgrade/upgrade.type.d.mts +23 -0
- package/api/upgrade/upgrade.type.mjs +16 -1
- package/assets/codemods/runner.mjs +7 -1
- package/assets/discover/official-packages.json +367 -0
- package/assets/docs/author-a-theme.doc.mjs +2 -2
- package/assets/docs/styling-overview.doc.mjs +1 -1
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/add-a-component.doc.mjs +1 -1
- package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
- package/assets/docs/tree/add-a-topic.doc.mjs +11 -8
- package/assets/docs/tree/agent-guidance.doc.mjs +2 -2
- package/assets/docs/tree/block-template.doc.mjs +6 -18
- package/assets/docs/tree/build-the-template.doc.mjs +1 -1
- package/assets/docs/tree/check-the-props.doc.mjs +2 -2
- package/assets/docs/tree/check-your-docs.doc.mjs +16 -7
- package/assets/docs/tree/checks.doc.mjs +7 -7
- package/assets/docs/tree/codemods.doc.mjs +4 -4
- package/assets/docs/tree/component-family.doc.mjs +3 -5
- package/assets/docs/tree/component-imports.doc.mjs +2 -2
- package/assets/docs/tree/components.doc.mjs +1 -1
- package/assets/docs/tree/debug-and-gap-reports.doc.mjs +25 -16
- package/assets/docs/tree/define-the-theme.doc.mjs +6 -5
- package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
- package/assets/docs/tree/docs.doc.mjs +3 -3
- package/assets/docs/tree/document-the-template.doc.mjs +1 -1
- package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
- package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
- package/assets/docs/tree/extend-or-replace.doc.mjs +6 -6
- package/assets/docs/tree/fonts-and-assets.doc.mjs +2 -2
- package/assets/docs/tree/generate-a-palette.doc.mjs +5 -3
- package/assets/docs/tree/grade-template-with-agent.doc.mjs +1 -1
- package/assets/docs/tree/integrations.doc.mjs +8 -0
- package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
- package/assets/docs/tree/layout.doc.mjs +6 -6
- package/assets/docs/tree/links.doc.mjs +5 -4
- package/assets/docs/tree/migration-setup.doc.mjs +3 -2
- package/assets/docs/tree/migration-verification.doc.mjs +0 -1
- package/assets/docs/tree/migration.doc.mjs +0 -1
- package/assets/docs/tree/package-and-test.doc.mjs +1 -1
- package/assets/docs/tree/page-template.doc.mjs +3 -7
- package/assets/docs/tree/publishing.doc.mjs +2 -2
- package/assets/docs/tree/quick-start.doc.mjs +37 -21
- package/assets/docs/tree/replace-a-core-component.doc.mjs +4 -6
- package/assets/docs/tree/replace-a-core-template.doc.mjs +5 -7
- package/assets/docs/tree/sections-and-placement.doc.mjs +4 -6
- package/assets/docs/tree/see-it-in-an-app.doc.mjs +4 -2
- package/assets/docs/tree/short-and-findable.doc.mjs +6 -5
- package/assets/docs/tree/single-component.doc.mjs +12 -4
- package/assets/docs/tree/start-a-template.doc.mjs +3 -3
- package/assets/docs/tree/subcomponent.doc.mjs +6 -6
- package/assets/docs/tree/template-assets.doc.mjs +2 -2
- package/assets/docs/tree/template-doc-overview.doc.mjs +5 -16
- package/assets/docs/tree/template-fonts.doc.mjs +1 -1
- package/assets/docs/tree/template-icons.doc.mjs +1 -1
- package/assets/docs/tree/template-images-media.doc.mjs +1 -1
- package/assets/docs/tree/template-styles.doc.mjs +2 -2
- package/assets/docs/tree/templates.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +3 -3
- package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
- package/assets/docs/tree/tokens-and-setup.doc.mjs +19 -15
- package/assets/docs/tree/troubleshooting.doc.mjs +8 -8
- package/assets/docs/tree/upgrading.doc.mjs +4 -4
- package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +3 -1
- package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
- package/assets/docs/tree/versioning.doc.mjs +6 -6
- package/assets/docs/tree/write-the-template-file.doc.mjs +2 -2
- package/authoring/codemod/codemod.doc.mjs +7 -3
- package/authoring/config/config.doc.mjs +9 -6
- package/authoring/config/type.ts +4 -2
- package/authoring/discover/discover.doc.mjs +1 -1
- package/authoring/doctypes/base/graph-fields.doc.mjs +6 -4
- package/authoring/doctypes/command/command.doc.mjs +14 -12
- package/authoring/doctypes/component/component.doc.mjs +29 -26
- package/authoring/doctypes/component/type.ts +7 -5
- package/authoring/doctypes/enum/enum.doc.mjs +7 -4
- package/authoring/doctypes/function/function.doc.mjs +9 -8
- package/authoring/doctypes/hook/hook.doc.mjs +5 -4
- package/authoring/doctypes/namespace/namespace.doc.mjs +7 -6
- package/authoring/doctypes/reference/reference.doc.mjs +11 -10
- package/authoring/doctypes/schema/schema.doc.mjs +9 -9
- package/authoring/doctypes/template/template.doc.mjs +2 -2
- package/authoring/doctypes/theme/theme.doc.mjs +2 -2
- package/authoring/gap-report/gap-report.doc.mjs +2 -2
- package/authoring/identity/identity.doc.mjs +3 -3
- package/authoring/integration/integration.doc.mjs +4 -3
- package/clients/cli/commands/build-theme.path-safety.test.mjs +155 -21
- package/clients/cli/commands/cli-postinstall.test.mjs +49 -1
- package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
- package/clients/cli/commands/discover.doc.mjs +1 -1
- package/clients/cli/commands/discover.mjs +144 -93
- package/clients/cli/commands/discover.no-source.test.mjs +447 -33
- package/clients/cli/commands/discover.sources.test.mjs +20 -1
- package/clients/cli/commands/doctor-integration-components.doc.mjs +3 -1
- package/clients/cli/commands/doctor-integration-docs.doc.mjs +17 -5
- package/clients/cli/commands/doctor-integration-validate.doc.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +34 -3
- package/clients/cli/commands/gap-report.doc.mjs +14 -2
- package/clients/cli/commands/gap-report.mjs +24 -2
- package/clients/cli/commands/gap-report.test.mjs +79 -0
- package/clients/cli/commands/integration-check.test.mjs +65 -0
- package/clients/cli/commands/integration.mjs +2 -3
- package/clients/cli/commands/json-contract.test.mjs +5 -1
- package/clients/cli/commands/setup-nudge.test.mjs +79 -1
- package/clients/cli/commands/swizzle.mjs +5 -3
- package/clients/cli/commands/swizzle.routing.test.mjs +18 -1
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/upgrade.doc.mjs +14 -0
- package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
- package/clients/cli/index.mjs +29 -5
- package/foundation/agent-docs/agent-doc-state.d.mts +14 -0
- package/foundation/agent-docs/agent-doc-state.mjs +38 -0
- package/foundation/agent-docs/agent-docs.d.mts +2 -1
- package/foundation/agent-docs/agent-docs.mjs +8 -6
- package/foundation/discovery/authoring-self-docs.mjs +1 -1
- package/foundation/discovery/copied-passages.d.mts +208 -0
- package/foundation/discovery/copied-passages.mjs +720 -0
- package/foundation/discovery/copied-passages.test.mjs +124 -0
- package/foundation/discovery/doc-types.d.mts +1 -1
- package/foundation/discovery/doc-types.mjs +3 -3
- package/foundation/discovery/doc-types.test.mjs +48 -0
- package/foundation/discovery/docs-discovery.d.mts +7 -0
- package/foundation/discovery/docs-discovery.mjs +26 -2
- package/foundation/discovery/project-typescript.d.mts +11 -2
- package/foundation/discovery/project-typescript.mjs +32 -4
- package/foundation/discovery/prop-drift.d.mts +1 -1
- package/foundation/discovery/prop-drift.mjs +2 -2
- package/foundation/discovery/prop-drift.test.mjs +55 -2
- package/foundation/discovery/type-text.mjs +1 -1
- package/foundation/discovery/type-text.test.mjs +1 -1
- package/foundation/doc-compiler/doc-loads.test.mjs +10 -2
- package/foundation/env/package-manager.d.mts +32 -0
- package/foundation/env/package-manager.mjs +47 -0
- package/foundation/env/package-manager.test.mjs +194 -7
- package/foundation/fs/module-loader.d.mts +2 -1
- package/foundation/fs/module-loader.mjs +60 -4
- package/foundation/fs/module-loader.test.mjs +194 -0
- package/foundation/response/response-types.doc.mjs +7 -6
- package/package.json +11 -11
- package/scripts/postinstall.mjs +3 -3
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {describe, expect, it} from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
PASSAGE_WORDS,
|
|
7
|
+
contentWords,
|
|
8
|
+
passageWords,
|
|
9
|
+
wordSpans,
|
|
10
|
+
proseWords,
|
|
11
|
+
sharedPassages,
|
|
12
|
+
} from './copied-passages.mjs';
|
|
13
|
+
|
|
14
|
+
const words = (/** @type {string} */ text) =>
|
|
15
|
+
passageWords(text).map(word => ({word, section: null, block: 1}));
|
|
16
|
+
|
|
17
|
+
describe('passageWords', () => {
|
|
18
|
+
it('sets aside case, links, and Markdown emphasis', () => {
|
|
19
|
+
expect(
|
|
20
|
+
passageWords('Use **`Button`** for {@link component:Button} actions.'),
|
|
21
|
+
).toEqual(['use', 'button', 'for', 'actions.']);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('proseWords', () => {
|
|
26
|
+
it('reads prose with its section, and skips code, references, and names', () => {
|
|
27
|
+
const doc = {
|
|
28
|
+
type: 'generic',
|
|
29
|
+
name: 'acme-guide',
|
|
30
|
+
title: 'Acme guide',
|
|
31
|
+
description: 'Intro words.',
|
|
32
|
+
sections: [
|
|
33
|
+
{
|
|
34
|
+
title: 'Setup',
|
|
35
|
+
content: [
|
|
36
|
+
{type: 'prose', text: 'Install it.'},
|
|
37
|
+
{type: 'code', lang: 'sh', code: 'npm install acme'},
|
|
38
|
+
{type: 'reference', ref: 'generic:other'},
|
|
39
|
+
{type: 'list', items: ['First step.']},
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
expect(proseWords(doc)).toEqual([
|
|
45
|
+
{word: 'intro', section: null, block: 1},
|
|
46
|
+
{word: 'words.', section: null, block: 1},
|
|
47
|
+
{word: 'install', section: 'Setup', block: 2},
|
|
48
|
+
{word: 'it.', section: 'Setup', block: 2},
|
|
49
|
+
{word: 'first', section: 'Setup', block: 3},
|
|
50
|
+
{word: 'step.', section: 'Setup', block: 3},
|
|
51
|
+
]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('sharedPassages', () => {
|
|
56
|
+
const passage = Array.from(
|
|
57
|
+
{length: PASSAGE_WORDS + 5},
|
|
58
|
+
(_, i) => `w${i}`,
|
|
59
|
+
).join(' ');
|
|
60
|
+
|
|
61
|
+
it('finds the longest shared run', () => {
|
|
62
|
+
expect(
|
|
63
|
+
sharedPassages(words(`a b ${passage} c`), words(`x ${passage} y`)),
|
|
64
|
+
).toEqual([{start: 2, length: PASSAGE_WORDS + 5}]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('never runs a passage from one string into the next', () => {
|
|
68
|
+
const half = Math.ceil(PASSAGE_WORDS / 2);
|
|
69
|
+
const first = passage.split(' ').slice(0, half).join(' ');
|
|
70
|
+
const second = passage.split(' ').slice(half, PASSAGE_WORDS).join(' ');
|
|
71
|
+
const split = [
|
|
72
|
+
...passageWords(first).map(word => ({word, section: null, block: 1})),
|
|
73
|
+
...passageWords(second).map(word => ({word, section: null, block: 2})),
|
|
74
|
+
];
|
|
75
|
+
expect(sharedPassages(split, words(`${first} ${second}`))).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('ignores a run shorter than the passage size', () => {
|
|
79
|
+
const short = passage
|
|
80
|
+
.split(' ')
|
|
81
|
+
.slice(0, PASSAGE_WORDS - 1)
|
|
82
|
+
.join(' ');
|
|
83
|
+
expect(
|
|
84
|
+
sharedPassages(words(`a ${short} b`), words(`c ${short} d`)),
|
|
85
|
+
).toEqual([]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('contentWords', () => {
|
|
90
|
+
it('counts distinct words that are not function words', () => {
|
|
91
|
+
const run = words('and it is in the way that you can do it for them');
|
|
92
|
+
expect(contentWords(run, 0, run.length)).toBe(1);
|
|
93
|
+
const fact = words('Name each codemod folder after the Core version.');
|
|
94
|
+
expect(contentWords(fact, 0, fact.length)).toBe(5);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('wordSpans', () => {
|
|
99
|
+
it('reads the words passageWords reads, with where each sits as written', () => {
|
|
100
|
+
const text = 'Run `astryx docs`, then {@link generic:theme} *now*.';
|
|
101
|
+
const spans = wordSpans(text);
|
|
102
|
+
expect(spans.map(span => span.word)).toEqual(passageWords(text));
|
|
103
|
+
expect(text.slice(spans[1].start, spans[2].end)).toBe('`astryx docs`');
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('passageWords and pointers', () => {
|
|
108
|
+
it('reads a link and a written docs command as no words', () => {
|
|
109
|
+
expect(
|
|
110
|
+
passageWords(
|
|
111
|
+
'See {@link generic:theme} or `astryx docs theme` (Share it).',
|
|
112
|
+
),
|
|
113
|
+
).toEqual(['see', 'or', 'share', 'it']);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('counts no punctuation-only token as a word', () => {
|
|
117
|
+
expect(
|
|
118
|
+
passageWords('Set the theme first, see {@link generic:theme}.'),
|
|
119
|
+
).toEqual(['set', 'the', 'theme', 'first', 'see']);
|
|
120
|
+
expect(
|
|
121
|
+
wordSpans('Read it - then {@link generic:theme}.').map(s => s.word),
|
|
122
|
+
).toEqual(['read', 'it', 'then']);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -54,7 +54,7 @@ export function checkDocTypes(files: DocFile[], options: {
|
|
|
54
54
|
* or not fully checked
|
|
55
55
|
*/
|
|
56
56
|
/** The line both surfaces print when the compiler is missing. */
|
|
57
|
-
export const DOC_TYPES_TYPESCRIPT_MISSING_REASON: "TypeScript is not installed in this project, so docs were not checked against the authoring types.
|
|
57
|
+
export const DOC_TYPES_TYPESCRIPT_MISSING_REASON: "TypeScript is not installed in this project, so docs were not checked against the authoring types. On TypeScript 7, add its TypeScript 6 API package (`npm install -D @typescript/typescript6`); otherwise install TypeScript 5.0 through 6 (`npm install -D typescript@6`). Then run this check again.";
|
|
58
58
|
export type DocRoot = "components" | "hooks" | "templates" | "docs" | "docs-overlay" | "themes";
|
|
59
59
|
export type DocFile = {
|
|
60
60
|
/**
|
|
@@ -41,6 +41,7 @@ import * as path from 'node:path';
|
|
|
41
41
|
import {fileURLToPath} from 'node:url';
|
|
42
42
|
import {
|
|
43
43
|
resolveProjectTypeScript,
|
|
44
|
+
TYPESCRIPT_FIX,
|
|
44
45
|
unusableTypeScriptReason,
|
|
45
46
|
} from './project-typescript.mjs';
|
|
46
47
|
|
|
@@ -91,8 +92,7 @@ import {
|
|
|
91
92
|
*/
|
|
92
93
|
|
|
93
94
|
/** The line both surfaces print when the compiler is missing. */
|
|
94
|
-
export const DOC_TYPES_TYPESCRIPT_MISSING_REASON =
|
|
95
|
-
'TypeScript is not installed in this project, so docs were not checked against the authoring types. Install typescript and run this check again.';
|
|
95
|
+
export const DOC_TYPES_TYPESCRIPT_MISSING_REASON = `TypeScript is not installed in this project, so docs were not checked against the authoring types. ${TYPESCRIPT_FIX}`;
|
|
96
96
|
|
|
97
97
|
const AUTHORING = '@astryxdesign/cli/authoring';
|
|
98
98
|
const AUTHORING_TYPES = path.resolve(
|
|
@@ -933,7 +933,7 @@ export async function checkDocTypes(files, options) {
|
|
|
933
933
|
if (major < needMajor || (major === needMajor && minor < needMinor)) {
|
|
934
934
|
return {
|
|
935
935
|
status: 'skipped',
|
|
936
|
-
reason: `TypeScript ${version} cannot check ${hasJs ? 'a JavaScript doc' : 'a doc'} against its type (that needs TypeScript ${needMajor}.${needMinor} or later), so docs were not checked.
|
|
936
|
+
reason: `TypeScript ${version} cannot check ${hasJs ? 'a JavaScript doc' : 'a doc'} against its type (that needs TypeScript ${needMajor}.${needMinor} or later), so docs were not checked. ${TYPESCRIPT_FIX}`,
|
|
937
937
|
docs: 0,
|
|
938
938
|
findings: [],
|
|
939
939
|
unchecked: [],
|
|
@@ -40,6 +40,37 @@ function withTypeScript() {
|
|
|
40
40
|
);
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* TypeScript 7 (no JavaScript compiler API) with the TypeScript 6 API package
|
|
45
|
+
* beside it, which re-exports a real TypeScript.
|
|
46
|
+
*/
|
|
47
|
+
function withTypeScript7AndItsTypeScript6Api() {
|
|
48
|
+
const ts7 = path.join(pkg, 'node_modules', 'typescript');
|
|
49
|
+
fs.mkdirSync(ts7, {recursive: true});
|
|
50
|
+
fs.writeFileSync(
|
|
51
|
+
path.join(ts7, 'package.json'),
|
|
52
|
+
JSON.stringify({
|
|
53
|
+
name: 'typescript',
|
|
54
|
+
version: '7.0.2',
|
|
55
|
+
exports: {'./package.json': './package.json'},
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
const ts6 = path.join(pkg, 'node_modules', '@typescript', 'typescript6');
|
|
59
|
+
fs.mkdirSync(ts6, {recursive: true});
|
|
60
|
+
fs.writeFileSync(
|
|
61
|
+
path.join(ts6, 'package.json'),
|
|
62
|
+
JSON.stringify({
|
|
63
|
+
name: '@typescript/typescript6',
|
|
64
|
+
version: '6.0.2',
|
|
65
|
+
main: 'index.js',
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(ts6, 'index.js'),
|
|
70
|
+
`module.exports = require(${JSON.stringify(TYPESCRIPT_DIR)});\n`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
43
74
|
/**
|
|
44
75
|
* @param {string} rel
|
|
45
76
|
* @param {string} text
|
|
@@ -117,6 +148,10 @@ describe('checkDocTypes', () => {
|
|
|
117
148
|
|
|
118
149
|
const report = await check([['components/Badge.doc.mjs', 'components']]);
|
|
119
150
|
|
|
151
|
+
// The reason names the version and the fix, so a --strict failure
|
|
152
|
+
// says what to install.
|
|
153
|
+
expect(report.reason).toContain('TypeScript 7.0.0');
|
|
154
|
+
expect(report.reason).toContain('npm install -D @typescript/typescript6');
|
|
120
155
|
expect(report).toEqual({
|
|
121
156
|
status: 'skipped',
|
|
122
157
|
reason: unusableTypeScriptReason('7.0.0', 'docs were not checked'),
|
|
@@ -128,6 +163,19 @@ describe('checkDocTypes', () => {
|
|
|
128
163
|
}
|
|
129
164
|
});
|
|
130
165
|
|
|
166
|
+
it("checks with TypeScript 7's TypeScript 6 API package when the project has one", async () => {
|
|
167
|
+
write(
|
|
168
|
+
'components/Badge.doc.mjs',
|
|
169
|
+
`export default ${JSON.stringify(COMPONENT)};\n`,
|
|
170
|
+
);
|
|
171
|
+
withTypeScript7AndItsTypeScript6Api();
|
|
172
|
+
|
|
173
|
+
const report = await check([['components/Badge.doc.mjs', 'components']]);
|
|
174
|
+
|
|
175
|
+
expect(report.status).toBe('checked');
|
|
176
|
+
expect(report.docs).toBe(1);
|
|
177
|
+
});
|
|
178
|
+
|
|
131
179
|
it('says there is nothing to check when the package has no doc files, with or without TypeScript', async () => {
|
|
132
180
|
const without = await check([]);
|
|
133
181
|
withTypeScript();
|
|
@@ -39,6 +39,13 @@
|
|
|
39
39
|
* @returns {Record<string, string>} topic name → absolute doc path
|
|
40
40
|
*/
|
|
41
41
|
export function discoverBuiltinTopics(): Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* The CLI's own guide files: every flat topic and every doc in its docs tree,
|
|
44
|
+
* without the dense, translated, and draft variants.
|
|
45
|
+
*
|
|
46
|
+
* @returns {string[]}
|
|
47
|
+
*/
|
|
48
|
+
export function builtinGuideFiles(): string[];
|
|
42
49
|
/**
|
|
43
50
|
* Load a topic doc from disk. A `.ts` doc is loaded through jiti, the rest
|
|
44
51
|
* natively; both the historical `export const docs` and the stamped
|
|
@@ -117,6 +117,23 @@ export function discoverBuiltinTopics() {
|
|
|
117
117
|
return topics;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/**
|
|
121
|
+
* The CLI's own guide files: every flat topic and every doc in its docs tree,
|
|
122
|
+
* without the dense, translated, and draft variants.
|
|
123
|
+
*
|
|
124
|
+
* @returns {string[]}
|
|
125
|
+
*/
|
|
126
|
+
export function builtinGuideFiles() {
|
|
127
|
+
const files = Object.values(discoverBuiltinTopics());
|
|
128
|
+
const tree = path.join(BUILTIN_DOCS_DIR, 'tree');
|
|
129
|
+
if (fs.existsSync(tree)) {
|
|
130
|
+
for (const file of fs.readdirSync(tree).sort()) {
|
|
131
|
+
if (BUILTIN_TOPIC_FILE_RE.test(file)) files.push(path.join(tree, file));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return files;
|
|
135
|
+
}
|
|
136
|
+
|
|
120
137
|
/**
|
|
121
138
|
* Load a topic doc from disk. A `.ts` doc is loaded through jiti, the rest
|
|
122
139
|
* natively; both the historical `export const docs` and the stamped
|
|
@@ -384,7 +401,11 @@ export function problemsInNamespace(doc) {
|
|
|
384
401
|
}
|
|
385
402
|
}
|
|
386
403
|
const slots = doc?.slots;
|
|
387
|
-
if (
|
|
404
|
+
if (
|
|
405
|
+
slots == null ||
|
|
406
|
+
typeof slots !== 'object' ||
|
|
407
|
+
Object.keys(slots).length === 0
|
|
408
|
+
) {
|
|
388
409
|
problems.push('slots: expected at least one slot');
|
|
389
410
|
return problems;
|
|
390
411
|
}
|
|
@@ -392,7 +413,10 @@ export function problemsInNamespace(doc) {
|
|
|
392
413
|
if (typeof slot?.title !== 'string' || slot.title === '') {
|
|
393
414
|
problems.push(`slots.${name}.title: expected a non-empty string`);
|
|
394
415
|
}
|
|
395
|
-
if (
|
|
416
|
+
if (
|
|
417
|
+
!Array.isArray(slot?.accepts?.kinds) ||
|
|
418
|
+
slot.accepts.kinds.length === 0
|
|
419
|
+
) {
|
|
396
420
|
problems.push(`slots.${name}.accepts.kinds: expected at least one kind`);
|
|
397
421
|
}
|
|
398
422
|
}
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Resolve the project's own `typescript`, trying each directory in order
|
|
5
|
+
* Resolve the project's own `typescript`, trying each directory in order;
|
|
6
|
+
* where it has no JavaScript compiler API (TypeScript 7), the TypeScript 6
|
|
7
|
+
* API package installed beside it.
|
|
6
8
|
* @param {Array<string | null | undefined>} dirs
|
|
7
9
|
* @returns {{ts: any, version: string} | {ts: null, version: string} | null}
|
|
8
10
|
* `ts: null` when a TypeScript is installed but has no `createProgram`
|
|
@@ -16,8 +18,15 @@ export function resolveProjectTypeScript(dirs: Array<string | null | undefined>)
|
|
|
16
18
|
} | null;
|
|
17
19
|
/**
|
|
18
20
|
* Why a check that needs the compiler did not run, when the TypeScript
|
|
19
|
-
* installed has no JavaScript compiler API.
|
|
21
|
+
* installed has no JavaScript compiler API, and what to do.
|
|
20
22
|
* @param {string} version
|
|
21
23
|
* @param {string} outcome what was not done, such as `docs were not checked`
|
|
22
24
|
*/
|
|
23
25
|
export function unusableTypeScriptReason(version: string, outcome: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* What to do when the project's TypeScript is missing or cannot be used:
|
|
28
|
+
* beside TypeScript 7, the TypeScript 6 API package it ships for tools;
|
|
29
|
+
* otherwise a version every check here reads, 5.0 (the oldest that checks a
|
|
30
|
+
* JavaScript doc) through 6.
|
|
31
|
+
*/
|
|
32
|
+
export const TYPESCRIPT_FIX: "On TypeScript 7, add its TypeScript 6 API package (`npm install -D @typescript/typescript6`); otherwise install TypeScript 5.0 through 6 (`npm install -D typescript@6`). Then run this check again.";
|
|
@@ -15,7 +15,16 @@ import * as path from 'node:path';
|
|
|
15
15
|
import {createRequire} from 'node:module';
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* The package TypeScript publishes beside TypeScript 7 for tools that need a
|
|
19
|
+
* JavaScript compiler API: TypeScript 6's. Kept in a constant so the type
|
|
20
|
+
* check does not treat the load as a static import of an uninstalled package.
|
|
21
|
+
*/
|
|
22
|
+
const TYPESCRIPT_6_API = '@typescript/typescript6';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolve the project's own `typescript`, trying each directory in order;
|
|
26
|
+
* where it has no JavaScript compiler API (TypeScript 7), the TypeScript 6
|
|
27
|
+
* API package installed beside it.
|
|
19
28
|
* @param {Array<string | null | undefined>} dirs
|
|
20
29
|
* @returns {{ts: any, version: string} | {ts: null, version: string} | null}
|
|
21
30
|
* `ts: null` when a TypeScript is installed but has no `createProgram`
|
|
@@ -43,19 +52,38 @@ export function resolveProjectTypeScript(dirs) {
|
|
|
43
52
|
unusable ??= installed();
|
|
44
53
|
} catch {
|
|
45
54
|
// Not installed from here, or installed without an entry this CLI can
|
|
46
|
-
// load
|
|
55
|
+
// load.
|
|
47
56
|
unusable ??= installed();
|
|
48
57
|
}
|
|
58
|
+
// TypeScript 7 has no JavaScript compiler API; the package TypeScript
|
|
59
|
+
// ships beside it for tools that need one carries TypeScript 6's.
|
|
60
|
+
try {
|
|
61
|
+
const ts = require(TYPESCRIPT_6_API);
|
|
62
|
+
if (ts && typeof ts.createProgram === 'function') {
|
|
63
|
+
return {ts, version: String(ts.version)};
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
// Not installed from here; try the next directory.
|
|
67
|
+
}
|
|
49
68
|
}
|
|
50
69
|
return unusable ? {ts: null, version: unusable} : null;
|
|
51
70
|
}
|
|
52
71
|
|
|
72
|
+
/**
|
|
73
|
+
* What to do when the project's TypeScript is missing or cannot be used:
|
|
74
|
+
* beside TypeScript 7, the TypeScript 6 API package it ships for tools;
|
|
75
|
+
* otherwise a version every check here reads, 5.0 (the oldest that checks a
|
|
76
|
+
* JavaScript doc) through 6.
|
|
77
|
+
*/
|
|
78
|
+
export const TYPESCRIPT_FIX =
|
|
79
|
+
'On TypeScript 7, add its TypeScript 6 API package (`npm install -D @typescript/typescript6`); otherwise install TypeScript 5.0 through 6 (`npm install -D typescript@6`). Then run this check again.';
|
|
80
|
+
|
|
53
81
|
/**
|
|
54
82
|
* Why a check that needs the compiler did not run, when the TypeScript
|
|
55
|
-
* installed has no JavaScript compiler API.
|
|
83
|
+
* installed has no JavaScript compiler API, and what to do.
|
|
56
84
|
* @param {string} version
|
|
57
85
|
* @param {string} outcome what was not done, such as `docs were not checked`
|
|
58
86
|
*/
|
|
59
87
|
export function unusableTypeScriptReason(version, outcome) {
|
|
60
|
-
return `TypeScript ${version} is installed in this project, but it has no JavaScript compiler API (\`createProgram\`) to read types with, so ${outcome}
|
|
88
|
+
return `TypeScript ${version} is installed in this project, but it has no JavaScript compiler API (\`createProgram\`) to read types with (TypeScript 7 ships none), so ${outcome}. ${TYPESCRIPT_FIX}`;
|
|
61
89
|
}
|
|
@@ -96,7 +96,7 @@ export function comparePropDocs(components: PropDriftComponent[], options: {
|
|
|
96
96
|
*/
|
|
97
97
|
export const PROPS_NOT_DOCUMENTED: readonly string[];
|
|
98
98
|
/** The line both surfaces print when the compiler is missing. */
|
|
99
|
-
export const TYPESCRIPT_MISSING_REASON: "TypeScript is not installed in this project, so documented props were not compared with the code.
|
|
99
|
+
export const TYPESCRIPT_MISSING_REASON: "TypeScript is not installed in this project, so documented props were not compared with the code. On TypeScript 7, add its TypeScript 6 API package (`npm install -D @typescript/typescript6`); otherwise install TypeScript 5.0 through 6 (`npm install -D typescript@6`). Then run this check again.";
|
|
100
100
|
export type CodeProp = {
|
|
101
101
|
name: string;
|
|
102
102
|
/**
|
|
@@ -47,6 +47,7 @@ import {loadComponentDoc} from './component-loader.mjs';
|
|
|
47
47
|
import {readPackageTsconfig} from './package-tsconfig.mjs';
|
|
48
48
|
import {
|
|
49
49
|
resolveProjectTypeScript,
|
|
50
|
+
TYPESCRIPT_FIX,
|
|
50
51
|
unusableTypeScriptReason,
|
|
51
52
|
} from './project-typescript.mjs';
|
|
52
53
|
import {judgeType} from './type-match.mjs';
|
|
@@ -158,8 +159,7 @@ const outside = (name, code) =>
|
|
|
158
159
|
const HOOK_NAME = /^use[A-Z0-9]/;
|
|
159
160
|
|
|
160
161
|
/** The line both surfaces print when the compiler is missing. */
|
|
161
|
-
export const TYPESCRIPT_MISSING_REASON =
|
|
162
|
-
'TypeScript is not installed in this project, so documented props were not compared with the code. Install typescript and run this check again.';
|
|
162
|
+
export const TYPESCRIPT_MISSING_REASON = `TypeScript is not installed in this project, so documented props were not compared with the code. ${TYPESCRIPT_FIX}`;
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
165
|
* @template T
|
|
@@ -58,6 +58,37 @@ function withTypeScript() {
|
|
|
58
58
|
);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* TypeScript 7 (no JavaScript compiler API) with the TypeScript 6 API package
|
|
63
|
+
* beside it, which re-exports a real TypeScript.
|
|
64
|
+
*/
|
|
65
|
+
function withTypeScript7AndItsTypeScript6Api() {
|
|
66
|
+
const ts7 = path.join(pkg, 'node_modules', 'typescript');
|
|
67
|
+
fs.mkdirSync(ts7, {recursive: true});
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(ts7, 'package.json'),
|
|
70
|
+
JSON.stringify({
|
|
71
|
+
name: 'typescript',
|
|
72
|
+
version: '7.0.2',
|
|
73
|
+
exports: {'./package.json': './package.json'},
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
const ts6 = path.join(pkg, 'node_modules', '@typescript', 'typescript6');
|
|
77
|
+
fs.mkdirSync(ts6, {recursive: true});
|
|
78
|
+
fs.writeFileSync(
|
|
79
|
+
path.join(ts6, 'package.json'),
|
|
80
|
+
JSON.stringify({
|
|
81
|
+
name: '@typescript/typescript6',
|
|
82
|
+
version: '6.0.2',
|
|
83
|
+
main: 'index.js',
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
fs.writeFileSync(
|
|
87
|
+
path.join(ts6, 'index.js'),
|
|
88
|
+
`module.exports = require(${JSON.stringify(TYPESCRIPT_DIR)});\n`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
61
92
|
/**
|
|
62
93
|
* Write `components/<name>.tsx` and its doc; returns its prop table, which
|
|
63
94
|
* also carries the component record (name, docPath, sourcePath).
|
|
@@ -176,7 +207,8 @@ describe('comparePropTables', () => {
|
|
|
176
207
|
expect(await compare([component('Badge', source, doc)])).toMatchObject({
|
|
177
208
|
findings: [],
|
|
178
209
|
typesCompared: 0,
|
|
179
|
-
typesSkipped:
|
|
210
|
+
typesSkipped:
|
|
211
|
+
'TypeScript 4.7.4 is older than 4.8, the oldest the type comparison reads; install TypeScript 5.0 through 6 (`npm install -D typescript@6`), or on TypeScript 7 add `@typescript/typescript6`, to judge them',
|
|
180
212
|
});
|
|
181
213
|
});
|
|
182
214
|
|
|
@@ -2179,6 +2211,23 @@ export const Hoc = withName((props: {label?: string}) => null);`;
|
|
|
2179
2211
|
]);
|
|
2180
2212
|
});
|
|
2181
2213
|
|
|
2214
|
+
it("compares with TypeScript 7's TypeScript 6 API package when the project has one", async () => {
|
|
2215
|
+
const badge = component(
|
|
2216
|
+
'Badge',
|
|
2217
|
+
'export function Badge(props: {label: string}) { return null; }\n',
|
|
2218
|
+
{props: [prop('label', 'number')]},
|
|
2219
|
+
);
|
|
2220
|
+
withTypeScript7AndItsTypeScript6Api();
|
|
2221
|
+
|
|
2222
|
+
const report = await compare([badge]);
|
|
2223
|
+
|
|
2224
|
+
expect(report.status).toBe('checked');
|
|
2225
|
+
// The comparison ran: the documented `number` is judged against `string`.
|
|
2226
|
+
expect(
|
|
2227
|
+
report.findings.map(f => `${f.component}.${f.prop} ${f.kind}`),
|
|
2228
|
+
).toContain('Badge.label type_mismatch');
|
|
2229
|
+
});
|
|
2230
|
+
|
|
2182
2231
|
it('skips with a reason, and compares nothing, without a TypeScript it can use', async () => {
|
|
2183
2232
|
const badge = component(
|
|
2184
2233
|
'Badge',
|
|
@@ -2196,6 +2245,7 @@ export const Hoc = withName((props: {label?: string}) => null);`;
|
|
|
2196
2245
|
});
|
|
2197
2246
|
|
|
2198
2247
|
expect(await compare([badge])).toEqual(skipped(TYPESCRIPT_MISSING_REASON));
|
|
2248
|
+
expect(TYPESCRIPT_MISSING_REASON).toContain('npm install -D typescript@6');
|
|
2199
2249
|
// TypeScript 7 has no JavaScript compiler API to load.
|
|
2200
2250
|
const dir = path.join(pkg, 'node_modules', 'typescript');
|
|
2201
2251
|
fs.mkdirSync(dir, {recursive: true});
|
|
@@ -2207,7 +2257,10 @@ export const Hoc = withName((props: {label?: string}) => null);`;
|
|
|
2207
2257
|
exports: {'./package.json': './package.json'},
|
|
2208
2258
|
}),
|
|
2209
2259
|
);
|
|
2210
|
-
|
|
2260
|
+
const report = await compare([badge]);
|
|
2261
|
+
expect(report.reason).toContain('TypeScript 7.0.0');
|
|
2262
|
+
expect(report.reason).toContain('npm install -D @typescript/typescript6');
|
|
2263
|
+
expect(report).toEqual(
|
|
2211
2264
|
skipped(
|
|
2212
2265
|
unusableTypeScriptReason(
|
|
2213
2266
|
'7.0.0',
|
|
@@ -46,7 +46,7 @@ export function typeTextTooOld(ts) {
|
|
|
46
46
|
const [major, minor] = String(ts.version).split('.').map(Number);
|
|
47
47
|
return major > 4 || (major === 4 && minor >= 8)
|
|
48
48
|
? null
|
|
49
|
-
: `TypeScript ${ts.version} is older than 4.8, the oldest the type comparison reads`;
|
|
49
|
+
: `TypeScript ${ts.version} is older than 4.8, the oldest the type comparison reads; install TypeScript 5.0 through 6 (\`npm install -D typescript@6\`), or on TypeScript 7 add \`@typescript/typescript6\`, to judge them`;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/** How tightly a printed type binds: a slot needs at least its level. */
|
|
@@ -176,7 +176,7 @@ describe('normalizeTypeText', () => {
|
|
|
176
176
|
'string[]',
|
|
177
177
|
]);
|
|
178
178
|
expect(typeTextTooOld({version: '4.7.4'})).toBe(
|
|
179
|
-
'TypeScript 4.7.4 is older than 4.8, the oldest the type comparison reads',
|
|
179
|
+
'TypeScript 4.7.4 is older than 4.8, the oldest the type comparison reads; install TypeScript 5.0 through 6 (`npm install -D typescript@6`), or on TypeScript 7 add `@typescript/typescript6`, to judge them',
|
|
180
180
|
);
|
|
181
181
|
for (const version of ['4.8.4', '5.0.4', '6.0.3', ts.version]) {
|
|
182
182
|
expect(typeTextTooOld({version})).toBeNull();
|
|
@@ -252,13 +252,21 @@ const RUNNERS = {
|
|
|
252
252
|
runs: '@babel/parser, on first use, to read the members a family doc lists in its components without running the doc',
|
|
253
253
|
sites: ['require(@babel/parser)'],
|
|
254
254
|
},
|
|
255
|
+
'foundation/discovery/copied-passages.mjs': {
|
|
256
|
+
runs: "the CLI's own guides and the package's topics, to compare their prose for passages two topics repeat",
|
|
257
|
+
sites: ['builtinGuideFiles', 'loadTopicModule'],
|
|
258
|
+
},
|
|
255
259
|
'foundation/discovery/docs-discovery.mjs': {
|
|
256
260
|
runs: 'a contributed topic, for its catalog fields: name, title, description, category, and relationships',
|
|
257
261
|
sites: ['importDocModule'],
|
|
258
262
|
},
|
|
259
263
|
'foundation/discovery/project-typescript.mjs': {
|
|
260
|
-
runs: "the project's own typescript, for the author checks that read source and docs with it, and its package.json for the version of one they cannot use",
|
|
261
|
-
sites: [
|
|
264
|
+
runs: "the project's own typescript, for the author checks that read source and docs with it, and its package.json for the version of one they cannot use; beside TypeScript 7, which has no compiler API, the TypeScript 6 API package @typescript/typescript6",
|
|
265
|
+
sites: [
|
|
266
|
+
'require(<computed>)',
|
|
267
|
+
'require(typescript)',
|
|
268
|
+
'require(typescript/package.json)',
|
|
269
|
+
],
|
|
262
270
|
},
|
|
263
271
|
'foundation/discovery/template-adapter.mjs': {
|
|
264
272
|
runs: '@babel/parser, to find the fixture references and page-local bindings in template source; and a template doc that does not validate, to read the replacement it declares',
|
|
@@ -157,6 +157,38 @@ export function getCliInvocation(targetDir?: string): string;
|
|
|
157
157
|
* @returns {string}
|
|
158
158
|
*/
|
|
159
159
|
export function formatCliCommand(command: string, targetDir?: string): string;
|
|
160
|
+
/**
|
|
161
|
+
* Quote one argument of a printed command so the shell it is pasted into
|
|
162
|
+
* passes it on unchanged. Printed commands target a POSIX shell (sh, bash,
|
|
163
|
+
* zsh), and PowerShell on Windows; cmd.exe is not a target. A value made only
|
|
164
|
+
* of letters, digits and `@ / . _ -` (and, on Windows, a drive path's `:` and
|
|
165
|
+
* `\`) stays bare. Any other value gets single quotes, inside which both
|
|
166
|
+
* shells read `$`, backticks, `%`, `!`, spaces and double quotes literally. An
|
|
167
|
+
* embedded single quote is written `'\''` for a POSIX shell and doubled for
|
|
168
|
+
* PowerShell, which also ends a single-quoted string at the typographic
|
|
169
|
+
* quotes `‘ ’ ‚ ‛`, so those are doubled too (`Macy’s` → `'Macy’’s'`).
|
|
170
|
+
*
|
|
171
|
+
* @param {string} value
|
|
172
|
+
* @param {string} [platform] the `process.platform` the command is printed for
|
|
173
|
+
* @returns {string}
|
|
174
|
+
*/
|
|
175
|
+
export function quoteCommandArgument(value: string, platform?: string): string;
|
|
176
|
+
/**
|
|
177
|
+
* A printed command that changes to `dir`, then runs `command` there, so it
|
|
178
|
+
* does the same work pasted from any folder. It runs `command` only when the
|
|
179
|
+
* change worked. For a POSIX shell: `cd <dir> && <command>`. For PowerShell
|
|
180
|
+
* on Windows: `cd -LiteralPath <dir> -ErrorAction Stop; <command>`, because
|
|
181
|
+
* `-LiteralPath` reads `[` and `]` in a folder name literally, and a failed
|
|
182
|
+
* change stops the line in Windows PowerShell 5.1, which has no `&&`, as in
|
|
183
|
+
* PowerShell 7.
|
|
184
|
+
*
|
|
185
|
+
* @param {string} dir an absolute folder
|
|
186
|
+
* @param {string} command the command to run there, its arguments already
|
|
187
|
+
* quoted with {@link quoteCommandArgument}
|
|
188
|
+
* @param {string} [platform] the `process.platform` the command is printed for
|
|
189
|
+
* @returns {string}
|
|
190
|
+
*/
|
|
191
|
+
export function commandInFolder(dir: string, command: string, platform?: string): string;
|
|
160
192
|
/** The published CLI package name — used for one-off (uninstalled) invocations. */
|
|
161
193
|
export const CLI_PACKAGE: "@astryxdesign/cli";
|
|
162
194
|
/** The CLI binary name — only resolves once the CLI is installed (or run via CLI_PACKAGE). */
|
|
@@ -512,3 +512,50 @@ export function formatCliCommand(command, targetDir) {
|
|
|
512
512
|
const stem = getCliInvocation(targetDir);
|
|
513
513
|
return sub ? `${stem} ${sub}` : stem;
|
|
514
514
|
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Quote one argument of a printed command so the shell it is pasted into
|
|
518
|
+
* passes it on unchanged. Printed commands target a POSIX shell (sh, bash,
|
|
519
|
+
* zsh), and PowerShell on Windows; cmd.exe is not a target. A value made only
|
|
520
|
+
* of letters, digits and `@ / . _ -` (and, on Windows, a drive path's `:` and
|
|
521
|
+
* `\`) stays bare. Any other value gets single quotes, inside which both
|
|
522
|
+
* shells read `$`, backticks, `%`, `!`, spaces and double quotes literally. An
|
|
523
|
+
* embedded single quote is written `'\''` for a POSIX shell and doubled for
|
|
524
|
+
* PowerShell, which also ends a single-quoted string at the typographic
|
|
525
|
+
* quotes `‘ ’ ‚ ‛`, so those are doubled too (`Macy’s` → `'Macy’’s'`).
|
|
526
|
+
*
|
|
527
|
+
* @param {string} value
|
|
528
|
+
* @param {string} [platform] the `process.platform` the command is printed for
|
|
529
|
+
* @returns {string}
|
|
530
|
+
*/
|
|
531
|
+
export function quoteCommandArgument(value, platform = process.platform) {
|
|
532
|
+
const text = String(value);
|
|
533
|
+
if (platform === 'win32') {
|
|
534
|
+
if (/^[A-Za-z0-9@/._:\\-]+$/u.test(text)) return text;
|
|
535
|
+
return `'${text.replace(/['\u2018\u2019\u201A\u201B]/gu, quote => quote + quote)}'`;
|
|
536
|
+
}
|
|
537
|
+
if (/^[A-Za-z0-9@/._-]+$/u.test(text)) return text;
|
|
538
|
+
return `'${text.replace(/'/gu, `'\\''`)}'`;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* A printed command that changes to `dir`, then runs `command` there, so it
|
|
543
|
+
* does the same work pasted from any folder. It runs `command` only when the
|
|
544
|
+
* change worked. For a POSIX shell: `cd <dir> && <command>`. For PowerShell
|
|
545
|
+
* on Windows: `cd -LiteralPath <dir> -ErrorAction Stop; <command>`, because
|
|
546
|
+
* `-LiteralPath` reads `[` and `]` in a folder name literally, and a failed
|
|
547
|
+
* change stops the line in Windows PowerShell 5.1, which has no `&&`, as in
|
|
548
|
+
* PowerShell 7.
|
|
549
|
+
*
|
|
550
|
+
* @param {string} dir an absolute folder
|
|
551
|
+
* @param {string} command the command to run there, its arguments already
|
|
552
|
+
* quoted with {@link quoteCommandArgument}
|
|
553
|
+
* @param {string} [platform] the `process.platform` the command is printed for
|
|
554
|
+
* @returns {string}
|
|
555
|
+
*/
|
|
556
|
+
export function commandInFolder(dir, command, platform = process.platform) {
|
|
557
|
+
const folder = quoteCommandArgument(dir, platform);
|
|
558
|
+
return platform === 'win32'
|
|
559
|
+
? `cd -LiteralPath ${folder} -ErrorAction Stop; ${command}`
|
|
560
|
+
: `cd ${folder} && ${command}`;
|
|
561
|
+
}
|