@magmonium/cli 0.8.28 → 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 +1008 -131
- package/codegen/invariant-catalog.js +2788 -0
- package/codegen/swagger-helper.js +478 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -24,6 +24,8 @@ const generateCommand = require('./commands/generate');
|
|
|
24
24
|
const patchWcEnvCommand = require('./commands/patch-wc-env');
|
|
25
25
|
const genCodeCommand = require('./commands/gen-code');
|
|
26
26
|
const genTestCommand = require('./commands/gen-test');
|
|
27
|
+
const validateCommand = require('./commands/validate');
|
|
28
|
+
const apiOpsCommand = require('./commands/api-ops');
|
|
27
29
|
|
|
28
30
|
const program = new Command();
|
|
29
31
|
|
|
@@ -57,5 +59,7 @@ generateCommand(program);
|
|
|
57
59
|
patchWcEnvCommand(program);
|
|
58
60
|
genCodeCommand(program);
|
|
59
61
|
genTestCommand(program);
|
|
62
|
+
validateCommand(program);
|
|
63
|
+
apiOpsCommand(program);
|
|
60
64
|
|
|
61
65
|
program.parse();
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { readDesignFiles } = require('./gen-code');
|
|
4
|
+
const { loadBundles, readApiSpec } = require('./validate');
|
|
5
|
+
|
|
6
|
+
const DESIGN_ROOT = '.magmonium';
|
|
7
|
+
|
|
8
|
+
// The design as read, when there is one: its AppTarget names the default spec
|
|
9
|
+
// URL, and its StoreMethods mark which ApiOperations are already drawn.
|
|
10
|
+
function designOf(root, codegen) {
|
|
11
|
+
if (!fs.existsSync(path.join(root, DESIGN_ROOT, 'index.json')))
|
|
12
|
+
return undefined;
|
|
13
|
+
const read = codegen.readDesignSource(readDesignFiles(root));
|
|
14
|
+
return read.ok ? read.source : undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function drawnIds(source) {
|
|
18
|
+
const ids = new Set();
|
|
19
|
+
Object.values(source ? source.features : {}).forEach((feature) =>
|
|
20
|
+
[
|
|
21
|
+
feature.methods,
|
|
22
|
+
...Object.values(feature.subStores || {}).map((sub) => sub.methods),
|
|
23
|
+
].forEach((methods) =>
|
|
24
|
+
Object.values(methods || {}).forEach((method) =>
|
|
25
|
+
ids.add(`${method.verb} ${method.path}`)
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
);
|
|
29
|
+
return ids;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The AppTarget's ApiOperations as the editor's API picker walks them: by
|
|
34
|
+
* domain, then path and verb (GLOSSARY.md ApiOperation). No filter lists the
|
|
35
|
+
* domains; `domain` lists its operations; `op` prints one whole — params,
|
|
36
|
+
* responses and the StateShapes they mint — for a StoreMethod to be drawn
|
|
37
|
+
* from.
|
|
38
|
+
*/
|
|
39
|
+
async function apiOps({ root = process.cwd(), spec, domain, op } = {}) {
|
|
40
|
+
const bundles = loadBundles();
|
|
41
|
+
const source = designOf(root, bundles.codegen);
|
|
42
|
+
const read = await readApiSpec(bundles, spec, source && source.appTarget);
|
|
43
|
+
const drawn = drawnIds(source);
|
|
44
|
+
// A listing row carries the summary's first sentence; `op` prints it whole.
|
|
45
|
+
const row = (one) => ({
|
|
46
|
+
id: one.id,
|
|
47
|
+
summary: (one.summary || '').split(/(?<=\.)\s/)[0].slice(0, 120),
|
|
48
|
+
drawn: drawn.has(one.id),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (op) {
|
|
52
|
+
const found = read.spec.operations.find((one) => one.id === op);
|
|
53
|
+
if (!found) throw new Error(`no ApiOperation "${op}" in ${read.where}`);
|
|
54
|
+
return {
|
|
55
|
+
spec: read.where,
|
|
56
|
+
operation: { ...found, drawn: drawn.has(found.id) },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (domain) {
|
|
60
|
+
const ops = read.spec.byDomain[domain];
|
|
61
|
+
if (!ops) throw new Error(`no domain "${domain}" in ${read.where}`);
|
|
62
|
+
return { spec: read.where, domain, operations: ops.map(row) };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
spec: read.where,
|
|
66
|
+
domains: Object.keys(read.spec.byDomain)
|
|
67
|
+
.sort()
|
|
68
|
+
.map((name) => ({
|
|
69
|
+
domain: name,
|
|
70
|
+
operations: read.spec.byDomain[name].length,
|
|
71
|
+
drawn: read.spec.byDomain[name].filter((one) => drawn.has(one.id))
|
|
72
|
+
.length,
|
|
73
|
+
})),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function printResult(result) {
|
|
78
|
+
if (result.operation) {
|
|
79
|
+
console.log(JSON.stringify(result.operation, null, 2));
|
|
80
|
+
} else if (result.operations) {
|
|
81
|
+
result.operations.forEach((one) =>
|
|
82
|
+
console.log(
|
|
83
|
+
`${one.drawn ? '✔' : ' '} ${one.id}${one.summary ? ` — ${one.summary}` : ''}`
|
|
84
|
+
)
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
result.domains.forEach((one) =>
|
|
88
|
+
console.log(
|
|
89
|
+
`${one.domain}: ${one.operations} operation(s), ${one.drawn} drawn`
|
|
90
|
+
)
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function apiOpsCommand(program) {
|
|
96
|
+
program
|
|
97
|
+
.command('api-ops')
|
|
98
|
+
.description(
|
|
99
|
+
"Browse the AppTarget's swagger.json the way the editor's API picker does: domains, a domain's operations, one operation whole"
|
|
100
|
+
)
|
|
101
|
+
.option('--root <path>', 'App root (default: cwd)')
|
|
102
|
+
.option(
|
|
103
|
+
'--spec <urlOrFile>',
|
|
104
|
+
"swagger.json (default: the Environment backend's)"
|
|
105
|
+
)
|
|
106
|
+
.option('--domain <name>', "List one domain's operations")
|
|
107
|
+
.option(
|
|
108
|
+
'--op <id>',
|
|
109
|
+
'Print one operation, e.g. "get /wallet/v1/coins/{id}/"'
|
|
110
|
+
)
|
|
111
|
+
.option('--json', 'Print as JSON')
|
|
112
|
+
.action(async (options) => {
|
|
113
|
+
try {
|
|
114
|
+
const result = await apiOps({
|
|
115
|
+
root: options.root || process.cwd(),
|
|
116
|
+
spec: options.spec,
|
|
117
|
+
domain: options.domain,
|
|
118
|
+
op: options.op,
|
|
119
|
+
});
|
|
120
|
+
if (options.json) console.log(JSON.stringify(result, null, 2));
|
|
121
|
+
else printResult(result);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error(`❌ api-ops: ${error.message}`);
|
|
124
|
+
process.exit(1);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
module.exports = apiOpsCommand;
|
|
130
|
+
module.exports.apiOps = apiOps;
|
package/bin/commands/gen-code.js
CHANGED