@magmonium/cli 0.8.29 → 0.8.30
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/api-ops.js +130 -0
- package/bin/commands/gen-code.js +1 -0
- package/bin/commands/validate.js +583 -0
- package/codegen/index.js +640 -94
- package/codegen/invariant-catalog.js +2788 -0
- package/codegen/swagger-helper.js +478 -0
- package/package.json +1 -1
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { execFileSync } = require('child_process');
|
|
4
|
+
const { stringify } = require('yaml');
|
|
5
|
+
const pkg = require('../../package.json');
|
|
6
|
+
const { readDesignFiles, isNewer } = require('./gen-code');
|
|
7
|
+
|
|
8
|
+
const DESIGN_ROOT = '.magmonium';
|
|
9
|
+
const THEME_FOLDER = 'theme';
|
|
10
|
+
const THEME_INVARIANT = 'theme-selection-resolves';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The editor's codegen, its Invariant catalogue and its `swagger.json` reader,
|
|
14
|
+
* bundled side by side at build time (ADR 0181). The last two reach codegen as
|
|
15
|
+
* the sibling bundle, so all three read one copy of every helper.
|
|
16
|
+
*/
|
|
17
|
+
function loadBundles() {
|
|
18
|
+
const dir = path.join(__dirname, '..', '..', 'codegen');
|
|
19
|
+
const names = {
|
|
20
|
+
codegen: 'index.js',
|
|
21
|
+
invariants: 'invariant-catalog.js',
|
|
22
|
+
swagger: 'swagger-helper.js',
|
|
23
|
+
};
|
|
24
|
+
const missing = Object.values(names).filter(
|
|
25
|
+
(name) => !fs.existsSync(path.join(dir, name))
|
|
26
|
+
);
|
|
27
|
+
if (missing.length) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`codegen bundle not found in ${dir} (${missing.join(', ')}) — run \`npx nx build cli\` and use dist/libs/cli`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return Object.fromEntries(
|
|
33
|
+
Object.entries(names).map(([key, name]) => [
|
|
34
|
+
key,
|
|
35
|
+
require(path.join(dir, name)),
|
|
36
|
+
])
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The AppTarget's `swagger.json` read the way the editor reads it — a URL, a
|
|
42
|
+
* file, or by default the one the Environment's backend serves (GLOSSARY.md
|
|
43
|
+
* ApiOperation). Never persisted; what outlives the read is the operations
|
|
44
|
+
* snapshot `--write` stores for the StoreMethods drawn from it (ADR 0181).
|
|
45
|
+
*/
|
|
46
|
+
async function readApiSpec(bundles, spec, appTarget) {
|
|
47
|
+
const where =
|
|
48
|
+
spec ||
|
|
49
|
+
(appTarget &&
|
|
50
|
+
bundles.codegen.swaggerUrlFromBackend(
|
|
51
|
+
appTarget.environment.hosts.backendUrl
|
|
52
|
+
));
|
|
53
|
+
if (!where)
|
|
54
|
+
throw new Error('no --spec and no backend URL to read swagger.json from');
|
|
55
|
+
let raw;
|
|
56
|
+
if (/^https?:\/\//i.test(where)) {
|
|
57
|
+
const response = await fetch(where);
|
|
58
|
+
if (!response.ok) throw new Error(`${where} answered ${response.status}`);
|
|
59
|
+
raw = await response.json();
|
|
60
|
+
} else {
|
|
61
|
+
raw = JSON.parse(fs.readFileSync(path.resolve(where), 'utf8'));
|
|
62
|
+
}
|
|
63
|
+
return { where, spec: bundles.swagger.readSpec(raw) };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Every API StoreMethod — a Feature's own and its SubStores' — with where it
|
|
67
|
+
// sits, so one that resolves to no ApiOperation is named rather than emitted
|
|
68
|
+
// wrong. A StorageMethod and a CacheMethod call no endpoint and resolve to none.
|
|
69
|
+
function unresolvedMethods(codegen, features, operationOf) {
|
|
70
|
+
const callsApi = (method) =>
|
|
71
|
+
!codegen.isStorageMethod(method) && !codegen.isCacheMethod(method);
|
|
72
|
+
return Object.values(features).flatMap((feature) =>
|
|
73
|
+
[
|
|
74
|
+
['', feature.methods],
|
|
75
|
+
...Object.entries(feature.subStores || {}).map(([id, sub]) => [
|
|
76
|
+
id,
|
|
77
|
+
sub.methods,
|
|
78
|
+
]),
|
|
79
|
+
].flatMap(([subStoreId, methods]) =>
|
|
80
|
+
Object.values(methods || {})
|
|
81
|
+
.filter((method) => callsApi(method) && !operationOf(method))
|
|
82
|
+
.map((method) => ({
|
|
83
|
+
id: `method-operation-resolves::${feature.id}:${method.id}`,
|
|
84
|
+
invariant: 'method-operation-resolves',
|
|
85
|
+
message: 'invariant-method-operation-unresolved',
|
|
86
|
+
subject: `${feature.name}.${method.name} → ${method.verb} ${method.path}`,
|
|
87
|
+
severity: 'blocking',
|
|
88
|
+
target: {
|
|
89
|
+
kind: 'panel',
|
|
90
|
+
panel: 'store',
|
|
91
|
+
featureId: feature.id,
|
|
92
|
+
...(subStoreId ? { subStoreId } : {}),
|
|
93
|
+
memberId: method.id,
|
|
94
|
+
},
|
|
95
|
+
}))
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* What the editor's gestures refuse and so never wrote as an Invariant
|
|
102
|
+
* (ADR 0041): a Control the ControlRelationship whitelist would not have let
|
|
103
|
+
* land where it sits, a type no ControlType names, a child id with no Control,
|
|
104
|
+
* a `parentId` disagreeing with the `childrens` that list it. A design drawn in
|
|
105
|
+
* the editor cannot hold one; a design written by hand can.
|
|
106
|
+
*/
|
|
107
|
+
function placementBreaches(codegen, screens) {
|
|
108
|
+
const known = new Set(Object.values(codegen.ControlType));
|
|
109
|
+
const breach = (screen, controlId, subject) => ({
|
|
110
|
+
id: `control-placement::${screen.meta.id}:${controlId}`,
|
|
111
|
+
invariant: 'control-placement',
|
|
112
|
+
message: 'invariant-control-placement',
|
|
113
|
+
subject: `${screen.meta.name} → ${subject}`,
|
|
114
|
+
severity: 'blocking',
|
|
115
|
+
target: { kind: 'control', screenId: screen.meta.id, controlId },
|
|
116
|
+
});
|
|
117
|
+
return screens.flatMap((screen) => {
|
|
118
|
+
const map = screen.state.controlMap || {};
|
|
119
|
+
const found = [];
|
|
120
|
+
(screen.state.rootControlIds || []).forEach((id) => {
|
|
121
|
+
if (!map[id]) found.push(breach(screen, id, `root ${id} has no Control`));
|
|
122
|
+
});
|
|
123
|
+
Object.values(map).forEach((control) => {
|
|
124
|
+
if (!known.has(control.type)) {
|
|
125
|
+
found.push(
|
|
126
|
+
breach(screen, control.id, `unknown type "${control.type}"`)
|
|
127
|
+
);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
(control.childrens || []).forEach((childId) => {
|
|
131
|
+
const child = map[childId];
|
|
132
|
+
if (!child) {
|
|
133
|
+
found.push(
|
|
134
|
+
breach(screen, control.id, `child ${childId} has no Control`)
|
|
135
|
+
);
|
|
136
|
+
} else if (child.parentId !== control.id) {
|
|
137
|
+
found.push(
|
|
138
|
+
breach(
|
|
139
|
+
screen,
|
|
140
|
+
childId,
|
|
141
|
+
`parentId is not ${control.type} ${control.id}`
|
|
142
|
+
)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
const parent = control.parentId ? map[control.parentId] : undefined;
|
|
147
|
+
if (control.parentId && !parent) {
|
|
148
|
+
found.push(
|
|
149
|
+
breach(
|
|
150
|
+
screen,
|
|
151
|
+
control.id,
|
|
152
|
+
`parent ${control.parentId} has no Control`
|
|
153
|
+
)
|
|
154
|
+
);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (
|
|
158
|
+
!parent &&
|
|
159
|
+
!(screen.state.rootControlIds || []).includes(control.id)
|
|
160
|
+
) {
|
|
161
|
+
found.push(breach(screen, control.id, 'no parent and not a root'));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// Asked as the drop would have been: the parent without this child, so
|
|
165
|
+
// a cardinality cap counts the siblings and not the Control itself.
|
|
166
|
+
const without = parent
|
|
167
|
+
? {
|
|
168
|
+
...map,
|
|
169
|
+
[parent.id]: {
|
|
170
|
+
...parent,
|
|
171
|
+
childrens: (parent.childrens || []).filter(
|
|
172
|
+
(id) => id !== control.id
|
|
173
|
+
),
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
: map;
|
|
177
|
+
if (
|
|
178
|
+
!codegen.canPlaceChild(
|
|
179
|
+
parent ? without[parent.id] : undefined,
|
|
180
|
+
control.type,
|
|
181
|
+
without
|
|
182
|
+
)
|
|
183
|
+
) {
|
|
184
|
+
found.push(
|
|
185
|
+
breach(
|
|
186
|
+
screen,
|
|
187
|
+
control.id,
|
|
188
|
+
`${control.type} may not sit under ${parent ? parent.type : 'the Screen root'}`
|
|
189
|
+
)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return found;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* What the Nav tree's gestures refuse and so never wrote as an Invariant
|
|
199
|
+
* (ADR 0041): a child id with no Nav, an App Nav no parent lists or two
|
|
200
|
+
* parents list, a child whose navId does not extend its parent's, two Default
|
|
201
|
+
* Navs in one sibling set, a `pageId` naming no routed Screen, a
|
|
202
|
+
* `componentKey` naming a Screen that is not an App Widget.
|
|
203
|
+
*/
|
|
204
|
+
function navBreaches(source, navMap = source.navMap || {}) {
|
|
205
|
+
const screens = new Map(source.screens.map(({ meta }) => [meta.id, meta]));
|
|
206
|
+
const breach = (navId, subject) => ({
|
|
207
|
+
id: `nav-structure::${navId}:${subject}`,
|
|
208
|
+
invariant: 'nav-structure',
|
|
209
|
+
message: 'invariant-nav-structure',
|
|
210
|
+
subject: `${navId} → ${subject}`,
|
|
211
|
+
severity: 'blocking',
|
|
212
|
+
target: { kind: 'nav', navId },
|
|
213
|
+
});
|
|
214
|
+
const found = [];
|
|
215
|
+
const parentsOf = new Map();
|
|
216
|
+
Object.values(navMap).forEach((nav) => {
|
|
217
|
+
(nav.children || []).forEach((childId) => {
|
|
218
|
+
if (!navMap[childId]) {
|
|
219
|
+
found.push(breach(nav.navId, `child ${childId} has no Nav`));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
parentsOf.set(childId, [...(parentsOf.get(childId) || []), nav.navId]);
|
|
223
|
+
if (!childId.startsWith(`${nav.navId}_`)) {
|
|
224
|
+
found.push(
|
|
225
|
+
breach(childId, `navId does not extend its parent ${nav.navId}`)
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
const defaults = (nav.children || []).filter(
|
|
230
|
+
(childId) => navMap[childId] && navMap[childId].isDefault
|
|
231
|
+
);
|
|
232
|
+
if (defaults.length > 1) {
|
|
233
|
+
found.push(
|
|
234
|
+
breach(
|
|
235
|
+
nav.navId,
|
|
236
|
+
`${defaults.length} Default Navs: ${defaults.join(', ')}`
|
|
237
|
+
)
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
if (nav.pageId) {
|
|
241
|
+
const screen = screens.get(nav.pageId);
|
|
242
|
+
if (!screen)
|
|
243
|
+
found.push(breach(nav.navId, `pageId ${nav.pageId} names no Screen`));
|
|
244
|
+
else if (screen.kind !== 'page' && screen.kind !== 'feature') {
|
|
245
|
+
found.push(
|
|
246
|
+
breach(
|
|
247
|
+
nav.navId,
|
|
248
|
+
`pageId names ${screen.kind} ${screen.name}, not a page or feature Screen`
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const widget = nav.componentKey && screens.get(nav.componentKey);
|
|
254
|
+
if (widget && widget.kind !== 'app_widget') {
|
|
255
|
+
found.push(
|
|
256
|
+
breach(
|
|
257
|
+
nav.navId,
|
|
258
|
+
`componentKey names ${widget.kind} ${widget.name}, not an App Widget`
|
|
259
|
+
)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
Object.values(navMap).forEach((nav) => {
|
|
264
|
+
if (nav.navId === 'root') return;
|
|
265
|
+
const parents = parentsOf.get(nav.navId) || [];
|
|
266
|
+
if (!parents.length) found.push(breach(nav.navId, 'no parent lists it'));
|
|
267
|
+
if (parents.length > 1)
|
|
268
|
+
found.push(breach(nav.navId, `listed by ${parents.join(', ')}`));
|
|
269
|
+
});
|
|
270
|
+
return found;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// The same questions of `nav.json` as written, before the codec reads it. The
|
|
274
|
+
// codec repairs an orphaned Nav and a second Default Nav on read, and does so
|
|
275
|
+
// silently — keeping the old default drops the claim that was just written —
|
|
276
|
+
// so a hand-written tree is judged as it stands on disk.
|
|
277
|
+
function rawNavBreaches(source, files) {
|
|
278
|
+
let raw;
|
|
279
|
+
try {
|
|
280
|
+
raw = JSON.parse(files[`${DESIGN_ROOT}/nav.json`] || 'null');
|
|
281
|
+
} catch {
|
|
282
|
+
return [];
|
|
283
|
+
}
|
|
284
|
+
if (!raw || !raw.navMap) return [];
|
|
285
|
+
const seen = new Set(navBreaches(source).map((one) => one.id));
|
|
286
|
+
return navBreaches(source, raw.navMap).filter((one) => !seen.has(one.id));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Theme names off the two tiers the editor's catalogue reads beside the
|
|
290
|
+
// authored ones: the app's `mag_assets/theme/` and the library's. Undefined
|
|
291
|
+
// when neither folder is on disk, so the caller skips the one Invariant that
|
|
292
|
+
// reads it rather than reporting every library theme as missing.
|
|
293
|
+
function themeCatalogOf(root) {
|
|
294
|
+
const dirs = [
|
|
295
|
+
path.join(root, 'mag_assets', THEME_FOLDER),
|
|
296
|
+
path.join(
|
|
297
|
+
root,
|
|
298
|
+
'node_modules',
|
|
299
|
+
'@magmonium',
|
|
300
|
+
'one',
|
|
301
|
+
'mag_assets',
|
|
302
|
+
THEME_FOLDER
|
|
303
|
+
),
|
|
304
|
+
].filter((dir) => fs.existsSync(dir));
|
|
305
|
+
if (!dirs.length) return undefined;
|
|
306
|
+
const names = dirs.flatMap((dir) =>
|
|
307
|
+
fs
|
|
308
|
+
.readdirSync(dir)
|
|
309
|
+
.filter((name) => !name.startsWith('.'))
|
|
310
|
+
.map((name) => name.replace(/\.[^.]+$/, ''))
|
|
311
|
+
);
|
|
312
|
+
return [...new Set(names)].sort();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Rewrites `.magmonium/` as the editor would Save it: canonical key order,
|
|
316
|
+
// compacted ControlMaps, a fresh WriteId. Removes only files inside
|
|
317
|
+
// `.magmonium/` the rewrite no longer produces.
|
|
318
|
+
function writeCanonical(root, codegen, ws, operations, before) {
|
|
319
|
+
const files = codegen.buildDesignSource(ws, operations);
|
|
320
|
+
for (const [rel, contents] of Object.entries(files)) {
|
|
321
|
+
const target = path.join(root, rel);
|
|
322
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
323
|
+
fs.writeFileSync(target, contents);
|
|
324
|
+
}
|
|
325
|
+
const removed = Object.keys(before).filter((rel) => !(rel in files));
|
|
326
|
+
removed.forEach((rel) => fs.rmSync(path.join(root, rel), { force: true }));
|
|
327
|
+
return { written: Object.keys(files).length, removed: removed.length };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// `.magmonium/` as it stood at a git ref, in the shape `readDesignFiles`
|
|
331
|
+
// answers — the design before this change, for `--baseline`.
|
|
332
|
+
function readDesignFilesAt(root, ref) {
|
|
333
|
+
const git = (...args) =>
|
|
334
|
+
execFileSync('git', args, {
|
|
335
|
+
cwd: root,
|
|
336
|
+
encoding: 'utf8',
|
|
337
|
+
maxBuffer: 1 << 28,
|
|
338
|
+
});
|
|
339
|
+
const files = {};
|
|
340
|
+
git('ls-tree', '-r', '--name-only', ref, '--', DESIGN_ROOT)
|
|
341
|
+
.split('\n')
|
|
342
|
+
.filter(Boolean)
|
|
343
|
+
.forEach((rel) => {
|
|
344
|
+
files[rel] = git('show', `${ref}:${rel}`);
|
|
345
|
+
});
|
|
346
|
+
return files;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Every Violation a read design holds: the editor's Invariants, plus what its
|
|
350
|
+
// gestures would have refused and what resolves to no ApiOperation.
|
|
351
|
+
function findViolations(bundles, source, catalog, operationOf, files = {}) {
|
|
352
|
+
const { codegen, invariants } = bundles;
|
|
353
|
+
return [
|
|
354
|
+
...placementBreaches(codegen, source.screens),
|
|
355
|
+
...navBreaches(source),
|
|
356
|
+
...rawNavBreaches(source, files),
|
|
357
|
+
...unresolvedMethods(codegen, source.features, operationOf),
|
|
358
|
+
...invariants
|
|
359
|
+
.runInvariants(invariants.invariantScopeOf(source, catalog || []))
|
|
360
|
+
.filter((one) => catalog || one.invariant !== THEME_INVARIANT),
|
|
361
|
+
].sort((a, b) =>
|
|
362
|
+
a.severity === b.severity ? 0 : a.severity === 'blocking' ? -1 : 1
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Asks of `.magmonium/` what the editor asks of an open design (GLOSSARY.md
|
|
368
|
+
* Invariant, ADR 0041): read it through the DesignSource codec, run every
|
|
369
|
+
* Invariant against it, and emit it in memory. A design that passes opens in
|
|
370
|
+
* the editor as written and generates without throwing.
|
|
371
|
+
*
|
|
372
|
+
* `status`: `unreadable` (the codec refused), `blocking` (a blocking
|
|
373
|
+
* Violation, or the emit threw), `ok` (advisory Violations at most).
|
|
374
|
+
*/
|
|
375
|
+
async function validate({
|
|
376
|
+
root = process.cwd(),
|
|
377
|
+
write = false,
|
|
378
|
+
spec,
|
|
379
|
+
baseline,
|
|
380
|
+
} = {}) {
|
|
381
|
+
const report = {
|
|
382
|
+
status: 'ok',
|
|
383
|
+
rewritten: false,
|
|
384
|
+
violations: [],
|
|
385
|
+
skipped: [],
|
|
386
|
+
emitError: undefined,
|
|
387
|
+
written: undefined,
|
|
388
|
+
};
|
|
389
|
+
if (!fs.existsSync(path.join(root, DESIGN_ROOT, 'index.json'))) {
|
|
390
|
+
return {
|
|
391
|
+
...report,
|
|
392
|
+
status: 'unreadable',
|
|
393
|
+
refusal: { reason: 'absent', path: `${DESIGN_ROOT}/index.json` },
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const bundles = loadBundles();
|
|
398
|
+
const { codegen } = bundles;
|
|
399
|
+
const files = readDesignFiles(root);
|
|
400
|
+
const read = codegen.readDesignSource(files);
|
|
401
|
+
if (!read.ok || !read.source) {
|
|
402
|
+
return {
|
|
403
|
+
...report,
|
|
404
|
+
status: 'unreadable',
|
|
405
|
+
refusal: read.refusal || { reason: 'unreadable' },
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
const source = read.source;
|
|
409
|
+
if (source.codegen && isNewer(source.codegen, pkg.version)) {
|
|
410
|
+
return {
|
|
411
|
+
...report,
|
|
412
|
+
status: 'unreadable',
|
|
413
|
+
refusal: { reason: 'newer', version: source.codegen },
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
// A migration or a repaired row: the editor would report this on its next
|
|
417
|
+
// read, and its next Save would write the change.
|
|
418
|
+
report.rewritten = Boolean(read.rewritten);
|
|
419
|
+
|
|
420
|
+
const catalog = themeCatalogOf(root);
|
|
421
|
+
if (!catalog) report.skipped.push(THEME_INVARIANT);
|
|
422
|
+
|
|
423
|
+
// With a spec, StoreMethods resolve against it and `--write` refreshes the
|
|
424
|
+
// snapshot from what they draw; without one, against the snapshot as stored.
|
|
425
|
+
let operations = source.operations;
|
|
426
|
+
if (spec !== undefined) {
|
|
427
|
+
const fetched = await readApiSpec(bundles, spec, source.appTarget);
|
|
428
|
+
report.spec = fetched.where;
|
|
429
|
+
operations = fetched.spec.operations;
|
|
430
|
+
}
|
|
431
|
+
const ws = {
|
|
432
|
+
...source,
|
|
433
|
+
operationOf: codegen.operationResolverOf(operations),
|
|
434
|
+
};
|
|
435
|
+
report.violations = findViolations(
|
|
436
|
+
bundles,
|
|
437
|
+
source,
|
|
438
|
+
catalog,
|
|
439
|
+
ws.operationOf,
|
|
440
|
+
files
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
// What the design already held at the baseline is not this change's to fix:
|
|
444
|
+
// marked, listed, and left out of the status.
|
|
445
|
+
if (baseline) {
|
|
446
|
+
const beforeFiles = readDesignFilesAt(root, baseline);
|
|
447
|
+
const before = codegen.readDesignSource(beforeFiles);
|
|
448
|
+
const held = new Set(
|
|
449
|
+
before.ok && before.source
|
|
450
|
+
? findViolations(
|
|
451
|
+
bundles,
|
|
452
|
+
before.source,
|
|
453
|
+
catalog,
|
|
454
|
+
codegen.operationResolverOf(
|
|
455
|
+
spec === undefined ? before.source.operations : operations
|
|
456
|
+
),
|
|
457
|
+
beforeFiles
|
|
458
|
+
).map((one) => one.id)
|
|
459
|
+
: []
|
|
460
|
+
);
|
|
461
|
+
report.baseline = baseline;
|
|
462
|
+
report.violations = report.violations.map((one) =>
|
|
463
|
+
held.has(one.id) ? { ...one, preexisting: true } : one
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
try {
|
|
467
|
+
codegen.emitGeneratedFiles(ws, stringify, false);
|
|
468
|
+
} catch (error) {
|
|
469
|
+
report.emitError = error && error.message ? error.message : String(error);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (
|
|
473
|
+
report.emitError ||
|
|
474
|
+
report.violations.some(
|
|
475
|
+
(one) => one.severity === 'blocking' && !one.preexisting
|
|
476
|
+
)
|
|
477
|
+
) {
|
|
478
|
+
report.status = 'blocking';
|
|
479
|
+
}
|
|
480
|
+
if (write && report.status !== 'blocking') {
|
|
481
|
+
report.written = writeCanonical(
|
|
482
|
+
root,
|
|
483
|
+
codegen,
|
|
484
|
+
ws,
|
|
485
|
+
spec === undefined ? source.operations : codegen.drawnOperations(ws),
|
|
486
|
+
files
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
return report;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const EXIT = { ok: 0, blocking: 1, unreadable: 2 };
|
|
493
|
+
|
|
494
|
+
function printReport(report) {
|
|
495
|
+
if (report.status === 'unreadable') {
|
|
496
|
+
const r = report.refusal || {};
|
|
497
|
+
console.error(
|
|
498
|
+
`❌ validate: cannot read ${DESIGN_ROOT}/ (${r.reason || 'unreadable'}${
|
|
499
|
+
r.path ? `: ${r.path}` : ''
|
|
500
|
+
}${r.version ? `, v${r.version}` : ''})`
|
|
501
|
+
);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (report.rewritten) {
|
|
505
|
+
console.warn(
|
|
506
|
+
`⚠️ validate: the codec migrated or repaired a row — run with --write to store it`
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
report.violations.forEach((one) => {
|
|
510
|
+
const mark = one.preexisting
|
|
511
|
+
? '· '
|
|
512
|
+
: one.severity === 'blocking'
|
|
513
|
+
? '❌'
|
|
514
|
+
: '⚠️ ';
|
|
515
|
+
console.log(
|
|
516
|
+
`${mark} ${one.invariant} — ${one.subject} (${JSON.stringify(one.target)})`
|
|
517
|
+
);
|
|
518
|
+
});
|
|
519
|
+
report.skipped.forEach((name) =>
|
|
520
|
+
console.log(`⏭️ ${name} skipped — no theme folder on disk`)
|
|
521
|
+
);
|
|
522
|
+
if (report.emitError) console.error(`❌ emit: ${report.emitError}`);
|
|
523
|
+
if (report.written) {
|
|
524
|
+
console.log(
|
|
525
|
+
`📝 ${DESIGN_ROOT}/ rewritten: ${report.written.written} file(s), ${report.written.removed} removed`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
const fresh = report.violations.filter((one) => !one.preexisting);
|
|
529
|
+
const blocking = fresh.filter((one) => one.severity === 'blocking');
|
|
530
|
+
const held = report.violations.length - fresh.length;
|
|
531
|
+
const tail = held ? `, ${held} already at ${report.baseline}` : '';
|
|
532
|
+
console.log(
|
|
533
|
+
report.status === 'ok'
|
|
534
|
+
? `✅ validate: ${fresh.length} advisory${tail}`
|
|
535
|
+
: `❌ validate: ${blocking.length} blocking, ${
|
|
536
|
+
fresh.length - blocking.length
|
|
537
|
+
} advisory${tail}${report.emitError ? ', emit failed' : ''}`
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function validateCommand(program) {
|
|
542
|
+
program
|
|
543
|
+
.command('validate')
|
|
544
|
+
.description(
|
|
545
|
+
'Check .magmonium/ the way the editor does: DesignSource codec, every Invariant, an in-memory emit'
|
|
546
|
+
)
|
|
547
|
+
.option('--root <path>', 'App root (default: cwd)')
|
|
548
|
+
.option('--json', 'Print the report as JSON')
|
|
549
|
+
.option(
|
|
550
|
+
'--spec [urlOrFile]',
|
|
551
|
+
"Resolve StoreMethods against swagger.json (default: the Environment backend's) instead of the stored operations snapshot"
|
|
552
|
+
)
|
|
553
|
+
.option(
|
|
554
|
+
'--baseline <ref>',
|
|
555
|
+
'Mark Violations the design already held at this git ref as preexisting; only new ones decide the status'
|
|
556
|
+
)
|
|
557
|
+
.option(
|
|
558
|
+
'--write',
|
|
559
|
+
'When nothing blocks, rewrite .magmonium/ in the form the editor saves it (canonical order, fresh write-id)'
|
|
560
|
+
)
|
|
561
|
+
.action(async (options) => {
|
|
562
|
+
let report;
|
|
563
|
+
try {
|
|
564
|
+
report = await validate({
|
|
565
|
+
root: options.root || process.cwd(),
|
|
566
|
+
write: Boolean(options.write),
|
|
567
|
+
spec: options.spec === true ? '' : options.spec,
|
|
568
|
+
baseline: options.baseline,
|
|
569
|
+
});
|
|
570
|
+
} catch (error) {
|
|
571
|
+
console.error(`❌ validate: ${error.message}`);
|
|
572
|
+
process.exit(2);
|
|
573
|
+
}
|
|
574
|
+
if (options.json) console.log(JSON.stringify(report, null, 2));
|
|
575
|
+
else printReport(report);
|
|
576
|
+
process.exit(EXIT[report.status]);
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
module.exports = validateCommand;
|
|
581
|
+
module.exports.validate = validate;
|
|
582
|
+
module.exports.loadBundles = loadBundles;
|
|
583
|
+
module.exports.readApiSpec = readApiSpec;
|