@domain-first/project-structure 8.1.1 → 8.3.0
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 +17 -2
- package/dist/201.js +1 -1
- package/dist/287.js +278 -9
- package/dist/38.js +268 -0
- package/dist/561.js +22 -5
- package/dist/agent-cli.d.ts +17 -0
- package/dist/agent-requests.d.ts +34 -0
- package/dist/backend-module-move.d.ts +16 -0
- package/dist/graph.cjs +933 -0
- package/dist/graph.d.ts +2 -0
- package/dist/graph.js +337 -0
- package/dist/index.cjs +300 -12
- package/dist/index.js +4 -2
- package/dist/init.cjs +111 -57
- package/dist/integration.cjs +297 -11
- package/dist/interaction.d.ts +9 -0
- package/dist/interaction.spec.d.ts +1 -0
- package/dist/lint.js +8 -273
- package/dist/project-graph.d.ts +53 -0
- package/dist/project-graph.spec.d.ts +1 -0
- package/dist/project-lint.d.ts +10 -0
- package/dist/react-module-move.d.ts +21 -0
- package/dist/react.cjs +304 -12
- package/dist/react.js +8 -2
- package/dist/use-cases/delete-backend-unit.d.ts +6 -0
- package/dist/use-cases/delete-react-module.d.ts +8 -0
- package/dist/use-cases/index.d.ts +2 -0
- package/package.json +3 -2
- package/skills/df-ps-plan-feature/SKILL.md +2 -0
- package/skills/df-ps-react/SKILL.md +5 -3
- package/skills/df-ps-react/references/generators.md +4 -0
- package/skills/df-ps-scaffold/SKILL.md +5 -4
- package/skills/df-ps-scaffold/references/agent-mode.md +1 -0
- package/skills/df-ps-scaffold/references/agent-operations.md +3 -0
- package/skills/df-ps-scaffold/references/units/delete.md +19 -0
- package/skills/df-ps-scaffold/scripts/drive.mjs +5 -0
package/dist/graph.cjs
ADDED
|
@@ -0,0 +1,933 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __webpack_require__ = {};
|
|
4
|
+
(()=>{
|
|
5
|
+
__webpack_require__.n = (module)=>{
|
|
6
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
7
|
+
__webpack_require__.d(getter, {
|
|
8
|
+
a: getter
|
|
9
|
+
});
|
|
10
|
+
return getter;
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
(()=>{
|
|
14
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
15
|
+
var define = (defs, kind)=>{
|
|
16
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
[kind]: defs[key]
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
define(getters, "get");
|
|
22
|
+
define(values, "value");
|
|
23
|
+
};
|
|
24
|
+
})();
|
|
25
|
+
(()=>{
|
|
26
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
const external_node_child_process_namespaceObject = require("node:child_process");
|
|
30
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
31
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
|
32
|
+
const external_node_os_namespaceObject = require("node:os");
|
|
33
|
+
var external_node_os_default = /*#__PURE__*/ __webpack_require__.n(external_node_os_namespaceObject);
|
|
34
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
35
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
36
|
+
const external_node_util_namespaceObject = require("node:util");
|
|
37
|
+
const external_zod_namespaceObject = require("zod");
|
|
38
|
+
var external_zod_default = /*#__PURE__*/ __webpack_require__.n(external_zod_namespaceObject);
|
|
39
|
+
const moduleKinds = [
|
|
40
|
+
'repositories',
|
|
41
|
+
'domain-services',
|
|
42
|
+
'application-ports',
|
|
43
|
+
'application-services',
|
|
44
|
+
'use-cases',
|
|
45
|
+
'commands',
|
|
46
|
+
'queries',
|
|
47
|
+
'infrastructure-services',
|
|
48
|
+
'presentation-services'
|
|
49
|
+
];
|
|
50
|
+
const domain = [
|
|
51
|
+
'repositories',
|
|
52
|
+
'domain-services'
|
|
53
|
+
];
|
|
54
|
+
const commandsAndQueries = [
|
|
55
|
+
'commands',
|
|
56
|
+
'queries'
|
|
57
|
+
];
|
|
58
|
+
const infrastructure = [
|
|
59
|
+
...domain,
|
|
60
|
+
'application-ports',
|
|
61
|
+
'infrastructure-services'
|
|
62
|
+
];
|
|
63
|
+
const dependency_rules_defaultDependencies = {
|
|
64
|
+
'domain-services': [
|
|
65
|
+
'domain-services'
|
|
66
|
+
],
|
|
67
|
+
repositories: [
|
|
68
|
+
'domain-services',
|
|
69
|
+
'application-ports',
|
|
70
|
+
'infrastructure-services'
|
|
71
|
+
],
|
|
72
|
+
'application-ports': infrastructure,
|
|
73
|
+
'application-services': [
|
|
74
|
+
...domain,
|
|
75
|
+
'application-ports',
|
|
76
|
+
...commandsAndQueries
|
|
77
|
+
],
|
|
78
|
+
'use-cases': [
|
|
79
|
+
...domain,
|
|
80
|
+
'application-ports',
|
|
81
|
+
'application-services',
|
|
82
|
+
...commandsAndQueries
|
|
83
|
+
],
|
|
84
|
+
commands: [
|
|
85
|
+
'infrastructure-services'
|
|
86
|
+
],
|
|
87
|
+
queries: [
|
|
88
|
+
'infrastructure-services'
|
|
89
|
+
],
|
|
90
|
+
'infrastructure-services': infrastructure,
|
|
91
|
+
'presentation-services': [
|
|
92
|
+
'application-ports',
|
|
93
|
+
'application-services',
|
|
94
|
+
'use-cases',
|
|
95
|
+
...commandsAndQueries,
|
|
96
|
+
'presentation-services'
|
|
97
|
+
]
|
|
98
|
+
};
|
|
99
|
+
const kindSchema = external_zod_default()["enum"](moduleKinds);
|
|
100
|
+
const dependencyRulesSchema = external_zod_default().partialRecord(kindSchema, external_zod_default().union([
|
|
101
|
+
external_zod_default().array(kindSchema),
|
|
102
|
+
external_zod_default().object({
|
|
103
|
+
add: external_zod_default().array(kindSchema).optional(),
|
|
104
|
+
remove: external_zod_default().array(kindSchema).optional()
|
|
105
|
+
})
|
|
106
|
+
]));
|
|
107
|
+
const dependency_rules_resolveDependencyRules = (config = {})=>{
|
|
108
|
+
const entries = moduleKinds.map((kind)=>{
|
|
109
|
+
const override = config[kind];
|
|
110
|
+
if (Array.isArray(override)) return [
|
|
111
|
+
kind,
|
|
112
|
+
new Set(override)
|
|
113
|
+
];
|
|
114
|
+
const kinds = new Set([
|
|
115
|
+
...dependency_rules_defaultDependencies[kind],
|
|
116
|
+
...override?.add ?? []
|
|
117
|
+
]);
|
|
118
|
+
for (const removed of override?.remove ?? [])kinds.delete(removed);
|
|
119
|
+
return [
|
|
120
|
+
kind,
|
|
121
|
+
kinds
|
|
122
|
+
];
|
|
123
|
+
});
|
|
124
|
+
return Object.fromEntries(entries);
|
|
125
|
+
};
|
|
126
|
+
dependency_rules_resolveDependencyRules();
|
|
127
|
+
const pendingFiles = new Map();
|
|
128
|
+
new Set();
|
|
129
|
+
const pendingMoves = new Map();
|
|
130
|
+
const pendingDeletions = new Map();
|
|
131
|
+
class FileSystemItem {
|
|
132
|
+
path;
|
|
133
|
+
constructor(path){
|
|
134
|
+
this.path = path;
|
|
135
|
+
}
|
|
136
|
+
subitem = (relativePath)=>new this.constructor(external_node_path_default().resolve(this.path, ...relativePath));
|
|
137
|
+
get exists() {
|
|
138
|
+
return external_node_fs_default().existsSync(this.path);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
class file_system_File extends FileSystemItem {
|
|
142
|
+
get exists() {
|
|
143
|
+
return pendingFiles.has(this.path) || pendingMoves.has(this.path) || external_node_fs_default().existsSync(this.path) && !pendingDeletions.has(this.path);
|
|
144
|
+
}
|
|
145
|
+
delete = (stopAt)=>{
|
|
146
|
+
pendingDeletions.set(this.path, stopAt);
|
|
147
|
+
};
|
|
148
|
+
moveTo = (targetPath, stopAt)=>{
|
|
149
|
+
pendingMoves.set(targetPath, {
|
|
150
|
+
sourcePath: this.path,
|
|
151
|
+
stopAt
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
get data() {
|
|
155
|
+
const pendingFile = pendingFiles.get(this.path);
|
|
156
|
+
if (pendingFile) return pendingFile.content;
|
|
157
|
+
if (!this.exists) throw new Error('File does not exist');
|
|
158
|
+
return external_node_fs_default().readFileSync(this.path, 'utf-8');
|
|
159
|
+
}
|
|
160
|
+
set data(content) {
|
|
161
|
+
pendingFiles.set(this.path, {
|
|
162
|
+
content,
|
|
163
|
+
replacesFile: true
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
update = (content)=>{
|
|
167
|
+
pendingFiles.set(this.path, {
|
|
168
|
+
content,
|
|
169
|
+
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
addLine = (newLine, separator = '\n')=>{
|
|
173
|
+
const content = this.exists ? this.data : '';
|
|
174
|
+
pendingFiles.set(this.path, {
|
|
175
|
+
content: [
|
|
176
|
+
content.trim(),
|
|
177
|
+
newLine.trim()
|
|
178
|
+
].filter((x)=>!!x).join(separator),
|
|
179
|
+
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
addLineIfMissing = (newLine, separator = '\n')=>{
|
|
183
|
+
const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
|
|
184
|
+
if (!alreadyAdded) this.addLine(newLine, separator);
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const implementationKinds = [
|
|
188
|
+
'repositories',
|
|
189
|
+
'commands',
|
|
190
|
+
'queries',
|
|
191
|
+
'ports'
|
|
192
|
+
];
|
|
193
|
+
const implementationLabelSchema = external_zod_default().string().regex(/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/);
|
|
194
|
+
const implementationOptionsSchema = external_zod_default().partialRecord(external_zod_default()["enum"](implementationKinds), external_zod_default().object({
|
|
195
|
+
primary: external_zod_default().array(implementationLabelSchema).default([]),
|
|
196
|
+
test: external_zod_default().array(implementationLabelSchema).default([])
|
|
197
|
+
}).strict());
|
|
198
|
+
class ProjectRoot {
|
|
199
|
+
path;
|
|
200
|
+
constructor(){
|
|
201
|
+
let dir = process.cwd();
|
|
202
|
+
while(dir !== external_node_path_default().parse(dir).root){
|
|
203
|
+
if (external_node_fs_default().existsSync(external_node_path_default().join(dir, 'package.json'))) {
|
|
204
|
+
this.path = dir;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
dir = external_node_path_default().dirname(dir);
|
|
208
|
+
}
|
|
209
|
+
throw new Error('No package.json was found');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const lintChecks = [
|
|
213
|
+
'context-isolation',
|
|
214
|
+
'shared-kernel',
|
|
215
|
+
'layers',
|
|
216
|
+
'dependencies',
|
|
217
|
+
'react',
|
|
218
|
+
'clock'
|
|
219
|
+
];
|
|
220
|
+
const configFileName = 'domain-first.project-structure.config.json';
|
|
221
|
+
const projectConfigSchema = external_zod_default().object({
|
|
222
|
+
rootFolder: external_zod_default().string().min(1)
|
|
223
|
+
}).passthrough();
|
|
224
|
+
const domainFirstPackages = [
|
|
225
|
+
'@domain-first/types',
|
|
226
|
+
'@domain-first/errors',
|
|
227
|
+
'@domain-first/handlers',
|
|
228
|
+
'@domain-first/wire',
|
|
229
|
+
'@domain-first/handlers-rest'
|
|
230
|
+
];
|
|
231
|
+
const rulesSchema = external_zod_default().object({
|
|
232
|
+
dependencies: dependencyRulesSchema.optional(),
|
|
233
|
+
lint: external_zod_default().partialRecord(external_zod_default()["enum"](lintChecks), external_zod_default().boolean()).optional()
|
|
234
|
+
});
|
|
235
|
+
const branchNameSchema = external_zod_default().string().regex(/^[a-z][a-z0-9]*$/, 'Use lowercase letters and digits, e.g. staging.');
|
|
236
|
+
const envBranchesSchema = external_zod_default().object({
|
|
237
|
+
names: external_zod_default().array(branchNameSchema).min(1),
|
|
238
|
+
test: branchNameSchema.optional()
|
|
239
|
+
}).refine(({ names })=>new Set(names).size === names.length, {
|
|
240
|
+
message: 'Branch names must be unique.'
|
|
241
|
+
}).refine(({ names, test })=>void 0 === test || names.includes(test), {
|
|
242
|
+
message: 'The test branch must be one of the branches.'
|
|
243
|
+
}).refine(({ names, test })=>void 0 === test || names.length > 1, {
|
|
244
|
+
message: 'The primary implementation needs a branch besides the test one.'
|
|
245
|
+
});
|
|
246
|
+
const backendConfigSchema = external_zod_default().object({
|
|
247
|
+
domainFirstPackages: external_zod_default().array(external_zod_default()["enum"](domainFirstPackages)),
|
|
248
|
+
envBranches: envBranchesSchema.optional(),
|
|
249
|
+
withInMemoryDb: external_zod_default().boolean().optional(),
|
|
250
|
+
testingLibrary: external_zod_default().string().optional(),
|
|
251
|
+
implementations: implementationOptionsSchema.optional(),
|
|
252
|
+
rules: rulesSchema.optional()
|
|
253
|
+
}).passthrough();
|
|
254
|
+
const reactConfigSchema = external_zod_default().object({
|
|
255
|
+
framework: external_zod_default()["enum"]([
|
|
256
|
+
'react',
|
|
257
|
+
'next'
|
|
258
|
+
]).optional(),
|
|
259
|
+
storybookFramework: external_zod_default().string().optional()
|
|
260
|
+
}).passthrough();
|
|
261
|
+
const frontendConfigSchema = external_zod_default().object({
|
|
262
|
+
react: reactConfigSchema.optional()
|
|
263
|
+
}).passthrough();
|
|
264
|
+
const configSchema = external_zod_default().object({
|
|
265
|
+
project: projectConfigSchema,
|
|
266
|
+
backend: backendConfigSchema.optional(),
|
|
267
|
+
frontend: frontendConfigSchema.optional()
|
|
268
|
+
}).passthrough();
|
|
269
|
+
const isOutdatedConfig = (value)=>'object' == typeof value && null !== value && !Array.isArray(value) && !('project' in value);
|
|
270
|
+
const outdatedConfigMessage = `${configFileName} uses the format of an earlier major version: run npx df-upgrade.`;
|
|
271
|
+
class ConfigOutdatedError extends Error {
|
|
272
|
+
constructor(){
|
|
273
|
+
super(outdatedConfigMessage);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
class ConfigFile {
|
|
277
|
+
data;
|
|
278
|
+
constructor(){
|
|
279
|
+
const raw = JSON.parse(new file_system_File(ConfigFile.path).data);
|
|
280
|
+
if (isOutdatedConfig(raw)) throw new ConfigOutdatedError();
|
|
281
|
+
this.data = configSchema.parse(raw);
|
|
282
|
+
}
|
|
283
|
+
static get Instance() {
|
|
284
|
+
return new ConfigFile();
|
|
285
|
+
}
|
|
286
|
+
static get backend() {
|
|
287
|
+
const backend = ConfigFile.Instance.data.backend;
|
|
288
|
+
if (!backend) throw new Error('Backend is not configured: run df-init --section backend.');
|
|
289
|
+
return backend;
|
|
290
|
+
}
|
|
291
|
+
static get path() {
|
|
292
|
+
return external_node_path_default().resolve(new ProjectRoot().path, configFileName);
|
|
293
|
+
}
|
|
294
|
+
static get exists() {
|
|
295
|
+
return new file_system_File(ConfigFile.path).exists;
|
|
296
|
+
}
|
|
297
|
+
static get outdated() {
|
|
298
|
+
if (!ConfigFile.exists) return false;
|
|
299
|
+
try {
|
|
300
|
+
return isOutdatedConfig(JSON.parse(new file_system_File(ConfigFile.path).data));
|
|
301
|
+
} catch {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
static write = (config)=>{
|
|
306
|
+
ConfigFile.stage(configSchema.parse(config));
|
|
307
|
+
};
|
|
308
|
+
static update = (sections)=>{
|
|
309
|
+
ConfigFile.stage(configSchema.parse({
|
|
310
|
+
...ConfigFile.Instance.data,
|
|
311
|
+
...sections
|
|
312
|
+
}));
|
|
313
|
+
};
|
|
314
|
+
static stage(config) {
|
|
315
|
+
new file_system_File(ConfigFile.path).update(`${JSON.stringify(config, null, 4)}\n`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const external_node_module_namespaceObject = require("node:module");
|
|
319
|
+
const stripJsonComments = (source)=>source.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, text)=>text ? text : '').replace(/,(\s*[}\]])/g, '$1');
|
|
320
|
+
const resolveExtends = (fromFile, target)=>{
|
|
321
|
+
if (target.startsWith('.') || external_node_path_default().isAbsolute(target)) {
|
|
322
|
+
const resolved = external_node_path_default().resolve(external_node_path_default().dirname(fromFile), target);
|
|
323
|
+
return resolved.endsWith('.json') ? resolved : `${resolved}.json`;
|
|
324
|
+
}
|
|
325
|
+
const require1 = (0, external_node_module_namespaceObject.createRequire)(fromFile);
|
|
326
|
+
for (const candidate of [
|
|
327
|
+
target,
|
|
328
|
+
`${target}/tsconfig.json`
|
|
329
|
+
])try {
|
|
330
|
+
return require1.resolve(candidate);
|
|
331
|
+
} catch {}
|
|
332
|
+
};
|
|
333
|
+
const compilerOption = (configFile, name, seen = new Set())=>{
|
|
334
|
+
if (seen.has(configFile) || !external_node_fs_default().existsSync(configFile)) return null;
|
|
335
|
+
seen.add(configFile);
|
|
336
|
+
let config;
|
|
337
|
+
try {
|
|
338
|
+
config = JSON.parse(stripJsonComments(external_node_fs_default().readFileSync(configFile, 'utf8')));
|
|
339
|
+
} catch {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
if (config.compilerOptions && name in config.compilerOptions) return config.compilerOptions[name];
|
|
343
|
+
const parents = [
|
|
344
|
+
config.extends ?? []
|
|
345
|
+
].flat().reverse();
|
|
346
|
+
for (const parent of parents){
|
|
347
|
+
const parentFile = resolveExtends(configFile, parent);
|
|
348
|
+
const value = parentFile ? compilerOption(parentFile, name, seen) : null;
|
|
349
|
+
if (void 0 !== value) return value;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
class unknown_format_naming_UnknownFormatNaming {
|
|
353
|
+
rawSource;
|
|
354
|
+
camelCase;
|
|
355
|
+
PascalCase;
|
|
356
|
+
constructor(rawSource){
|
|
357
|
+
this.rawSource = rawSource;
|
|
358
|
+
const source = rawSource.trim();
|
|
359
|
+
if (!source.includes('-')) {
|
|
360
|
+
this.camelCase = source.substring(0, 1).toLowerCase() + source.substring(1);
|
|
361
|
+
this.PascalCase = source.substring(0, 1).toUpperCase() + source.substring(1);
|
|
362
|
+
}
|
|
363
|
+
const words = source.split('-').filter((word)=>!!word);
|
|
364
|
+
this.camelCase = words.map((word, index)=>0 === index ? word.toLowerCase() : word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
365
|
+
this.PascalCase = words.map((word)=>word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
366
|
+
}
|
|
367
|
+
get fileName() {
|
|
368
|
+
return this.rawSource;
|
|
369
|
+
}
|
|
370
|
+
get variableName() {
|
|
371
|
+
return this.camelCase;
|
|
372
|
+
}
|
|
373
|
+
get ClassName() {
|
|
374
|
+
return this.PascalCase;
|
|
375
|
+
}
|
|
376
|
+
get withSpaces() {
|
|
377
|
+
return this.fileName.replaceAll('-', ' ');
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const wiring_layers = [
|
|
381
|
+
'domain',
|
|
382
|
+
'application',
|
|
383
|
+
'infrastructure',
|
|
384
|
+
'presentation'
|
|
385
|
+
];
|
|
386
|
+
const branchedTypeFiles = {
|
|
387
|
+
repositories: (fileBase)=>[
|
|
388
|
+
'domain',
|
|
389
|
+
'aggregates',
|
|
390
|
+
fileBase.replace(/-repository$/, ''),
|
|
391
|
+
`${fileBase}.ts`
|
|
392
|
+
],
|
|
393
|
+
commands: (fileBase)=>[
|
|
394
|
+
'application',
|
|
395
|
+
'commands',
|
|
396
|
+
`${fileBase}.ts`
|
|
397
|
+
],
|
|
398
|
+
queries: (fileBase)=>[
|
|
399
|
+
'application',
|
|
400
|
+
'queries',
|
|
401
|
+
`${fileBase}.ts`
|
|
402
|
+
],
|
|
403
|
+
'application-ports': (fileBase)=>[
|
|
404
|
+
'application',
|
|
405
|
+
'ports',
|
|
406
|
+
`${fileBase}.ts`
|
|
407
|
+
]
|
|
408
|
+
};
|
|
409
|
+
const layerOf = (file, contextPath)=>{
|
|
410
|
+
const [firstSegment] = external_node_path_default().relative(contextPath, file).split(external_node_path_default().sep);
|
|
411
|
+
return wiring_layers.find((layer)=>layer === firstSegment);
|
|
412
|
+
};
|
|
413
|
+
const withoutScriptExtension = (file)=>file.replace(/\.(?:c|m)?[jt]s$/, '');
|
|
414
|
+
const wiring_namedImportFiles = (source, fromFile, resolve = (specifier, file)=>specifier.startsWith('.') ? external_node_path_default().resolve(external_node_path_default().dirname(file), specifier) : void 0)=>{
|
|
415
|
+
const result = new Map();
|
|
416
|
+
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g)){
|
|
417
|
+
const target = resolve(importPath, fromFile);
|
|
418
|
+
if (target) for (const name of names.split(',')){
|
|
419
|
+
const cleanName = name.replace(/^\s*type\s+/, '').trim().split(/\s+as\s+/).at(-1);
|
|
420
|
+
if (cleanName) result.set(cleanName, `${withoutScriptExtension(target)}.ts`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return result;
|
|
424
|
+
};
|
|
425
|
+
const isEndpointWire = (wiringRelativePath)=>wiringRelativePath.startsWith([
|
|
426
|
+
'presentation',
|
|
427
|
+
'rest'
|
|
428
|
+
].join(external_node_path_default().sep));
|
|
429
|
+
const wireClassDefinitions = (source)=>[
|
|
430
|
+
...codeOnly(source).matchAll(/(?:export\s+)?const\s+(\w+)\s*=\s*(?:\w+\(\s*)*wireClass\(\s*(\w+)\s*,/g)
|
|
431
|
+
].map(([, wireName, className])=>({
|
|
432
|
+
wireName,
|
|
433
|
+
className
|
|
434
|
+
}));
|
|
435
|
+
const wiredClassNames = (source, wireName, initializerStart, branched)=>{
|
|
436
|
+
const definitions = wireClassDefinitions(source);
|
|
437
|
+
if (!branched) return definitions.filter((x)=>x.wireName === wireName).map((x)=>x.className);
|
|
438
|
+
const openIndex = source.indexOf('(', initializerStart);
|
|
439
|
+
const branches = codeOnly(source).slice(openIndex, closingIndex(source, openIndex));
|
|
440
|
+
const referenced = new Set(branches.match(/\w+/g));
|
|
441
|
+
return definitions.filter((x)=>referenced.has(x.wireName)).map((x)=>x.className);
|
|
442
|
+
};
|
|
443
|
+
const wiring_readWiredModule = (wireFile, contextPath)=>{
|
|
444
|
+
const wiringRelativePath = external_node_path_default().relative(external_node_path_default().join(contextPath, 'wiring'), wireFile);
|
|
445
|
+
if (isEndpointWire(wiringRelativePath)) return;
|
|
446
|
+
const source = new file_system_File(wireFile).data;
|
|
447
|
+
const wireName = `wire${new unknown_format_naming_UnknownFormatNaming(external_node_path_default().basename(wireFile, '.ts').replace(/^wire-/, '')).ClassName}`;
|
|
448
|
+
const exportMatch = new RegExp(`export\\s+const\\s+${wireName}\\s*=\\s*`).exec(source);
|
|
449
|
+
if (!exportMatch) return;
|
|
450
|
+
const branched = source.slice(exportMatch.index + exportMatch[0].length).startsWith('envBranchedWire(');
|
|
451
|
+
const imports = wiring_namedImportFiles(source, wireFile);
|
|
452
|
+
const implementations = wiredClassNames(source, wireName, exportMatch.index + exportMatch[0].length, branched).flatMap((className)=>{
|
|
453
|
+
const file = imports.get(className);
|
|
454
|
+
return file ? [
|
|
455
|
+
{
|
|
456
|
+
className,
|
|
457
|
+
file
|
|
458
|
+
}
|
|
459
|
+
] : [];
|
|
460
|
+
});
|
|
461
|
+
if (!implementations.length) return;
|
|
462
|
+
const typeName = wireName.slice(4);
|
|
463
|
+
const [wiringFolder] = wiringRelativePath.split(external_node_path_default().sep);
|
|
464
|
+
const typeFileSegments = branchedTypeFiles[wiringFolder]?.(external_node_path_default().basename(wireFile, '.ts').replace(/^wire-/, ''));
|
|
465
|
+
const typeFile = branched ? typeFileSegments && external_node_path_default().join(contextPath, ...typeFileSegments) : implementations[0].file;
|
|
466
|
+
const layer = typeFile && layerOf(typeFile, contextPath);
|
|
467
|
+
const implementationsLayer = layerOf(implementations[0].file, contextPath);
|
|
468
|
+
if (!typeFile || !layer || !implementationsLayer) return;
|
|
469
|
+
return {
|
|
470
|
+
wireName,
|
|
471
|
+
typeName,
|
|
472
|
+
wireFile,
|
|
473
|
+
typeFile,
|
|
474
|
+
layer,
|
|
475
|
+
implementations,
|
|
476
|
+
branched,
|
|
477
|
+
implementationsLayer,
|
|
478
|
+
kind: wiringFolder
|
|
479
|
+
};
|
|
480
|
+
};
|
|
481
|
+
const wiring_findWiredModules = (contextPath)=>{
|
|
482
|
+
const result = [];
|
|
483
|
+
const walk = (folderPath)=>{
|
|
484
|
+
if (!external_node_fs_default().existsSync(folderPath)) return;
|
|
485
|
+
for (const entry of external_node_fs_default().readdirSync(folderPath, {
|
|
486
|
+
withFileTypes: true
|
|
487
|
+
})){
|
|
488
|
+
const entryPath = external_node_path_default().join(folderPath, entry.name);
|
|
489
|
+
if (entry.isDirectory()) walk(entryPath);
|
|
490
|
+
else if (/^wire-.*(?<!\.spec)\.ts$/.test(entry.name)) {
|
|
491
|
+
const wiredModule = wiring_readWiredModule(entryPath, contextPath);
|
|
492
|
+
if (wiredModule) result.push(wiredModule);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
walk(external_node_path_default().join(contextPath, 'wiring'));
|
|
497
|
+
return result.sort((a, b)=>a.typeName.localeCompare(b.typeName));
|
|
498
|
+
};
|
|
499
|
+
const codeOnly = (source)=>{
|
|
500
|
+
let result = '';
|
|
501
|
+
let index = 0;
|
|
502
|
+
const blank = (text)=>text.replace(/[^\n]/g, ' ');
|
|
503
|
+
while(index < source.length){
|
|
504
|
+
const pair = source.slice(index, index + 2);
|
|
505
|
+
const char = source[index];
|
|
506
|
+
let end = index + 1;
|
|
507
|
+
if ('//' === pair) {
|
|
508
|
+
const lineEnd = source.indexOf('\n', index);
|
|
509
|
+
end = -1 === lineEnd ? source.length : lineEnd;
|
|
510
|
+
result += blank(source.slice(index, end));
|
|
511
|
+
} else if ('/*' === pair) {
|
|
512
|
+
const commentEnd = source.indexOf('*/', index + 2);
|
|
513
|
+
end = -1 === commentEnd ? source.length : commentEnd + 2;
|
|
514
|
+
result += blank(source.slice(index, end));
|
|
515
|
+
} else if ('=>' === pair) {
|
|
516
|
+
end = index + 2;
|
|
517
|
+
result += ' ';
|
|
518
|
+
} else if ("'" === char || '"' === char || '`' === char) {
|
|
519
|
+
while(end < source.length && source[end] !== char)end += '\\' === source[end] ? 2 : 1;
|
|
520
|
+
end = Math.min(end + 1, source.length);
|
|
521
|
+
result += char + blank(source.slice(index + 1, end - 1));
|
|
522
|
+
result += end - 1 > index ? source[end - 1] : '';
|
|
523
|
+
} else result += char;
|
|
524
|
+
index = end;
|
|
525
|
+
}
|
|
526
|
+
return result;
|
|
527
|
+
};
|
|
528
|
+
const brackets = {
|
|
529
|
+
'(': ')',
|
|
530
|
+
'[': ']',
|
|
531
|
+
'{': '}',
|
|
532
|
+
'<': '>'
|
|
533
|
+
};
|
|
534
|
+
const closingIndex = (source, openIndex)=>{
|
|
535
|
+
const code = codeOnly(source);
|
|
536
|
+
let depth = 0;
|
|
537
|
+
for(let index = openIndex; index < code.length; index++){
|
|
538
|
+
const char = code[index];
|
|
539
|
+
if (char === code[openIndex]) depth++;
|
|
540
|
+
else if (char === brackets[code[openIndex]]) {
|
|
541
|
+
depth--;
|
|
542
|
+
if (0 === depth) return index;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return -1;
|
|
546
|
+
};
|
|
547
|
+
const topLevelItemRanges = (content)=>{
|
|
548
|
+
const code = codeOnly(content);
|
|
549
|
+
const ranges = [];
|
|
550
|
+
let depth = 0;
|
|
551
|
+
let itemStart = 0;
|
|
552
|
+
for(let index = 0; index <= code.length; index++){
|
|
553
|
+
const char = code[index];
|
|
554
|
+
if (void 0 !== char && char in brackets) depth++;
|
|
555
|
+
else if (Object.values(brackets).includes(char)) depth--;
|
|
556
|
+
if (index === code.length || ',' === char && 0 === depth) {
|
|
557
|
+
const itemCode = code.slice(itemStart, index);
|
|
558
|
+
const first = itemCode.search(/\S/);
|
|
559
|
+
const last = itemCode.search(/\S\s*$/);
|
|
560
|
+
if (-1 !== first) ranges.push({
|
|
561
|
+
start: itemStart + first,
|
|
562
|
+
end: itemStart + last + 1
|
|
563
|
+
});
|
|
564
|
+
itemStart = index + 1;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return ranges;
|
|
568
|
+
};
|
|
569
|
+
const splitTopLevel = (content)=>topLevelItemRanges(content).map(({ start, end })=>content.slice(start, end));
|
|
570
|
+
const wireListRange = (source, className)=>{
|
|
571
|
+
const match = new RegExp(`wireClass\\(\\s*${className}\\s*,\\s*\\[`).exec(codeOnly(source));
|
|
572
|
+
if (!match) throw new Error(`wireClass(${className}, [...]) not found`);
|
|
573
|
+
const openIndex = match.index + match[0].length - 1;
|
|
574
|
+
return {
|
|
575
|
+
openIndex,
|
|
576
|
+
closeIndex: closingIndex(source, openIndex)
|
|
577
|
+
};
|
|
578
|
+
};
|
|
579
|
+
const wiring_wiredDependencyNames = (source, className)=>{
|
|
580
|
+
const { openIndex, closeIndex } = wireListRange(source, className);
|
|
581
|
+
return splitTopLevel(source.slice(openIndex + 1, closeIndex));
|
|
582
|
+
};
|
|
583
|
+
const readAliases = (projectRoot)=>{
|
|
584
|
+
const tsconfig = external_node_path_default().join(projectRoot, 'tsconfig.json');
|
|
585
|
+
const paths = compilerOption(tsconfig, 'paths');
|
|
586
|
+
if (!paths || 'object' != typeof paths) return [];
|
|
587
|
+
const baseUrl = compilerOption(tsconfig, 'baseUrl');
|
|
588
|
+
const base = external_node_path_default().resolve(projectRoot, 'string' == typeof baseUrl ? baseUrl : '.');
|
|
589
|
+
return Object.entries(paths).filter(([, targets])=>targets.length).map(([key, [target]])=>{
|
|
590
|
+
const wildcard = key.endsWith('*');
|
|
591
|
+
return {
|
|
592
|
+
prefix: wildcard ? key.slice(0, -1) : key,
|
|
593
|
+
wildcard,
|
|
594
|
+
target: external_node_path_default().resolve(base, wildcard ? target.replace(/\*$/, '') : target)
|
|
595
|
+
};
|
|
596
|
+
}).sort((a, b)=>b.prefix.length - a.prefix.length);
|
|
597
|
+
};
|
|
598
|
+
const resolveSpecifier = (specifier, fromFile, aliases)=>{
|
|
599
|
+
if (specifier.startsWith('.')) return external_node_path_default().resolve(external_node_path_default().dirname(fromFile), specifier);
|
|
600
|
+
for (const { prefix, wildcard, target } of aliases)if (wildcard ? specifier.startsWith(prefix) : specifier === prefix) return external_node_path_default().join(target, wildcard ? specifier.slice(prefix.length) : '');
|
|
601
|
+
};
|
|
602
|
+
const sharedKernel = 'shared-kernel';
|
|
603
|
+
const contextsOf = (contextsFolder)=>external_node_fs_default().existsSync(contextsFolder) ? external_node_fs_default().readdirSync(contextsFolder, {
|
|
604
|
+
withFileTypes: true
|
|
605
|
+
}).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name).sort() : [];
|
|
606
|
+
const projectGraph = (projectRoot, rootFolder)=>{
|
|
607
|
+
const root = external_node_path_default().resolve(projectRoot, rootFolder);
|
|
608
|
+
const contextsFolder = external_node_path_default().join(root, 'bounded-contexts');
|
|
609
|
+
const owners = [
|
|
610
|
+
sharedKernel,
|
|
611
|
+
...contextsOf(contextsFolder)
|
|
612
|
+
];
|
|
613
|
+
const ownerPath = (owner)=>owner === sharedKernel ? external_node_path_default().join(root, sharedKernel) : external_node_path_default().join(contextsFolder, owner);
|
|
614
|
+
const byWireFile = new Map();
|
|
615
|
+
const wired = [];
|
|
616
|
+
for (const owner of owners)for (const module of wiring_findWiredModules(ownerPath(owner))){
|
|
617
|
+
const node = {
|
|
618
|
+
id: `${owner}:${module.typeName}`,
|
|
619
|
+
owner,
|
|
620
|
+
typeName: module.typeName,
|
|
621
|
+
layer: module.layer,
|
|
622
|
+
implementations: module.implementations.map(({ className })=>className)
|
|
623
|
+
};
|
|
624
|
+
byWireFile.set(module.wireFile, node);
|
|
625
|
+
wired.push([
|
|
626
|
+
module,
|
|
627
|
+
node
|
|
628
|
+
]);
|
|
629
|
+
}
|
|
630
|
+
const aliases = readAliases(projectRoot);
|
|
631
|
+
const edges = [];
|
|
632
|
+
for (const [module, node] of wired){
|
|
633
|
+
const source = new file_system_File(module.wireFile).data;
|
|
634
|
+
const imported = wiring_namedImportFiles(source, module.wireFile, (specifier, file)=>resolveSpecifier(specifier, file, aliases));
|
|
635
|
+
for (const { className } of module.implementations)for (const name of wiring_wiredDependencyNames(source, className)){
|
|
636
|
+
const file = imported.get(name);
|
|
637
|
+
const target = file && byWireFile.get(file);
|
|
638
|
+
if (target) edges.push({
|
|
639
|
+
from: node.id,
|
|
640
|
+
to: target.id,
|
|
641
|
+
implementation: className
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return {
|
|
646
|
+
owners,
|
|
647
|
+
modules: wired.map(([, node])=>node),
|
|
648
|
+
edges
|
|
649
|
+
};
|
|
650
|
+
};
|
|
651
|
+
const sanitize = (value)=>value.replace(/[^A-Za-z0-9]/g, '_');
|
|
652
|
+
const quoted = (value)=>`"${value.replace(/"/g, '#quot;')}"`;
|
|
653
|
+
const ownerOf = (graph, id)=>graph.modules.find((module)=>module.id === id)?.owner;
|
|
654
|
+
const typeOf = (graph, id)=>graph.modules.find((module)=>module.id === id)?.typeName ?? id;
|
|
655
|
+
const contextMap = (graph, idPrefix = '')=>{
|
|
656
|
+
const nodeId = (value)=>`${idPrefix}${sanitize(value)}`;
|
|
657
|
+
const links = new Map();
|
|
658
|
+
for (const edge of graph.edges){
|
|
659
|
+
const from = ownerOf(graph, edge.from);
|
|
660
|
+
const to = ownerOf(graph, edge.to);
|
|
661
|
+
if (!from || !to || from === to) continue;
|
|
662
|
+
const key = `${from}\u0000${to}`;
|
|
663
|
+
const set = links.get(key) ?? new Set();
|
|
664
|
+
set.add(to === sharedKernel ? typeOf(graph, edge.to) : `${typeOf(graph, edge.from)} → ${typeOf(graph, edge.to)}`);
|
|
665
|
+
links.set(key, set);
|
|
666
|
+
}
|
|
667
|
+
const lines = [
|
|
668
|
+
'flowchart LR'
|
|
669
|
+
];
|
|
670
|
+
for (const owner of graph.owners)lines.push(owner === sharedKernel ? ` ${nodeId(owner)}[(${quoted(owner)})]` : ` ${nodeId(owner)}[${quoted(owner)}]`);
|
|
671
|
+
for (const [key, labels] of [
|
|
672
|
+
...links
|
|
673
|
+
].sort(([a], [b])=>a.localeCompare(b))){
|
|
674
|
+
const [from, to] = key.split('\u0000');
|
|
675
|
+
const label = [
|
|
676
|
+
...labels
|
|
677
|
+
].sort().join('<br/>');
|
|
678
|
+
lines.push(to === sharedKernel ? ` ${nodeId(from)} -.->|${quoted(label)}| ${nodeId(to)}` : ` ${nodeId(from)} -->|${quoted(label)}| ${nodeId(to)}`);
|
|
679
|
+
}
|
|
680
|
+
return lines.join('\n');
|
|
681
|
+
};
|
|
682
|
+
const ownerGraph = (graph, owner, idPrefix = '')=>{
|
|
683
|
+
const nodeId = (value)=>`${idPrefix}${sanitize(value)}`;
|
|
684
|
+
const own = graph.modules.filter((module)=>module.owner === owner);
|
|
685
|
+
const ownIds = new Set(own.map((module)=>module.id));
|
|
686
|
+
const edges = new Map();
|
|
687
|
+
for (const edge of graph.edges){
|
|
688
|
+
if (!ownIds.has(edge.from)) continue;
|
|
689
|
+
const key = `${edge.from}\u0000${edge.to}`;
|
|
690
|
+
const set = edges.get(key) ?? new Set();
|
|
691
|
+
set.add(edge.implementation);
|
|
692
|
+
edges.set(key, set);
|
|
693
|
+
}
|
|
694
|
+
const external = [
|
|
695
|
+
...new Set([
|
|
696
|
+
...edges.keys()
|
|
697
|
+
].map((key)=>key.split('\u0000')[1]).filter((id)=>!ownIds.has(id)))
|
|
698
|
+
].sort();
|
|
699
|
+
const lines = [
|
|
700
|
+
'flowchart TB'
|
|
701
|
+
];
|
|
702
|
+
const node = (id)=>`${nodeId(id)}[${quoted(typeOf(graph, id))}]`;
|
|
703
|
+
for (const layer of wiring_layers){
|
|
704
|
+
const members = own.filter((module)=>module.layer === layer);
|
|
705
|
+
if (members.length) {
|
|
706
|
+
lines.push(` subgraph ${nodeId(`${owner}_${layer}`)}[${quoted(layer)}]`);
|
|
707
|
+
for (const module of members)lines.push(` ${node(module.id)}`);
|
|
708
|
+
lines.push(' end');
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
for (const other of [
|
|
712
|
+
...new Set(external.map((id)=>ownerOf(graph, id) ?? ''))
|
|
713
|
+
]){
|
|
714
|
+
lines.push(` subgraph ${nodeId(`owner_${other}`)}[${quoted(other)}]`);
|
|
715
|
+
for (const id of external.filter((x)=>ownerOf(graph, x) === other))lines.push(` ${node(id)}`);
|
|
716
|
+
lines.push(' end');
|
|
717
|
+
}
|
|
718
|
+
for (const [key, implementations] of [
|
|
719
|
+
...edges
|
|
720
|
+
].sort(([a], [b])=>a.localeCompare(b))){
|
|
721
|
+
const [from, to] = key.split('\u0000');
|
|
722
|
+
const all = graph.modules.find((module)=>module.id === from)?.implementations.length ?? 0;
|
|
723
|
+
const label = implementations.size < all ? `|${quoted([
|
|
724
|
+
...implementations
|
|
725
|
+
].sort().join(', '))}|` : '';
|
|
726
|
+
lines.push(` ${nodeId(from)} -->${label} ${nodeId(to)}`);
|
|
727
|
+
}
|
|
728
|
+
return lines.join('\n');
|
|
729
|
+
};
|
|
730
|
+
const formatOfFile = (file)=>({
|
|
731
|
+
'.md': 'markdown',
|
|
732
|
+
'.mmd': 'mermaid',
|
|
733
|
+
'.mermaid': 'mermaid',
|
|
734
|
+
'.json': 'json',
|
|
735
|
+
'.html': 'html'
|
|
736
|
+
})[external_node_path_default().extname(file).toLowerCase()];
|
|
737
|
+
const escapeHtml = (value)=>value.replace(/[&<>"]/g, (char)=>({
|
|
738
|
+
'&': '&',
|
|
739
|
+
'<': '<',
|
|
740
|
+
'>': '>',
|
|
741
|
+
'"': '"'
|
|
742
|
+
})[char] ?? char);
|
|
743
|
+
const mermaidScript = 'https://cdn.jsdelivr.net/npm/mermaid@11.4.1/dist/mermaid.esm.min.mjs';
|
|
744
|
+
const graphHtml = (graph, title, owner)=>{
|
|
745
|
+
const sections = owner ? [
|
|
746
|
+
[
|
|
747
|
+
owner,
|
|
748
|
+
ownerGraph(graph, owner, 'c0_')
|
|
749
|
+
]
|
|
750
|
+
] : [
|
|
751
|
+
[
|
|
752
|
+
'Bounded contexts',
|
|
753
|
+
contextMap(graph, 'c0_')
|
|
754
|
+
],
|
|
755
|
+
...graph.owners.filter((name)=>graph.modules.some((module)=>module.owner === name)).map((name, index)=>[
|
|
756
|
+
name,
|
|
757
|
+
ownerGraph(graph, name, `c${index + 1}_`)
|
|
758
|
+
])
|
|
759
|
+
];
|
|
760
|
+
return `<!doctype html>
|
|
761
|
+
<html lang="en">
|
|
762
|
+
<head>
|
|
763
|
+
<meta charset="utf-8">
|
|
764
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
765
|
+
<title>${escapeHtml(title)}</title>
|
|
766
|
+
<style>
|
|
767
|
+
body { margin: 0 auto; max-width: 1200px; padding: 24px 16px; font-family: ui-monospace, Menlo, Consolas, monospace; background: #fafaf7; color: #1d1d1b; }
|
|
768
|
+
h1 { font-size: 22px; } h2 { margin-top: 40px; font-size: 18px; border-bottom: 2px solid #1d1d1b; padding-bottom: 6px; }
|
|
769
|
+
.mermaid { overflow-x: auto; }
|
|
770
|
+
</style>
|
|
771
|
+
</head>
|
|
772
|
+
<body>
|
|
773
|
+
<h1>${escapeHtml(title)}</h1>
|
|
774
|
+
${sections.map(([name, chart])=>`<h2>${escapeHtml(name)}</h2>\n<pre class="mermaid">\n${escapeHtml(chart)}\n</pre>`).join('\n')}
|
|
775
|
+
<script type="module">
|
|
776
|
+
import mermaid from '${mermaidScript}';
|
|
777
|
+
mermaid.initialize({ startOnLoad: false });
|
|
778
|
+
// One chart at a time: charts rendered together disturb each other's layout.
|
|
779
|
+
for (const node of document.querySelectorAll('.mermaid')) {
|
|
780
|
+
await mermaid.run({ nodes: [node] });
|
|
781
|
+
}
|
|
782
|
+
</script>
|
|
783
|
+
</body>
|
|
784
|
+
</html>
|
|
785
|
+
`;
|
|
786
|
+
};
|
|
787
|
+
const renderGraph = (graph, format, { owner, title = 'Project structure' } = {})=>{
|
|
788
|
+
if ('html' === format) return graphHtml(graph, title, owner);
|
|
789
|
+
if ('json' === format) {
|
|
790
|
+
const modules = owner ? graph.modules.filter((module)=>module.owner === owner) : graph.modules;
|
|
791
|
+
const ids = new Set(modules.map((module)=>module.id));
|
|
792
|
+
return JSON.stringify({
|
|
793
|
+
owners: owner ? [
|
|
794
|
+
owner
|
|
795
|
+
] : graph.owners,
|
|
796
|
+
modules,
|
|
797
|
+
edges: owner ? graph.edges.filter((edge)=>ids.has(edge.from)) : graph.edges
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
const chart = owner ? ownerGraph(graph, owner) : contextMap(graph);
|
|
801
|
+
return 'markdown' === format ? `\`\`\`mermaid\n${chart}\n\`\`\`` : chart;
|
|
802
|
+
};
|
|
803
|
+
const help = `Usage: df-graph [--context <name>] [--json | --markdown | --html] [--out <file>]
|
|
804
|
+
|
|
805
|
+
Shows the project's structure as a Mermaid flowchart, read from the wiring.
|
|
806
|
+
Without options it prints the bounded contexts and the Shared Kernel, with
|
|
807
|
+
the cross-context ports that reach a neighbor's handlers (solid arrows) and
|
|
808
|
+
the use of the Shared Kernel (dashed arrows).
|
|
809
|
+
|
|
810
|
+
Options:
|
|
811
|
+
--context <name> the wired modules of one bounded context (or shared-kernel)
|
|
812
|
+
by layer, with their dependencies
|
|
813
|
+
--markdown wrap the chart in a \`\`\`mermaid block, e.g. for a README
|
|
814
|
+
--json the graph as JSON: owners, modules and dependencies
|
|
815
|
+
--html a page with the rendered charts (all contexts without
|
|
816
|
+
--context), opened in the browser; Mermaid loads from a CDN
|
|
817
|
+
--out <file> write to a file instead; the extension picks the format:
|
|
818
|
+
.md, .mmd/.mermaid, .json or .html
|
|
819
|
+
-h, --help show this help`;
|
|
820
|
+
const { values } = (0, external_node_util_namespaceObject.parseArgs)({
|
|
821
|
+
options: {
|
|
822
|
+
help: {
|
|
823
|
+
type: 'boolean',
|
|
824
|
+
short: 'h',
|
|
825
|
+
default: false
|
|
826
|
+
},
|
|
827
|
+
context: {
|
|
828
|
+
type: 'string'
|
|
829
|
+
},
|
|
830
|
+
json: {
|
|
831
|
+
type: 'boolean',
|
|
832
|
+
default: false
|
|
833
|
+
},
|
|
834
|
+
markdown: {
|
|
835
|
+
type: 'boolean',
|
|
836
|
+
default: false
|
|
837
|
+
},
|
|
838
|
+
html: {
|
|
839
|
+
type: 'boolean',
|
|
840
|
+
default: false
|
|
841
|
+
},
|
|
842
|
+
out: {
|
|
843
|
+
type: 'string'
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
if (values.help) {
|
|
848
|
+
console.log(help);
|
|
849
|
+
process.exit(0);
|
|
850
|
+
}
|
|
851
|
+
const fail = (message)=>{
|
|
852
|
+
console.log(message);
|
|
853
|
+
process.exit(1);
|
|
854
|
+
};
|
|
855
|
+
const flagged = [
|
|
856
|
+
'json',
|
|
857
|
+
'markdown',
|
|
858
|
+
'html'
|
|
859
|
+
].filter((flag)=>values[flag]);
|
|
860
|
+
if (flagged.length > 1) fail('Choose one of --json, --markdown and --html.');
|
|
861
|
+
const fileFormat = void 0 === values.out ? void 0 : formatOfFile(values.out);
|
|
862
|
+
if (void 0 !== values.out && !fileFormat) fail('--out needs a .md, .mmd, .mermaid, .json or .html file.');
|
|
863
|
+
const flagFormat = flagged[0];
|
|
864
|
+
if (fileFormat && flagFormat && fileFormat !== flagFormat) fail(`--${flagFormat} doesn't match ${values.out}.`);
|
|
865
|
+
const graph_format = fileFormat ?? flagFormat ?? 'mermaid';
|
|
866
|
+
if (!ConfigFile.exists) fail('No domain-first.project-structure.config.json found: run df-init first to initialize the project.');
|
|
867
|
+
if (ConfigFile.outdated) fail(outdatedConfigMessage);
|
|
868
|
+
const graph_config = ConfigFile.Instance.data;
|
|
869
|
+
if (!graph_config.backend) fail('Backend is not configured: run df-init --section backend.');
|
|
870
|
+
if (!graph_config.backend?.domainFirstPackages.includes('@domain-first/wire')) fail('df-graph reads the wiring: it needs @domain-first/wire.');
|
|
871
|
+
const graph_projectRoot = new ProjectRoot().path;
|
|
872
|
+
const graph_graph = projectGraph(graph_projectRoot, graph_config.project.rootFolder);
|
|
873
|
+
const graph_owner = values.context;
|
|
874
|
+
if (void 0 !== graph_owner && !graph_graph.owners.includes(graph_owner)) fail(`Unknown context: ${graph_owner}. Known: ${graph_graph.owners.join(', ')}.`);
|
|
875
|
+
if (!graph_graph.modules.length) console.error(`No wired modules yet: generate some with df-scaffold (${sharedKernel} and bounded contexts are scanned).`);
|
|
876
|
+
const packageName = (()=>{
|
|
877
|
+
try {
|
|
878
|
+
return JSON.parse(external_node_fs_default().readFileSync(external_node_path_default().join(graph_projectRoot, 'package.json'), 'utf8')).name;
|
|
879
|
+
} catch {
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
})();
|
|
883
|
+
const output = renderGraph(graph_graph, graph_format, {
|
|
884
|
+
owner: graph_owner,
|
|
885
|
+
title: [
|
|
886
|
+
packageName ?? external_node_path_default().basename(graph_projectRoot),
|
|
887
|
+
graph_owner
|
|
888
|
+
].filter(Boolean).join(' / ')
|
|
889
|
+
});
|
|
890
|
+
const openInBrowser = (file)=>{
|
|
891
|
+
const [command, args] = 'darwin' === process.platform ? [
|
|
892
|
+
'open',
|
|
893
|
+
[
|
|
894
|
+
file
|
|
895
|
+
]
|
|
896
|
+
] : 'win32' === process.platform ? [
|
|
897
|
+
'cmd',
|
|
898
|
+
[
|
|
899
|
+
'/c',
|
|
900
|
+
'start',
|
|
901
|
+
'',
|
|
902
|
+
file
|
|
903
|
+
]
|
|
904
|
+
] : [
|
|
905
|
+
'xdg-open',
|
|
906
|
+
[
|
|
907
|
+
file
|
|
908
|
+
]
|
|
909
|
+
];
|
|
910
|
+
const child = (0, external_node_child_process_namespaceObject.spawn)(command, args, {
|
|
911
|
+
stdio: 'ignore',
|
|
912
|
+
detached: true
|
|
913
|
+
});
|
|
914
|
+
child.on('error', ()=>console.log(`Open ${file} in a browser.`));
|
|
915
|
+
child.unref();
|
|
916
|
+
};
|
|
917
|
+
if (void 0 !== values.out) {
|
|
918
|
+
const file = external_node_path_default().resolve(values.out);
|
|
919
|
+
external_node_fs_default().mkdirSync(external_node_path_default().dirname(file), {
|
|
920
|
+
recursive: true
|
|
921
|
+
});
|
|
922
|
+
external_node_fs_default().writeFileSync(file, `${output}\n`);
|
|
923
|
+
console.log(`Wrote ${external_node_path_default().relative(process.cwd(), file) || file}.`);
|
|
924
|
+
} else if ('html' === graph_format) {
|
|
925
|
+
const file = external_node_path_default().join(external_node_os_default().tmpdir(), `df-graph-${(packageName ?? external_node_path_default().basename(graph_projectRoot)).replace(/[^\w-]+/g, '-')}${graph_owner ? `-${graph_owner}` : ''}.html`);
|
|
926
|
+
external_node_fs_default().writeFileSync(file, output);
|
|
927
|
+
console.log(`Wrote ${file}; opening it in the browser.`);
|
|
928
|
+
openInBrowser(file);
|
|
929
|
+
} else console.log(output);
|
|
930
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
931
|
+
Object.defineProperty(exports, '__esModule', {
|
|
932
|
+
value: true
|
|
933
|
+
});
|