ddd-express 0.0.0-stage → 1.0.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/CHANGELOG.md +34 -0
- package/LICENSE +21 -0
- package/README.ar.md +117 -0
- package/README.md +314 -2
- package/dist/analyze/check.d.ts +74 -0
- package/dist/analyze/check.js +503 -0
- package/dist/analyze/document.d.ts +26 -0
- package/dist/analyze/document.js +126 -0
- package/dist/analyze/map.d.ts +19 -0
- package/dist/analyze/map.js +283 -0
- package/dist/analyze/openapi.d.ts +7 -0
- package/dist/analyze/openapi.js +236 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +12 -0
- package/dist/core/errors.d.ts +5 -0
- package/dist/core/errors.js +13 -0
- package/dist/core/fields.d.ts +38 -0
- package/dist/core/fields.js +233 -0
- package/dist/core/markers.d.ts +25 -0
- package/dist/core/markers.js +60 -0
- package/dist/core/names.d.ts +30 -0
- package/dist/core/names.js +168 -0
- package/dist/core/output.d.ts +21 -0
- package/dist/core/output.js +29 -0
- package/dist/core/project.d.ts +135 -0
- package/dist/core/project.js +287 -0
- package/dist/core/writer.d.ts +39 -0
- package/dist/core/writer.js +138 -0
- package/dist/generators/analyze.d.ts +16 -0
- package/dist/generators/analyze.js +174 -0
- package/dist/generators/apply.d.ts +28 -0
- package/dist/generators/apply.js +234 -0
- package/dist/generators/common.d.ts +49 -0
- package/dist/generators/common.js +173 -0
- package/dist/generators/domain.d.ts +30 -0
- package/dist/generators/domain.js +198 -0
- package/dist/generators/engine.d.ts +71 -0
- package/dist/generators/engine.js +552 -0
- package/dist/generators/project.d.ts +10 -0
- package/dist/generators/project.js +214 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +14 -0
- package/dist/model/commands.d.ts +28 -0
- package/dist/model/commands.js +228 -0
- package/dist/model/examples.d.ts +6 -0
- package/dist/model/examples.js +152 -0
- package/dist/model/parser.d.ts +28 -0
- package/dist/model/parser.js +222 -0
- package/dist/model/types.d.ts +98 -0
- package/dist/model/types.js +2 -0
- package/dist/model/validate.d.ts +30 -0
- package/dist/model/validate.js +215 -0
- package/dist/run.d.ts +4 -0
- package/dist/run.js +506 -0
- package/dist/scan/project-scan.d.ts +77 -0
- package/dist/scan/project-scan.js +226 -0
- package/dist/scan/source.d.ts +60 -0
- package/dist/scan/source.js +274 -0
- package/dist/templates/aggregate.d.ts +51 -0
- package/dist/templates/aggregate.js +407 -0
- package/dist/templates/building-blocks.d.ts +30 -0
- package/dist/templates/building-blocks.js +155 -0
- package/dist/templates/code.d.ts +14 -0
- package/dist/templates/code.js +51 -0
- package/dist/templates/command.d.ts +47 -0
- package/dist/templates/command.js +376 -0
- package/dist/templates/context.d.ts +16 -0
- package/dist/templates/context.js +43 -0
- package/dist/templates/field-code.d.ts +47 -0
- package/dist/templates/field-code.js +227 -0
- package/dist/templates/postgres.d.ts +25 -0
- package/dist/templates/postgres.js +113 -0
- package/dist/templates/project.d.ts +20 -0
- package/dist/templates/project.js +153 -0
- package/dist/templates/shared-kernel.d.ts +6 -0
- package/dist/templates/shared-kernel.js +44 -0
- package/package.json +58 -4
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.RULES = void 0;
|
|
37
|
+
exports.loadCheckConfig = loadCheckConfig;
|
|
38
|
+
exports.checkProject = checkProject;
|
|
39
|
+
exports.missingTerms = missingTerms;
|
|
40
|
+
exports.fixGlossaries = fixGlossaries;
|
|
41
|
+
const fs = __importStar(require("node:fs"));
|
|
42
|
+
const path = __importStar(require("node:path"));
|
|
43
|
+
const node_module_1 = require("node:module");
|
|
44
|
+
const names_1 = require("../core/names");
|
|
45
|
+
const project_1 = require("../core/project");
|
|
46
|
+
const markers_1 = require("../core/markers");
|
|
47
|
+
const context_1 = require("../templates/context");
|
|
48
|
+
const project_scan_1 = require("../scan/project-scan");
|
|
49
|
+
const source_1 = require("../scan/source");
|
|
50
|
+
exports.RULES = {
|
|
51
|
+
'domain-isolation': {
|
|
52
|
+
severity: 'error',
|
|
53
|
+
summary: 'The domain layer depends only on itself, the shared domain kernel and pure libraries.',
|
|
54
|
+
},
|
|
55
|
+
'application-isolation': {
|
|
56
|
+
severity: 'error',
|
|
57
|
+
summary: 'Use cases depend on the domain and on ports — never on adapters, Express or a database.',
|
|
58
|
+
},
|
|
59
|
+
'context-boundaries': {
|
|
60
|
+
severity: 'error',
|
|
61
|
+
summary: "A bounded context never reaches into another one; it may only use the other's events or public index.",
|
|
62
|
+
},
|
|
63
|
+
'shared-kernel': {
|
|
64
|
+
severity: 'error',
|
|
65
|
+
summary: 'The shared kernel knows no bounded context, and its domain part knows no adapter.',
|
|
66
|
+
},
|
|
67
|
+
'aggregate-references': {
|
|
68
|
+
severity: 'warning',
|
|
69
|
+
summary: 'An aggregate references other aggregates by id, not by importing them.',
|
|
70
|
+
},
|
|
71
|
+
'event-naming': {
|
|
72
|
+
severity: 'warning',
|
|
73
|
+
summary: 'Domain events are facts: named in the past tense (VisitCompleted, not CompleteVisit).',
|
|
74
|
+
},
|
|
75
|
+
'silent-state-change': {
|
|
76
|
+
severity: 'warning',
|
|
77
|
+
summary: 'A behaviour that changes the state records an event, so other contexts can react.',
|
|
78
|
+
},
|
|
79
|
+
'ubiquitous-language': {
|
|
80
|
+
severity: 'warning',
|
|
81
|
+
summary: 'Every aggregate, entity, value object, event and service is in the GLOSSARY.md of its context.',
|
|
82
|
+
},
|
|
83
|
+
'anemic-aggregate': {
|
|
84
|
+
severity: 'info',
|
|
85
|
+
summary: 'An aggregate with behaviour (not only create/update/delete) captures the business language.',
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
const IO_BUILTINS = new Set([
|
|
89
|
+
'fs',
|
|
90
|
+
'fs/promises',
|
|
91
|
+
'http',
|
|
92
|
+
'https',
|
|
93
|
+
'http2',
|
|
94
|
+
'net',
|
|
95
|
+
'tls',
|
|
96
|
+
'dgram',
|
|
97
|
+
'dns',
|
|
98
|
+
'child_process',
|
|
99
|
+
'cluster',
|
|
100
|
+
'worker_threads',
|
|
101
|
+
'readline',
|
|
102
|
+
'repl',
|
|
103
|
+
'inspector',
|
|
104
|
+
'v8',
|
|
105
|
+
'vm',
|
|
106
|
+
]);
|
|
107
|
+
const INFRASTRUCTURE_PACKAGES = [
|
|
108
|
+
'express',
|
|
109
|
+
'koa',
|
|
110
|
+
'fastify',
|
|
111
|
+
'hapi',
|
|
112
|
+
'@hapi/',
|
|
113
|
+
'@nestjs/',
|
|
114
|
+
'pg',
|
|
115
|
+
'pg-promise',
|
|
116
|
+
'mysql',
|
|
117
|
+
'mysql2',
|
|
118
|
+
'mariadb',
|
|
119
|
+
'sqlite3',
|
|
120
|
+
'better-sqlite3',
|
|
121
|
+
'mongodb',
|
|
122
|
+
'mongoose',
|
|
123
|
+
'@prisma/client',
|
|
124
|
+
'prisma',
|
|
125
|
+
'typeorm',
|
|
126
|
+
'sequelize',
|
|
127
|
+
'knex',
|
|
128
|
+
'drizzle-orm',
|
|
129
|
+
'@mikro-orm/',
|
|
130
|
+
'redis',
|
|
131
|
+
'ioredis',
|
|
132
|
+
'amqplib',
|
|
133
|
+
'kafkajs',
|
|
134
|
+
'@aws-sdk/',
|
|
135
|
+
'aws-sdk',
|
|
136
|
+
'firebase-admin',
|
|
137
|
+
'axios',
|
|
138
|
+
'node-fetch',
|
|
139
|
+
'got',
|
|
140
|
+
'undici',
|
|
141
|
+
'nodemailer',
|
|
142
|
+
'twilio',
|
|
143
|
+
'stripe',
|
|
144
|
+
'socket.io',
|
|
145
|
+
'ws',
|
|
146
|
+
];
|
|
147
|
+
const DOMAIN_FOLDERS = new Set(['events', 'ports', 'services', 'value-objects']);
|
|
148
|
+
const builtins = new Set(node_module_1.builtinModules.flatMap((name) => [name, `node:${name}`]));
|
|
149
|
+
function packageName(specifier) {
|
|
150
|
+
if (specifier.startsWith('@'))
|
|
151
|
+
return specifier.split('/').slice(0, 2).join('/');
|
|
152
|
+
return specifier.split('/')[0];
|
|
153
|
+
}
|
|
154
|
+
function isBuiltin(specifier) {
|
|
155
|
+
return builtins.has(specifier) || specifier.startsWith('node:');
|
|
156
|
+
}
|
|
157
|
+
function builtinName(specifier) {
|
|
158
|
+
return specifier.replace(/^node:/, '');
|
|
159
|
+
}
|
|
160
|
+
function matchesPackage(name, list) {
|
|
161
|
+
return list.some((item) => (item.endsWith('/') ? name.startsWith(item) : name === item));
|
|
162
|
+
}
|
|
163
|
+
function walk(dir, out = []) {
|
|
164
|
+
if (!fs.existsSync(dir))
|
|
165
|
+
return out;
|
|
166
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
167
|
+
if (entry.name === 'node_modules' || entry.name.startsWith('.'))
|
|
168
|
+
continue;
|
|
169
|
+
const full = path.join(dir, entry.name);
|
|
170
|
+
if (entry.isDirectory())
|
|
171
|
+
walk(full, out);
|
|
172
|
+
else if (/\.(ts|mts|cts|tsx|js|mjs|cjs)$/.test(entry.name) && !entry.name.endsWith('.d.ts'))
|
|
173
|
+
out.push(full);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
function loadCheckConfig(project) {
|
|
178
|
+
const config = (0, project_1.readJson)(path.join(project.root, project_1.CONFIG_FILE));
|
|
179
|
+
return config?.check ?? {};
|
|
180
|
+
}
|
|
181
|
+
/** Runs every architecture rule on the project. */
|
|
182
|
+
function checkProject(project, config = {}, scan = (0, project_scan_1.scanProject)(project)) {
|
|
183
|
+
const findings = [];
|
|
184
|
+
const contexts = new Set((0, project_1.findContexts)(project));
|
|
185
|
+
const relToRoot = (file) => path.relative(project.root, file).split(path.sep).join('/');
|
|
186
|
+
const ignored = (rel) => (config.ignore ?? []).some((prefix) => rel.startsWith(prefix.replace(/^\.\//, '')));
|
|
187
|
+
const severityOf = (rule) => config.rules?.[rule] ?? exports.RULES[rule].severity;
|
|
188
|
+
const report = (finding) => {
|
|
189
|
+
const severity = severityOf(finding.rule);
|
|
190
|
+
if (severity === 'off' || ignored(finding.file))
|
|
191
|
+
return;
|
|
192
|
+
findings.push({ ...finding, severity });
|
|
193
|
+
};
|
|
194
|
+
const classify = (abs) => {
|
|
195
|
+
const relSrc = path.relative(project.src, abs).split(path.sep).join('/');
|
|
196
|
+
if (relSrc.startsWith('..'))
|
|
197
|
+
return null;
|
|
198
|
+
const parts = relSrc.split('/');
|
|
199
|
+
const base = parts[parts.length - 1];
|
|
200
|
+
const isIndex = /^index\.[cm]?[jt]sx?$/.test(base);
|
|
201
|
+
if (parts.length === 1)
|
|
202
|
+
return { rel: relSrc, context: null, layer: 'root', aggregate: null, isEvent: false, isIndex };
|
|
203
|
+
if (parts[0] === 'shared') {
|
|
204
|
+
const sub = parts[1];
|
|
205
|
+
const layer = sub === 'domain'
|
|
206
|
+
? 'shared-domain'
|
|
207
|
+
: sub === 'application'
|
|
208
|
+
? 'shared-application'
|
|
209
|
+
: sub === 'infrastructure'
|
|
210
|
+
? 'shared-infrastructure'
|
|
211
|
+
: sub === 'http'
|
|
212
|
+
? 'shared-http'
|
|
213
|
+
: sub === 'testing'
|
|
214
|
+
? 'shared-testing'
|
|
215
|
+
: 'shared-other';
|
|
216
|
+
return { rel: relSrc, context: null, layer, aggregate: null, isEvent: false, isIndex };
|
|
217
|
+
}
|
|
218
|
+
const context = parts[0];
|
|
219
|
+
if (!contexts.has(context))
|
|
220
|
+
return { rel: relSrc, context: null, layer: 'root', aggregate: null, isEvent: false, isIndex };
|
|
221
|
+
const top = parts[1];
|
|
222
|
+
if (parts.length === 2) {
|
|
223
|
+
const composition = base.startsWith(`${context}.module.`) || isIndex;
|
|
224
|
+
return { rel: relSrc, context, layer: composition ? 'composition' : 'context-other', aggregate: null, isEvent: false, isIndex };
|
|
225
|
+
}
|
|
226
|
+
if (top === 'domain') {
|
|
227
|
+
const folder = parts[2];
|
|
228
|
+
const aggregate = parts.length > 3 && !DOMAIN_FOLDERS.has(folder) ? folder : null;
|
|
229
|
+
return { rel: relSrc, context, layer: 'domain', aggregate, isEvent: folder === 'events', isIndex };
|
|
230
|
+
}
|
|
231
|
+
if (top === 'application')
|
|
232
|
+
return { rel: relSrc, context, layer: 'application', aggregate: null, isEvent: false, isIndex };
|
|
233
|
+
if (top === 'infrastructure')
|
|
234
|
+
return { rel: relSrc, context, layer: 'infrastructure', aggregate: null, isEvent: false, isIndex };
|
|
235
|
+
return { rel: relSrc, context, layer: 'context-other', aggregate: null, isEvent: false, isIndex };
|
|
236
|
+
};
|
|
237
|
+
const resolveRelative = (from, specifier) => {
|
|
238
|
+
const target = path.resolve(path.dirname(from), specifier.replace(/\.(js|mjs|cjs|ts|mts|cts)$/, ''));
|
|
239
|
+
for (const candidate of [`${target}.ts`, `${target}.tsx`, `${target}.js`, path.join(target, 'index.ts'), path.join(target, 'index.js')]) {
|
|
240
|
+
if (fs.existsSync(candidate))
|
|
241
|
+
return candidate;
|
|
242
|
+
}
|
|
243
|
+
return `${target}.ts`;
|
|
244
|
+
};
|
|
245
|
+
const files = walk(project.src).filter((file) => !ignored(relToRoot(file)));
|
|
246
|
+
for (const file of files) {
|
|
247
|
+
const info = classify(file);
|
|
248
|
+
if (!info)
|
|
249
|
+
continue;
|
|
250
|
+
const rel = relToRoot(file);
|
|
251
|
+
const isTest = /\.(spec|test)\.[cm]?[jt]sx?$/.test(file);
|
|
252
|
+
if (isTest || info.layer === 'shared-testing')
|
|
253
|
+
continue;
|
|
254
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
255
|
+
for (const ref of (0, source_1.importsOf)(source)) {
|
|
256
|
+
const spec = ref.specifier;
|
|
257
|
+
const at = { file: rel, line: ref.line };
|
|
258
|
+
// ---- packages and built-ins
|
|
259
|
+
if (!spec.startsWith('.') && !spec.startsWith('/')) {
|
|
260
|
+
const name = packageName(spec);
|
|
261
|
+
if (info.layer === 'domain' || info.layer === 'shared-domain') {
|
|
262
|
+
if (isBuiltin(spec)) {
|
|
263
|
+
if (IO_BUILTINS.has(builtinName(spec))) {
|
|
264
|
+
report({
|
|
265
|
+
rule: info.layer === 'domain' ? 'domain-isolation' : 'shared-kernel',
|
|
266
|
+
...at,
|
|
267
|
+
message: `The domain imports "${spec}" (input/output).`,
|
|
268
|
+
hint: 'Hide it behind a port in domain/ports and implement it in infrastructure/.',
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else if (!(config.allowInDomain ?? []).includes(name)) {
|
|
273
|
+
report({
|
|
274
|
+
rule: info.layer === 'domain' ? 'domain-isolation' : 'shared-kernel',
|
|
275
|
+
...at,
|
|
276
|
+
message: `The domain imports the package "${name}".`,
|
|
277
|
+
hint: matchesPackage(name, INFRASTRUCTURE_PACKAGES)
|
|
278
|
+
? 'Keep frameworks and databases out of the domain: move this code to infrastructure/.'
|
|
279
|
+
: `If it is a pure library, allow it in ${project_1.CONFIG_FILE}: "check": { "allowInDomain": ["${name}"] }.`,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else if (info.layer === 'application' || info.layer === 'shared-application') {
|
|
284
|
+
if ((isBuiltin(spec) && IO_BUILTINS.has(builtinName(spec))) || (!isBuiltin(spec) && matchesPackage(name, INFRASTRUCTURE_PACKAGES))) {
|
|
285
|
+
report({
|
|
286
|
+
rule: info.layer === 'application' ? 'application-isolation' : 'shared-kernel',
|
|
287
|
+
...at,
|
|
288
|
+
message: `The application layer imports "${spec}".`,
|
|
289
|
+
hint: 'Use cases talk to ports (interfaces); the adapter that uses this package belongs in infrastructure/.',
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
// ---- files of the project
|
|
296
|
+
const target = classify(resolveRelative(file, spec));
|
|
297
|
+
if (!target)
|
|
298
|
+
continue;
|
|
299
|
+
// context boundaries
|
|
300
|
+
if (target.context && target.context !== info.context) {
|
|
301
|
+
const allowed = target.isEvent || (target.layer === 'composition' && target.isIndex);
|
|
302
|
+
if (info.layer.startsWith('shared')) {
|
|
303
|
+
report({
|
|
304
|
+
rule: 'shared-kernel',
|
|
305
|
+
...at,
|
|
306
|
+
message: `The shared kernel imports the ${target.context} context (${target.rel}).`,
|
|
307
|
+
hint: 'The shared kernel is used by every context and must not depend on any of them.',
|
|
308
|
+
});
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (!allowed) {
|
|
312
|
+
report({
|
|
313
|
+
rule: 'context-boundaries',
|
|
314
|
+
...at,
|
|
315
|
+
message: info.context
|
|
316
|
+
? `The ${info.context} context reaches into ${target.context} (${target.rel}).`
|
|
317
|
+
: `This file reaches into the ${target.context} context (${target.rel}).`,
|
|
318
|
+
hint: `React to ${target.context}'s events (ddd-express handler ... --on <Event>), or import its public index.ts.`,
|
|
319
|
+
});
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// layers inside a context
|
|
324
|
+
if (info.layer === 'domain') {
|
|
325
|
+
const sameContextDomain = target.context === info.context && target.layer === 'domain';
|
|
326
|
+
if (!sameContextDomain && target.layer !== 'shared-domain' && !(target.isEvent && target.context !== info.context)) {
|
|
327
|
+
report({
|
|
328
|
+
rule: 'domain-isolation',
|
|
329
|
+
...at,
|
|
330
|
+
message: `The domain imports ${describeLayer(target)} (${target.rel}).`,
|
|
331
|
+
hint: 'The domain must not know use cases, adapters or HTTP. Invert the dependency with a port.',
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
else if (sameContextDomain &&
|
|
335
|
+
info.aggregate &&
|
|
336
|
+
target.aggregate &&
|
|
337
|
+
info.aggregate !== target.aggregate) {
|
|
338
|
+
report({
|
|
339
|
+
rule: 'aggregate-references',
|
|
340
|
+
...at,
|
|
341
|
+
message: `${pascal(info.aggregate)} imports the ${pascal(target.aggregate)} aggregate.`,
|
|
342
|
+
hint: `Reference ${pascal(target.aggregate)} by its id; a domain service can work with both.`,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else if (info.layer === 'application') {
|
|
347
|
+
if (['infrastructure', 'composition', 'shared-infrastructure', 'shared-http', 'shared-testing'].includes(target.layer)) {
|
|
348
|
+
report({
|
|
349
|
+
rule: 'application-isolation',
|
|
350
|
+
...at,
|
|
351
|
+
message: `The application layer imports ${describeLayer(target)} (${target.rel}).`,
|
|
352
|
+
hint: 'Depend on a port (interface) instead; the module file wires the adapter in.',
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
else if (info.layer === 'shared-domain') {
|
|
357
|
+
if (target.layer !== 'shared-domain') {
|
|
358
|
+
report({
|
|
359
|
+
rule: 'shared-kernel',
|
|
360
|
+
...at,
|
|
361
|
+
message: `shared/domain imports ${describeLayer(target)} (${target.rel}).`,
|
|
362
|
+
hint: 'The domain part of the shared kernel depends on nothing else.',
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
else if (info.layer === 'shared-application') {
|
|
367
|
+
if (!['shared-domain', 'shared-application'].includes(target.layer)) {
|
|
368
|
+
report({
|
|
369
|
+
rule: 'shared-kernel',
|
|
370
|
+
...at,
|
|
371
|
+
message: `shared/application imports ${describeLayer(target)} (${target.rel}).`,
|
|
372
|
+
hint: 'The application part of the shared kernel depends only on its domain part.',
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// ---- rules on the model the code describes
|
|
379
|
+
for (const context of scan.contexts) {
|
|
380
|
+
for (const event of context.events) {
|
|
381
|
+
const words = (0, names_1.splitWords)(event.name);
|
|
382
|
+
if ((0, names_1.readsLikeCommand)(words)) {
|
|
383
|
+
report({
|
|
384
|
+
rule: 'event-naming',
|
|
385
|
+
file: event.file,
|
|
386
|
+
message: `${event.name} reads like a command, not like a fact that happened.`,
|
|
387
|
+
hint: 'Name events in the past tense, noun first: OrderPlaced, VisitCompleted, InvoicePaid.',
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
for (const aggregate of context.aggregates) {
|
|
392
|
+
for (const command of aggregate.commands) {
|
|
393
|
+
if (command.to && command.events.length === 0) {
|
|
394
|
+
report({
|
|
395
|
+
rule: 'silent-state-change',
|
|
396
|
+
file: aggregate.file,
|
|
397
|
+
message: `${aggregate.name}.${command.name}() changes ${aggregate.stateField} to "${command.to}" without recording an event.`,
|
|
398
|
+
hint: 'Record a domain event (e.g. "emits ...") so the rest of the system can react.',
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (aggregate.commands.length === 0) {
|
|
403
|
+
report({
|
|
404
|
+
rule: 'anemic-aggregate',
|
|
405
|
+
file: aggregate.file,
|
|
406
|
+
message: `${aggregate.name} has no behaviour besides create/update/delete.`,
|
|
407
|
+
hint: `Model what happens in business terms: npx ddd-express command "<name>: <from> -> <to> emits ${aggregate.name}<Event>" --aggregate ${aggregate.kebab} --context ${context.name}`,
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
for (const missing of missingTerms(scan, context.name)) {
|
|
412
|
+
report({
|
|
413
|
+
rule: 'ubiquitous-language',
|
|
414
|
+
file: context.glossary.file ?? `${context.dir}/GLOSSARY.md`,
|
|
415
|
+
message: context.glossary.file
|
|
416
|
+
? `"${missing.term}" (${missing.kind.charAt(0).toLowerCase()}${missing.kind.slice(1)}) is not in the Ubiquitous Language.`
|
|
417
|
+
: `${context.pascal} has no GLOSSARY.md.`,
|
|
418
|
+
hint: 'Describe it in GLOSSARY.md — or run "npx ddd-express check --fix" to add the missing rows.',
|
|
419
|
+
});
|
|
420
|
+
if (!context.glossary.file)
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const order = { error: 0, warning: 1, info: 2 };
|
|
425
|
+
findings.sort((a, b) => a.file.localeCompare(b.file) || (a.line ?? 0) - (b.line ?? 0) || order[a.severity] - order[b.severity]);
|
|
426
|
+
return { findings, filesChecked: files.length, contexts: contexts.size };
|
|
427
|
+
}
|
|
428
|
+
function describeLayer(info) {
|
|
429
|
+
switch (info.layer) {
|
|
430
|
+
case 'domain':
|
|
431
|
+
return `the domain of ${info.context}`;
|
|
432
|
+
case 'application':
|
|
433
|
+
return 'the application layer';
|
|
434
|
+
case 'infrastructure':
|
|
435
|
+
return 'an adapter (infrastructure)';
|
|
436
|
+
case 'composition':
|
|
437
|
+
return 'the module (composition root)';
|
|
438
|
+
case 'shared-domain':
|
|
439
|
+
return 'the shared domain kernel';
|
|
440
|
+
case 'shared-application':
|
|
441
|
+
return 'shared/application';
|
|
442
|
+
case 'shared-infrastructure':
|
|
443
|
+
return 'shared/infrastructure';
|
|
444
|
+
case 'shared-http':
|
|
445
|
+
return 'the HTTP layer';
|
|
446
|
+
case 'shared-testing':
|
|
447
|
+
return 'test helpers';
|
|
448
|
+
default:
|
|
449
|
+
return 'a file outside the layers';
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const pascal = (kebab) => (0, names_1.namesFromWords)((0, names_1.splitWords)(kebab)).pascal;
|
|
453
|
+
/** Domain terms of a context that its glossary does not mention. */
|
|
454
|
+
function missingTerms(scan, contextName) {
|
|
455
|
+
const context = scan.contexts.find((item) => item.name === contextName);
|
|
456
|
+
if (!context)
|
|
457
|
+
return [];
|
|
458
|
+
const known = new Set(context.glossary.terms.map((term) => term.term));
|
|
459
|
+
const out = [];
|
|
460
|
+
const add = (term, kind) => {
|
|
461
|
+
if (!known.has(term) && !out.some((item) => item.term === term)) {
|
|
462
|
+
out.push({ term, kind, human: (0, names_1.splitWords)(term).join(' ') });
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
for (const aggregate of context.aggregates) {
|
|
466
|
+
add(aggregate.name, 'Aggregate root');
|
|
467
|
+
for (const entity of aggregate.entities)
|
|
468
|
+
add(entity, `Entity inside ${aggregate.name}`);
|
|
469
|
+
}
|
|
470
|
+
for (const value of context.valueObjects)
|
|
471
|
+
add(value, 'Value object');
|
|
472
|
+
const crud = new Set(context.aggregates.flatMap((aggregate) => ['Created', 'Updated', 'Deleted'].map((suffix) => `${aggregate.name}${suffix}`)));
|
|
473
|
+
for (const event of context.events)
|
|
474
|
+
if (!crud.has(event.name))
|
|
475
|
+
add(event.name, 'Domain event');
|
|
476
|
+
for (const service of context.services)
|
|
477
|
+
add(service, 'Domain service');
|
|
478
|
+
return out;
|
|
479
|
+
}
|
|
480
|
+
/** Adds the missing terms to every GLOSSARY.md. Returns how many rows were added. */
|
|
481
|
+
function fixGlossaries(project, writer, scan = (0, project_scan_1.scanProject)(project)) {
|
|
482
|
+
let added = 0;
|
|
483
|
+
for (const context of scan.contexts) {
|
|
484
|
+
const names = (0, names_1.namesFromWords)((0, names_1.splitWords)(context.name));
|
|
485
|
+
const file = (0, project_1.contextPaths)(project, names).glossary;
|
|
486
|
+
const missing = missingTerms(scan, context.name);
|
|
487
|
+
if (missing.length === 0)
|
|
488
|
+
continue;
|
|
489
|
+
writer.update(file, (current) => {
|
|
490
|
+
let next = current;
|
|
491
|
+
for (const item of missing)
|
|
492
|
+
next = (0, markers_1.ensureLine)(next, (0, context_1.glossaryRow)(item.term, item.kind, names, item.human));
|
|
493
|
+
return next;
|
|
494
|
+
}, () => {
|
|
495
|
+
let next = (0, context_1.contextGlossary)(names);
|
|
496
|
+
for (const item of missing)
|
|
497
|
+
next = (0, markers_1.ensureLine)(next, (0, context_1.glossaryRow)(item.term, item.kind, names, item.human));
|
|
498
|
+
return next;
|
|
499
|
+
});
|
|
500
|
+
added += missing.length;
|
|
501
|
+
}
|
|
502
|
+
return added;
|
|
503
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** A tiny document model rendered to Markdown (GitHub renders the Mermaid blocks) or to one HTML page. */
|
|
2
|
+
export type Block = {
|
|
3
|
+
kind: 'heading';
|
|
4
|
+
level: 1 | 2 | 3 | 4;
|
|
5
|
+
text: string;
|
|
6
|
+
id?: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'paragraph';
|
|
9
|
+
text: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: 'note';
|
|
12
|
+
text: string;
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'list';
|
|
15
|
+
items: string[];
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'table';
|
|
18
|
+
header: string[];
|
|
19
|
+
rows: string[][];
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'mermaid';
|
|
22
|
+
code: string;
|
|
23
|
+
};
|
|
24
|
+
export declare function slug(text: string): string;
|
|
25
|
+
export declare function toMarkdown(blocks: Block[]): string;
|
|
26
|
+
export declare function toHtml(title: string, blocks: Block[]): string;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** A tiny document model rendered to Markdown (GitHub renders the Mermaid blocks) or to one HTML page. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.slug = slug;
|
|
5
|
+
exports.toMarkdown = toMarkdown;
|
|
6
|
+
exports.toHtml = toHtml;
|
|
7
|
+
function slug(text) {
|
|
8
|
+
return text
|
|
9
|
+
.toLowerCase()
|
|
10
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
11
|
+
.replace(/^-|-$/g, '');
|
|
12
|
+
}
|
|
13
|
+
const escapeCell = (text) => text.replace(/\|/g, '\\|').replace(/\n/g, ' ');
|
|
14
|
+
function toMarkdown(blocks) {
|
|
15
|
+
const out = [];
|
|
16
|
+
for (const block of blocks) {
|
|
17
|
+
switch (block.kind) {
|
|
18
|
+
case 'heading':
|
|
19
|
+
out.push(`${'#'.repeat(block.level)} ${block.text}`, '');
|
|
20
|
+
break;
|
|
21
|
+
case 'paragraph':
|
|
22
|
+
out.push(block.text, '');
|
|
23
|
+
break;
|
|
24
|
+
case 'note':
|
|
25
|
+
out.push(`> ${block.text}`, '');
|
|
26
|
+
break;
|
|
27
|
+
case 'list':
|
|
28
|
+
out.push(...block.items.map((item) => `- ${item}`), '');
|
|
29
|
+
break;
|
|
30
|
+
case 'table':
|
|
31
|
+
if (block.rows.length === 0)
|
|
32
|
+
break;
|
|
33
|
+
out.push(`| ${block.header.map(escapeCell).join(' | ')} |`);
|
|
34
|
+
out.push(`|${block.header.map(() => '---').join('|')}|`);
|
|
35
|
+
for (const row of block.rows)
|
|
36
|
+
out.push(`| ${row.map(escapeCell).join(' | ')} |`);
|
|
37
|
+
out.push('');
|
|
38
|
+
break;
|
|
39
|
+
case 'mermaid':
|
|
40
|
+
out.push('```mermaid', block.code.trimEnd(), '```', '');
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return `${out.join('\n').trimEnd()}\n`;
|
|
45
|
+
}
|
|
46
|
+
const escapeHtml = (text) => text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
47
|
+
/** **bold**, `code` and [text](url) inside HTML text. */
|
|
48
|
+
function inline(text) {
|
|
49
|
+
return escapeHtml(text)
|
|
50
|
+
.replace(/`([^`]+)`/g, '<code>$1</code>')
|
|
51
|
+
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
52
|
+
.replace(/\[([^\]]+)\]\((https?:[^)]+)\)/g, '<a href="$2">$1</a>');
|
|
53
|
+
}
|
|
54
|
+
function toHtml(title, blocks) {
|
|
55
|
+
const toc = blocks
|
|
56
|
+
.filter((block) => block.kind === 'heading' && block.level === 2)
|
|
57
|
+
.map((block) => `<a href="#${block.id ?? slug(block.text)}">${escapeHtml(block.text)}</a>`)
|
|
58
|
+
.join('');
|
|
59
|
+
const body = blocks
|
|
60
|
+
.map((block) => {
|
|
61
|
+
switch (block.kind) {
|
|
62
|
+
case 'heading': {
|
|
63
|
+
const id = block.id ?? slug(block.text);
|
|
64
|
+
return `<h${block.level} id="${id}">${inline(block.text)}</h${block.level}>`;
|
|
65
|
+
}
|
|
66
|
+
case 'paragraph':
|
|
67
|
+
return `<p>${inline(block.text)}</p>`;
|
|
68
|
+
case 'note':
|
|
69
|
+
return `<p class="note">${inline(block.text)}</p>`;
|
|
70
|
+
case 'list':
|
|
71
|
+
return `<ul>${block.items.map((item) => `<li>${inline(item)}</li>`).join('')}</ul>`;
|
|
72
|
+
case 'table':
|
|
73
|
+
if (block.rows.length === 0)
|
|
74
|
+
return '';
|
|
75
|
+
return `<div class="table"><table><thead><tr>${block.header.map((cell) => `<th>${inline(cell)}</th>`).join('')}</tr></thead><tbody>${block.rows
|
|
76
|
+
.map((row) => `<tr>${row.map((cell) => `<td>${inline(cell)}</td>`).join('')}</tr>`)
|
|
77
|
+
.join('')}</tbody></table></div>`;
|
|
78
|
+
case 'mermaid':
|
|
79
|
+
return `<pre class="mermaid">${escapeHtml(block.code)}</pre>`;
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
.join('\n');
|
|
83
|
+
return `<!DOCTYPE html>
|
|
84
|
+
<html lang="en">
|
|
85
|
+
<head>
|
|
86
|
+
<meta charset="utf-8">
|
|
87
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
88
|
+
<title>${escapeHtml(title)}</title>
|
|
89
|
+
<style>
|
|
90
|
+
:root { color-scheme: light dark; --bg: #ffffff; --fg: #1f2328; --muted: #59636e; --line: #d1d9e0; --card: #f6f8fa; --accent: #0969da; }
|
|
91
|
+
@media (prefers-color-scheme: dark) { :root { --bg: #0d1117; --fg: #e6edf3; --muted: #9198a1; --line: #30363d; --card: #151b23; --accent: #4493f8; } }
|
|
92
|
+
* { box-sizing: border-box; }
|
|
93
|
+
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; }
|
|
94
|
+
header { position: sticky; top: 0; z-index: 2; background: var(--bg); border-bottom: 1px solid var(--line); padding: 12px 24px; display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
|
|
95
|
+
header strong { margin-right: 8px; }
|
|
96
|
+
header a { color: var(--muted); text-decoration: none; font-size: 14px; }
|
|
97
|
+
header a:hover { color: var(--accent); }
|
|
98
|
+
main { max-width: 1100px; margin: 0 auto; padding: 8px 24px 64px; }
|
|
99
|
+
h1 { font-size: 30px; margin: 28px 0 8px; }
|
|
100
|
+
h2 { font-size: 22px; margin: 40px 0 12px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
|
|
101
|
+
h3 { font-size: 17px; margin: 28px 0 8px; }
|
|
102
|
+
h4 { font-size: 15px; margin: 20px 0 6px; color: var(--muted); }
|
|
103
|
+
code { font: 13px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: 1px 5px; }
|
|
104
|
+
.note { color: var(--muted); border-left: 3px solid var(--line); padding-left: 12px; }
|
|
105
|
+
.table { overflow-x: auto; }
|
|
106
|
+
table { border-collapse: collapse; width: 100%; margin: 8px 0 16px; font-size: 14px; }
|
|
107
|
+
th, td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
|
|
108
|
+
th { background: var(--card); }
|
|
109
|
+
pre.mermaid { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px; overflow-x: auto; text-align: center; }
|
|
110
|
+
a { color: var(--accent); }
|
|
111
|
+
</style>
|
|
112
|
+
</head>
|
|
113
|
+
<body>
|
|
114
|
+
<header><strong>${escapeHtml(title)}</strong>${toc}</header>
|
|
115
|
+
<main>
|
|
116
|
+
${body}
|
|
117
|
+
</main>
|
|
118
|
+
<script type="module">
|
|
119
|
+
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
|
|
120
|
+
const dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
121
|
+
mermaid.initialize({ startOnLoad: true, theme: dark ? 'dark' : 'default', securityLevel: 'loose' });
|
|
122
|
+
</script>
|
|
123
|
+
</body>
|
|
124
|
+
</html>
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Field } from '../core/fields';
|
|
2
|
+
import type { ScannedAggregate, ScannedProject } from '../scan/project-scan';
|
|
3
|
+
import { type Block } from './document';
|
|
4
|
+
export declare function fieldType(field: Field): string;
|
|
5
|
+
export declare function summaryCounts(scan: ScannedProject): {
|
|
6
|
+
contexts: number;
|
|
7
|
+
aggregates: number;
|
|
8
|
+
behaviours: number;
|
|
9
|
+
events: number;
|
|
10
|
+
policies: number;
|
|
11
|
+
valueObjects: number;
|
|
12
|
+
};
|
|
13
|
+
/** Bounded contexts and the events that flow between them. */
|
|
14
|
+
export declare function contextMapDiagram(scan: ScannedProject): string;
|
|
15
|
+
/** Event Storming view: behaviour → domain event → policy, grouped by context. */
|
|
16
|
+
export declare function eventFlowDiagram(scan: ScannedProject): string | null;
|
|
17
|
+
/** The life cycle of an aggregate with a state field. */
|
|
18
|
+
export declare function stateDiagram(aggregate: ScannedAggregate): string | null;
|
|
19
|
+
export declare function buildMap(scan: ScannedProject, generatedOn: string): Block[];
|