create-erxes 0.0.0-stage → 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/README.md +23 -2
- package/dist/index.js +481 -0
- package/dist/templates/base/.agents/skills/endpoint/SKILL.md +20 -0
- package/dist/templates/base/.agents/skills/schema-change/SKILL.md +28 -0
- package/dist/templates/base/.agents/skills/verify/SKILL.md +24 -0
- package/dist/templates/base/.github/workflows/ci.yml +44 -0
- package/dist/templates/base/AGENTS.md +59 -0
- package/dist/templates/base/CLAUDE.md +1 -0
- package/dist/templates/base/Dockerfile +13 -0
- package/dist/templates/base/_dockerignore +4 -0
- package/dist/templates/base/_env.example +15 -0
- package/dist/templates/base/_gitignore +11 -0
- package/dist/templates/base/_node-version +1 -0
- package/dist/templates/base/api/AGENTS.md +22 -0
- package/dist/templates/base/api/drizzle/0000_items.sql +13 -0
- package/dist/templates/base/api/drizzle/meta/0000_snapshot.json +126 -0
- package/dist/templates/base/api/drizzle/meta/_journal.json +13 -0
- package/dist/templates/base/api/drizzle.config.ts +16 -0
- package/dist/templates/base/api/package.json +47 -0
- package/dist/templates/base/api/src/__tests__/server.test.ts +51 -0
- package/dist/templates/base/api/src/config.ts +43 -0
- package/dist/templates/base/api/src/core.ts +37 -0
- package/dist/templates/base/api/src/db/client.ts +30 -0
- package/dist/templates/base/api/src/db/migrate.ts +80 -0
- package/dist/templates/base/api/src/db/schema.ts +34 -0
- package/dist/templates/base/api/src/db/table.ts +8 -0
- package/dist/templates/base/api/src/logger.ts +5 -0
- package/dist/templates/base/api/src/main.ts +23 -0
- package/dist/templates/base/api/src/migrate.ts +9 -0
- package/dist/templates/base/api/src/modules/items/__tests__/items.test.ts +92 -0
- package/dist/templates/base/api/src/modules/items/items.ts +127 -0
- package/dist/templates/base/api/src/orpc.ts +25 -0
- package/dist/templates/base/api/src/router.ts +10 -0
- package/dist/templates/base/api/src/server.ts +58 -0
- package/dist/templates/base/api/src/telemetry.ts +27 -0
- package/dist/templates/base/api/src/test/core-functions.sql +18 -0
- package/dist/templates/base/api/src/test/setup.ts +68 -0
- package/dist/templates/base/api/tsconfig.json +9 -0
- package/dist/templates/base/knip.ts +21 -0
- package/dist/templates/base/package.json +30 -0
- package/dist/templates/base/pnpm-workspace.yaml +20 -0
- package/dist/templates/base/tools/check-generated.ts +70 -0
- package/dist/templates/base/tools/check-rls.sh +30 -0
- package/dist/templates/base/tools/check-rls.sql +109 -0
- package/dist/templates/base/tools/no-org-id.sql +5 -0
- package/dist/templates/base/tools/test/no-skips.ts +58 -0
- package/dist/templates/base/tsconfig.base.json +16 -0
- package/dist/templates/base/tsconfig.json +9 -0
- package/dist/templates/base/ui/AGENTS.md +34 -0
- package/dist/templates/base/ui/package.json +35 -0
- package/dist/templates/base/ui/rsbuild.config.ts +39 -0
- package/dist/templates/base/ui/src/__Pascal__.tsx +14 -0
- package/dist/templates/base/ui/src/config.tsx +11 -0
- package/dist/templates/base/ui/src/index.ts +3 -0
- package/dist/templates/base/ui/src/items/ItemSheet.tsx +107 -0
- package/dist/templates/base/ui/src/items/ItemsPage.tsx +143 -0
- package/dist/templates/base/ui/src/items/useItems.ts +79 -0
- package/dist/templates/base/ui/src/orpc.ts +32 -0
- package/dist/templates/base/ui/src/styles.css +8 -0
- package/dist/templates/base/ui/tsconfig.json +11 -0
- package/dist/templates/base/vite.config.ts +62 -0
- package/dist/templates/migrate/.agents/skills/plugin-migrate/SKILL.md +66 -0
- package/dist/templates/migrate/migration/checklist.ts +429 -0
- package/dist/templates/migrate/migration/core-map.ts +150 -0
- package/dist/templates/migrate/migration/import-map.ts +4 -0
- package/dist/templates/migrate/migration/inventory.ts +881 -0
- package/dist/templates/migrate/package.json +15 -0
- package/dist/templates/migrate/scripts/import-mongo.ts +143 -0
- package/package.json +32 -3
|
@@ -0,0 +1,881 @@
|
|
|
1
|
+
// Inventory of one old erxes plugin: what it declares, stores, serves and calls.
|
|
2
|
+
// It reads git objects only (rev-parse, ls-tree, cat-file), never the working
|
|
3
|
+
// tree or a database, and writes code metadata (paths, names, counts) to
|
|
4
|
+
// migration/inventory.json. Ported from erxes tools/inventory/old-erxes.ts.
|
|
5
|
+
//
|
|
6
|
+
// pnpm migrate:inventory -- --from <old erxes checkout> --plugin <name> [--ref origin/main]
|
|
7
|
+
//
|
|
8
|
+
// With no flags it reruns against the source recorded in inventory.json.
|
|
9
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
10
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { dirname, join, normalize, resolve } from 'node:path';
|
|
12
|
+
import { parseArgs } from 'node:util';
|
|
13
|
+
import { Lang, parse, type SgNode } from '@ast-grep/napi';
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
|
|
16
|
+
const output = resolve(import.meta.dirname, 'inventory.json');
|
|
17
|
+
|
|
18
|
+
// One order on every machine, so a rerun on another one changes no line.
|
|
19
|
+
const alphabetical = (a: string, b: string) => a.localeCompare(b, 'en');
|
|
20
|
+
const argv = process.argv.slice(2);
|
|
21
|
+
|
|
22
|
+
const { values: args } = parseArgs({
|
|
23
|
+
// `pnpm migrate:inventory -- …` passes the `--` through.
|
|
24
|
+
args: argv[0] === '--' ? argv.slice(1) : argv,
|
|
25
|
+
options: {
|
|
26
|
+
from: { type: 'string' },
|
|
27
|
+
plugin: { type: 'string' },
|
|
28
|
+
ref: { type: 'string' },
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const Recorded = z.object({
|
|
33
|
+
source: z.object({ from: z.string(), plugin: z.string(), ref: z.string() }),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const recorded = existsSync(output)
|
|
37
|
+
? Recorded.safeParse(JSON.parse(readFileSync(output, 'utf8'))).data?.source
|
|
38
|
+
: undefined;
|
|
39
|
+
|
|
40
|
+
const given = args.from ?? recorded?.from;
|
|
41
|
+
const plugin = args.plugin ?? recorded?.plugin;
|
|
42
|
+
const ref = args.ref ?? recorded?.ref ?? 'origin/main';
|
|
43
|
+
|
|
44
|
+
if (!given || !plugin) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
'Usage: pnpm migrate:inventory -- --from <old erxes checkout> --plugin <name> [--ref origin/main]',
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Git takes all three as arguments, so none may read as an option.
|
|
51
|
+
if (!/^[a-z][a-z0-9_]*$/.test(plugin)) throw new Error(`Not an old plugin name: ${plugin}`);
|
|
52
|
+
|
|
53
|
+
if (!/^\w[\w./-]*$/.test(ref)) throw new Error(`Not a git ref: ${ref}`);
|
|
54
|
+
|
|
55
|
+
const from = resolve(given);
|
|
56
|
+
|
|
57
|
+
const started = performance.now();
|
|
58
|
+
const apiRoot = `backend/plugins/${plugin}_api/`;
|
|
59
|
+
const uiRoot = `frontend/plugins/${plugin}_ui/`;
|
|
60
|
+
const sourceFile = /\.(ts|tsx|js|jsx|mts|cts)$/;
|
|
61
|
+
const testFile = /(?:\/__tests__\/)|(?:\.(?:test|spec)\.\w+$)/;
|
|
62
|
+
const workerDir = /^(workers?|crons?)$/i;
|
|
63
|
+
const workerName = /^(worker|cron)|(workers?|crons?)$/i;
|
|
64
|
+
const eventHelper = /^(sendDbEventLog|sendEvent|sendAutomation\w*|sendNotification\w*)$/;
|
|
65
|
+
const webhookPath = /webhook|receive|callback|hook\b/i;
|
|
66
|
+
const httpMethods = new Set(['get', 'post', 'put', 'patch', 'delete', 'all', 'use']);
|
|
67
|
+
|
|
68
|
+
// Calls and constructors that register a queue worker or a schedule.
|
|
69
|
+
const workerApis = new Set([
|
|
70
|
+
'new Worker',
|
|
71
|
+
'new CronJob',
|
|
72
|
+
'createWorker',
|
|
73
|
+
'createMQWorkerWithListeners',
|
|
74
|
+
'upsertJobScheduler',
|
|
75
|
+
'cron.schedule',
|
|
76
|
+
'scheduleJob',
|
|
77
|
+
'agenda.define',
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Meta registrations: which array, in which files, names one registered thing.
|
|
82
|
+
* Permission actions are objects; the string lists under `defaultGroups` grant
|
|
83
|
+
* them to a default role and are not new actions.
|
|
84
|
+
*/
|
|
85
|
+
const REGISTRATIONS = [
|
|
86
|
+
{
|
|
87
|
+
kind: 'permission',
|
|
88
|
+
file: /\/meta\/permissions/,
|
|
89
|
+
key: 'actions',
|
|
90
|
+
id: ['name'],
|
|
91
|
+
objectsOnly: true,
|
|
92
|
+
},
|
|
93
|
+
{ kind: 'default role', file: /\/meta\/permissions/, key: 'defaultGroups', id: ['id', 'name'] },
|
|
94
|
+
{ kind: 'notification', file: /\/meta\/notifications/, key: 'events', id: ['name'] },
|
|
95
|
+
{ kind: 'automation trigger', file: /automation/i, key: 'triggers', id: ['type', 'label'] },
|
|
96
|
+
{ kind: 'automation action', file: /automation/i, key: 'actions', id: ['type', 'label'] },
|
|
97
|
+
{ kind: 'segment', file: /segment/i, key: 'contentTypes', id: ['type', 'contentType'] },
|
|
98
|
+
{
|
|
99
|
+
kind: 'import-export',
|
|
100
|
+
file: /(?:import-export)|(?:\/main\.ts$)/,
|
|
101
|
+
key: 'types',
|
|
102
|
+
id: ['contentType'],
|
|
103
|
+
},
|
|
104
|
+
] as const;
|
|
105
|
+
|
|
106
|
+
type Field = string | { dynamic: string } | { missing: true };
|
|
107
|
+
|
|
108
|
+
const git = (...gitArgs: string[]) =>
|
|
109
|
+
execFileSync('git', ['-C', from, ...gitArgs], { encoding: 'utf8', maxBuffer: 1 << 28 });
|
|
110
|
+
|
|
111
|
+
/** Reads every blob in one `git cat-file --batch` process. */
|
|
112
|
+
const readBlobs = (sha: string, paths: string[]) => {
|
|
113
|
+
const result = spawnSync('git', ['-C', from, 'cat-file', '--batch'], {
|
|
114
|
+
input: paths.map((path) => `${sha}:${path}\n`).join(''),
|
|
115
|
+
maxBuffer: 1 << 30,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (result.status !== 0) throw new Error(`git cat-file failed: ${result.stderr.toString()}`);
|
|
119
|
+
|
|
120
|
+
const out = result.stdout;
|
|
121
|
+
const blobs = new Map<string, string>();
|
|
122
|
+
let offset = 0;
|
|
123
|
+
|
|
124
|
+
for (const path of paths) {
|
|
125
|
+
const headerEnd = out.indexOf(10, offset);
|
|
126
|
+
const [, type, size] = out.subarray(offset, headerEnd).toString().split(' ');
|
|
127
|
+
|
|
128
|
+
if (type !== 'blob') throw new Error(`${path} is not a blob (${type})`);
|
|
129
|
+
|
|
130
|
+
const start = headerEnd + 1;
|
|
131
|
+
|
|
132
|
+
blobs.set(path, out.subarray(start, start + Number(size)).toString('utf8'));
|
|
133
|
+
offset = start + Number(size) + 1;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return blobs;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/** The module a file belongs to: `src/modules/<module>/…`, else `(plugin)`. */
|
|
140
|
+
const moduleOf = (path: string) => /\/src\/modules\/([^/]+)\//.exec(path)?.[1] ?? '(plugin)';
|
|
141
|
+
|
|
142
|
+
const sourceText = (node: SgNode) => node.text().replaceAll(/\s+/g, ' ');
|
|
143
|
+
|
|
144
|
+
const unwrap = (node: SgNode): SgNode => {
|
|
145
|
+
const wrapped = ['parenthesized_expression', 'as_expression', 'satisfies_expression'];
|
|
146
|
+
const inner = node.namedChildren()[0];
|
|
147
|
+
|
|
148
|
+
return inner && wrapped.some((kind) => node.is(kind)) ? unwrap(inner) : node;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/** The value of a string literal or a template without substitutions. */
|
|
152
|
+
const literal = (node: SgNode) => {
|
|
153
|
+
if (node.kind() === 'string') return node.text().slice(1, -1);
|
|
154
|
+
|
|
155
|
+
if (node.kind() === 'template_string' && !node.has({ rule: { kind: 'template_substitution' } }))
|
|
156
|
+
return node.text().slice(1, -1);
|
|
157
|
+
|
|
158
|
+
return undefined;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** A `key: value` pair of an object literal. */
|
|
162
|
+
const pairOf = (object: SgNode, key: string) =>
|
|
163
|
+
object
|
|
164
|
+
.namedChildren()
|
|
165
|
+
.find(
|
|
166
|
+
(member) =>
|
|
167
|
+
member.kind() === 'pair' &&
|
|
168
|
+
(literal(member.field('key') ?? member) ?? member.field('key')?.text()) === key,
|
|
169
|
+
)
|
|
170
|
+
?.field('value');
|
|
171
|
+
|
|
172
|
+
const field = (object: SgNode, key: string): Field | undefined => {
|
|
173
|
+
const spreads: string[] = [];
|
|
174
|
+
|
|
175
|
+
for (const member of object.namedChildren()) {
|
|
176
|
+
const name = member.field('key');
|
|
177
|
+
const value = member.field('value');
|
|
178
|
+
|
|
179
|
+
if (member.kind() === 'pair' && name && value) {
|
|
180
|
+
if ((literal(name) ?? name.text()) === key)
|
|
181
|
+
return literal(unwrap(value)) ?? { dynamic: sourceText(value) };
|
|
182
|
+
} else if (member.kind() === 'shorthand_property_identifier' && member.text() === key) {
|
|
183
|
+
return { dynamic: key };
|
|
184
|
+
} else if (member.kind() === 'spread_element') {
|
|
185
|
+
spreads.push(sourceText(member));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return spreads.length > 0 ? { dynamic: spreads.join(', ') } : undefined;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** `foo(...)` is `foo`, `a.b.foo(...)` is `foo`, `cron.schedule(...)` is `cron.schedule`. */
|
|
193
|
+
const calleeName = (call: SgNode) => {
|
|
194
|
+
const callee = call.field('function') ?? call.field('constructor');
|
|
195
|
+
const property = callee?.field('property');
|
|
196
|
+
|
|
197
|
+
if (!callee) return '';
|
|
198
|
+
|
|
199
|
+
if (callee.kind() === 'identifier') return callee.text();
|
|
200
|
+
|
|
201
|
+
if (!property) return sourceText(callee);
|
|
202
|
+
|
|
203
|
+
const object = callee.field('object');
|
|
204
|
+
|
|
205
|
+
return object?.kind() === 'identifier' && workerApis.has(`${object.text()}.${property.text()}`)
|
|
206
|
+
? `${object.text()}.${property.text()}`
|
|
207
|
+
: property.text();
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const argumentsOf = (call: SgNode) =>
|
|
211
|
+
(call.field('arguments')?.namedChildren() ?? []).map((arg) => unwrap(arg));
|
|
212
|
+
|
|
213
|
+
const lineOf = (node: SgNode) => node.range().start.line + 1;
|
|
214
|
+
|
|
215
|
+
/** The name a value is assigned to: `const x = …`, `x: …` or `export default`. */
|
|
216
|
+
const assignedName = (node: SgNode) => {
|
|
217
|
+
for (const ancestor of node.ancestors()) {
|
|
218
|
+
if (ancestor.kind() === 'variable_declarator') return ancestor.field('name')?.text();
|
|
219
|
+
|
|
220
|
+
if (ancestor.kind() === 'pair') return ancestor.field('key')?.text();
|
|
221
|
+
|
|
222
|
+
if (ancestor.kind() === 'export_statement') return 'default';
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return undefined;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// GraphQL SDL ------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
/** Removes balanced `( … )` groups, so `a(x: [Int]): T` reads `a: T`. */
|
|
231
|
+
const dropArguments = (text: string) => {
|
|
232
|
+
let out = '';
|
|
233
|
+
let depth = 0;
|
|
234
|
+
|
|
235
|
+
for (const char of text) {
|
|
236
|
+
if (char === '(') depth += 1;
|
|
237
|
+
else if (char === ')') depth -= 1;
|
|
238
|
+
else if (depth === 0) out += char;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return out;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
/** Field names and return types in an SDL field list. */
|
|
245
|
+
const fieldsOf = (body: string) =>
|
|
246
|
+
[...dropArguments(body).matchAll(/([A-Za-z_]\w*)\s*:\s*(\[?\s*[A-Za-z_]\w*!?\s*\]?!?)/g)].map(
|
|
247
|
+
(match) => ({ name: match[1] ?? '', returns: (match[2] ?? '').replaceAll(/\s+/g, '') }),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const OPERATION_BLOCK = /(?:extend\s+)?type\s+(Query|Mutation|Subscription)\s*\{([^}]*)\}/g;
|
|
251
|
+
const DEFINITION = /\b(type|input|enum|interface|union|scalar)\s+([A-Za-z_]\w*)/g;
|
|
252
|
+
|
|
253
|
+
const sdlOf = (template: SgNode, name: string) => {
|
|
254
|
+
const text = template
|
|
255
|
+
.text()
|
|
256
|
+
.slice(1, -1)
|
|
257
|
+
.replaceAll(/\$\{[^}]*\}/g, '')
|
|
258
|
+
.replaceAll(/#[^\n]*/g, '');
|
|
259
|
+
|
|
260
|
+
const operations: { kind: string; name: string; returns: string }[] = [];
|
|
261
|
+
const types: { kind: string; name: string }[] = [];
|
|
262
|
+
|
|
263
|
+
const listKind = /subscriptions$/i.test(name)
|
|
264
|
+
? 'subscription'
|
|
265
|
+
: /mutations$/i.test(name)
|
|
266
|
+
? 'mutation'
|
|
267
|
+
: /queries$/i.test(name)
|
|
268
|
+
? 'query'
|
|
269
|
+
: undefined;
|
|
270
|
+
|
|
271
|
+
if (listKind) {
|
|
272
|
+
for (const op of fieldsOf(text)) operations.push({ kind: listKind, ...op });
|
|
273
|
+
|
|
274
|
+
return { operations, types };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
for (const [, kind = '', body = ''] of text.matchAll(OPERATION_BLOCK))
|
|
278
|
+
for (const op of fieldsOf(body)) operations.push({ kind: kind.toLowerCase(), ...op });
|
|
279
|
+
|
|
280
|
+
for (const [, kind = '', typeName = ''] of text
|
|
281
|
+
.replaceAll(OPERATION_BLOCK, '')
|
|
282
|
+
.matchAll(DEFINITION))
|
|
283
|
+
types.push({ kind, name: typeName });
|
|
284
|
+
|
|
285
|
+
return { operations, types };
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// Mongoose ---------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
const typeOf = (value: SgNode): string => {
|
|
291
|
+
const node = unwrap(value);
|
|
292
|
+
|
|
293
|
+
if (node.kind() === 'array') {
|
|
294
|
+
const first = node.namedChildren()[0];
|
|
295
|
+
|
|
296
|
+
return `[${first ? typeOf(first) : 'Mixed'}]`;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (node.kind() === 'object') {
|
|
300
|
+
const type = pairOf(node, 'type');
|
|
301
|
+
|
|
302
|
+
return type ? typeOf(type) : 'object';
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (node.kind() === 'new_expression' && calleeName(node) === 'Schema') return 'subdocument';
|
|
306
|
+
|
|
307
|
+
return sourceText(node).replace(/^(mongoose\.)?Schema\.Types\./, '');
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const schemaFieldsOf = (object: SgNode) =>
|
|
311
|
+
object.namedChildren().flatMap((member) => {
|
|
312
|
+
const key = member.field('key');
|
|
313
|
+
const value = member.field('value');
|
|
314
|
+
|
|
315
|
+
if (member.kind() === 'spread_element') return [{ name: sourceText(member), type: 'spread' }];
|
|
316
|
+
|
|
317
|
+
if (member.kind() !== 'pair' || !key || !value) return [];
|
|
318
|
+
|
|
319
|
+
const options = unwrap(value).kind() === 'object' ? unwrap(value) : undefined;
|
|
320
|
+
|
|
321
|
+
return [
|
|
322
|
+
{
|
|
323
|
+
name: literal(key) ?? key.text(),
|
|
324
|
+
type: typeOf(value),
|
|
325
|
+
...(options && pairOf(options, 'required') && { required: true }),
|
|
326
|
+
...(options && pairOf(options, 'default') && { default: true }),
|
|
327
|
+
},
|
|
328
|
+
];
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// Scanning ---------------------------------------------------------------------
|
|
332
|
+
|
|
333
|
+
const resolveImport = (file: string, specifier: string, paths: Set<string>) => {
|
|
334
|
+
const pluginRoot = file.startsWith(apiRoot) ? `${apiRoot}src/` : `${uiRoot}src/`;
|
|
335
|
+
|
|
336
|
+
const base = specifier.startsWith('.')
|
|
337
|
+
? normalize(join(dirname(file), specifier))
|
|
338
|
+
: specifier.startsWith('~/')
|
|
339
|
+
? `${pluginRoot}${specifier.slice(2)}`
|
|
340
|
+
: specifier.startsWith('@/')
|
|
341
|
+
? `${pluginRoot}modules/${specifier.slice(2)}`
|
|
342
|
+
: undefined;
|
|
343
|
+
|
|
344
|
+
if (!base) return undefined;
|
|
345
|
+
|
|
346
|
+
return ['', '.ts', '.tsx', '.js', '/index.ts', '/index.tsx']
|
|
347
|
+
.map((suffix) => `${base}${suffix}`)
|
|
348
|
+
.find((candidate) => paths.has(candidate));
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const scanFile = (file: string, source: string, paths: Set<string>) => {
|
|
352
|
+
const root = parse(file.endsWith('x') ? Lang.Tsx : Lang.TypeScript, source).root();
|
|
353
|
+
const module = moduleOf(file);
|
|
354
|
+
const trpcNames = new Set(['sendTRPCMessage']);
|
|
355
|
+
/** Local name → the file it comes from and its name there. */
|
|
356
|
+
const imports = new Map<string, { file: string; name: string }>();
|
|
357
|
+
|
|
358
|
+
/** A name defined here or imported: `file#name`. */
|
|
359
|
+
const origin = (local: string) => {
|
|
360
|
+
const imported = imports.get(local);
|
|
361
|
+
|
|
362
|
+
return imported ? `${imported.file}#${imported.name}` : `${file}#${local}`;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const uiImports: string[] = [];
|
|
366
|
+
const workerEvidence = new Set<string>();
|
|
367
|
+
const constants: [string, string][] = [];
|
|
368
|
+
|
|
369
|
+
for (const statement of root.findAll({ rule: { kind: 'import_statement' } })) {
|
|
370
|
+
const specifier = literal(statement.field('source') ?? statement) ?? '';
|
|
371
|
+
const target = resolveImport(file, specifier, paths);
|
|
372
|
+
const clause = statement.find({ rule: { kind: 'import_clause' } });
|
|
373
|
+
|
|
374
|
+
for (const part of clause?.namedChildren() ?? []) {
|
|
375
|
+
if (part.kind() === 'identifier') {
|
|
376
|
+
if (target) imports.set(part.text(), { file: target, name: 'default' });
|
|
377
|
+
|
|
378
|
+
if (specifier.startsWith('ui-modules')) uiImports.push('default');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
for (const specifierNode of part.findAll({ rule: { kind: 'import_specifier' } })) {
|
|
382
|
+
const name = specifierNode.field('name')?.text() ?? '';
|
|
383
|
+
const alias = specifierNode.field('alias')?.text();
|
|
384
|
+
|
|
385
|
+
if (target) imports.set(alias ?? name, { file: target, name });
|
|
386
|
+
|
|
387
|
+
if (specifier.startsWith('ui-modules')) uiImports.push(name);
|
|
388
|
+
|
|
389
|
+
if (name === 'sendTRPCMessage' && alias) trpcNames.add(alias);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (const declarator of root.findAll({ rule: { kind: 'variable_declarator' } })) {
|
|
395
|
+
const value = declarator.field('value');
|
|
396
|
+
const text = value ? literal(unwrap(value)) : undefined;
|
|
397
|
+
|
|
398
|
+
const name = declarator.field('name')?.text() ?? '';
|
|
399
|
+
|
|
400
|
+
if (text !== undefined) constants.push([name, text]);
|
|
401
|
+
|
|
402
|
+
// `const Paths = { Detail: 'x' }` gives `Paths.Detail`.
|
|
403
|
+
if (value && unwrap(value).kind() === 'object') {
|
|
404
|
+
for (const pair of unwrap(value).namedChildren()) {
|
|
405
|
+
const key = pair.field('key');
|
|
406
|
+
const member = pair.field('value');
|
|
407
|
+
const memberText = member ? literal(unwrap(member)) : undefined;
|
|
408
|
+
|
|
409
|
+
if (pair.kind() === 'pair' && key && memberText !== undefined)
|
|
410
|
+
constants.push([`${name}.${literal(key) ?? key.text()}`, memberText]);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// `enum Paths { Detail = 'x' }` gives `Paths.Detail`.
|
|
416
|
+
for (const declaration of root.findAll({ rule: { kind: 'enum_declaration' } })) {
|
|
417
|
+
const name = declaration.field('name')?.text() ?? '';
|
|
418
|
+
|
|
419
|
+
for (const member of declaration.findAll({ rule: { kind: 'enum_assignment' } })) {
|
|
420
|
+
const memberValue = member.field('value');
|
|
421
|
+
const memberText = memberValue ? literal(memberValue) : undefined;
|
|
422
|
+
|
|
423
|
+
if (memberText !== undefined)
|
|
424
|
+
constants.push([`${name}.${member.field('name')?.text() ?? ''}`, memberText]);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const trpcCalls = [];
|
|
429
|
+
const eventCalls = [];
|
|
430
|
+
const schemas = [];
|
|
431
|
+
const models = [];
|
|
432
|
+
const loaders: [string, string][] = [];
|
|
433
|
+
const routes = [];
|
|
434
|
+
const mounts = [];
|
|
435
|
+
const env = [];
|
|
436
|
+
|
|
437
|
+
for (const call of root.findAll({
|
|
438
|
+
rule: { any: [{ kind: 'call_expression' }, { kind: 'new_expression' }] },
|
|
439
|
+
})) {
|
|
440
|
+
const isNew = call.kind() === 'new_expression';
|
|
441
|
+
const name = isNew ? `new ${calleeName(call)}` : calleeName(call);
|
|
442
|
+
const line = lineOf(call);
|
|
443
|
+
const [arg, second] = argumentsOf(call);
|
|
444
|
+
const object = arg?.kind() === 'object' ? arg : undefined;
|
|
445
|
+
|
|
446
|
+
if (workerApis.has(name)) workerEvidence.add(name);
|
|
447
|
+
|
|
448
|
+
if (trpcNames.has(name)) {
|
|
449
|
+
const missing = { missing: true } as const;
|
|
450
|
+
const method = object && field(object, 'method');
|
|
451
|
+
|
|
452
|
+
trpcCalls.push(
|
|
453
|
+
object
|
|
454
|
+
? {
|
|
455
|
+
file,
|
|
456
|
+
line,
|
|
457
|
+
module,
|
|
458
|
+
pluginName: field(object, 'pluginName') ?? missing,
|
|
459
|
+
target: field(object, 'module') ?? missing,
|
|
460
|
+
action: field(object, 'action') ?? missing,
|
|
461
|
+
...(method && { method }),
|
|
462
|
+
}
|
|
463
|
+
: { file, line, module, dynamic: arg ? sourceText(arg) : '<no argument>' },
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (eventHelper.test(name)) {
|
|
468
|
+
const type = object && field(object, 'type');
|
|
469
|
+
const action = object && field(object, 'action');
|
|
470
|
+
|
|
471
|
+
eventCalls.push({
|
|
472
|
+
file,
|
|
473
|
+
line,
|
|
474
|
+
module,
|
|
475
|
+
helper: name,
|
|
476
|
+
...(type && { type }),
|
|
477
|
+
...(action && { action }),
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (name === 'new Schema' && object) {
|
|
482
|
+
const options = second?.kind() === 'object' ? second : undefined;
|
|
483
|
+
|
|
484
|
+
schemas.push({
|
|
485
|
+
file,
|
|
486
|
+
line,
|
|
487
|
+
module,
|
|
488
|
+
name: assignedName(call) ?? '',
|
|
489
|
+
timestamps: Boolean(options && pairOf(options, 'timestamps')),
|
|
490
|
+
fields: schemaFieldsOf(object),
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
if (name === 'model' && !isNew && arg && literal(arg) !== undefined) {
|
|
495
|
+
const loader = second?.kind() === 'call_expression' ? calleeName(second) : second?.text();
|
|
496
|
+
|
|
497
|
+
models.push({
|
|
498
|
+
file,
|
|
499
|
+
line,
|
|
500
|
+
collection: literal(arg) ?? '',
|
|
501
|
+
model: assignedTarget(call),
|
|
502
|
+
loader: loader ? origin(loader) : undefined,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (name === 'loadClass') {
|
|
507
|
+
const owner = call.field('function')?.field('object')?.text();
|
|
508
|
+
|
|
509
|
+
const enclosing = call
|
|
510
|
+
.ancestors()
|
|
511
|
+
.find(
|
|
512
|
+
(ancestor) =>
|
|
513
|
+
ancestor.kind() === 'variable_declarator' || ancestor.kind() === 'function_declaration',
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
if (owner && enclosing)
|
|
517
|
+
loaders.push([`${file}#${enclosing.field('name')?.text() ?? ''}`, origin(owner)]);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const callee = call.field('function');
|
|
521
|
+
const receiver = callee?.field('object')?.text() ?? '';
|
|
522
|
+
const method = callee?.field('property')?.text() ?? '';
|
|
523
|
+
|
|
524
|
+
if (!isNew && httpMethods.has(method) && /^(app|\w*router)$/i.test(receiver) && arg) {
|
|
525
|
+
const path = literal(arg);
|
|
526
|
+
|
|
527
|
+
if (path !== undefined && method === 'use') {
|
|
528
|
+
const target = second && imports.get(second.text())?.file;
|
|
529
|
+
|
|
530
|
+
if (target) mounts.push({ file, prefix: path, target });
|
|
531
|
+
} else if (path !== undefined) {
|
|
532
|
+
routes.push({
|
|
533
|
+
file,
|
|
534
|
+
line,
|
|
535
|
+
module,
|
|
536
|
+
method: method.toUpperCase(),
|
|
537
|
+
path,
|
|
538
|
+
webhook: webhookPath.test(path),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (name === 'getEnv' && object) {
|
|
544
|
+
const envName = field(object, 'name');
|
|
545
|
+
|
|
546
|
+
if (typeof envName === 'string') env.push({ name: envName, file, line });
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
for (const member of root.findAll({ rule: { kind: 'member_expression' } })) {
|
|
551
|
+
if (member.field('object')?.text() !== 'process.env') continue;
|
|
552
|
+
|
|
553
|
+
env.push({ name: member.field('property')?.text() ?? '', file, line: lineOf(member) });
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
for (const declarator of root.findAll({ rule: { kind: 'variable_declarator' } })) {
|
|
557
|
+
const pattern = declarator.field('name');
|
|
558
|
+
const body = declarator.field('value')?.field('body');
|
|
559
|
+
|
|
560
|
+
// `export const loadXClass = () => xSchema`
|
|
561
|
+
if (body?.kind() === 'identifier')
|
|
562
|
+
loaders.push([`${file}#${pattern?.text() ?? ''}`, origin(body.text())]);
|
|
563
|
+
|
|
564
|
+
if (declarator.field('value')?.text() !== 'process.env' || pattern?.kind() !== 'object_pattern')
|
|
565
|
+
continue;
|
|
566
|
+
|
|
567
|
+
for (const key of pattern.findAll({ rule: { kind: 'shorthand_property_identifier_pattern' } }))
|
|
568
|
+
env.push({ name: key.text(), file, line: lineOf(key) });
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// GraphQL SDL in template strings named types / queries / mutations / subscriptions, or gql``.
|
|
572
|
+
const graphql = { operations: [] as object[], types: [] as object[] };
|
|
573
|
+
|
|
574
|
+
for (const template of root.findAll({ rule: { kind: 'template_string' } })) {
|
|
575
|
+
const tagged = template.parent()?.kind() === 'call_expression';
|
|
576
|
+
const name = assignedName(template) ?? '';
|
|
577
|
+
|
|
578
|
+
if (!/(types?|queries|mutations|subscriptions)$/i.test(name) && !tagged) continue;
|
|
579
|
+
|
|
580
|
+
if (!/\/(graphql|apollo)\//.test(file) || testFile.test(file)) continue;
|
|
581
|
+
|
|
582
|
+
const sdl = sdlOf(template, name);
|
|
583
|
+
const at = lineOf(template);
|
|
584
|
+
|
|
585
|
+
graphql.operations.push(...sdl.operations.map((op) => ({ ...op, file, line: at, module })));
|
|
586
|
+
graphql.types.push(...sdl.types.map((type) => ({ ...type, file, line: at, module })));
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// React Router: <Route path> elements, nested paths joined within the file.
|
|
590
|
+
const uiRoutes = [];
|
|
591
|
+
|
|
592
|
+
const jsx = file.endsWith('x')
|
|
593
|
+
? root.findAll({
|
|
594
|
+
rule: { any: [{ kind: 'jsx_element' }, { kind: 'jsx_self_closing_element' }] },
|
|
595
|
+
})
|
|
596
|
+
: [];
|
|
597
|
+
|
|
598
|
+
for (const element of jsx) {
|
|
599
|
+
const opening = element.kind() === 'jsx_element' ? element.field('open_tag') : element;
|
|
600
|
+
|
|
601
|
+
if (opening?.field('name')?.text() !== 'Route') continue;
|
|
602
|
+
|
|
603
|
+
const segments = [element, ...element.ancestors()]
|
|
604
|
+
.filter((node) => node.kind() === 'jsx_element' || node.kind() === 'jsx_self_closing_element')
|
|
605
|
+
.map((node) => routeAttributes(node.kind() === 'jsx_element' ? node.field('open_tag') : node))
|
|
606
|
+
.filter((attributes) => attributes.route)
|
|
607
|
+
.map((attributes) => attributes.path)
|
|
608
|
+
.toReversed();
|
|
609
|
+
|
|
610
|
+
const own = routeAttributes(opening);
|
|
611
|
+
|
|
612
|
+
// Redirects are not pages.
|
|
613
|
+
if (!own.element || own.element === 'Navigate') continue;
|
|
614
|
+
|
|
615
|
+
uiRoutes.push({
|
|
616
|
+
file,
|
|
617
|
+
line: lineOf(element),
|
|
618
|
+
path: segments.filter(Boolean).join('/').replaceAll(/\/+/g, '/') || '/',
|
|
619
|
+
element: own.element,
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Meta registrations.
|
|
624
|
+
const registrations = [];
|
|
625
|
+
|
|
626
|
+
for (const rule of REGISTRATIONS) {
|
|
627
|
+
if (!rule.file.test(file)) continue;
|
|
628
|
+
|
|
629
|
+
for (const pair of root.findAll({ rule: { kind: 'pair' } })) {
|
|
630
|
+
if (pair.field('key')?.text() !== rule.key || pair.field('value')?.kind() !== 'array')
|
|
631
|
+
continue;
|
|
632
|
+
|
|
633
|
+
for (const entry of pair.field('value')?.namedChildren() ?? []) {
|
|
634
|
+
if ('objectsOnly' in rule && entry.kind() !== 'object') continue;
|
|
635
|
+
|
|
636
|
+
const value =
|
|
637
|
+
entry.kind() === 'object'
|
|
638
|
+
? rule.id.map((key) => field(entry, key)).find((candidate) => candidate !== undefined)
|
|
639
|
+
: literal(entry);
|
|
640
|
+
|
|
641
|
+
if (value === undefined) continue;
|
|
642
|
+
|
|
643
|
+
// The group is the owner's `name` (a permission module), else the key
|
|
644
|
+
// the owner sits under (`import: { types }`, `export: { types }`).
|
|
645
|
+
const owner = pair.parent();
|
|
646
|
+
const name = owner && field(owner, 'name');
|
|
647
|
+
const above = owner?.parent();
|
|
648
|
+
|
|
649
|
+
const group =
|
|
650
|
+
typeof name === 'string'
|
|
651
|
+
? name
|
|
652
|
+
: above?.kind() === 'pair'
|
|
653
|
+
? above.field('key')?.text()
|
|
654
|
+
: undefined;
|
|
655
|
+
|
|
656
|
+
registrations.push({
|
|
657
|
+
kind: rule.kind,
|
|
658
|
+
file,
|
|
659
|
+
line: lineOf(entry),
|
|
660
|
+
id: value,
|
|
661
|
+
...(group && { group }),
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
const segments = file.split('/');
|
|
668
|
+
const stem = (segments.at(-1) ?? '').replace(/\.[^.]+$/, '');
|
|
669
|
+
|
|
670
|
+
if (segments.slice(0, -1).some((segment) => workerDir.test(segment)) || workerName.test(stem))
|
|
671
|
+
workerEvidence.add('path');
|
|
672
|
+
|
|
673
|
+
return {
|
|
674
|
+
trpcCalls,
|
|
675
|
+
eventCalls,
|
|
676
|
+
schemas,
|
|
677
|
+
models,
|
|
678
|
+
loaders,
|
|
679
|
+
routes,
|
|
680
|
+
mounts,
|
|
681
|
+
env,
|
|
682
|
+
graphql,
|
|
683
|
+
uiRoutes,
|
|
684
|
+
registrations,
|
|
685
|
+
constants,
|
|
686
|
+
uiImports: uiImports.length ? { file, names: uiImports } : undefined,
|
|
687
|
+
workerEvidence: [...workerEvidence].toSorted(alphabetical),
|
|
688
|
+
};
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
/** `models.Task = db.model(…)` is `Task`. */
|
|
692
|
+
function assignedTarget(call: SgNode) {
|
|
693
|
+
const assignment = call.ancestors().find((node) => node.kind() === 'assignment_expression');
|
|
694
|
+
const left = assignment?.field('left');
|
|
695
|
+
|
|
696
|
+
return left?.field('property')?.text() ?? left?.text() ?? assignedName(call);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function routeAttributes(opening: SgNode | null) {
|
|
700
|
+
const attributes = opening?.findAll({ rule: { kind: 'jsx_attribute' } }) ?? [];
|
|
701
|
+
|
|
702
|
+
const value = (name: string) =>
|
|
703
|
+
attributes.find((attribute) => attribute.child(0)?.text() === name)?.child(2);
|
|
704
|
+
|
|
705
|
+
const pathNode = value('path');
|
|
706
|
+
const elementNode = value('element');
|
|
707
|
+
|
|
708
|
+
const component = elementNode?.find({
|
|
709
|
+
rule: { any: [{ kind: 'jsx_self_closing_element' }, { kind: 'jsx_opening_element' }] },
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
return {
|
|
713
|
+
route: opening?.field('name')?.text() === 'Route',
|
|
714
|
+
path: pathNode ? (literal(pathNode) ?? sourceText(pathNode)) : '',
|
|
715
|
+
element: component?.field('name')?.text(),
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const countBy = <T>(items: T[], key: (item: T) => string) => {
|
|
720
|
+
const counts = new Map<string, number>();
|
|
721
|
+
|
|
722
|
+
for (const item of items) counts.set(key(item), (counts.get(key(item)) ?? 0) + 1);
|
|
723
|
+
|
|
724
|
+
return Object.fromEntries([...counts].toSorted(([a], [b]) => a.localeCompare(b)));
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
const sha = git('rev-parse', '--verify', `${ref}^{commit}`).trim();
|
|
728
|
+
|
|
729
|
+
const allPaths = git('ls-tree', '-r', '-z', '--name-only', sha, '--', apiRoot, uiRoot)
|
|
730
|
+
.split('\0')
|
|
731
|
+
.filter(Boolean)
|
|
732
|
+
.toSorted(alphabetical);
|
|
733
|
+
|
|
734
|
+
if (!allPaths.some((path) => path.startsWith(apiRoot)))
|
|
735
|
+
throw new Error(`${ref} has no ${apiRoot} in ${from}`);
|
|
736
|
+
|
|
737
|
+
const pathSet = new Set(allPaths);
|
|
738
|
+
const sources = allPaths.filter((path) => sourceFile.test(path) && !testFile.test(path));
|
|
739
|
+
const blobs = readBlobs(sha, sources);
|
|
740
|
+
const scans = sources.map((file) => scanFile(file, blobs.get(file) ?? '', pathSet));
|
|
741
|
+
|
|
742
|
+
type Scan = (typeof scans)[number];
|
|
743
|
+
|
|
744
|
+
type ListKey = { [K in keyof Scan]: Scan[K] extends readonly unknown[] ? K : never }[keyof Scan];
|
|
745
|
+
|
|
746
|
+
const all = <K extends ListKey>(key: K) => scans.map((scan) => scan[key]).flat();
|
|
747
|
+
|
|
748
|
+
// Constants by name, so `type: TASK_TARGET` reads as its string.
|
|
749
|
+
const constants = new Map(scans.flatMap((scan) => scan.constants));
|
|
750
|
+
|
|
751
|
+
const resolveField = (value: Field): Field =>
|
|
752
|
+
typeof value === 'object' && 'dynamic' in value && constants.has(value.dynamic)
|
|
753
|
+
? (constants.get(value.dynamic) ?? value)
|
|
754
|
+
: value;
|
|
755
|
+
|
|
756
|
+
// Which schema each collection stores: `db.model('x', loadXClass(...))` and `xSchema.loadClass(...)`.
|
|
757
|
+
const loaders = new Map(all('loaders'));
|
|
758
|
+
const schemas = all('schemas');
|
|
759
|
+
const schemaRefs = new Map(schemas.map((schema) => [`${schema.file}#${schema.name}`, schema]));
|
|
760
|
+
|
|
761
|
+
const collections = all('models').map((model) => {
|
|
762
|
+
const schemaRef = model.loader ? (loaders.get(model.loader) ?? model.loader) : undefined;
|
|
763
|
+
const schema = schemaRef ? schemaRefs.get(schemaRef) : undefined;
|
|
764
|
+
|
|
765
|
+
return {
|
|
766
|
+
...model,
|
|
767
|
+
schema: schema ? schemaRef : null,
|
|
768
|
+
module: schema?.module ?? moduleOf(model.file),
|
|
769
|
+
};
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
// Express mounts: `router.use('/facebook', facebookRouter)` prefixes that file's routes.
|
|
773
|
+
const mounts = all('mounts');
|
|
774
|
+
|
|
775
|
+
const prefixOf = (file: string, seen = new Set<string>()): string => {
|
|
776
|
+
const mount = mounts.find((candidate) => candidate.target === file);
|
|
777
|
+
|
|
778
|
+
if (!mount || seen.has(file)) return '';
|
|
779
|
+
|
|
780
|
+
seen.add(file);
|
|
781
|
+
|
|
782
|
+
return `${prefixOf(mount.file, seen)}${mount.prefix}`;
|
|
783
|
+
};
|
|
784
|
+
|
|
785
|
+
const routes = all('routes').map((route) => {
|
|
786
|
+
const path = `${prefixOf(route.file)}${route.path}`.replaceAll(/\/+/g, '/');
|
|
787
|
+
|
|
788
|
+
return { ...route, path, webhook: route.webhook || webhookPath.test(path) };
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
const uiModuleImports = scans.flatMap((scan) => (scan.uiImports ? [scan.uiImports] : []));
|
|
792
|
+
|
|
793
|
+
const env = [...new Map(all('env').map((entry) => [entry.name, entry])).values()].toSorted((a, b) =>
|
|
794
|
+
a.name.localeCompare(b.name),
|
|
795
|
+
);
|
|
796
|
+
|
|
797
|
+
const registrations = all('registrations').map((entry) => ({
|
|
798
|
+
...entry,
|
|
799
|
+
id: resolveField(entry.id),
|
|
800
|
+
}));
|
|
801
|
+
|
|
802
|
+
const eventCalls = all('eventCalls').map((call) => ({
|
|
803
|
+
...call,
|
|
804
|
+
...('type' in call && call.type && { type: resolveField(call.type) }),
|
|
805
|
+
}));
|
|
806
|
+
|
|
807
|
+
const workerFiles = sources.flatMap((file, index) => {
|
|
808
|
+
const evidence = scans[index]?.workerEvidence ?? [];
|
|
809
|
+
|
|
810
|
+
return evidence.length > 0 ? [{ file, evidence }] : [];
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
const trpcCalls = all('trpcCalls');
|
|
814
|
+
const graphqlOperations = scans.flatMap((scan) => scan.graphql.operations);
|
|
815
|
+
const graphqlTypes = scans.flatMap((scan) => scan.graphql.types);
|
|
816
|
+
|
|
817
|
+
const uiRoutes = all('uiRoutes')
|
|
818
|
+
.filter((route) => route.file.startsWith(uiRoot))
|
|
819
|
+
.map((route) => ({
|
|
820
|
+
...route,
|
|
821
|
+
// `path={Paths.Detail}` reads as the constant's value.
|
|
822
|
+
path: route.path.replaceAll(
|
|
823
|
+
/\{([^}]+)\}/g,
|
|
824
|
+
(_match, expression: string) => constants.get(expression.trim()) ?? `:${expression.trim()}`,
|
|
825
|
+
),
|
|
826
|
+
}));
|
|
827
|
+
|
|
828
|
+
const modules = [...new Set(sources.map(moduleOf))].toSorted(alphabetical);
|
|
829
|
+
|
|
830
|
+
const inventory = {
|
|
831
|
+
source: { repo: 'erxes/erxes', from, plugin, ref, sha },
|
|
832
|
+
counts: {
|
|
833
|
+
sourceFiles: sources.length,
|
|
834
|
+
modules: modules.length,
|
|
835
|
+
graphqlTypes: graphqlTypes.length,
|
|
836
|
+
graphqlOperations: graphqlOperations.length,
|
|
837
|
+
graphqlOperationsByKind: countBy(graphqlOperations, (op) =>
|
|
838
|
+
'kind' in op ? String(op.kind) : '',
|
|
839
|
+
),
|
|
840
|
+
mongooseSchemas: schemas.length,
|
|
841
|
+
collections: collections.length,
|
|
842
|
+
expressRoutes: routes.length,
|
|
843
|
+
webhooks: routes.filter((route) => route.webhook).length,
|
|
844
|
+
uiRoutes: uiRoutes.length,
|
|
845
|
+
trpcCalls: trpcCalls.length,
|
|
846
|
+
trpcCallsByTarget: countBy(trpcCalls, (call) =>
|
|
847
|
+
'pluginName' in call && typeof call.pluginName === 'string' ? call.pluginName : '<dynamic>',
|
|
848
|
+
),
|
|
849
|
+
eventHelperCalls: eventCalls.length,
|
|
850
|
+
uiModulesFiles: uiModuleImports.length,
|
|
851
|
+
uiModulesSymbols: new Set(uiModuleImports.flatMap((entry) => entry.names)).size,
|
|
852
|
+
workerFiles: workerFiles.length,
|
|
853
|
+
registrations: registrations.length,
|
|
854
|
+
registrationsByKind: countBy(registrations, (entry) => entry.kind),
|
|
855
|
+
envVars: env.length,
|
|
856
|
+
},
|
|
857
|
+
modules,
|
|
858
|
+
graphql: { types: graphqlTypes, operations: graphqlOperations },
|
|
859
|
+
schemas,
|
|
860
|
+
collections,
|
|
861
|
+
routes,
|
|
862
|
+
uiRoutes,
|
|
863
|
+
trpcCalls,
|
|
864
|
+
eventCalls,
|
|
865
|
+
uiModules: {
|
|
866
|
+
files: uiModuleImports.length,
|
|
867
|
+
symbols: countBy(
|
|
868
|
+
uiModuleImports.flatMap((entry) => [...new Set(entry.names)]),
|
|
869
|
+
(name) => name,
|
|
870
|
+
),
|
|
871
|
+
},
|
|
872
|
+
workerFiles,
|
|
873
|
+
registrations,
|
|
874
|
+
env,
|
|
875
|
+
};
|
|
876
|
+
|
|
877
|
+
writeFileSync(output, `${JSON.stringify(inventory, null, 2)}\n`);
|
|
878
|
+
|
|
879
|
+
console.info(inventory.counts);
|
|
880
|
+
|
|
881
|
+
console.info(`wrote ${output} in ${Math.round(performance.now() - started)} ms`);
|