@astryxdesign/cli 0.7.3-canary.b74cf21 → 0.7.3-canary.cb489ff
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/api/doctor/doctor.test.mjs +28 -3
- package/api/integration/pack-check.mjs +110 -4
- package/api/integration/pack-check.test.mjs +69 -0
- package/api/integration/validate-integration-gaps.test.mjs +292 -0
- package/api/integration/validate-integration.d.mts +2 -1
- package/api/integration/validate-integration.mjs +10 -6
- package/api/integration/validate-integration.type.d.mts +1 -0
- package/api/integration/validate-integration.type.mjs +1 -0
- 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/is-my-code-good.doc.mjs +287 -0
- package/assets/docs/tree/template-grading-rubric.doc.mjs +16 -37
- 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 +56 -0
- package/foundation/discovery/template-adapter.mjs +18 -1
- package/foundation/discovery/template-needs.mjs +1 -44
- package/foundation/discovery/theme-discovery.mjs +147 -3
- package/foundation/doc-compiler/read.mjs +59 -1
- package/foundation/doc-compiler/read.test.mjs +11 -2
- 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 +20 -1
- package/foundation/integrations/integrations.mjs +59 -2
- package/foundation/integrations/validate-contributions.d.mts +19 -0
- package/foundation/integrations/validate-contributions.mjs +172 -3
- package/package.json +9 -9
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file `astryx upgrade` from a folder inside a project: a `--path` you type
|
|
5
|
+
* is read from that folder, however you spell it, and without `--path` the
|
|
6
|
+
* upgrade scans the project's own src. The option's default must not read as
|
|
7
|
+
* a typed `./src`. The `--json` result names files relative to the folder the
|
|
8
|
+
* command ran in.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {describe, it, expect, beforeEach, afterEach} from 'vitest';
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import * as os from 'node:os';
|
|
14
|
+
import * as path from 'node:path';
|
|
15
|
+
import {runCli} from '../../../test-utils/run-cli.mjs';
|
|
16
|
+
|
|
17
|
+
/** Core 0.7.0 renamed this prop; its codemod renames it on Typeahead. */
|
|
18
|
+
const OLD_PROP_SOURCE =
|
|
19
|
+
"import {Typeahead} from '@astryxdesign/core';\n" +
|
|
20
|
+
'export const App = () => <Typeahead label="Fruit" emptySearchResultsText="None" />;\n';
|
|
21
|
+
|
|
22
|
+
let root;
|
|
23
|
+
|
|
24
|
+
/** @param {string} rel @param {string} body */
|
|
25
|
+
function write(rel, body) {
|
|
26
|
+
const file = path.join(root, rel);
|
|
27
|
+
fs.mkdirSync(path.dirname(file), {recursive: true});
|
|
28
|
+
fs.writeFileSync(file, body);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @param {string} rel */
|
|
32
|
+
const renamed = rel =>
|
|
33
|
+
fs
|
|
34
|
+
.readFileSync(path.join(root, rel), 'utf-8')
|
|
35
|
+
.includes('emptySearchText="None"');
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-upgrade-subfolder-'));
|
|
39
|
+
write(
|
|
40
|
+
'package.json',
|
|
41
|
+
JSON.stringify({
|
|
42
|
+
name: 'app',
|
|
43
|
+
private: true,
|
|
44
|
+
dependencies: {'@astryxdesign/core': '0.7.0'},
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
write(
|
|
48
|
+
'node_modules/@astryxdesign/core/package.json',
|
|
49
|
+
JSON.stringify({name: '@astryxdesign/core', version: '0.7.0'}),
|
|
50
|
+
);
|
|
51
|
+
write('src/App.tsx', OLD_PROP_SOURCE);
|
|
52
|
+
// A folder inside the project with source of its own and no package.json.
|
|
53
|
+
write('frontend/src/App.tsx', OLD_PROP_SOURCE);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
fs.rmSync(root, {recursive: true, force: true});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/** @param {string[]} args @param {string} [folder] */
|
|
61
|
+
async function upgradeFrom(args, folder = 'frontend') {
|
|
62
|
+
const r = await runCli(
|
|
63
|
+
['upgrade', '--from', '0.6.8', ...args, '--apply', '--json'],
|
|
64
|
+
{
|
|
65
|
+
cwd: path.join(root, folder),
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
expect(r.status).toBe(0);
|
|
69
|
+
return JSON.parse(r.stdout);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe('upgrade --path from a folder inside the project', () => {
|
|
73
|
+
it.each([['./src'], ['src'], ['src/'], ['./src/']])(
|
|
74
|
+
'reads --path %s from the folder you ran in',
|
|
75
|
+
async spelling => {
|
|
76
|
+
const result = await upgradeFrom(['--path', spelling]);
|
|
77
|
+
|
|
78
|
+
expect(result.data.modifiedFiles).toEqual(['src/App.tsx']);
|
|
79
|
+
expect(renamed('frontend/src/App.tsx')).toBe(true);
|
|
80
|
+
expect(renamed('src/App.tsx')).toBe(false);
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
it("scans the project's src without --path", async () => {
|
|
85
|
+
const result = await upgradeFrom([]);
|
|
86
|
+
|
|
87
|
+
expect(result.data.modifiedFiles).toEqual(['../src/App.tsx']);
|
|
88
|
+
expect(renamed('src/App.tsx')).toBe(true);
|
|
89
|
+
expect(renamed('frontend/src/App.tsx')).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('names files from the project root when run there', async () => {
|
|
93
|
+
const result = await upgradeFrom([], '.');
|
|
94
|
+
|
|
95
|
+
expect(result.data.modifiedFiles).toEqual(['src/App.tsx']);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -40,7 +40,7 @@ import {parseConfig} from '../../authoring/config/parse.mjs';
|
|
|
40
40
|
import {
|
|
41
41
|
loadIntegrations,
|
|
42
42
|
loadLocalIntegration,
|
|
43
|
-
|
|
43
|
+
resolveInstalledIntegrationDir,
|
|
44
44
|
} from '../integrations/integrations.mjs';
|
|
45
45
|
import {loadAutolinkCandidates} from '../integrations/autolink.mjs';
|
|
46
46
|
import {
|
|
@@ -65,7 +65,7 @@ import {
|
|
|
65
65
|
discoverValidIntegrationComponents,
|
|
66
66
|
} from '../discovery/component-discovery.mjs';
|
|
67
67
|
import {resolveComponentReplacements} from '../discovery/component-replacement.mjs';
|
|
68
|
-
import {CLI_ROOT, findCoreDir, findProjectCoreDir} from '../fs/paths.mjs';
|
|
68
|
+
import {CLI_ROOT, findCoreDir, findPackageRoot, findProjectCoreDir} from '../fs/paths.mjs';
|
|
69
69
|
import {
|
|
70
70
|
applyTemplateReplacements,
|
|
71
71
|
effectiveTemplateDiscovery,
|
|
@@ -223,7 +223,7 @@ async function loadConfiguredIntegrations(specs, {cwd, fresh}) {
|
|
|
223
223
|
/** @type {string|undefined} */
|
|
224
224
|
let packageDir;
|
|
225
225
|
try {
|
|
226
|
-
packageDir =
|
|
226
|
+
packageDir = resolveInstalledIntegrationDir(spec, cwd);
|
|
227
227
|
} catch {
|
|
228
228
|
// Not a bare package name: there is no install location to record.
|
|
229
229
|
}
|
|
@@ -326,22 +326,6 @@ export const CONFIG_BASENAMES = [
|
|
|
326
326
|
export const DEFAULT_ISSUES_URL =
|
|
327
327
|
'https://github.com/facebook/astryx/issues/new';
|
|
328
328
|
|
|
329
|
-
/**
|
|
330
|
-
* Find the directory of the nearest package.json walking up from startDir.
|
|
331
|
-
* @param {string} startDir
|
|
332
|
-
* @returns {string|null}
|
|
333
|
-
*/
|
|
334
|
-
function findPackageRoot(startDir) {
|
|
335
|
-
let dir = startDir;
|
|
336
|
-
for (let i = 0; i < 50; i++) {
|
|
337
|
-
if (fs.existsSync(path.join(dir, 'package.json'))) return dir;
|
|
338
|
-
const parent = path.dirname(dir);
|
|
339
|
-
if (parent === dir) break;
|
|
340
|
-
dir = parent;
|
|
341
|
-
}
|
|
342
|
-
return null;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
329
|
/**
|
|
346
330
|
* Whether this project accepts `debug` handlers contributed by the
|
|
347
331
|
* integrations it loads.
|
|
@@ -506,7 +490,8 @@ export class Project {
|
|
|
506
490
|
// The project root: the config's directory when there is one (findConfigPath
|
|
507
491
|
// resolves the config as a sibling of the nearest package.json, so the two
|
|
508
492
|
// agree), otherwise that package.json's directory. Dependencies are declared
|
|
509
|
-
// there, and
|
|
493
|
+
// there, and installed packages resolve from there up the node_modules
|
|
494
|
+
// chain.
|
|
510
495
|
const projectDir = configPath
|
|
511
496
|
? path.dirname(configPath)
|
|
512
497
|
: (findPackageRoot(cwd) ?? cwd);
|
|
@@ -291,6 +291,32 @@ describe('Project.load', () => {
|
|
|
291
291
|
});
|
|
292
292
|
});
|
|
293
293
|
|
|
294
|
+
describe('Project.issues and a near-miss manifest key', () => {
|
|
295
|
+
it.each([
|
|
296
|
+
[
|
|
297
|
+
'alone, while the loaded integration fills providerId from the package name',
|
|
298
|
+
{providerID: '@acme/widgets'},
|
|
299
|
+
'Fix: rename "providerID" to "providerId" in astryx.integration.mjs.',
|
|
300
|
+
],
|
|
301
|
+
[
|
|
302
|
+
'beside the key it suggests',
|
|
303
|
+
{providerID: '@acme/widgets', providerId: '@acme/widgets'},
|
|
304
|
+
'Fix: remove "providerID" from astryx.integration.mjs; it already sets "providerId".',
|
|
305
|
+
],
|
|
306
|
+
])(
|
|
307
|
+
'reads the keys the manifest declares: providerID %s',
|
|
308
|
+
async (_label, unknownKeys, fix) => {
|
|
309
|
+
scaffold({unknownKeys});
|
|
310
|
+
const project = await Project.load(tmpDir);
|
|
311
|
+
const keys = (await project.issues()).filter(
|
|
312
|
+
issue => issue.code === 'unknown_manifest_key',
|
|
313
|
+
);
|
|
314
|
+
expect(keys).toHaveLength(1);
|
|
315
|
+
expect(keys[0].message).toContain(fix);
|
|
316
|
+
},
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
|
|
294
320
|
describe('Project.load and the cliCommand an integration declares', () => {
|
|
295
321
|
it('records it, so the picker prints commands with it', async () => {
|
|
296
322
|
scaffold({cliCommand: 'acme astryx'});
|
|
@@ -830,6 +856,36 @@ describe('Project loads each integration in isolation', () => {
|
|
|
830
856
|
});
|
|
831
857
|
});
|
|
832
858
|
|
|
859
|
+
describe('Project finds configured integrations a workspace hoists', () => {
|
|
860
|
+
// npm, Yarn and Bun workspaces install a member's dependencies at the
|
|
861
|
+
// workspace root, so the member's own node_modules does not hold them.
|
|
862
|
+
it('loads an integration named in a member config from the workspace root', async () => {
|
|
863
|
+
scaffold();
|
|
864
|
+
fs.writeFileSync(
|
|
865
|
+
path.join(tmpDir, 'package.json'),
|
|
866
|
+
JSON.stringify({name: 'root', private: true, workspaces: ['apps/*']}),
|
|
867
|
+
);
|
|
868
|
+
fs.rmSync(path.join(tmpDir, 'astryx.config.mjs'));
|
|
869
|
+
const member = path.join(tmpDir, 'apps', 'web');
|
|
870
|
+
fs.mkdirSync(member, {recursive: true});
|
|
871
|
+
fs.writeFileSync(path.join(member, 'package.json'), JSON.stringify({name: 'web'}));
|
|
872
|
+
fs.writeFileSync(
|
|
873
|
+
path.join(member, 'astryx.config.mjs'),
|
|
874
|
+
`export default ${JSON.stringify({integrations: ['@acme/widgets']})};\n`,
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
const project = await Project.load(member);
|
|
878
|
+
|
|
879
|
+
const widgets = (await project.components()).filter(
|
|
880
|
+
component => component.package === '@acme/widgets',
|
|
881
|
+
);
|
|
882
|
+
expect(widgets.map(component => component.name)).toEqual(['Widget']);
|
|
883
|
+
expect(await project.issues()).not.toContainEqual(
|
|
884
|
+
expect.objectContaining({package: '@acme/widgets', code: 'integration_error'}),
|
|
885
|
+
);
|
|
886
|
+
});
|
|
887
|
+
});
|
|
888
|
+
|
|
833
889
|
describe('Project.issuesUrl routing', () => {
|
|
834
890
|
it('routes core to config.issuesUrl, falling back to the default', async () => {
|
|
835
891
|
scaffold({issuesUrl: 'https://example.com/core'});
|
|
@@ -1192,6 +1192,23 @@ function reportSamePackageReplacements(pkg, autolinked, templates, errors) {
|
|
|
1192
1192
|
}
|
|
1193
1193
|
}
|
|
1194
1194
|
|
|
1195
|
+
/**
|
|
1196
|
+
* The fix for a template doc with no same-stem `.tsx`: rename a same-stem
|
|
1197
|
+
* source written in another extension, or add the `.tsx`.
|
|
1198
|
+
* @param {string} docPath
|
|
1199
|
+
* @param {string} sourcePath the `.tsx` it needs
|
|
1200
|
+
*/
|
|
1201
|
+
function missingTemplateSourceFix(docPath, sourcePath) {
|
|
1202
|
+
const source = path.basename(sourcePath);
|
|
1203
|
+
const stem = source.slice(0, -'.tsx'.length);
|
|
1204
|
+
const other = ['.jsx', '.js', '.ts', '.mjs']
|
|
1205
|
+
.map(extension => `${stem}${extension}`)
|
|
1206
|
+
.find(file => fs.existsSync(path.join(path.dirname(sourcePath), file)));
|
|
1207
|
+
return other
|
|
1208
|
+
? `Found ${other}, but a template source must be a .tsx file. Fix: rename ${other} to ${source}.`
|
|
1209
|
+
: `Fix: add ${source} beside ${path.basename(docPath)}, with the template as its default export.`;
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1195
1212
|
/**
|
|
1196
1213
|
* Discover the templates contributed by a SINGLE integration. Same per-template
|
|
1197
1214
|
* rules as {@link discoverIntegrationTemplates} (same-stem source required,
|
|
@@ -1225,7 +1242,7 @@ export async function discoverIntegrationTemplatesForOne(integration) {
|
|
|
1225
1242
|
errors.push({
|
|
1226
1243
|
package: pkgLabel,
|
|
1227
1244
|
template: id,
|
|
1228
|
-
message: `Template "${id}" is missing its same-stem source file ${path.basename(sourcePath)}
|
|
1245
|
+
message: `Template "${id}" is missing its same-stem source file ${path.basename(sourcePath)}. ${missingTemplateSourceFix(docPath, sourcePath)}`,
|
|
1229
1246
|
});
|
|
1230
1247
|
// A replacement this unusable template declares still counts, so its
|
|
1231
1248
|
// target fails closed instead of going to a sibling.
|
|
@@ -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
|
|
@@ -22,6 +22,7 @@ import * as path from 'node:path';
|
|
|
22
22
|
import {lowerDoc} from '../doc-compiler/compile.mjs';
|
|
23
23
|
import {packageSource} from '../doc-compiler/source.mjs';
|
|
24
24
|
import {CLI_ROOT} from '../fs/paths.mjs';
|
|
25
|
+
import {formatCliCommand} from '../env/package-manager.mjs';
|
|
25
26
|
import {LOCAL_THEME_ROOT} from '../config/theme-state.mjs';
|
|
26
27
|
import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
|
|
27
28
|
|
|
@@ -958,6 +959,126 @@ function declaresThemeDoc(ast, exportDefault) {
|
|
|
958
959
|
});
|
|
959
960
|
}
|
|
960
961
|
|
|
962
|
+
/**
|
|
963
|
+
* Whether a file is the module `astryx theme build` writes rather than a
|
|
964
|
+
* theme source: plain JavaScript that carries the build's generated header
|
|
965
|
+
* and never calls defineTheme.
|
|
966
|
+
* @param {string} file
|
|
967
|
+
*/
|
|
968
|
+
function isBuiltThemeModule(file) {
|
|
969
|
+
if (!/\.m?js$/u.test(file)) return false;
|
|
970
|
+
let text;
|
|
971
|
+
try {
|
|
972
|
+
text = fs.readFileSync(file, 'utf8');
|
|
973
|
+
} catch {
|
|
974
|
+
return false;
|
|
975
|
+
}
|
|
976
|
+
return (
|
|
977
|
+
/@generated by `astryx theme build`/u.test(text) &&
|
|
978
|
+
!/\bdefineTheme\b/u.test(text)
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/**
|
|
983
|
+
* The fix when a descriptor's stem names both a theme source and the module
|
|
984
|
+
* `astryx theme build` writes beside it (`brand.ts` and its built `brand.js`):
|
|
985
|
+
* the build names its output after the theme, so the source and descriptor
|
|
986
|
+
* take the `<name>Theme` stem instead. Null when no built module is among
|
|
987
|
+
* the matches, or the stem already ends in `Theme`.
|
|
988
|
+
* @param {string} sourceDir
|
|
989
|
+
* @param {string[]} sources same-stem module file names
|
|
990
|
+
* @param {string} doc the descriptor's file name
|
|
991
|
+
* @param {string} exportName the descriptor's stem
|
|
992
|
+
* @returns {string | null}
|
|
993
|
+
*/
|
|
994
|
+
function builtThemeSourceFix(sourceDir, sources, doc, exportName) {
|
|
995
|
+
if (exportName.endsWith('Theme')) return null;
|
|
996
|
+
const built = sources.filter(file =>
|
|
997
|
+
isBuiltThemeModule(path.join(sourceDir, file)),
|
|
998
|
+
);
|
|
999
|
+
const source = sources.filter(file => !built.includes(file));
|
|
1000
|
+
if (built.length === 0 || source.length !== 1) return null;
|
|
1001
|
+
const extension = source[0].slice(exportName.length);
|
|
1002
|
+
const stem = `${exportName}Theme`;
|
|
1003
|
+
return `Fix: ${built.map(file => `"${file}"`).join(', ')} is the module \`${formatCliCommand('theme build', sourceDir)}\` writes, which takes the theme's name; rename ${source[0]} to ${stem}${extension} and ${doc} to ${stem}${THEME_DOC_SUFFIX}, export \`${stem}\` from it, and build again.`;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* Why a descriptor pairs with the built module and the built module lacks
|
|
1008
|
+
* the export the descriptor's stem names (`ocean.doc.mjs` beside a built
|
|
1009
|
+
* `ocean.js` that exports `oceanTheme`): the descriptor is named for the
|
|
1010
|
+
* build's output, not the theme's source. The source is named only when the
|
|
1011
|
+
* built module's `Source:` header, or a source whose defineTheme gives the
|
|
1012
|
+
* theme's name, confirms it; otherwise the message stays generic. Null when
|
|
1013
|
+
* the entry is not a built module.
|
|
1014
|
+
* @param {string} label the descriptor, as messages name it
|
|
1015
|
+
* @param {string} sourceDir
|
|
1016
|
+
* @param {string} entry the file the descriptor pairs with
|
|
1017
|
+
* @param {string} exportName the descriptor's stem
|
|
1018
|
+
* @param {string} doc the descriptor's file name
|
|
1019
|
+
* @param {string} slug the theme's name
|
|
1020
|
+
* @param {string[]} files the theme folder's files
|
|
1021
|
+
* @returns {string | null}
|
|
1022
|
+
*/
|
|
1023
|
+
function builtModuleDescriptorMessage(
|
|
1024
|
+
label,
|
|
1025
|
+
sourceDir,
|
|
1026
|
+
entry,
|
|
1027
|
+
exportName,
|
|
1028
|
+
doc,
|
|
1029
|
+
slug,
|
|
1030
|
+
files,
|
|
1031
|
+
) {
|
|
1032
|
+
if (!isBuiltThemeModule(path.join(sourceDir, entry))) return null;
|
|
1033
|
+
const source = confirmedThemeSource(sourceDir, entry, slug, files);
|
|
1034
|
+
const head = `${label} shares its stem with the built module "${entry}", which does not export "${exportName}": a descriptor must share its stem with the source that calls defineTheme.`;
|
|
1035
|
+
if (source == null) {
|
|
1036
|
+
return `${head} Fix: rename ${doc} to share its stem with the theme's source (the module that calls defineTheme, such as ${exportName}Theme.ts).`;
|
|
1037
|
+
}
|
|
1038
|
+
const stem = source.replace(/\.[^.]+$/u, '');
|
|
1039
|
+
return `${head} The theme's source is "${source}". Fix: rename ${doc} to ${stem}${THEME_DOC_SUFFIX}.`;
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/**
|
|
1043
|
+
* The theme's source, when something confirms it: the file the built
|
|
1044
|
+
* module's `Source:` header names, if it is in the folder, else the one
|
|
1045
|
+
* module in the folder whose defineTheme gives the theme's name as a
|
|
1046
|
+
* literal. Null otherwise: a `<name>Theme` file alone may be a helper.
|
|
1047
|
+
* @param {string} sourceDir
|
|
1048
|
+
* @param {string} built the built module's file name
|
|
1049
|
+
* @param {string} slug
|
|
1050
|
+
* @param {string[]} files
|
|
1051
|
+
* @returns {string | null}
|
|
1052
|
+
*/
|
|
1053
|
+
function confirmedThemeSource(sourceDir, built, slug, files) {
|
|
1054
|
+
const own = files.filter(
|
|
1055
|
+
file =>
|
|
1056
|
+
!file.includes('/') &&
|
|
1057
|
+
file !== built &&
|
|
1058
|
+
THEME_MODULE_EXTENSIONS.some(extension => file.endsWith(extension)),
|
|
1059
|
+
);
|
|
1060
|
+
/** @param {string} file */
|
|
1061
|
+
const read = file => {
|
|
1062
|
+
try {
|
|
1063
|
+
return fs.readFileSync(path.join(sourceDir, file), 'utf8');
|
|
1064
|
+
} catch {
|
|
1065
|
+
return '';
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
const header = /^\s*\*?\s*Source:\s*(\S+)\s*$/mu.exec(read(built));
|
|
1069
|
+
if (header) {
|
|
1070
|
+
const named = path.posix.basename(header[1].replace(/\\/gu, '/'));
|
|
1071
|
+
if (own.includes(named)) return named;
|
|
1072
|
+
}
|
|
1073
|
+
const quoted = slug.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
|
|
1074
|
+
const naming = new RegExp(
|
|
1075
|
+
`\\bdefineTheme\\s*\\(\\s*\\{[^}]*\\bname\\s*:\\s*['"\`]${quoted}['"\`]`,
|
|
1076
|
+
'u',
|
|
1077
|
+
);
|
|
1078
|
+
const matches = own.filter(file => naming.test(read(file)));
|
|
1079
|
+
return matches.length === 1 ? matches[0] : null;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
961
1082
|
/** A module named for the theme it exports, such as `oceanTheme.ts`. */
|
|
962
1083
|
const THEME_SOURCE_RE = /^[$A-Z_a-z][$\w]*Theme\.(?:mjs|js|mts|ts|tsx|jsx)$/u;
|
|
963
1084
|
|
|
@@ -1240,8 +1361,17 @@ export function discoverThemeDirectory(
|
|
|
1240
1361
|
: evidence.includes('/')
|
|
1241
1362
|
? `; "${evidence}" is in a subfolder`
|
|
1242
1363
|
: ` beside "${evidence}"`;
|
|
1364
|
+
const themeDoc = formatCliCommand('docs authoring theme-doc', sourceDir);
|
|
1365
|
+
const fix =
|
|
1366
|
+
docs.length > 1
|
|
1367
|
+
? `Fix: keep the one descriptor that shares its stem with the theme's source and delete the rest.`
|
|
1368
|
+
: evidence.includes('/')
|
|
1369
|
+
? `Fix: move "${evidence}" up into ${slug}/, beside where its descriptor goes.`
|
|
1370
|
+
: THEME_SOURCE_RE.test(evidence)
|
|
1371
|
+
? `Fix: add ${evidence.replace(/\.[^.]+$/u, '')}${THEME_DOC_SUFFIX} beside it with type: 'theme' and name: '${slug}'; \`${themeDoc}\` lists its fields.`
|
|
1372
|
+
: `Fix: add a descriptor that shares its stem with the theme's source, with type: 'theme' and name: '${slug}'; \`${themeDoc}\` lists its fields.`;
|
|
1243
1373
|
throw new Error(
|
|
1244
|
-
`${theme} must contain exactly one same-stem .doc.mjs descriptor; found ${docs.length}${detail}
|
|
1374
|
+
`${theme} must contain exactly one same-stem .doc.mjs descriptor; found ${docs.length}${detail}. ${fix}`,
|
|
1245
1375
|
);
|
|
1246
1376
|
}
|
|
1247
1377
|
const nested = descriptors.find(file => file.includes('/'));
|
|
@@ -1267,8 +1397,13 @@ export function discoverThemeDirectory(
|
|
|
1267
1397
|
extension => `${exportName}${extension}`,
|
|
1268
1398
|
).filter(file => listing.files.includes(file));
|
|
1269
1399
|
if (sources.length !== 1) {
|
|
1400
|
+
const fix =
|
|
1401
|
+
sources.length > 1
|
|
1402
|
+
? (builtThemeSourceFix(sourceDir, sources, docs[0], exportName) ??
|
|
1403
|
+
`Fix: keep one of them; the theme's source is a single module.`)
|
|
1404
|
+
: `Fix: add ${exportName}.ts beside ${docs[0]}, exporting \`${exportName}\` from defineTheme.`;
|
|
1270
1405
|
throw new Error(
|
|
1271
|
-
`${theme} must contain exactly one same-stem source for ${docs[0]}; found ${sources.length}${sources.length > 1 ? `: ${quoted(sources)}` : ''}
|
|
1406
|
+
`${theme} must contain exactly one same-stem source for ${docs[0]}; found ${sources.length}${sources.length > 1 ? `: ${quoted(sources)}` : ''}. ${fix}`,
|
|
1272
1407
|
);
|
|
1273
1408
|
}
|
|
1274
1409
|
|
|
@@ -1314,7 +1449,16 @@ export function discoverThemeDirectory(
|
|
|
1314
1449
|
)
|
|
1315
1450
|
) {
|
|
1316
1451
|
throw new ThemeRuntimeExportError(
|
|
1317
|
-
|
|
1452
|
+
builtModuleDescriptorMessage(
|
|
1453
|
+
label,
|
|
1454
|
+
sourceDir,
|
|
1455
|
+
entry,
|
|
1456
|
+
exportName,
|
|
1457
|
+
docs[0],
|
|
1458
|
+
slug,
|
|
1459
|
+
listing.files,
|
|
1460
|
+
) ??
|
|
1461
|
+
`Theme "${slug}" for ${owner} entry "${entry}" does not export "${exportName}".`,
|
|
1318
1462
|
);
|
|
1319
1463
|
}
|
|
1320
1464
|
} catch (error) {
|
|
@@ -173,8 +173,23 @@ export async function readDocView(file, options) {
|
|
|
173
173
|
);
|
|
174
174
|
if (result.loadFailed) throw result.loadFailure;
|
|
175
175
|
if (result.missing) {
|
|
176
|
-
if (strict)
|
|
176
|
+
if (strict) {
|
|
177
|
+
// A doc that exports nothing its reader takes (an empty file, or one
|
|
178
|
+
// with other exports) is said plainly, with what it does export and
|
|
179
|
+
// the fix; the schema would only say "(root): Invalid input".
|
|
180
|
+
const exported =
|
|
181
|
+
options.exports ??
|
|
182
|
+
/** @type {Record<string, readonly string[]>} */ (DOC_EXPORTS)[
|
|
183
|
+
options.root
|
|
184
|
+
] ??
|
|
185
|
+
[];
|
|
186
|
+
const problem =
|
|
187
|
+
result.missingValue === undefined
|
|
188
|
+
? await missingDocProblem(file, options, exported)
|
|
189
|
+
: null;
|
|
190
|
+
if (problem) throw new Error(`${options.label ?? file} ${problem}`);
|
|
177
191
|
parserFor(options.root)(result.missingValue, options.label ?? file);
|
|
192
|
+
}
|
|
178
193
|
return result.missingValue;
|
|
179
194
|
}
|
|
180
195
|
if (strict && result.failed) throw result.failure;
|
|
@@ -182,6 +197,49 @@ export async function readDocView(file, options) {
|
|
|
182
197
|
return result.view;
|
|
183
198
|
}
|
|
184
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Why a doc file exports no doc, with the fix: it is empty, or it exports
|
|
202
|
+
* other names than the ones its reader takes. Null when neither explains it.
|
|
203
|
+
* @param {string} file
|
|
204
|
+
* @param {ReadOptions} options
|
|
205
|
+
* @param {readonly string[]} exported the export names the reader takes
|
|
206
|
+
* @returns {Promise<string | null>}
|
|
207
|
+
*/
|
|
208
|
+
async function missingDocProblem(file, options, exported) {
|
|
209
|
+
// A theme descriptor is read statically, never imported.
|
|
210
|
+
if (options.root === 'themes') return null;
|
|
211
|
+
const fix =
|
|
212
|
+
'Fix: export the doc object as the default export: `export default {type: ..., name: ..., ...}`.';
|
|
213
|
+
let text;
|
|
214
|
+
try {
|
|
215
|
+
text = fs.readFileSync(file, 'utf8');
|
|
216
|
+
} catch {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const code = text
|
|
220
|
+
.replace(/^\uFEFF/u, '')
|
|
221
|
+
.replace(/\/\*[\s\S]*?\*\//gu, '')
|
|
222
|
+
.replace(/(^|[^:])\/\/.*$/gmu, '$1')
|
|
223
|
+
.trim();
|
|
224
|
+
if (code === '') return `is empty, so it documents nothing. ${fix}`;
|
|
225
|
+
/** @type {string[]} */
|
|
226
|
+
let names;
|
|
227
|
+
try {
|
|
228
|
+
const mod = await LOADERS[options.loader ?? 'user'](file);
|
|
229
|
+
names = Object.keys(mod ?? {}).filter(name => name !== 'default');
|
|
230
|
+
} catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
const takes = exported.map(name =>
|
|
234
|
+
name === 'default' ? 'default export' : `\`${name}\` export`,
|
|
235
|
+
);
|
|
236
|
+
const found =
|
|
237
|
+
names.length > 0
|
|
238
|
+
? ` (it exports ${names.map(name => `\`${name}\``).join(', ')})`
|
|
239
|
+
: ' (it exports nothing)';
|
|
240
|
+
return `has no ${takes.join(' or ')}${found}, so it documents nothing. ${fix}`;
|
|
241
|
+
}
|
|
242
|
+
|
|
185
243
|
/**
|
|
186
244
|
* Load one file as its root reads it: the doc its module exports and, for a
|
|
187
245
|
* component or hook, the translation it exports for `lang`.
|
|
@@ -109,10 +109,19 @@ describe('readDocView', () => {
|
|
|
109
109
|
}
|
|
110
110
|
});
|
|
111
111
|
|
|
112
|
-
it(
|
|
112
|
+
it('reads a file with no doc as undefined, or, checked, says what it exports instead', async () => {
|
|
113
113
|
const file = write('Empty.doc.mjs', 'export const unrelated = 1;\n');
|
|
114
114
|
await expect(loadDocs(file)).resolves.toBeUndefined();
|
|
115
|
-
await expect(loadComponentDoc(file)).rejects.toThrow(
|
|
115
|
+
await expect(loadComponentDoc(file)).rejects.toThrow(
|
|
116
|
+
`${file} has no default export or \`docs\` export (it exports \`unrelated\`), so it documents nothing. Fix: export the doc object as the default export`,
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('says a checked doc file is empty', async () => {
|
|
121
|
+
const file = write('Blank.doc.mjs', '// nothing yet\n');
|
|
122
|
+
await expect(loadComponentDoc(file)).rejects.toThrow(
|
|
123
|
+
`${file} is empty, so it documents nothing.`,
|
|
124
|
+
);
|
|
116
125
|
});
|
|
117
126
|
|
|
118
127
|
it('applies the translation a module exports for the reading language', async () => {
|