@sixbell-telco/sdk-tools 0.0.0 → 0.1.1
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/CHANGELOG.md +38 -0
- package/README.md +277 -2
- package/data/security-floors.json +47 -0
- package/package.json +40 -4
- package/recipes/README.md +53 -0
- package/recipes/crud-list.md +21 -0
- package/recipes/custom-component.md +35 -0
- package/recipes/detail-page.md +19 -0
- package/recipes/form-dialog.md +17 -0
- package/recipes/parts/crud-list/i18n.json +83 -0
- package/recipes/parts/crud-list/src/app/features/__name__/__name__.routes.ts.tpl +30 -0
- package/recipes/parts/crud-list/src/app/features/__name__/pages/__name__-list/__name__-list.component.html.tpl +102 -0
- package/recipes/parts/crud-list/src/app/features/__name__/pages/__name__-list/__name__-list.component.spec.ts.tpl +101 -0
- package/recipes/parts/crud-list/src/app/features/__name__/pages/__name__-list/__name__-list.component.ts.tpl +143 -0
- package/recipes/parts/detail-page/i18n.json +32 -0
- package/recipes/parts/detail-page/src/app/features/__name__/pages/__name__-detail/__name__-detail.component.html.tpl +35 -0
- package/recipes/parts/detail-page/src/app/features/__name__/pages/__name__-detail/__name__-detail.component.spec.ts.tpl +58 -0
- package/recipes/parts/detail-page/src/app/features/__name__/pages/__name__-detail/__name__-detail.component.ts.tpl +68 -0
- package/recipes/parts/entity/i18n.json +5 -0
- package/recipes/parts/entity/src/app/features/__name__/__name__.model.ts.tpl +9 -0
- package/recipes/parts/entity/src/app/features/__name__/__name__.service.spec.ts.tpl +29 -0
- package/recipes/parts/entity/src/app/features/__name__/__name__.service.ts.tpl +33 -0
- package/recipes/parts/form-dialog/i18n.json +35 -0
- package/recipes/parts/form-dialog/src/app/features/__name__/components/__name__-form-dialog/__name__-form-dialog.component.html.tpl +33 -0
- package/recipes/parts/form-dialog/src/app/features/__name__/components/__name__-form-dialog/__name__-form-dialog.component.spec.ts.tpl +69 -0
- package/recipes/parts/form-dialog/src/app/features/__name__/components/__name__-form-dialog/__name__-form-dialog.component.ts.tpl +73 -0
- package/src/add/add.mjs +251 -0
- package/src/check/config.mjs +112 -0
- package/src/check/exceptions.mjs +103 -0
- package/src/check/federation.mjs +224 -0
- package/src/check/finding.mjs +23 -0
- package/src/check/harness.mjs +146 -0
- package/src/check/i18n.mjs +218 -0
- package/src/check/integration.mjs +566 -0
- package/src/check/security.mjs +165 -0
- package/src/check/steps.mjs +37 -0
- package/src/check/styles.mjs +347 -0
- package/src/check/wiring.mjs +198 -0
- package/src/check.mjs +333 -0
- package/src/cli.mjs +551 -0
- package/src/docs/docs.mjs +154 -0
- package/src/doctor/doctor.mjs +492 -0
- package/src/format.mjs +102 -0
- package/src/info/info.mjs +149 -0
- package/src/init/init.mjs +490 -0
- package/src/init/merge.mjs +191 -0
- package/src/init/template.mjs +292 -0
- package/src/mcp/catalog.mjs +248 -0
- package/src/mcp/glossary.mjs +75 -0
- package/src/mcp/manifest.mjs +53 -0
- package/src/mcp/recipes.mjs +198 -0
- package/src/mcp/server.mjs +249 -0
- package/src/mcp/upgrade-guide.mjs +116 -0
- package/src/mcp/usage.mjs +48 -0
- package/src/project/project.mjs +220 -0
- package/src/types.d.ts +448 -0
- package/src/upgrade/lockstep.mjs +153 -0
- package/src/upgrade/upgrade.mjs +623 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdk-tools docs`: a component's API from the installed SDK manifest, or a recipe, in the terminal.
|
|
3
|
+
* The answers are the MCP's own (get_component, list_components, get_recipe): this module only
|
|
4
|
+
* picks which one and prints it.
|
|
5
|
+
*/
|
|
6
|
+
import { getComponent, listComponents, NotFoundError } from '../mcp/catalog.mjs';
|
|
7
|
+
import { loadManifest } from '../mcp/manifest.mjs';
|
|
8
|
+
import { findRecipe, loadRecipes } from '../mcp/recipes.mjs';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import('../types.js').DocsResult} DocsResult
|
|
12
|
+
* @typedef {import('../types.js').ComponentDetail} ComponentDetail
|
|
13
|
+
* @typedef {import('../types.js').Recipe} Recipe
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Edit distance, for near-match suggestions.
|
|
18
|
+
* @param {string} a
|
|
19
|
+
* @param {string} b
|
|
20
|
+
*/
|
|
21
|
+
function distance(a, b) {
|
|
22
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
23
|
+
for (let i = 1; i <= a.length; i++) {
|
|
24
|
+
let previous = row[0];
|
|
25
|
+
row[0] = i;
|
|
26
|
+
for (let j = 1; j <= b.length; j++) {
|
|
27
|
+
const current = row[j];
|
|
28
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, previous + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
29
|
+
previous = current;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return row[b.length];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The names closest to `query` (by edit distance, or sharing its words), at most five.
|
|
37
|
+
* @param {string} query
|
|
38
|
+
* @param {string[]} names
|
|
39
|
+
* @returns {string[]}
|
|
40
|
+
*/
|
|
41
|
+
export function nearest(query, names) {
|
|
42
|
+
const q = query
|
|
43
|
+
.trim()
|
|
44
|
+
.toLowerCase()
|
|
45
|
+
.replace(/^<|\/?>$/g, '');
|
|
46
|
+
const bare = q.replace(/^st-/, '');
|
|
47
|
+
return names
|
|
48
|
+
.map((name) => {
|
|
49
|
+
const n = name.toLowerCase();
|
|
50
|
+
const contains = bare.length >= 3 && n.includes(bare);
|
|
51
|
+
return { name, score: contains ? 1 : Math.min(distance(q, n), distance(bare, n.replace(/^st-/, ''))) };
|
|
52
|
+
})
|
|
53
|
+
.filter((entry) => entry.score <= Math.max(2, Math.floor(bare.length / 3)))
|
|
54
|
+
.sort((a, b) => a.score - b.score || a.name.localeCompare(b.name))
|
|
55
|
+
.slice(0, 5)
|
|
56
|
+
.map((entry) => entry.name);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Looks up `query` as a recipe id, then as a component; with no query, lists both.
|
|
61
|
+
* @param {{ cwd?: string, query?: string, manifestPath?: string }} [options]
|
|
62
|
+
* @returns {DocsResult}
|
|
63
|
+
*/
|
|
64
|
+
export function docs({ cwd = process.cwd(), query, manifestPath } = {}) {
|
|
65
|
+
const recipes = loadRecipes();
|
|
66
|
+
const recipeList = recipes.map((r) => ({ id: r.id, title: r.title, summary: r.summary }));
|
|
67
|
+
if (!query) {
|
|
68
|
+
const { manifest } = loadManifest({ cwd, manifestPath });
|
|
69
|
+
const { sdkVersion, components } = listComponents(manifest);
|
|
70
|
+
return { kind: 'list', sdkVersion, components, recipes: recipeList };
|
|
71
|
+
}
|
|
72
|
+
const id = query.trim().toLowerCase();
|
|
73
|
+
if (recipes.some((r) => r.id === id)) return { kind: 'recipe', recipe: findRecipe(recipes, id) };
|
|
74
|
+
const { manifest } = loadManifest({ cwd, manifestPath });
|
|
75
|
+
try {
|
|
76
|
+
return { kind: 'component', component: getComponent(manifest, query) };
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (!(error instanceof NotFoundError)) throw error;
|
|
79
|
+
const names = [...manifest.declarations.map((d) => d.selector?.split(',')[0].trim() ?? d.pipe ?? d.name), ...recipes.map((r) => r.id)];
|
|
80
|
+
return { kind: 'not-found', query, sdkVersion: manifest.version, suggestions: nearest(query, names) };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {ComponentDetail} c
|
|
86
|
+
* @returns {string[]}
|
|
87
|
+
*/
|
|
88
|
+
function componentLines(c) {
|
|
89
|
+
const lines = [`${c.selector} ${c.name} (${c.kind}), @sixbell-telco/sdk ${c.sdkVersion}`, c.import];
|
|
90
|
+
if (c.deprecated) lines.push(`Deprecated${typeof c.deprecated === 'string' ? `: ${c.deprecated}` : ''}`);
|
|
91
|
+
if (c.description) lines.push('', c.description);
|
|
92
|
+
lines.push('', 'Inputs:');
|
|
93
|
+
if (!c.inputs.length) lines.push(' none');
|
|
94
|
+
for (const i of c.inputs) {
|
|
95
|
+
const facts = [
|
|
96
|
+
i.required ? 'required' : '',
|
|
97
|
+
i.default !== undefined ? `default ${i.default}` : '',
|
|
98
|
+
i.twoWay ? 'two-way' : '',
|
|
99
|
+
i.deprecated ? 'deprecated' : '',
|
|
100
|
+
].filter(Boolean);
|
|
101
|
+
lines.push(
|
|
102
|
+
` ${i.name}: ${i.values?.length ? i.values.map((v) => `'${v}'`).join(' | ') : i.type}${facts.length ? ` (${facts.join(', ')})` : ''}`,
|
|
103
|
+
);
|
|
104
|
+
if (i.description) lines.push(` ${i.description.split('\n')[0]}`);
|
|
105
|
+
}
|
|
106
|
+
lines.push('', 'Outputs:');
|
|
107
|
+
if (!c.outputs.length) lines.push(' none');
|
|
108
|
+
for (const o of c.outputs) lines.push(` ${o.name}: ${o.type}${o.deprecated ? ' (deprecated)' : ''}`);
|
|
109
|
+
if (c.slots.length) lines.push('', `Slots: ${c.slots.join(', ')}`);
|
|
110
|
+
if (c.related.length) lines.push(`Related: ${c.related.join(', ')}`);
|
|
111
|
+
if (c.example) lines.push('', 'Example:', ...c.example.split('\n').map((line) => ` ${line}`));
|
|
112
|
+
if (c.stories.length) lines.push('', 'Stories:', ...c.stories.map((s) => ` ${s.url}`));
|
|
113
|
+
return lines;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param {Recipe} r
|
|
118
|
+
* @returns {string[]}
|
|
119
|
+
*/
|
|
120
|
+
function recipeLines(r) {
|
|
121
|
+
const lines = [`${r.title} (recipe ${r.id})`, r.summary, '', r.body];
|
|
122
|
+
if (r.files.length) lines.push('', `sdk-tools add ${r.id} <name> writes:`, ...r.files.map((f) => ` ${f.path}`));
|
|
123
|
+
return lines;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* @param {DocsResult} result
|
|
128
|
+
* @returns {string}
|
|
129
|
+
*/
|
|
130
|
+
export function formatDocs(result) {
|
|
131
|
+
switch (result.kind) {
|
|
132
|
+
case 'component':
|
|
133
|
+
return componentLines(result.component).join('\n');
|
|
134
|
+
case 'recipe':
|
|
135
|
+
return recipeLines(result.recipe).join('\n');
|
|
136
|
+
case 'not-found':
|
|
137
|
+
return [
|
|
138
|
+
`No component or recipe "${result.query}" in @sixbell-telco/sdk ${result.sdkVersion}.`,
|
|
139
|
+
result.suggestions.length ? `Did you mean: ${result.suggestions.join(', ')}?` : 'Run sdk-tools docs to list them.',
|
|
140
|
+
].join('\n');
|
|
141
|
+
default: {
|
|
142
|
+
const width = Math.max(...result.components.map((c) => c.selector.length), 8);
|
|
143
|
+
return [
|
|
144
|
+
`Components (@sixbell-telco/sdk ${result.sdkVersion}):`,
|
|
145
|
+
...result.components.map((c) => ` ${c.selector.padEnd(width)} ${c.description ?? ''}`.trimEnd()),
|
|
146
|
+
'',
|
|
147
|
+
'Recipes:',
|
|
148
|
+
...result.recipes.map((r) => ` ${r.id.padEnd(width)} ${r.summary}`),
|
|
149
|
+
'',
|
|
150
|
+
'Run sdk-tools docs <selector|recipe>.',
|
|
151
|
+
].join('\n');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `sdk-tools doctor`: the environment and the drift `check` does not judge. Node and npm against
|
|
3
|
+
* package.json engines, the files every MFE keeps (DESIGN.md, AGENTS.md, the product config and its
|
|
4
|
+
* layout block), agent tooling tracked in git, a starter placeholder left beside real features, and
|
|
5
|
+
* the template files the app lacks. Ids keep their mfe-tools names; each finding carries its fix.
|
|
6
|
+
*
|
|
7
|
+
* Template drift is judged against the role's template as `sdk-tools init` renders it: a local
|
|
8
|
+
* checkout found the way init finds one (--template, --template-root, SDK_TOOLS_TEMPLATE_ROOT, the
|
|
9
|
+
* folders above the app), never fetched. A file is drift only when that template has it.
|
|
10
|
+
*/
|
|
11
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
12
|
+
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
13
|
+
import { join, resolve } from 'node:path';
|
|
14
|
+
import { declaredRange } from '../info/info.mjs';
|
|
15
|
+
import { resolveTemplate, templateFiles } from '../init/template.mjs';
|
|
16
|
+
import { readJson, readProject, readText } from '../project/project.mjs';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {import('../types.js').DoctorFinding} DoctorFinding
|
|
20
|
+
* @typedef {import('../types.js').DoctorReport} DoctorReport
|
|
21
|
+
* @typedef {import('../types.js').ProjectInfo} ProjectInfo
|
|
22
|
+
* @typedef {import('../types.js').Severity} Severity
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const SDK = '@sixbell-telco/sdk';
|
|
26
|
+
const FOUNDATION_CONFIG = 'mfe-foundation.config.json';
|
|
27
|
+
const CHILD_INTERCEPTOR = 'src/app/core/interceptors/error/child-error.interceptor.ts';
|
|
28
|
+
const CHILD_ERROR_POLICY = 'src/app/core/interceptors/error/http-error-policy.util.ts';
|
|
29
|
+
/** Agent tooling is installed per checkout and never committed (the skills lock included). */
|
|
30
|
+
const AGENT_LOCAL_PATHS = ['.agents', '.claude', '.opencode', '.cursor', 'skills.lock.yaml', 'skills-lock.json'];
|
|
31
|
+
/** Starter features the templates ship to be replaced by the first real feature. */
|
|
32
|
+
const PLACEHOLDER_FEATURES = new Set(['host', 'microfrontend', 'example']);
|
|
33
|
+
/** Template features that stay: the status pages. */
|
|
34
|
+
const STATUS_FEATURES = new Set(['no-access', 'not-found', 'offline', 'server-error']);
|
|
35
|
+
|
|
36
|
+
/** Template files an MFE keeps, with the id that reports a missing copy. */
|
|
37
|
+
const TEMPLATE_FILES = [
|
|
38
|
+
{ rule: 'MFE_JEST_001', file: 'jest.config.js', why: 'the root Jest config' },
|
|
39
|
+
{ rule: 'MFE_PROXY_001', file: 'proxy-config.json.example', why: 'the proxy template people copy' },
|
|
40
|
+
{ rule: 'MFE_PROXY_002', file: 'proxy-config.json', why: 'the local proxy config the dev server reads' },
|
|
41
|
+
{ rule: 'MFE_SEC_DEPS_011', file: 'scripts/security-check.mjs', why: 'the security floor check lint runs' },
|
|
42
|
+
{ rule: 'MFE_TEST_SHELL_001', file: 'src/app/core/models/Menu.spec.ts', why: 'a shell spec' },
|
|
43
|
+
{ rule: 'MFE_TEST_SHELL_001', file: 'src/app/core/services/navigation/navigation.service.spec.ts', why: 'a shell spec' },
|
|
44
|
+
{ rule: 'MFE_TEST_SHELL_001', file: 'src/app/core/layouts/main-layout/main-layout.component.spec.ts', why: 'a shell spec' },
|
|
45
|
+
{ rule: 'MFE_TEST_SHELL_001', file: 'src/app/core/providers/layout-account.provider.spec.ts', why: 'a shell spec' },
|
|
46
|
+
{ rule: 'MFE_CHILD_ERR_001', file: CHILD_INTERCEPTOR, why: 'the child error interceptor', role: 'child' },
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} rule
|
|
51
|
+
* @param {Severity} severity
|
|
52
|
+
* @param {string} file
|
|
53
|
+
* @param {string} message
|
|
54
|
+
* @param {string} fix
|
|
55
|
+
* @returns {DoctorFinding}
|
|
56
|
+
*/
|
|
57
|
+
const finding = (rule, severity, file, message, fix) => ({ rule, severity, file, line: 0, column: 0, message, fix });
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `x.y.z` (missing parts are 0) as numbers, or null.
|
|
61
|
+
* @param {string} text
|
|
62
|
+
* @returns {number[] | null}
|
|
63
|
+
*/
|
|
64
|
+
function parseVersion(text) {
|
|
65
|
+
const match = /^v?(\d+)(?:\.(\d+|x|\*))?(?:\.(\d+|x|\*))?/.exec(text.trim());
|
|
66
|
+
return match ? [match[1], match[2], match[3]].map((part) => (part === undefined || part === 'x' || part === '*' ? 0 : Number(part))) : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {number[]} a
|
|
71
|
+
* @param {number[]} b
|
|
72
|
+
*/
|
|
73
|
+
const compare = (a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2];
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether `version` satisfies an npm engines range: `||` alternatives of space-separated
|
|
77
|
+
* comparators (`^`, `~`, `>=`, `>`, `<=`, `<`, `=`, bare or `x` versions). Unknown syntax passes.
|
|
78
|
+
* @param {string} version
|
|
79
|
+
* @param {string} range
|
|
80
|
+
*/
|
|
81
|
+
export function satisfies(version, range) {
|
|
82
|
+
const v = parseVersion(version);
|
|
83
|
+
if (!v) return true;
|
|
84
|
+
return range.split('||').some((alternative) =>
|
|
85
|
+
alternative
|
|
86
|
+
.trim()
|
|
87
|
+
.split(/\s+/)
|
|
88
|
+
.filter(Boolean)
|
|
89
|
+
.every((comparator) => {
|
|
90
|
+
const match = /^(\^|~|>=|<=|>|<|=)?\s*(.+)$/.exec(comparator);
|
|
91
|
+
const bound = match ? parseVersion(match[2]) : null;
|
|
92
|
+
if (!match || !bound) return true;
|
|
93
|
+
const parts = match[2].split('.').filter((part) => /^\d+$/.test(part)).length;
|
|
94
|
+
switch (match[1]) {
|
|
95
|
+
case '>=':
|
|
96
|
+
return compare(v, bound) >= 0;
|
|
97
|
+
case '>':
|
|
98
|
+
return compare(v, bound) > 0;
|
|
99
|
+
case '<=':
|
|
100
|
+
return compare(v, bound) <= 0;
|
|
101
|
+
case '<':
|
|
102
|
+
return compare(v, bound) < 0;
|
|
103
|
+
case '^': {
|
|
104
|
+
const next = bound[0] > 0 ? [bound[0] + 1, 0, 0] : bound[1] > 0 ? [0, bound[1] + 1, 0] : [0, 0, bound[2] + 1];
|
|
105
|
+
return compare(v, bound) >= 0 && compare(v, next) < 0;
|
|
106
|
+
}
|
|
107
|
+
case '~': {
|
|
108
|
+
const next = parts <= 1 ? [bound[0] + 1, 0, 0] : [bound[0], bound[1] + 1, 0];
|
|
109
|
+
return compare(v, bound) >= 0 && compare(v, next) < 0;
|
|
110
|
+
}
|
|
111
|
+
default: {
|
|
112
|
+
// `22`, `22.x`, `=22.1.0`: the parts given must match.
|
|
113
|
+
return v.slice(0, Math.max(parts, 1)).every((part, index) => part === bound[index]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The npm on PATH, from the npm user agent when npm runs us, else `npm --version`. */
|
|
121
|
+
function npmVersion() {
|
|
122
|
+
const fromAgent = /\bnpm\/(\d+\.\d+\.\d+)/.exec(process.env.npm_config_user_agent ?? '')?.[1];
|
|
123
|
+
if (fromAgent) return fromAgent;
|
|
124
|
+
const run = spawnSync('npm', ['--version'], { encoding: 'utf8', shell: process.platform === 'win32', timeout: 10_000 });
|
|
125
|
+
return run.status === 0 ? run.stdout.trim() : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @param {ProjectInfo} project
|
|
130
|
+
* @param {{ node?: string, npm?: string | null }} versions
|
|
131
|
+
* @returns {{ environment: DoctorReport['environment'], findings: DoctorFinding[] }}
|
|
132
|
+
*/
|
|
133
|
+
function environment(project, versions) {
|
|
134
|
+
const engines = project.packageJson?.engines ?? {};
|
|
135
|
+
const nodeVersion = versions.node ?? process.versions.node;
|
|
136
|
+
const npm = versions.npm !== undefined ? versions.npm : engines.npm ? npmVersion() : null;
|
|
137
|
+
const nvmrc = readText(project.root, '.nvmrc')?.trim() || null;
|
|
138
|
+
/** @type {DoctorFinding[]} */
|
|
139
|
+
const findings = [];
|
|
140
|
+
const node = { version: nodeVersion, required: engines.node ?? null, ok: !engines.node || satisfies(nodeVersion, engines.node) };
|
|
141
|
+
if (!node.ok)
|
|
142
|
+
findings.push(
|
|
143
|
+
finding(
|
|
144
|
+
'ENV_NODE_001',
|
|
145
|
+
'error',
|
|
146
|
+
'package.json',
|
|
147
|
+
`Node ${nodeVersion} does not satisfy engines.node "${engines.node}"; npm ci fails or the build differs from CI.`,
|
|
148
|
+
nvmrc ? `nvm install ${nvmrc} && nvm use ${nvmrc} (the version in .nvmrc)` : `install a Node that satisfies "${engines.node}"`,
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
const npmCheck = { version: npm, required: engines.npm ?? null, ok: !engines.npm || npm === null || satisfies(npm, engines.npm) };
|
|
152
|
+
if (!npmCheck.ok)
|
|
153
|
+
findings.push(
|
|
154
|
+
finding(
|
|
155
|
+
'ENV_NPM_001',
|
|
156
|
+
'error',
|
|
157
|
+
'package.json',
|
|
158
|
+
`npm ${npm} does not satisfy engines.npm "${engines.npm}".`,
|
|
159
|
+
`npm install -g npm@"${engines.npm}"`,
|
|
160
|
+
),
|
|
161
|
+
);
|
|
162
|
+
return { environment: { node, npm: npmCheck, nvmrc }, findings };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The role's template checkout, found locally the way init finds it; null when none is found.
|
|
167
|
+
* @param {ProjectInfo} project
|
|
168
|
+
* @param {{ templateDir?: string, roots?: string[] }} options
|
|
169
|
+
*/
|
|
170
|
+
function localTemplate(project, { templateDir, roots = [] }) {
|
|
171
|
+
if (project.role !== 'host' && project.role !== 'child') return { template: null, reason: null };
|
|
172
|
+
try {
|
|
173
|
+
const template = resolveTemplate(project.role, { target: project.root, templateDir, roots, offline: true });
|
|
174
|
+
return { template, reason: null };
|
|
175
|
+
} catch (error) {
|
|
176
|
+
return { template: null, reason: error instanceof Error ? error.message : String(error) };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Agent tooling the app's git tracks (`git ls-files`, read only); empty outside a git checkout.
|
|
182
|
+
* @param {string} root
|
|
183
|
+
* @returns {string[]}
|
|
184
|
+
*/
|
|
185
|
+
function trackedAgentRoots(root) {
|
|
186
|
+
let tracked;
|
|
187
|
+
try {
|
|
188
|
+
tracked = execFileSync('git', ['-C', root, 'ls-files', '--', ...AGENT_LOCAL_PATHS], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
189
|
+
} catch {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
return [
|
|
193
|
+
...new Set(
|
|
194
|
+
tracked
|
|
195
|
+
.split('\n')
|
|
196
|
+
.filter(Boolean)
|
|
197
|
+
.map((file) => file.split('/')[0]),
|
|
198
|
+
),
|
|
199
|
+
].sort();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {string} root
|
|
204
|
+
* @returns {string[]}
|
|
205
|
+
*/
|
|
206
|
+
function featureFolders(root) {
|
|
207
|
+
const dir = join(root, 'src/app/features');
|
|
208
|
+
try {
|
|
209
|
+
return readdirSync(dir)
|
|
210
|
+
.filter((name) => statSync(join(dir, name)).isDirectory())
|
|
211
|
+
.sort();
|
|
212
|
+
} catch {
|
|
213
|
+
return [];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Package and kit findings every app with a package.json gets.
|
|
219
|
+
* @param {ProjectInfo} project
|
|
220
|
+
* @returns {DoctorFinding[]}
|
|
221
|
+
*/
|
|
222
|
+
function kitFindings(project) {
|
|
223
|
+
const range = declaredRange(project.packageJson, SDK);
|
|
224
|
+
if (!range)
|
|
225
|
+
return [
|
|
226
|
+
finding(
|
|
227
|
+
'CU_TARGET_SDK_MISSING',
|
|
228
|
+
'error',
|
|
229
|
+
'package.json',
|
|
230
|
+
`package.json does not depend on ${SDK}; the kit tools answer from the installed SDK.`,
|
|
231
|
+
`npm install ${SDK}@latest`,
|
|
232
|
+
),
|
|
233
|
+
];
|
|
234
|
+
const major = Number(/\d+/.exec(range)?.[0] ?? NaN);
|
|
235
|
+
if (Number.isFinite(major) && major < 4)
|
|
236
|
+
return [
|
|
237
|
+
finding(
|
|
238
|
+
'BUTTON_VARIANT_001',
|
|
239
|
+
'warn',
|
|
240
|
+
'package.json',
|
|
241
|
+
`package.json declares ${SDK} ${range}, before 4: <st-button> defaulted to primary there and the kit rules assume SDK 4.`,
|
|
242
|
+
'sdk-tools upgrade --dry-run, then sdk-tools upgrade',
|
|
243
|
+
),
|
|
244
|
+
];
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Findings for a host or child MFE.
|
|
250
|
+
* @param {ProjectInfo} project
|
|
251
|
+
* @param {{ template: import('../types.js').TemplateSource | null, reason: string | null }} found
|
|
252
|
+
* @returns {DoctorFinding[]}
|
|
253
|
+
*/
|
|
254
|
+
function mfeFindings(project, { template, reason }) {
|
|
255
|
+
const { root } = project;
|
|
256
|
+
const has = (/** @type {string} */ file) => existsSync(join(root, file));
|
|
257
|
+
/** @type {DoctorFinding[]} */
|
|
258
|
+
const findings = [];
|
|
259
|
+
const init = 'sdk-tools init --dry-run, then sdk-tools init (adds the template files the app lacks)';
|
|
260
|
+
|
|
261
|
+
if (!project.packageJson) {
|
|
262
|
+
findings.push(
|
|
263
|
+
finding(
|
|
264
|
+
'MFE_SEC_DEPS_001',
|
|
265
|
+
'error',
|
|
266
|
+
'package.json',
|
|
267
|
+
'package.json is missing or unreadable.',
|
|
268
|
+
'restore package.json from git, or sdk-tools init in an empty folder',
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
} else findings.push(...kitFindings(project));
|
|
272
|
+
|
|
273
|
+
if (!has('DESIGN.md'))
|
|
274
|
+
findings.push(finding('MFE_DOC_002', 'error', 'DESIGN.md', 'DESIGN.md is missing: agents and reviewers read the styling rules there.', init));
|
|
275
|
+
if (!has('AGENTS.md'))
|
|
276
|
+
findings.push(
|
|
277
|
+
finding(
|
|
278
|
+
'MFE_DOC_001',
|
|
279
|
+
'warn',
|
|
280
|
+
'AGENTS.md',
|
|
281
|
+
'AGENTS.md is missing: agents working on the app have no project rules.',
|
|
282
|
+
'add AGENTS.md with the project rules (the skills point agents at it)',
|
|
283
|
+
),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
if (!has(FOUNDATION_CONFIG))
|
|
287
|
+
findings.push(
|
|
288
|
+
finding(
|
|
289
|
+
'MFE_LAYOUT_CONFIG_003',
|
|
290
|
+
'warn',
|
|
291
|
+
FOUNDATION_CONFIG,
|
|
292
|
+
`No ${FOUNDATION_CONFIG}: the role, identity and st-app-layout settings are guessed from the code.`,
|
|
293
|
+
`${init}; it asks for the layout keys once`,
|
|
294
|
+
),
|
|
295
|
+
);
|
|
296
|
+
else {
|
|
297
|
+
const config = readJson(root, FOUNDATION_CONFIG);
|
|
298
|
+
if (!config || typeof config !== 'object' || Array.isArray(config))
|
|
299
|
+
findings.push(
|
|
300
|
+
finding(
|
|
301
|
+
'MFE_LAYOUT_CONFIG_001',
|
|
302
|
+
'error',
|
|
303
|
+
FOUNDATION_CONFIG,
|
|
304
|
+
`${FOUNDATION_CONFIG} is not a readable JSON object.`,
|
|
305
|
+
'fix the JSON syntax (git diff shows the last edit)',
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
else if (!config.layout || typeof config.layout !== 'object' || Array.isArray(config.layout))
|
|
309
|
+
findings.push(
|
|
310
|
+
finding(
|
|
311
|
+
'MFE_LAYOUT_CONFIG_002',
|
|
312
|
+
'error',
|
|
313
|
+
FOUNDATION_CONFIG,
|
|
314
|
+
`${FOUNDATION_CONFIG} has no layout block, so the st-app-layout settings are not recorded.`,
|
|
315
|
+
`add "layout": { "defaultLayout": "modern", "topLevelGroups": "collapsible", "activeMatch": "route" } with the app's values (sdk-tools init merges the template block)`,
|
|
316
|
+
),
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
for (const tracked of trackedAgentRoots(root))
|
|
321
|
+
findings.push(
|
|
322
|
+
finding(
|
|
323
|
+
'MFE_AGENT_TRACKED_001',
|
|
324
|
+
'error',
|
|
325
|
+
tracked,
|
|
326
|
+
`${tracked} is tracked by git; agent tooling is installed per checkout and never committed.`,
|
|
327
|
+
`git rm -r --cached ${tracked} and add it to .gitignore`,
|
|
328
|
+
),
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
const features = featureFolders(root);
|
|
332
|
+
const placeholders = features.filter((name) => PLACEHOLDER_FEATURES.has(name));
|
|
333
|
+
const real = features.filter((name) => !PLACEHOLDER_FEATURES.has(name) && !STATUS_FEATURES.has(name));
|
|
334
|
+
if (real.length)
|
|
335
|
+
for (const name of placeholders)
|
|
336
|
+
findings.push(
|
|
337
|
+
finding(
|
|
338
|
+
'MFE_FEATURE_003',
|
|
339
|
+
'error',
|
|
340
|
+
`src/app/features/${name}`,
|
|
341
|
+
`The starter feature "${name}" is still here next to ${real.join(', ')}.`,
|
|
342
|
+
`delete src/app/features/${name} and its routes and menu entries`,
|
|
343
|
+
),
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
if (project.role === 'child') {
|
|
347
|
+
if (!has('src/app/app.routes.ts'))
|
|
348
|
+
findings.push(finding('MFE_CHILD_001', 'error', 'src/app/app.routes.ts', 'The child has no standalone routes (src/app/app.routes.ts).', init));
|
|
349
|
+
if (!has('src/assets/configuration/mfe_config.json'))
|
|
350
|
+
findings.push(
|
|
351
|
+
finding(
|
|
352
|
+
'MFE_CHILD_002',
|
|
353
|
+
'error',
|
|
354
|
+
'src/assets/configuration/mfe_config.json',
|
|
355
|
+
'The child has no runtime config (src/assets/configuration/mfe_config.json).',
|
|
356
|
+
init,
|
|
357
|
+
),
|
|
358
|
+
);
|
|
359
|
+
const interceptor = readText(root, CHILD_INTERCEPTOR);
|
|
360
|
+
if (interceptor !== null) {
|
|
361
|
+
if (/navigate\(\s*\[\s*['"]\/?server-error['"]|navigateToServerError/.test(interceptor))
|
|
362
|
+
findings.push(
|
|
363
|
+
finding(
|
|
364
|
+
'MFE_CHILD_ERR_002',
|
|
365
|
+
'error',
|
|
366
|
+
CHILD_INTERCEPTOR,
|
|
367
|
+
'The child error interceptor navigates to /server-error; in a child a 5xx is a toast and EMPTY, the host owns the status pages.',
|
|
368
|
+
'remove the navigation and show a toast (compare with the template interceptor: sdk-tools init --dry-run)',
|
|
369
|
+
),
|
|
370
|
+
);
|
|
371
|
+
const policy = `${interceptor}\n${readText(root, CHILD_ERROR_POLICY) ?? ''}`;
|
|
372
|
+
const unauthorized = /HttpError\.Unauthorized|status\s*===\s*401|case\s+401/.test(policy);
|
|
373
|
+
const forbidden = /HttpError\.Forbidden|status\s*===\s*403|case\s+403/.test(policy);
|
|
374
|
+
if (!unauthorized || !forbidden || !/return\s+throwError\(/.test(interceptor))
|
|
375
|
+
findings.push(
|
|
376
|
+
finding(
|
|
377
|
+
'MFE_CHILD_ERR_003',
|
|
378
|
+
'error',
|
|
379
|
+
CHILD_INTERCEPTOR,
|
|
380
|
+
'The child error interceptor (or http-error-policy.util.ts) does not classify 401 and 403 and rethrow them with throwError, so auth failures are swallowed.',
|
|
381
|
+
'handle 401/403 and return throwError(() => error) as the template interceptor does (sdk-tools init --dry-run shows it)',
|
|
382
|
+
),
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (!template) {
|
|
388
|
+
findings.push(
|
|
389
|
+
finding(
|
|
390
|
+
'DOCTOR_TEMPLATE_001',
|
|
391
|
+
'warn',
|
|
392
|
+
'',
|
|
393
|
+
`No local checkout of the ${project.role} template, so template drift (MFE_JEST_001, MFE_PROXY_*, MFE_SEC_DEPS_011, MFE_TEST_SHELL_001, MFE_CHILD_ERR_001) is not judged: ${reason}`,
|
|
394
|
+
'pass --template <checkout> or --template-root <dir>, or set SDK_TOOLS_TEMPLATE_ROOT',
|
|
395
|
+
),
|
|
396
|
+
);
|
|
397
|
+
return findings;
|
|
398
|
+
}
|
|
399
|
+
const inTemplate = new Set(templateFiles(template.path));
|
|
400
|
+
for (const entry of TEMPLATE_FILES) {
|
|
401
|
+
if (entry.role && entry.role !== project.role) continue;
|
|
402
|
+
if (!inTemplate.has(entry.file) || has(entry.file)) continue;
|
|
403
|
+
findings.push(finding(entry.rule, 'error', entry.file, `${entry.file} is missing: the ${project.role} template has it (${entry.why}).`, init));
|
|
404
|
+
}
|
|
405
|
+
return findings;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Runs the doctor on the app at `cwd`.
|
|
410
|
+
* @param {{ cwd?: string, templateDir?: string, roots?: string[], versions?: { node?: string, npm?: string | null } }} [options]
|
|
411
|
+
* @returns {DoctorReport}
|
|
412
|
+
*/
|
|
413
|
+
export function doctor({ cwd = process.cwd(), templateDir, roots = [], versions = {} } = {}) {
|
|
414
|
+
const root = resolve(cwd);
|
|
415
|
+
const project = readProject(root);
|
|
416
|
+
const env = environment(project, versions);
|
|
417
|
+
/** @type {DoctorFinding[]} */
|
|
418
|
+
const findings = [...env.findings];
|
|
419
|
+
/** @type {DoctorReport['template']} */
|
|
420
|
+
let templateInfo = null;
|
|
421
|
+
if (project.role === 'host' || project.role === 'child') {
|
|
422
|
+
const found = localTemplate(project, { templateDir, roots });
|
|
423
|
+
if (found.template)
|
|
424
|
+
templateInfo = { repo: found.template.repo, path: found.template.path, source: found.template.source, commit: found.template.commit };
|
|
425
|
+
findings.push(...mfeFindings(project, found));
|
|
426
|
+
} else if (!project.packageJson) {
|
|
427
|
+
findings.push(
|
|
428
|
+
finding(
|
|
429
|
+
'CU_PACKAGE_JSON_MISSING',
|
|
430
|
+
'error',
|
|
431
|
+
'package.json',
|
|
432
|
+
'No package.json here: this is not an app folder.',
|
|
433
|
+
"run sdk-tools doctor from the app's folder, or sdk-tools init <dir> --host|--child to create one",
|
|
434
|
+
),
|
|
435
|
+
);
|
|
436
|
+
} else {
|
|
437
|
+
findings.push(...kitFindings(project));
|
|
438
|
+
if (!existsSync(join(root, 'DESIGN.md')))
|
|
439
|
+
findings.push(
|
|
440
|
+
finding(
|
|
441
|
+
'CU_DESIGN_MD_MISSING',
|
|
442
|
+
'warn',
|
|
443
|
+
'DESIGN.md',
|
|
444
|
+
'DESIGN.md is missing: agents have no styling rules for this app.',
|
|
445
|
+
'add DESIGN.md (the MFE templates ship one to start from)',
|
|
446
|
+
),
|
|
447
|
+
);
|
|
448
|
+
if (!existsSync(join(root, 'AGENTS.md')))
|
|
449
|
+
findings.push(
|
|
450
|
+
finding(
|
|
451
|
+
'CU_AGENTS_MD_MISSING',
|
|
452
|
+
'warn',
|
|
453
|
+
'AGENTS.md',
|
|
454
|
+
'AGENTS.md is missing: agents working on the app have no project rules.',
|
|
455
|
+
'add AGENTS.md with the project rules',
|
|
456
|
+
),
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
const errors = findings.filter((f) => f.severity === 'error').length;
|
|
460
|
+
return {
|
|
461
|
+
root,
|
|
462
|
+
role: project.role ?? 'none',
|
|
463
|
+
environment: env.environment,
|
|
464
|
+
template: templateInfo,
|
|
465
|
+
findings,
|
|
466
|
+
counts: { errors, warnings: findings.length - errors },
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* @param {DoctorReport} report
|
|
472
|
+
* @returns {string}
|
|
473
|
+
*/
|
|
474
|
+
export function formatDoctor(report) {
|
|
475
|
+
const { node, npm } = report.environment;
|
|
476
|
+
const lines = [
|
|
477
|
+
`${report.root} (${report.role})`,
|
|
478
|
+
` node ${node.version}${node.required ? ` (engines ${node.required})` : ''}${node.ok ? '' : ' MISMATCH'}`,
|
|
479
|
+
` npm ${npm.version ?? 'not checked'}${npm.required ? ` (engines ${npm.required})` : ''}${npm.ok ? '' : ' MISMATCH'}`,
|
|
480
|
+
];
|
|
481
|
+
if (report.template) lines.push(` template ${report.template.repo} at ${report.template.path}`);
|
|
482
|
+
lines.push('');
|
|
483
|
+
for (const f of report.findings) {
|
|
484
|
+
lines.push(`${f.severity === 'error' ? 'error' : 'warn '} ${f.rule}${f.file ? ` ${f.file}` : ''}`, ` ${f.message}`, ` fix: ${f.fix}`);
|
|
485
|
+
}
|
|
486
|
+
const { errors, warnings } = report.counts;
|
|
487
|
+
lines.push(
|
|
488
|
+
report.findings.length ? '' : 'No problems found.',
|
|
489
|
+
`${errors} error${errors === 1 ? '' : 's'}, ${warnings} warning${warnings === 1 ? '' : 's'}`,
|
|
490
|
+
);
|
|
491
|
+
return lines.join('\n');
|
|
492
|
+
}
|