@magmonium/cli 0.8.30 → 0.10.3
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/bin/cli.js +4 -0
- package/bin/commands/gesture.js +200 -0
- package/bin/commands/inspect.js +141 -0
- package/bin/commands/validate.js +250 -11
- package/codegen/control-configs.json +1006 -0
- package/codegen/gesture-catalog.js +2436 -0
- package/codegen/index.js +10 -2
- package/codegen/inspect-catalog.js +1548 -0
- package/codegen/invariant-catalog.js +1555 -221
- package/codegen/message-text.json +441 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -26,6 +26,8 @@ const genCodeCommand = require('./commands/gen-code');
|
|
|
26
26
|
const genTestCommand = require('./commands/gen-test');
|
|
27
27
|
const validateCommand = require('./commands/validate');
|
|
28
28
|
const apiOpsCommand = require('./commands/api-ops');
|
|
29
|
+
const gestureCommand = require('./commands/gesture');
|
|
30
|
+
const inspectCommand = require('./commands/inspect');
|
|
29
31
|
|
|
30
32
|
const program = new Command();
|
|
31
33
|
|
|
@@ -61,5 +63,7 @@ genCodeCommand(program);
|
|
|
61
63
|
genTestCommand(program);
|
|
62
64
|
validateCommand(program);
|
|
63
65
|
apiOpsCommand(program);
|
|
66
|
+
gestureCommand(program);
|
|
67
|
+
inspectCommand(program);
|
|
64
68
|
|
|
65
69
|
program.parse();
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { readDesignFiles } = require('./gen-code');
|
|
4
|
+
const {
|
|
5
|
+
DESIGN_ROOT,
|
|
6
|
+
appTypeScript,
|
|
7
|
+
findViolations,
|
|
8
|
+
loadBundles,
|
|
9
|
+
themeCatalogOf,
|
|
10
|
+
writeCanonical,
|
|
11
|
+
} = require('./validate');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runs one of the editor's gestures — or an ordered batch of them — against
|
|
15
|
+
* `.magmonium/` exactly as the editor runs it (gesture-catalog, 0128_13): the
|
|
16
|
+
* same writes, seeds, cleanups and refusals, so an AI edit is one the editor
|
|
17
|
+
* could have made.
|
|
18
|
+
*
|
|
19
|
+
* Dry by default: the report says what each gesture did (its notes), which
|
|
20
|
+
* files it would change, and every Violation the result holds that the design
|
|
21
|
+
* did not. `--write` lands it only when nothing new blocks — the Invariants
|
|
22
|
+
* and the codec-drop diff judged against the design before the gesture — in
|
|
23
|
+
* the form the editor saves (canonical order, a fresh `write-id`).
|
|
24
|
+
*
|
|
25
|
+
* `status`: `ok`, `refused` (a gesture refused, or its result holds a new
|
|
26
|
+
* blocking Violation — nothing written), `invalid` (bad arguments, an unknown
|
|
27
|
+
* gesture, or a design the codec cannot read).
|
|
28
|
+
*/
|
|
29
|
+
async function gesture({ root = process.cwd(), name, args, batch, write = false }) {
|
|
30
|
+
const bundles = loadBundles();
|
|
31
|
+
const { codegen, gestures } = bundles;
|
|
32
|
+
const report = { status: 'ok', notes: [], changed: [], removed: [], canonicalised: [], violations: [] };
|
|
33
|
+
const invalid = (reason, detail) => ({ ...report, status: 'invalid', refusal: { reason, ...(detail ? { detail } : {}) } });
|
|
34
|
+
|
|
35
|
+
const steps = batch ?? [{ gesture: name, args: args ?? {} }];
|
|
36
|
+
if (!Array.isArray(steps) || !steps.length) return invalid('batch-empty');
|
|
37
|
+
const resolved = [];
|
|
38
|
+
for (const [index, step] of steps.entries()) {
|
|
39
|
+
const found = step && gestures.gestureByName(step.gesture);
|
|
40
|
+
if (!found) return invalid('gesture-unknown', { step: index, gesture: step && step.gesture });
|
|
41
|
+
resolved.push({ gesture: found, args: step.args ?? {} });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!fs.existsSync(path.join(root, DESIGN_ROOT, 'index.json')))
|
|
45
|
+
return invalid('design-absent', { path: `${DESIGN_ROOT}/index.json` });
|
|
46
|
+
const files = readDesignFiles(root);
|
|
47
|
+
const read = codegen.readDesignSource(files);
|
|
48
|
+
if (!read.ok || !read.source) return invalid('design-unreadable', read.refusal);
|
|
49
|
+
const source = read.source;
|
|
50
|
+
|
|
51
|
+
// `function-save` compiles the body the way the function editor does, then
|
|
52
|
+
// runs its cases the way Run cases does (0132_01).
|
|
53
|
+
const ts = appTypeScript(root);
|
|
54
|
+
gestures.setFunctionBodyCheck(ts ? gestures.functionBodyCheckFor(ts) : undefined);
|
|
55
|
+
gestures.setFunctionCaseRun(ts ? gestures.functionCaseRunFor(ts) : undefined);
|
|
56
|
+
|
|
57
|
+
const run = gestures.runBatch(source, resolved);
|
|
58
|
+
report.notes = run.notes;
|
|
59
|
+
if (!run.ok || !run.source) {
|
|
60
|
+
return {
|
|
61
|
+
...report,
|
|
62
|
+
status: 'refused',
|
|
63
|
+
refusal: {
|
|
64
|
+
gesture: steps[run.failedAt ?? 0].gesture,
|
|
65
|
+
step: run.failedAt ?? 0,
|
|
66
|
+
...(run.refusal || { reason: 'refused' }),
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Judged as the editor would read the result back: written canonically,
|
|
72
|
+
// then through the codec again, so a row the gesture left unreadable shows
|
|
73
|
+
// as a drop rather than vanishing.
|
|
74
|
+
const operationOf = codegen.operationResolverOf(source.operations);
|
|
75
|
+
const catalog = themeCatalogOf(root);
|
|
76
|
+
const canonicalBefore = codegen.buildDesignSource({ ...source, operationOf }, source.operations);
|
|
77
|
+
const afterFiles = codegen.buildDesignSource({ ...run.source, operationOf }, source.operations);
|
|
78
|
+
const reread = codegen.readDesignSource(afterFiles);
|
|
79
|
+
if (!reread.ok || !reread.source)
|
|
80
|
+
return { ...report, status: 'refused', refusal: { reason: 'result-unreadable', detail: reread.refusal } };
|
|
81
|
+
|
|
82
|
+
const held = new Set(
|
|
83
|
+
findViolations(bundles, source, catalog, operationOf, files, ts).map((one) => one.id)
|
|
84
|
+
);
|
|
85
|
+
report.violations = findViolations(bundles, reread.source, catalog, operationOf, afterFiles, ts).filter(
|
|
86
|
+
(one) => !held.has(one.id)
|
|
87
|
+
);
|
|
88
|
+
report.changed = Object.keys(afterFiles).filter((rel) => canonicalBefore[rel] !== afterFiles[rel]);
|
|
89
|
+
report.removed = Object.keys(canonicalBefore).filter((rel) => !(rel in afterFiles));
|
|
90
|
+
// Files the gesture did not change but a write still rewrites: the design
|
|
91
|
+
// on disk is not in the form the editor saves — the migration its next Save
|
|
92
|
+
// would make too.
|
|
93
|
+
report.canonicalised = Object.keys(afterFiles).filter(
|
|
94
|
+
(rel) => !report.changed.includes(rel) && files[rel] !== afterFiles[rel]
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
if (report.violations.some((one) => one.severity === 'blocking')) {
|
|
98
|
+
return { ...report, status: 'refused', refusal: { reason: 'violations' } };
|
|
99
|
+
}
|
|
100
|
+
if (write) {
|
|
101
|
+
report.written = writeCanonical(root, codegen, { ...run.source, operationOf }, source.operations, files);
|
|
102
|
+
}
|
|
103
|
+
return report;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const EXIT = { ok: 0, refused: 1, invalid: 2 };
|
|
107
|
+
|
|
108
|
+
const parseJson = (text, what) => {
|
|
109
|
+
try {
|
|
110
|
+
return JSON.parse(text);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new Error(`${what} is not JSON: ${error.message}`);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
function printReport(report) {
|
|
117
|
+
if (report.status === 'invalid') {
|
|
118
|
+
console.error(`❌ gesture: ${report.refusal.reason}${report.refusal.detail ? ` ${JSON.stringify(report.refusal.detail)}` : ''}`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
report.notes.forEach((note) => console.log(`• ${note}`));
|
|
122
|
+
if (report.status === 'refused') {
|
|
123
|
+
const r = report.refusal;
|
|
124
|
+
console.error(
|
|
125
|
+
`❌ refused${r.gesture ? ` — ${r.gesture} (step ${r.step})` : ''}: ${r.reason}${r.detail ? ` ${JSON.stringify(r.detail)}` : ''}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
report.violations.forEach((one) =>
|
|
129
|
+
console.log(`${one.severity === 'blocking' ? '❌' : '⚠️'} ${one.invariant}: ${one.subject}`)
|
|
130
|
+
);
|
|
131
|
+
if (report.status === 'ok') {
|
|
132
|
+
console.log(
|
|
133
|
+
report.written
|
|
134
|
+
? `✅ written: ${report.changed.length} file(s) changed${report.removed.length ? `, ${report.removed.length} removed` : ''}`
|
|
135
|
+
: `✅ dry run: would change ${report.changed.length} file(s) — pass --write to land it`
|
|
136
|
+
);
|
|
137
|
+
report.changed.forEach((rel) => console.log(` ${rel}`));
|
|
138
|
+
if (report.canonicalised.length)
|
|
139
|
+
console.log(
|
|
140
|
+
` + ${report.canonicalised.length} file(s) ${report.written ? 'rewritten' : 'to rewrite'} in the editor's saved form only (no content change): ${report.canonicalised.join(', ')}`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function gestureCommand(program) {
|
|
146
|
+
program
|
|
147
|
+
.command('gesture [name]')
|
|
148
|
+
.description(
|
|
149
|
+
"Run one of the m-one-ui editor's gestures on .magmonium/ — the editor's own writes, seeds and refusals. Dry run unless --write."
|
|
150
|
+
)
|
|
151
|
+
.option('--root <path>', 'App root (default: cwd)')
|
|
152
|
+
.option('--args <json>', 'The gesture\'s arguments as JSON, e.g. \'{"screenId":"…"}\'')
|
|
153
|
+
.option('--args-file <path>', 'Read the arguments from a JSON file')
|
|
154
|
+
.option('--batch <path>', 'A JSON file of [{ "gesture", "args" }] run in order, all or nothing')
|
|
155
|
+
.option('--list', 'List every gesture with its arguments')
|
|
156
|
+
.option('--write', 'Land the result when nothing new blocks (canonical form, fresh write-id)')
|
|
157
|
+
.option('--json', 'Print the report as JSON')
|
|
158
|
+
.action(async (name, options) => {
|
|
159
|
+
let report;
|
|
160
|
+
try {
|
|
161
|
+
if (options.list) {
|
|
162
|
+
const { gestures } = loadBundles();
|
|
163
|
+
const list = gestures.GESTURES.map(({ name: gestureName, summary, args }) => ({
|
|
164
|
+
name: gestureName,
|
|
165
|
+
summary,
|
|
166
|
+
args,
|
|
167
|
+
}));
|
|
168
|
+
if (options.json) console.log(JSON.stringify(list, null, 2));
|
|
169
|
+
else
|
|
170
|
+
list.forEach((one) => {
|
|
171
|
+
console.log(`${one.name} — ${one.summary}`);
|
|
172
|
+
Object.entries(one.args).forEach(([key, arg]) =>
|
|
173
|
+
console.log(` ${key}${arg.required ? '' : '?'} (${arg.type}): ${arg.doc}`)
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const root = options.root || process.cwd();
|
|
179
|
+
const args = options.argsFile
|
|
180
|
+
? parseJson(fs.readFileSync(path.resolve(options.argsFile), 'utf8'), '--args-file')
|
|
181
|
+
: options.args
|
|
182
|
+
? parseJson(options.args, '--args')
|
|
183
|
+
: undefined;
|
|
184
|
+
const batch = options.batch
|
|
185
|
+
? parseJson(fs.readFileSync(path.resolve(options.batch), 'utf8'), '--batch')
|
|
186
|
+
: undefined;
|
|
187
|
+
if (!name && !batch) throw new Error('name a gesture, pass --batch, or --list');
|
|
188
|
+
report = await gesture({ root, name, args, batch, write: Boolean(options.write) });
|
|
189
|
+
} catch (error) {
|
|
190
|
+
console.error(`❌ gesture: ${error.message}`);
|
|
191
|
+
process.exit(EXIT.invalid);
|
|
192
|
+
}
|
|
193
|
+
if (options.json) console.log(JSON.stringify(report, null, 2));
|
|
194
|
+
else printReport(report);
|
|
195
|
+
process.exit(EXIT[report.status] ?? EXIT.invalid);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
module.exports = gestureCommand;
|
|
200
|
+
module.exports.gesture = gesture;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { readDesignFiles } = require('./gen-code');
|
|
4
|
+
const {
|
|
5
|
+
DESIGN_ROOT,
|
|
6
|
+
loadBundles,
|
|
7
|
+
messageText,
|
|
8
|
+
readApiSpec,
|
|
9
|
+
readDesignFilesAt,
|
|
10
|
+
} = require('./validate');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Asks one of the m-one-ui editor's read-only questions of `.magmonium/`
|
|
14
|
+
* (inspect-catalog, 0129_02): who uses a store member, what names a Screen or
|
|
15
|
+
* a PureFunction, what is drawn from an ApiOperation, what the backend
|
|
16
|
+
* changed under the design, which OneFunctions exist, what changed between
|
|
17
|
+
* two refs — the answers the editor's Usage, Used by and Recheck panels give.
|
|
18
|
+
*
|
|
19
|
+
* Never writes. `status`: `ok`, or `invalid` (an unknown inspection, a
|
|
20
|
+
* missing argument, an id the design does not hold, a design the codec cannot
|
|
21
|
+
* read, a ref git cannot show).
|
|
22
|
+
*/
|
|
23
|
+
async function inspect({ root = process.cwd(), name, args = {}, spec, bundles = loadBundles() }) {
|
|
24
|
+
const { codegen, inspections } = bundles;
|
|
25
|
+
const invalid = (reason, detail) => ({
|
|
26
|
+
status: 'invalid',
|
|
27
|
+
inspection: name,
|
|
28
|
+
refusal: { reason, ...(detail ? { detail } : {}) },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const inspection = inspections.inspectionByName(name);
|
|
32
|
+
if (!inspection) return invalid('inspection-unknown', { inspection: name });
|
|
33
|
+
|
|
34
|
+
const atRef = name === 'change' && args.to;
|
|
35
|
+
if (!atRef && !fs.existsSync(path.join(root, DESIGN_ROOT, 'index.json')))
|
|
36
|
+
return invalid('design-absent', { path: `${DESIGN_ROOT}/index.json` });
|
|
37
|
+
|
|
38
|
+
const input = {};
|
|
39
|
+
if (name === 'change') {
|
|
40
|
+
if (!args.from) return invalid('args-invalid', { missing: ['from'] });
|
|
41
|
+
input.baseFiles = readDesignFilesAt(root, args.from);
|
|
42
|
+
input.files = args.to ? readDesignFilesAt(root, args.to) : readDesignFiles(root);
|
|
43
|
+
}
|
|
44
|
+
const read = codegen.readDesignSource(input.files ?? readDesignFiles(root));
|
|
45
|
+
if (!read.ok || !read.source) return invalid('design-unreadable', read.refusal);
|
|
46
|
+
input.source = read.source;
|
|
47
|
+
|
|
48
|
+
let specRead;
|
|
49
|
+
if (name === 'drift') {
|
|
50
|
+
const { where, spec: apiSpec } = await readApiSpec(bundles, spec, read.source.appTarget);
|
|
51
|
+
input.spec = apiSpec;
|
|
52
|
+
specRead = where;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const run = inspections.runInspection(inspection, input, args);
|
|
56
|
+
if (!run.ok) return invalid(run.refusal.reason, run.refusal.detail);
|
|
57
|
+
// A SpecDrift row carries its message as a key, like a Violation.
|
|
58
|
+
const result =
|
|
59
|
+
name === 'drift'
|
|
60
|
+
? run.result.map((row) => ({ ...row, text: messageText(row.message) }))
|
|
61
|
+
: run.result;
|
|
62
|
+
return { status: 'ok', inspection: name, ...(specRead ? { spec: specRead } : {}), result };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const EXIT = { ok: 0, invalid: 2 };
|
|
66
|
+
|
|
67
|
+
const ARG_OPTIONS = ['member', 'screen', 'function', 'op', 'reach', 'from', 'to'];
|
|
68
|
+
|
|
69
|
+
function printReport(report) {
|
|
70
|
+
if (report.status !== 'ok') {
|
|
71
|
+
const r = report.refusal;
|
|
72
|
+
console.error(`❌ inspect ${report.inspection ?? ''}: ${r.reason}${r.detail ? ` ${JSON.stringify(r.detail)}` : ''}`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const rows = Array.isArray(report.result) ? report.result : undefined;
|
|
76
|
+
if (rows && !rows.length) {
|
|
77
|
+
console.log('✅ nothing');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
console.log(JSON.stringify(report.result, null, 2));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function inspectCommand(program) {
|
|
84
|
+
program
|
|
85
|
+
.command('inspect [name]')
|
|
86
|
+
.description(
|
|
87
|
+
"Ask one of the m-one-ui editor's read-only questions of .magmonium/ — usage, used-by, api-holders, drift, one-functions, change. Never writes."
|
|
88
|
+
)
|
|
89
|
+
.option('--root <path>', 'App root (default: cwd)')
|
|
90
|
+
.option('--member <id>', 'usage: the store member id')
|
|
91
|
+
.option('--screen <id>', 'used-by: the Screen id')
|
|
92
|
+
.option('--function <id>', 'used-by: the PureFunction id')
|
|
93
|
+
.option('--op <"VERB /path">', 'api-holders: the ApiOperation, e.g. "GET /orders/{id}"')
|
|
94
|
+
.option('--reach <reach>', 'one-functions: only those offered at this reach')
|
|
95
|
+
.option('--from <ref>', 'change: the base git ref')
|
|
96
|
+
.option('--to <ref>', 'change: the compared git ref (default: the working tree)')
|
|
97
|
+
.option('--spec [urlOrFile]', "drift: swagger.json to read (default: the Environment backend's)")
|
|
98
|
+
.option('--list', 'List every inspection with its arguments')
|
|
99
|
+
.option('--json', 'Print the report as JSON')
|
|
100
|
+
.action(async (name, options) => {
|
|
101
|
+
let report;
|
|
102
|
+
try {
|
|
103
|
+
if (options.list) {
|
|
104
|
+
const { inspections } = loadBundles();
|
|
105
|
+
const list = inspections.INSPECTIONS.map(({ name: inspectionName, summary, args }) => ({
|
|
106
|
+
name: inspectionName,
|
|
107
|
+
summary,
|
|
108
|
+
args,
|
|
109
|
+
}));
|
|
110
|
+
if (options.json) console.log(JSON.stringify(list, null, 2));
|
|
111
|
+
else
|
|
112
|
+
list.forEach((one) => {
|
|
113
|
+
console.log(`${one.name} — ${one.summary}`);
|
|
114
|
+
Object.entries(one.args).forEach(([key, arg]) =>
|
|
115
|
+
console.log(` --${key}${arg.required ? '' : '?'}: ${arg.doc}`)
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!name) throw new Error('name an inspection, or --list');
|
|
121
|
+
const args = Object.fromEntries(
|
|
122
|
+
ARG_OPTIONS.filter((key) => options[key] !== undefined).map((key) => [key, String(options[key])])
|
|
123
|
+
);
|
|
124
|
+
report = await inspect({
|
|
125
|
+
root: options.root || process.cwd(),
|
|
126
|
+
name,
|
|
127
|
+
args,
|
|
128
|
+
spec: options.spec === true ? '' : options.spec,
|
|
129
|
+
});
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error(`❌ inspect: ${error.message}`);
|
|
132
|
+
process.exit(EXIT.invalid);
|
|
133
|
+
}
|
|
134
|
+
if (options.json) console.log(JSON.stringify(report, null, 2));
|
|
135
|
+
else printReport(report);
|
|
136
|
+
process.exit(EXIT[report.status] ?? EXIT.invalid);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = inspectCommand;
|
|
141
|
+
module.exports.inspect = inspect;
|
package/bin/commands/validate.js
CHANGED
|
@@ -19,6 +19,8 @@ function loadBundles() {
|
|
|
19
19
|
const names = {
|
|
20
20
|
codegen: 'index.js',
|
|
21
21
|
invariants: 'invariant-catalog.js',
|
|
22
|
+
gestures: 'gesture-catalog.js',
|
|
23
|
+
inspections: 'inspect-catalog.js',
|
|
22
24
|
swagger: 'swagger-helper.js',
|
|
23
25
|
};
|
|
24
26
|
const missing = Object.values(names).filter(
|
|
@@ -346,19 +348,247 @@ function readDesignFilesAt(root, ref) {
|
|
|
346
348
|
return files;
|
|
347
349
|
}
|
|
348
350
|
|
|
351
|
+
/**
|
|
352
|
+
* The AppRepo's own `typescript`, which the PureFunction body check runs on
|
|
353
|
+
* (0128_12) — not a dependency of the cli, so a repo without one skips the
|
|
354
|
+
* check and says so rather than failing.
|
|
355
|
+
*/
|
|
356
|
+
function appTypeScript(root) {
|
|
357
|
+
try {
|
|
358
|
+
return require(require.resolve('typescript', { paths: [root] }));
|
|
359
|
+
} catch {
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Every PureFunction body checked the way the editor checks it: against its
|
|
366
|
+
* signature and the curated pure lib, so a body that does not compile or
|
|
367
|
+
* reaches past its parameters is blocking here as it is in the function
|
|
368
|
+
* editor (ADR 0046).
|
|
369
|
+
*/
|
|
370
|
+
function bodyBreaches(bundles, source, ts) {
|
|
371
|
+
if (!ts) return [];
|
|
372
|
+
const check = bundles.gestures.functionBodyCheckFor(ts);
|
|
373
|
+
const owned = [
|
|
374
|
+
...Object.values(source.functions || {}).map((fn) => [fn, undefined]),
|
|
375
|
+
...Object.values(source.features || {}).flatMap((feature) =>
|
|
376
|
+
Object.values(feature.functions || {}).map((fn) => [fn, feature])
|
|
377
|
+
),
|
|
378
|
+
];
|
|
379
|
+
return owned.flatMap(([fn, feature]) => {
|
|
380
|
+
const problems = check(source, fn, feature && feature.id);
|
|
381
|
+
if (!problems.length) return [];
|
|
382
|
+
const first = problems[0];
|
|
383
|
+
return [
|
|
384
|
+
{
|
|
385
|
+
id: `function-body-invalid::${fn.id}`,
|
|
386
|
+
invariant: 'function-body-invalid',
|
|
387
|
+
message: 'invariant-function-body-invalid',
|
|
388
|
+
subject: `${feature ? feature.name : 'shared'} · ${fn.name} (${first.line}: ${first.message})`,
|
|
389
|
+
severity: 'blocking',
|
|
390
|
+
target: {
|
|
391
|
+
kind: 'function',
|
|
392
|
+
...(feature ? { featureId: feature.id } : {}),
|
|
393
|
+
functionId: fn.id,
|
|
394
|
+
},
|
|
395
|
+
problems,
|
|
396
|
+
},
|
|
397
|
+
];
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* What the emit would leave out (GLOSSARY.md EmitGap), the Repair Register's
|
|
403
|
+
* list: advisory, as the editor holds it — a design skill refuses any one new
|
|
404
|
+
* against its baseline (0128_05).
|
|
405
|
+
*/
|
|
406
|
+
function emitGapAdvisories(bundles, source, operationOf) {
|
|
407
|
+
return bundles.invariants
|
|
408
|
+
.collectEmitGaps({
|
|
409
|
+
screens: source.screens,
|
|
410
|
+
features: source.features,
|
|
411
|
+
navMap: source.navMap,
|
|
412
|
+
functions: source.functions,
|
|
413
|
+
enums: source.enums,
|
|
414
|
+
operationOf,
|
|
415
|
+
})
|
|
416
|
+
.map((gap) => ({
|
|
417
|
+
id: `emit-gap-${gap.kind}::${gap.screenId}::${gap.subject}`,
|
|
418
|
+
invariant: `emit-gap-${gap.kind}`,
|
|
419
|
+
message: gap.message,
|
|
420
|
+
subject: `${gap.screenName} → ${gap.subject}${gap.detail ? ` (${gap.detail})` : ''}`,
|
|
421
|
+
severity: 'advisory',
|
|
422
|
+
target: gap.screenId
|
|
423
|
+
? { kind: 'screen', screenId: gap.screenId }
|
|
424
|
+
: { kind: 'panel', panel: 'config' },
|
|
425
|
+
}));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Config keys a Control carries that its inspector form does not list,
|
|
429
|
+
// because a side panel or the gesture itself writes them: the born name, the
|
|
430
|
+
// TextSource's label and `noTranslate`, a link's URLs, a table's columns, a
|
|
431
|
+
// menu's options, a button's key, a SubmitTarget, a pick, and a remote's or a
|
|
432
|
+
// doc's mount wiring (0128_12, measured on the store and invest designs).
|
|
433
|
+
const SIDE_PANEL_KEYS = new Set([
|
|
434
|
+
'name', 'label', 'noTranslate', 'url', 'murl', 'img_sizes', 'columns',
|
|
435
|
+
'options', 'buttonKey', 'submitTarget', 'selection', 'mountName', 'outFn',
|
|
436
|
+
'urlMode', 'remote', 'remoteName', 'designSrc',
|
|
437
|
+
]);
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* The English text of each message key (`message-text.json`, compiled from
|
|
441
|
+
* the m-one-ui i18n assets, 0129_03), so a report says what the editor says
|
|
442
|
+
* rather than only its key. A key with no text reads as itself.
|
|
443
|
+
*/
|
|
444
|
+
let messageTexts;
|
|
445
|
+
// The checks only the cli makes — the editor's gestures refuse what they catch,
|
|
446
|
+
// so no editor panel ever says them and no i18n asset holds their text.
|
|
447
|
+
const CLI_MESSAGE_TEXT = {
|
|
448
|
+
'invariant-codec-dropped':
|
|
449
|
+
'The editor would drop this row on read — a reserved or duplicate name, a clashing Screen name, an unknown Control type',
|
|
450
|
+
'invariant-control-configs':
|
|
451
|
+
"This Control's configs hold a key or value its inspector does not offer",
|
|
452
|
+
'invariant-control-placement':
|
|
453
|
+
'This Control sits where the editor would not let it land — the ControlRelationship whitelist, an unknown type, or a child or parent link that disagrees',
|
|
454
|
+
'invariant-function-body-invalid': 'This PureFunction body does not compile',
|
|
455
|
+
'invariant-method-operation-unresolved':
|
|
456
|
+
'This StoreMethod resolves to no ApiOperation — neither the stored operations nor the spec has its verb and path',
|
|
457
|
+
'invariant-nav-structure':
|
|
458
|
+
'The Nav tree is malformed — a missing or doubly-listed child, a navId off its parent, two Default Navs, or a pageId or componentKey naming the wrong Screen',
|
|
459
|
+
};
|
|
460
|
+
function messageText(key) {
|
|
461
|
+
if (!messageTexts) {
|
|
462
|
+
const file = path.join(__dirname, '..', '..', 'codegen', 'message-text.json');
|
|
463
|
+
messageTexts = {
|
|
464
|
+
...CLI_MESSAGE_TEXT,
|
|
465
|
+
...(fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : {}),
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
return messageTexts[key] ?? key;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// The packages whose version a design is stamped with on `--write`, by the
|
|
472
|
+
// `index.json` field that records it.
|
|
473
|
+
const PIN_STAMPS = { codegen: '@magmonium/cli', one: '@magmonium/one' };
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* A package the AppRepo's `package.json` pins below the version the design is
|
|
477
|
+
* stamped with (0132_03): the AppRepo's `gen-code` then refuses the design, or
|
|
478
|
+
* the emitted code compiles against an older `@magmonium/one` than it was
|
|
479
|
+
* written for — `ScreenOverlayTrigger` has no `screen` in 0.4.55. Advisory: the
|
|
480
|
+
* design is sound, the shell around it is behind. Ranges read as their floor.
|
|
481
|
+
*/
|
|
482
|
+
function pinsBehindStamp(packageJson, indexText) {
|
|
483
|
+
let index;
|
|
484
|
+
try {
|
|
485
|
+
index = JSON.parse(indexText || '{}');
|
|
486
|
+
} catch {
|
|
487
|
+
return [];
|
|
488
|
+
}
|
|
489
|
+
const pins = { ...(packageJson?.dependencies || {}), ...(packageJson?.devDependencies || {}) };
|
|
490
|
+
return Object.entries(PIN_STAMPS).flatMap(([field, name]) => {
|
|
491
|
+
const stamp = index[field];
|
|
492
|
+
const pin = typeof pins[name] === 'string' ? pins[name].replace(/^[^\d]*/, '') : '';
|
|
493
|
+
if (!stamp || !pin || !isNewer(stamp, pin)) return [];
|
|
494
|
+
return [
|
|
495
|
+
{
|
|
496
|
+
id: `pins-behind-stamp::${name}`,
|
|
497
|
+
invariant: 'pins-behind-stamp',
|
|
498
|
+
message: 'pins-behind-stamp',
|
|
499
|
+
text: `package.json pins ${name} ${pin}, below the ${stamp} this design is stamped with — run npm install --save-exact ${name}@${stamp}`,
|
|
500
|
+
subject: `${name} ${pin} < ${stamp}`,
|
|
501
|
+
severity: 'advisory',
|
|
502
|
+
target: { kind: 'panel', panel: 'config' },
|
|
503
|
+
},
|
|
504
|
+
];
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function readPackageJson(root) {
|
|
509
|
+
try {
|
|
510
|
+
return JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
|
|
511
|
+
} catch {
|
|
512
|
+
return undefined;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function controlConfigsCatalog() {
|
|
517
|
+
const file = path.join(__dirname, '..', '..', 'codegen', 'control-configs.json');
|
|
518
|
+
return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : undefined;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* A Control's `configs` read against what its inspector offers
|
|
523
|
+
* (`control-configs.json`, compiled from the m-form `<type>_controls.yml`):
|
|
524
|
+
* a key no form lists and no side panel writes, or a dropdown value the
|
|
525
|
+
* field does not offer. Advisory — the editor accepts both on read, and a
|
|
526
|
+
* form-less ControlType is not judged at all; a design skill refuses any new
|
|
527
|
+
* one against its baseline (0128_05).
|
|
528
|
+
*/
|
|
529
|
+
function configsAdvisories(catalog, source) {
|
|
530
|
+
if (!catalog) return [];
|
|
531
|
+
// A layout key some form offers for some type (margin, padding, …) is one a
|
|
532
|
+
// shared mixin writes, not a typo.
|
|
533
|
+
const anyForm = new Set(Object.values(catalog).flatMap((keys) => Object.keys(keys)));
|
|
534
|
+
return source.screens.flatMap(({ meta, state }) =>
|
|
535
|
+
Object.values(state.controlMap || {}).flatMap((control) => {
|
|
536
|
+
const known = catalog[control.type];
|
|
537
|
+
if (!known) return [];
|
|
538
|
+
return Object.entries(control.configs || {}).flatMap(([key, value]) => {
|
|
539
|
+
const at = `${meta.name} → ${control.configs && control.configs.name ? control.configs.name : control.id}`;
|
|
540
|
+
const finding = (problem) => ({
|
|
541
|
+
id: `control-configs::${meta.id}:${control.id}:${key}`,
|
|
542
|
+
invariant: 'control-configs',
|
|
543
|
+
message: 'invariant-control-configs',
|
|
544
|
+
subject: `${at}: ${problem}`,
|
|
545
|
+
severity: 'advisory',
|
|
546
|
+
target: { kind: 'control', screenId: meta.id, controlId: control.id },
|
|
547
|
+
});
|
|
548
|
+
if (!(key in known)) {
|
|
549
|
+
return SIDE_PANEL_KEYS.has(key) || anyForm.has(key)
|
|
550
|
+
? []
|
|
551
|
+
: [finding(`\`${key}\` is not a ${control.type} config`)];
|
|
552
|
+
}
|
|
553
|
+
const allowed = known[key];
|
|
554
|
+
// `''` is a dropdown's unset row; objects are bound or authored values.
|
|
555
|
+
if (!allowed || value === undefined || value === null || value === '' || typeof value === 'object')
|
|
556
|
+
return [];
|
|
557
|
+
return allowed.includes(String(value))
|
|
558
|
+
? []
|
|
559
|
+
: [finding(`\`${key}: ${value}\` is not one of ${allowed.filter(Boolean).join(', ')}`)];
|
|
560
|
+
});
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
349
565
|
// Every Violation a read design holds: the editor's Invariants, plus what its
|
|
350
|
-
// gestures would have refused
|
|
351
|
-
|
|
566
|
+
// gestures would have refused, what resolves to no ApiOperation, the rows the
|
|
567
|
+
// codec dropped on read, every body that does not compile, and the EmitGaps.
|
|
568
|
+
function findViolations(
|
|
569
|
+
bundles,
|
|
570
|
+
source,
|
|
571
|
+
catalog,
|
|
572
|
+
operationOf,
|
|
573
|
+
files = {},
|
|
574
|
+
ts = undefined
|
|
575
|
+
) {
|
|
352
576
|
const { codegen, invariants } = bundles;
|
|
353
577
|
return [
|
|
578
|
+
...invariants.codecDrops(files, source, DESIGN_ROOT),
|
|
354
579
|
...placementBreaches(codegen, source.screens),
|
|
355
580
|
...navBreaches(source),
|
|
356
581
|
...rawNavBreaches(source, files),
|
|
357
582
|
...unresolvedMethods(codegen, source.features, operationOf),
|
|
583
|
+
...bodyBreaches(bundles, source, ts),
|
|
584
|
+
...emitGapAdvisories(bundles, source, operationOf),
|
|
585
|
+
...configsAdvisories(controlConfigsCatalog(), source),
|
|
358
586
|
...invariants
|
|
359
587
|
.runInvariants(invariants.invariantScopeOf(source, catalog || []))
|
|
360
588
|
.filter((one) => catalog || one.invariant !== THEME_INVARIANT),
|
|
361
|
-
]
|
|
589
|
+
]
|
|
590
|
+
.map((one) => ({ ...one, text: messageText(one.message) }))
|
|
591
|
+
.sort((a, b) =>
|
|
362
592
|
a.severity === b.severity ? 0 : a.severity === 'blocking' ? -1 : 1
|
|
363
593
|
);
|
|
364
594
|
}
|
|
@@ -419,6 +649,8 @@ async function validate({
|
|
|
419
649
|
|
|
420
650
|
const catalog = themeCatalogOf(root);
|
|
421
651
|
if (!catalog) report.skipped.push(THEME_INVARIANT);
|
|
652
|
+
const ts = appTypeScript(root);
|
|
653
|
+
if (!ts) report.skipped.push('function-body-invalid');
|
|
422
654
|
|
|
423
655
|
// With a spec, StoreMethods resolve against it and `--write` refreshes the
|
|
424
656
|
// snapshot from what they draw; without one, against the snapshot as stored.
|
|
@@ -432,13 +664,10 @@ async function validate({
|
|
|
432
664
|
...source,
|
|
433
665
|
operationOf: codegen.operationResolverOf(operations),
|
|
434
666
|
};
|
|
435
|
-
report.violations =
|
|
436
|
-
bundles,
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
ws.operationOf,
|
|
440
|
-
files
|
|
441
|
-
);
|
|
667
|
+
report.violations = [
|
|
668
|
+
...findViolations(bundles, source, catalog, ws.operationOf, files, ts),
|
|
669
|
+
...pinsBehindStamp(readPackageJson(root), files[`${DESIGN_ROOT}/index.json`]),
|
|
670
|
+
];
|
|
442
671
|
|
|
443
672
|
// What the design already held at the baseline is not this change's to fix:
|
|
444
673
|
// marked, listed, and left out of the status.
|
|
@@ -454,7 +683,8 @@ async function validate({
|
|
|
454
683
|
codegen.operationResolverOf(
|
|
455
684
|
spec === undefined ? before.source.operations : operations
|
|
456
685
|
),
|
|
457
|
-
beforeFiles
|
|
686
|
+
beforeFiles,
|
|
687
|
+
ts
|
|
458
688
|
).map((one) => one.id)
|
|
459
689
|
: []
|
|
460
690
|
);
|
|
@@ -515,6 +745,7 @@ function printReport(report) {
|
|
|
515
745
|
console.log(
|
|
516
746
|
`${mark} ${one.invariant} — ${one.subject} (${JSON.stringify(one.target)})`
|
|
517
747
|
);
|
|
748
|
+
if (one.text !== one.message) console.log(` ${one.text}`);
|
|
518
749
|
});
|
|
519
750
|
report.skipped.forEach((name) =>
|
|
520
751
|
console.log(`⏭️ ${name} skipped — no theme folder on disk`)
|
|
@@ -581,3 +812,11 @@ module.exports = validateCommand;
|
|
|
581
812
|
module.exports.validate = validate;
|
|
582
813
|
module.exports.loadBundles = loadBundles;
|
|
583
814
|
module.exports.readApiSpec = readApiSpec;
|
|
815
|
+
module.exports.findViolations = findViolations;
|
|
816
|
+
module.exports.writeCanonical = writeCanonical;
|
|
817
|
+
module.exports.themeCatalogOf = themeCatalogOf;
|
|
818
|
+
module.exports.appTypeScript = appTypeScript;
|
|
819
|
+
module.exports.readDesignFilesAt = readDesignFilesAt;
|
|
820
|
+
module.exports.messageText = messageText;
|
|
821
|
+
module.exports.pinsBehindStamp = pinsBehindStamp;
|
|
822
|
+
module.exports.DESIGN_ROOT = DESIGN_ROOT;
|