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,174 @@
|
|
|
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.generateMap = generateMap;
|
|
37
|
+
exports.generateOpenApi = generateOpenApi;
|
|
38
|
+
exports.runCheck = runCheck;
|
|
39
|
+
exports.rulesHelp = rulesHelp;
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const errors_1 = require("../core/errors");
|
|
42
|
+
const output_1 = require("../core/output");
|
|
43
|
+
const project_1 = require("../core/project");
|
|
44
|
+
const writer_1 = require("../core/writer");
|
|
45
|
+
const check_1 = require("../analyze/check");
|
|
46
|
+
const document_1 = require("../analyze/document");
|
|
47
|
+
const map_1 = require("../analyze/map");
|
|
48
|
+
const openapi_1 = require("../analyze/openapi");
|
|
49
|
+
const project_scan_1 = require("../scan/project-scan");
|
|
50
|
+
const common_1 = require("./common");
|
|
51
|
+
function today() {
|
|
52
|
+
return new Date().toISOString().slice(0, 10);
|
|
53
|
+
}
|
|
54
|
+
/** Writes `content` to `out` (relative to the project), or prints it when out is "-". */
|
|
55
|
+
function emit(writer, root, out, content, io) {
|
|
56
|
+
if (out === '-') {
|
|
57
|
+
io.out(content.trimEnd());
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const file = path.resolve(root, out);
|
|
61
|
+
writer.update(file, () => content, () => content);
|
|
62
|
+
return file;
|
|
63
|
+
}
|
|
64
|
+
// =============================================================================================
|
|
65
|
+
// ddd-express map
|
|
66
|
+
// =============================================================================================
|
|
67
|
+
function generateMap(options, io) {
|
|
68
|
+
const format = (options.format ?? 'md').toLowerCase();
|
|
69
|
+
if (!['md', 'markdown', 'html', 'json'].includes(format)) {
|
|
70
|
+
throw new errors_1.CliError(`Unknown format "${options.format}".`, 'Use md (default), html or json.');
|
|
71
|
+
}
|
|
72
|
+
const project = (0, project_1.loadProject)(options.cwd, (0, common_1.overrides)(options));
|
|
73
|
+
const scan = (0, project_scan_1.scanProject)(project);
|
|
74
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: true });
|
|
75
|
+
const outcome = (0, common_1.newOutcome)('Domain map', writer);
|
|
76
|
+
const defaults = { md: 'docs/DOMAIN.md', markdown: 'docs/DOMAIN.md', html: 'docs/domain.html', json: 'docs/domain.json' };
|
|
77
|
+
const out = options.out ?? defaults[format];
|
|
78
|
+
let content;
|
|
79
|
+
if (format === 'json') {
|
|
80
|
+
const { root: _root, ...portable } = scan;
|
|
81
|
+
void _root;
|
|
82
|
+
content = `${JSON.stringify(portable, null, 2)}\n`;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const blocks = (0, map_1.buildMap)(scan, today());
|
|
86
|
+
content = format === 'html' ? (0, document_1.toHtml)(`Domain map — ${scan.name}`, blocks) : (0, document_1.toMarkdown)(blocks);
|
|
87
|
+
}
|
|
88
|
+
const file = emit(writer, project.root, out, content, io);
|
|
89
|
+
if (!file)
|
|
90
|
+
return null;
|
|
91
|
+
const counts = (0, map_1.summaryCounts)(scan);
|
|
92
|
+
outcome.notes.push(`${counts.contexts} contexts · ${counts.aggregates} aggregates · ${counts.behaviours} behaviours · ${counts.events} events · ${counts.policies} policies`, format === 'html'
|
|
93
|
+
? `Open ${path.relative(project.root, file)} in a browser.`
|
|
94
|
+
: format === 'json'
|
|
95
|
+
? 'Feed it to your own tools.'
|
|
96
|
+
: `GitHub renders the diagrams of ${path.relative(project.root, file)}. Re-run "npx ddd-express map" after changes (or in CI).`);
|
|
97
|
+
return outcome;
|
|
98
|
+
}
|
|
99
|
+
// =============================================================================================
|
|
100
|
+
// ddd-express openapi
|
|
101
|
+
// =============================================================================================
|
|
102
|
+
function generateOpenApi(options, io) {
|
|
103
|
+
const format = (options.format ?? 'json').toLowerCase();
|
|
104
|
+
if (!['json', 'html'].includes(format))
|
|
105
|
+
throw new errors_1.CliError(`Unknown format "${options.format}".`, 'Use json (default) or html.');
|
|
106
|
+
const project = (0, project_1.loadProject)(options.cwd, (0, common_1.overrides)(options));
|
|
107
|
+
const scan = (0, project_scan_1.scanProject)(project);
|
|
108
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: true });
|
|
109
|
+
const outcome = (0, common_1.newOutcome)('OpenAPI', writer);
|
|
110
|
+
const spec = (0, openapi_1.buildOpenApi)(scan);
|
|
111
|
+
const content = format === 'html' ? (0, openapi_1.apiReferenceHtml)(spec, scan.name) : `${JSON.stringify(spec, null, 2)}\n`;
|
|
112
|
+
const file = emit(writer, project.root, options.out ?? (format === 'html' ? 'docs/api.html' : 'openapi.json'), content, io);
|
|
113
|
+
if (!file)
|
|
114
|
+
return null;
|
|
115
|
+
const operations = Object.values(spec.paths).reduce((sum, item) => sum + Object.keys(item).length, 0);
|
|
116
|
+
outcome.notes.push(`${operations} operations on ${Object.keys(spec.paths).length} paths (OpenAPI 3.1).`, format === 'html'
|
|
117
|
+
? `Open ${path.relative(project.root, file)} in a browser to explore and try the API.`
|
|
118
|
+
: 'Use it with Swagger UI, Scalar, Postman or a client generator. "--format html" builds a browsable reference.');
|
|
119
|
+
return outcome;
|
|
120
|
+
}
|
|
121
|
+
/** Prints the findings; returns the exit code. */
|
|
122
|
+
function runCheck(options, io) {
|
|
123
|
+
const project = (0, project_1.loadProject)(options.cwd, (0, common_1.overrides)(options));
|
|
124
|
+
const config = (0, check_1.loadCheckConfig)(project);
|
|
125
|
+
let scan = (0, project_scan_1.scanProject)(project);
|
|
126
|
+
let fixed = 0;
|
|
127
|
+
if (options.fix) {
|
|
128
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: true });
|
|
129
|
+
fixed = (0, check_1.fixGlossaries)(project, writer, scan);
|
|
130
|
+
if (fixed > 0 && !options.dryRun)
|
|
131
|
+
scan = (0, project_scan_1.scanProject)(project);
|
|
132
|
+
}
|
|
133
|
+
const result = (0, check_1.checkProject)(project, config, scan);
|
|
134
|
+
const errors = result.findings.filter((finding) => finding.severity === 'error').length;
|
|
135
|
+
const warnings = result.findings.filter((finding) => finding.severity === 'warning').length;
|
|
136
|
+
const infos = result.findings.filter((finding) => finding.severity === 'info').length;
|
|
137
|
+
const failed = errors > 0 || (options.strict && warnings > 0);
|
|
138
|
+
if (options.json) {
|
|
139
|
+
io.out(JSON.stringify({ files: result.filesChecked, contexts: result.contexts, fixed, summary: { errors, warnings, infos }, findings: result.findings }, null, 2));
|
|
140
|
+
return failed ? 1 : 0;
|
|
141
|
+
}
|
|
142
|
+
io.out((0, output_1.paint)(io, 'bold', `ddd-express check`) + (0, output_1.paint)(io, 'gray', ` · ${result.contexts} contexts · ${result.filesChecked} files`));
|
|
143
|
+
if (fixed > 0)
|
|
144
|
+
io.out((0, output_1.paint)(io, 'green', ` ✔ added ${fixed} missing term${fixed === 1 ? '' : 's'} to the glossaries${options.dryRun ? ' (dry run)' : ''}`));
|
|
145
|
+
let current = '';
|
|
146
|
+
const styles = { error: 'red', warning: 'yellow', info: 'blue' };
|
|
147
|
+
for (const finding of result.findings) {
|
|
148
|
+
if (finding.file !== current) {
|
|
149
|
+
current = finding.file;
|
|
150
|
+
io.out(`\n${(0, output_1.paint)(io, 'bold', finding.file)}`);
|
|
151
|
+
}
|
|
152
|
+
const where = finding.line ? `${finding.line}`.padStart(4) : ' ';
|
|
153
|
+
io.out(`${where} ${(0, output_1.paint)(io, styles[finding.severity], finding.severity.padEnd(7))} ${(0, output_1.paint)(io, 'gray', finding.rule.padEnd(22))} ${finding.message}`);
|
|
154
|
+
if (finding.hint)
|
|
155
|
+
io.out(`${' '.repeat(39)}${(0, output_1.paint)(io, 'gray', `→ ${finding.hint}`)}`);
|
|
156
|
+
}
|
|
157
|
+
io.out('');
|
|
158
|
+
if (errors + warnings === 0) {
|
|
159
|
+
io.out((0, output_1.paint)(io, 'green', `✔ The architecture holds: no problems in ${result.filesChecked} files.`) +
|
|
160
|
+
(infos > 0 ? (0, output_1.paint)(io, 'gray', ` (${infos} suggestion${infos === 1 ? '' : 's'})`) : ''));
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const parts = [`${errors} error${errors === 1 ? '' : 's'}`, `${warnings} warning${warnings === 1 ? '' : 's'}`];
|
|
164
|
+
if (infos > 0)
|
|
165
|
+
parts.push(`${infos} suggestion${infos === 1 ? '' : 's'}`);
|
|
166
|
+
io.out((0, output_1.paint)(io, failed ? 'red' : 'yellow', `${failed ? '✖' : '!'} ${parts.join(', ')}`));
|
|
167
|
+
if (!failed && warnings > 0)
|
|
168
|
+
io.out((0, output_1.paint)(io, 'gray', ' Warnings do not fail the check; use --strict to make them fail.'));
|
|
169
|
+
}
|
|
170
|
+
return failed ? 1 : 0;
|
|
171
|
+
}
|
|
172
|
+
function rulesHelp() {
|
|
173
|
+
return Object.entries(check_1.RULES).map(([id, rule]) => ` ${id.padEnd(22)} ${rule.severity.padEnd(8)} ${rule.summary}`);
|
|
174
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type ResolvedModel } from '../model/validate';
|
|
2
|
+
import { type CommonOptions, type Outcome } from './common';
|
|
3
|
+
import { Engine, type WriteOptions } from './engine';
|
|
4
|
+
export declare const MODEL_FILE = "domain.ddd";
|
|
5
|
+
/** Reads and validates a model file. Throws ModelError listing every problem. */
|
|
6
|
+
export declare function loadModel(file: string, displayName?: string): ResolvedModel;
|
|
7
|
+
export declare function exampleModel(name: string): string;
|
|
8
|
+
export interface ApplySummary {
|
|
9
|
+
contexts: number;
|
|
10
|
+
aggregates: number;
|
|
11
|
+
commands: number;
|
|
12
|
+
valueObjects: number;
|
|
13
|
+
events: number;
|
|
14
|
+
services: number;
|
|
15
|
+
entities: number;
|
|
16
|
+
handlers: number;
|
|
17
|
+
upToDate: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Generates everything the model describes that does not exist yet. Existing code is never
|
|
21
|
+
* changed, except to add new commands, wiring and glossary rows.
|
|
22
|
+
*/
|
|
23
|
+
export declare function applyModel(engine: Engine, resolved: ResolvedModel, options: WriteOptions): ApplySummary;
|
|
24
|
+
export declare function describeSummary(summary: ApplySummary): string;
|
|
25
|
+
export declare function generateApply(file: string | undefined, options: CommonOptions): Outcome;
|
|
26
|
+
export declare function generateModelFile(options: CommonOptions & {
|
|
27
|
+
example?: string;
|
|
28
|
+
}): Outcome;
|
|
@@ -0,0 +1,234 @@
|
|
|
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.MODEL_FILE = void 0;
|
|
37
|
+
exports.loadModel = loadModel;
|
|
38
|
+
exports.exampleModel = exampleModel;
|
|
39
|
+
exports.applyModel = applyModel;
|
|
40
|
+
exports.describeSummary = describeSummary;
|
|
41
|
+
exports.generateApply = generateApply;
|
|
42
|
+
exports.generateModelFile = generateModelFile;
|
|
43
|
+
const fs = __importStar(require("node:fs"));
|
|
44
|
+
const path = __importStar(require("node:path"));
|
|
45
|
+
const errors_1 = require("../core/errors");
|
|
46
|
+
const project_1 = require("../core/project");
|
|
47
|
+
const writer_1 = require("../core/writer");
|
|
48
|
+
const examples_1 = require("../model/examples");
|
|
49
|
+
const parser_1 = require("../model/parser");
|
|
50
|
+
const validate_1 = require("../model/validate");
|
|
51
|
+
const source_1 = require("../scan/source");
|
|
52
|
+
const common_1 = require("./common");
|
|
53
|
+
const engine_1 = require("./engine");
|
|
54
|
+
exports.MODEL_FILE = 'domain.ddd';
|
|
55
|
+
/** Reads and validates a model file. Throws ModelError listing every problem. */
|
|
56
|
+
function loadModel(file, displayName = path.basename(file)) {
|
|
57
|
+
if (!fs.existsSync(file)) {
|
|
58
|
+
throw new errors_1.CliError(`${displayName} not found.`, 'Create one with "npx ddd-express model" (a starter) or "npx ddd-express model --example healthcare".');
|
|
59
|
+
}
|
|
60
|
+
return (0, validate_1.validateModel)((0, parser_1.parseModel)(fs.readFileSync(file, 'utf8'), displayName));
|
|
61
|
+
}
|
|
62
|
+
function exampleModel(name) {
|
|
63
|
+
const example = examples_1.EXAMPLES[name];
|
|
64
|
+
if (!example) {
|
|
65
|
+
throw new errors_1.CliError(`Unknown example "${name}".`, `Examples: ${Object.entries(examples_1.EXAMPLES)
|
|
66
|
+
.map(([key, value]) => `${key} (${value.description})`)
|
|
67
|
+
.join('; ')}.`);
|
|
68
|
+
}
|
|
69
|
+
return example.model;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Generates everything the model describes that does not exist yet. Existing code is never
|
|
73
|
+
* changed, except to add new commands, wiring and glossary rows.
|
|
74
|
+
*/
|
|
75
|
+
function applyModel(engine, resolved, options) {
|
|
76
|
+
const summary = {
|
|
77
|
+
contexts: 0,
|
|
78
|
+
aggregates: 0,
|
|
79
|
+
commands: 0,
|
|
80
|
+
valueObjects: 0,
|
|
81
|
+
events: 0,
|
|
82
|
+
services: 0,
|
|
83
|
+
entities: 0,
|
|
84
|
+
handlers: 0,
|
|
85
|
+
upToDate: 0,
|
|
86
|
+
};
|
|
87
|
+
const { model } = resolved;
|
|
88
|
+
const exists = (file) => engine.writer.exists(file);
|
|
89
|
+
// 1. contexts
|
|
90
|
+
for (const context of model.contexts) {
|
|
91
|
+
if (!exists((0, project_1.ts)((0, project_1.contextPaths)(engine.project, context.names).module)))
|
|
92
|
+
summary.contexts += 1;
|
|
93
|
+
engine.context(context.names);
|
|
94
|
+
}
|
|
95
|
+
// 2. value objects, standalone events, services
|
|
96
|
+
for (const context of model.contexts) {
|
|
97
|
+
const ctx = (0, project_1.contextPaths)(engine.project, context.names);
|
|
98
|
+
for (const value of context.valueObjects) {
|
|
99
|
+
if (exists((0, project_1.ts)(path.join(ctx.valueObjects, value.names.kebab))) && !options.force) {
|
|
100
|
+
summary.upToDate += 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
engine.valueObject(ctx, value.names, value.single, value.fields, options);
|
|
104
|
+
summary.valueObjects += 1;
|
|
105
|
+
}
|
|
106
|
+
for (const event of context.events) {
|
|
107
|
+
if (exists((0, project_1.ts)(path.join(ctx.events, `${event.names.kebab}.event`))) && !options.force) {
|
|
108
|
+
summary.upToDate += 1;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
engine.event(ctx, event.names, event.fields, options);
|
|
112
|
+
summary.events += 1;
|
|
113
|
+
}
|
|
114
|
+
for (const service of context.services) {
|
|
115
|
+
if (exists((0, project_1.ts)(path.join(ctx.services, `${service.names.kebab}.service`))) && !options.force) {
|
|
116
|
+
summary.upToDate += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
engine.service(ctx, service.names, options);
|
|
120
|
+
summary.services += 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// 3. aggregates, their commands and entities
|
|
124
|
+
for (const context of model.contexts) {
|
|
125
|
+
const ctx = (0, project_1.contextPaths)(engine.project, context.names);
|
|
126
|
+
const declaredEvents = new Map(context.events.map((event) => [event.names.pascal, event.fields]));
|
|
127
|
+
for (const aggregate of context.aggregates) {
|
|
128
|
+
const resolvedAggregate = resolved.aggregates.get(aggregate);
|
|
129
|
+
if (!resolvedAggregate)
|
|
130
|
+
continue;
|
|
131
|
+
const paths = (0, project_1.aggregatePaths)(ctx, aggregate.names);
|
|
132
|
+
const source = engine.writer.read((0, project_1.ts)(paths.file));
|
|
133
|
+
if (source === null || options.force) {
|
|
134
|
+
engine.aggregate(context.names, aggregate.names, aggregate.fields, aggregate.commands, {
|
|
135
|
+
...options,
|
|
136
|
+
route: resolvedAggregate.route,
|
|
137
|
+
declaredEvents,
|
|
138
|
+
});
|
|
139
|
+
summary.aggregates += 1;
|
|
140
|
+
summary.commands += aggregate.commands.length;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const shape = (0, source_1.readAggregate)(source, aggregate.names.pascal);
|
|
144
|
+
if (!shape) {
|
|
145
|
+
engine.outcome.warnings.push(`${engine.writer.relative((0, project_1.ts)(paths.file))} was changed too much to add to it; skipped.`);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const missingFields = aggregate.fields.filter((field) => !shape.fields.some((existing) => existing.names.camel === field.names.camel));
|
|
149
|
+
if (missingFields.length > 0) {
|
|
150
|
+
engine.outcome.warnings.push(`${aggregate.names.pascal} in the code has no ${missingFields.map((field) => `"${field.names.camel}"`).join(', ')}. ` +
|
|
151
|
+
'New fields touch many files: add them by hand, or regenerate the aggregate with --force (overwrites its files).');
|
|
152
|
+
}
|
|
153
|
+
for (const raw of aggregate.commands) {
|
|
154
|
+
if (shape.methods.includes(raw.names.camel)) {
|
|
155
|
+
summary.upToDate += 1;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
engine.command(ctx, aggregate.names, raw, options, declaredEvents);
|
|
159
|
+
summary.commands += 1;
|
|
160
|
+
}
|
|
161
|
+
summary.upToDate += 1;
|
|
162
|
+
}
|
|
163
|
+
for (const entity of aggregate.entities) {
|
|
164
|
+
if (exists((0, project_1.ts)(path.join(paths.dir, entity.names.kebab))) && !options.force) {
|
|
165
|
+
summary.upToDate += 1;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
engine.entity(ctx, aggregate.names, entity.names, entity.fields, options);
|
|
169
|
+
summary.entities += 1;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// 4. policies: who reacts to which event
|
|
174
|
+
for (const { context, policy, publisher } of resolved.policies) {
|
|
175
|
+
const ctx = (0, project_1.contextPaths)(engine.project, context.names);
|
|
176
|
+
const { fileBase } = engine.handlerNames(policy.handler?.pascal ?? null, policy.event);
|
|
177
|
+
if (exists((0, project_1.ts)(path.join(ctx.application, `${fileBase}.handler`))) && !options.force) {
|
|
178
|
+
summary.upToDate += 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
engine.handler(ctx, policy.handler?.pascal ?? null, policy.event, publisher?.kebab ?? null, options);
|
|
182
|
+
summary.handlers += 1;
|
|
183
|
+
}
|
|
184
|
+
return summary;
|
|
185
|
+
}
|
|
186
|
+
function describeSummary(summary) {
|
|
187
|
+
const parts = [
|
|
188
|
+
[summary.contexts, 'context'],
|
|
189
|
+
[summary.aggregates, 'aggregate'],
|
|
190
|
+
[summary.commands, 'command'],
|
|
191
|
+
[summary.entities, 'entity'],
|
|
192
|
+
[summary.valueObjects, 'value object'],
|
|
193
|
+
[summary.events, 'event'],
|
|
194
|
+
[summary.services, 'service'],
|
|
195
|
+
[summary.handlers, 'policy'],
|
|
196
|
+
];
|
|
197
|
+
const plural = (count, word) => `${count} ${count === 1 ? word : word.endsWith('y') ? `${word.slice(0, -1)}ies` : `${word}s`}`;
|
|
198
|
+
const added = parts.filter(([count]) => count > 0).map(([count, word]) => plural(count, word));
|
|
199
|
+
return added.length > 0 ? `Added ${added.join(', ')}.` : 'Everything in the model already exists in the code.';
|
|
200
|
+
}
|
|
201
|
+
// =============================================================================================
|
|
202
|
+
// ddd-express apply [file]
|
|
203
|
+
// =============================================================================================
|
|
204
|
+
function generateApply(file, options) {
|
|
205
|
+
const project = (0, project_1.loadProject)(options.cwd, (0, common_1.overrides)(options));
|
|
206
|
+
const modelPath = path.resolve(file ? options.cwd : project.root, file ?? exports.MODEL_FILE);
|
|
207
|
+
const resolved = loadModel(modelPath, path.relative(project.root, modelPath) || exports.MODEL_FILE);
|
|
208
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: options.force });
|
|
209
|
+
const engine = new engine_1.Engine(project, writer, `Applying ${path.relative(project.root, modelPath)}`);
|
|
210
|
+
engine.foundation();
|
|
211
|
+
const summary = applyModel(engine, resolved, { force: options.force, skipTests: options.skipTests });
|
|
212
|
+
engine.outcome.notes.unshift(describeSummary(summary));
|
|
213
|
+
if (summary.contexts + summary.aggregates + summary.commands > 0) {
|
|
214
|
+
engine.outcome.notes.push('', 'Next: npm test · npx ddd-express map · npx ddd-express check');
|
|
215
|
+
}
|
|
216
|
+
return engine.outcome;
|
|
217
|
+
}
|
|
218
|
+
// =============================================================================================
|
|
219
|
+
// ddd-express model [--example <name>]
|
|
220
|
+
// =============================================================================================
|
|
221
|
+
function generateModelFile(options) {
|
|
222
|
+
const project = (0, project_1.loadProject)(options.cwd, (0, common_1.overrides)(options));
|
|
223
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: options.force });
|
|
224
|
+
const engine = new engine_1.Engine(project, writer, options.example ? `Example model "${options.example}"` : 'Starter model');
|
|
225
|
+
const content = options.example ? exampleModel(options.example) : examples_1.STARTER_MODEL;
|
|
226
|
+
(0, validate_1.validateModel)((0, parser_1.parseModel)(content, exports.MODEL_FILE));
|
|
227
|
+
const target = path.join(project.root, exports.MODEL_FILE);
|
|
228
|
+
if (writer.exists(target) && !options.force) {
|
|
229
|
+
throw new errors_1.CliError(`${exports.MODEL_FILE} already exists.`, 'Edit it and run "npx ddd-express apply", or use --force to replace it.');
|
|
230
|
+
}
|
|
231
|
+
writer.create(target, content);
|
|
232
|
+
engine.outcome.notes.push(`Edit ${exports.MODEL_FILE}, then generate the code: npx ddd-express apply`);
|
|
233
|
+
return engine.outcome;
|
|
234
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Names } from '../core/names';
|
|
2
|
+
import { type ContextPaths, type DddConfig, type ModuleSystem, type Project, type TestRunner } from '../core/project';
|
|
3
|
+
import { Writer } from '../core/writer';
|
|
4
|
+
/** Options shared by every command. */
|
|
5
|
+
export interface CommonOptions {
|
|
6
|
+
cwd: string;
|
|
7
|
+
dryRun: boolean;
|
|
8
|
+
force: boolean;
|
|
9
|
+
skipTests: boolean;
|
|
10
|
+
srcDir?: string;
|
|
11
|
+
testRunner?: TestRunner;
|
|
12
|
+
moduleSystem?: ModuleSystem;
|
|
13
|
+
}
|
|
14
|
+
/** What a command did: the file changes, and what the user should do next. */
|
|
15
|
+
export interface Outcome {
|
|
16
|
+
title: string;
|
|
17
|
+
writer: Writer;
|
|
18
|
+
notes: string[];
|
|
19
|
+
warnings: string[];
|
|
20
|
+
}
|
|
21
|
+
export declare function overrides(options: CommonOptions): Partial<DddConfig>;
|
|
22
|
+
export declare function openProject(options: CommonOptions): {
|
|
23
|
+
project: Project;
|
|
24
|
+
writer: Writer;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Makes sure the shared kernel, src/contexts.ts and ddd.config.json exist. Only missing files are
|
|
28
|
+
* written (existing ones are never touched, even with --force), so it is safe on every run.
|
|
29
|
+
* Returns true when something was added.
|
|
30
|
+
*/
|
|
31
|
+
export declare function ensureFoundation(project: Project, writer: Writer): boolean;
|
|
32
|
+
/** Shared-kernel files that exist but differ from the generated version (e.g. the project had its own). */
|
|
33
|
+
export declare function customizedSharedFiles(project: Project, writer: Writer): string[];
|
|
34
|
+
export declare function contextNames(input: string): Names;
|
|
35
|
+
/**
|
|
36
|
+
* Creates the bounded context when it does not exist yet, and mounts it in src/contexts.ts.
|
|
37
|
+
* Returns its paths and whether it was created now.
|
|
38
|
+
*/
|
|
39
|
+
export declare function ensureContext(project: Project, writer: Writer, names: Names, outcome: Outcome): {
|
|
40
|
+
context: ContextPaths;
|
|
41
|
+
created: boolean;
|
|
42
|
+
};
|
|
43
|
+
/** For commands that add to an existing context: the context must exist. */
|
|
44
|
+
export declare function requireContext(project: Project, writer: Writer, given: string | undefined): ContextPaths;
|
|
45
|
+
/** Adds an export line to a barrel file (creating the barrel when needed). */
|
|
46
|
+
export declare function addToBarrel(writer: Writer, barrel: string, line: string): void;
|
|
47
|
+
/** Adds a row to the context glossary, unless the term is already there. */
|
|
48
|
+
export declare function addGlossaryRow(writer: Writer, glossary: string, term: string, row: string): void;
|
|
49
|
+
export declare function newOutcome(title: string, writer: Writer): Outcome;
|
|
@@ -0,0 +1,173 @@
|
|
|
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.overrides = overrides;
|
|
37
|
+
exports.openProject = openProject;
|
|
38
|
+
exports.ensureFoundation = ensureFoundation;
|
|
39
|
+
exports.customizedSharedFiles = customizedSharedFiles;
|
|
40
|
+
exports.contextNames = contextNames;
|
|
41
|
+
exports.ensureContext = ensureContext;
|
|
42
|
+
exports.requireContext = requireContext;
|
|
43
|
+
exports.addToBarrel = addToBarrel;
|
|
44
|
+
exports.addGlossaryRow = addGlossaryRow;
|
|
45
|
+
exports.newOutcome = newOutcome;
|
|
46
|
+
const path = __importStar(require("node:path"));
|
|
47
|
+
const errors_1 = require("../core/errors");
|
|
48
|
+
const markers_1 = require("../core/markers");
|
|
49
|
+
const names_1 = require("../core/names");
|
|
50
|
+
const project_1 = require("../core/project");
|
|
51
|
+
const writer_1 = require("../core/writer");
|
|
52
|
+
const context_1 = require("../templates/context");
|
|
53
|
+
const project_2 = require("../templates/project");
|
|
54
|
+
const shared_kernel_1 = require("../templates/shared-kernel");
|
|
55
|
+
function overrides(options) {
|
|
56
|
+
const out = {};
|
|
57
|
+
if (options.srcDir)
|
|
58
|
+
out.srcDir = options.srcDir;
|
|
59
|
+
if (options.testRunner)
|
|
60
|
+
out.testRunner = options.testRunner;
|
|
61
|
+
if (options.moduleSystem)
|
|
62
|
+
out.moduleSystem = options.moduleSystem;
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
function openProject(options) {
|
|
66
|
+
const project = (0, project_1.loadProject)(options.cwd, overrides(options));
|
|
67
|
+
const writer = new writer_1.Writer({ root: project.root, dryRun: options.dryRun, force: options.force });
|
|
68
|
+
return { project, writer };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Makes sure the shared kernel, src/contexts.ts and ddd.config.json exist. Only missing files are
|
|
72
|
+
* written (existing ones are never touched, even with --force), so it is safe on every run.
|
|
73
|
+
* Returns true when something was added.
|
|
74
|
+
*/
|
|
75
|
+
function ensureFoundation(project, writer) {
|
|
76
|
+
let added = false;
|
|
77
|
+
const create = (file, content) => {
|
|
78
|
+
if (writer.exists(file))
|
|
79
|
+
return;
|
|
80
|
+
writer.create(file, content);
|
|
81
|
+
added = true;
|
|
82
|
+
};
|
|
83
|
+
for (const [file, content] of (0, shared_kernel_1.sharedKernelFiles)(project))
|
|
84
|
+
create(file, content);
|
|
85
|
+
create((0, project_1.ts)((0, project_1.contextsFile)(project)), (0, project_2.contextsRegistry)(project));
|
|
86
|
+
create(path.join(project.root, project_1.CONFIG_FILE), (0, project_2.configFile)(project.config));
|
|
87
|
+
return added;
|
|
88
|
+
}
|
|
89
|
+
/** Shared-kernel files that exist but differ from the generated version (e.g. the project had its own). */
|
|
90
|
+
function customizedSharedFiles(project, writer) {
|
|
91
|
+
const out = [];
|
|
92
|
+
for (const [file, content] of (0, shared_kernel_1.sharedKernelFiles)(project)) {
|
|
93
|
+
const current = writer.read(file);
|
|
94
|
+
if (current !== null && current.trim() !== content.trim())
|
|
95
|
+
out.push(writer.relative(file));
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
function contextNames(input) {
|
|
100
|
+
const names = (0, names_1.toNames)(input, 'context name');
|
|
101
|
+
if (names.kebab === 'shared') {
|
|
102
|
+
throw new errors_1.CliError('"shared" is reserved for the shared kernel; pick another context name.');
|
|
103
|
+
}
|
|
104
|
+
(0, names_1.assertNotReserved)(names, 'context name');
|
|
105
|
+
return names;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Creates the bounded context when it does not exist yet, and mounts it in src/contexts.ts.
|
|
109
|
+
* Returns its paths and whether it was created now.
|
|
110
|
+
*/
|
|
111
|
+
function ensureContext(project, writer, names, outcome) {
|
|
112
|
+
const context = (0, project_1.contextPaths)(project, names);
|
|
113
|
+
if (writer.exists((0, project_1.ts)(context.module)))
|
|
114
|
+
return { context, created: false };
|
|
115
|
+
writer.create((0, project_1.ts)(context.module), (0, context_1.contextModule)(project, context));
|
|
116
|
+
writer.create((0, project_1.ts)(context.index), (0, context_1.contextIndex)(project, context));
|
|
117
|
+
if (!writer.exists(context.glossary))
|
|
118
|
+
writer.create(context.glossary, (0, context_1.contextGlossary)(names));
|
|
119
|
+
const registry = (0, project_1.ts)((0, project_1.contextsFile)(project));
|
|
120
|
+
const { imports, mount } = (0, context_1.registryInsertion)(project, context);
|
|
121
|
+
let registered = false;
|
|
122
|
+
writer.update(registry, (current) => {
|
|
123
|
+
if (current.includes(mount.trim())) {
|
|
124
|
+
registered = true;
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const withImports = (0, markers_1.addImports)(current, imports);
|
|
128
|
+
const mounted = withImports && (0, markers_1.insertAboveMarker)(withImports, markers_1.MARKERS.contexts, mount, 'none');
|
|
129
|
+
if (!mounted)
|
|
130
|
+
return null;
|
|
131
|
+
registered = true;
|
|
132
|
+
return mounted;
|
|
133
|
+
});
|
|
134
|
+
if (!registered) {
|
|
135
|
+
outcome.warnings.push(`Could not register the context in ${writer.relative(registry)} (marker "${markers_1.MARKERS.contexts}" not found). Add it yourself:`, ...imports.map((line) => ` ${line}`), ` ${mount.trim()}`);
|
|
136
|
+
}
|
|
137
|
+
return { context, created: true };
|
|
138
|
+
}
|
|
139
|
+
/** For commands that add to an existing context: the context must exist. */
|
|
140
|
+
function requireContext(project, writer, given) {
|
|
141
|
+
let names;
|
|
142
|
+
if (given) {
|
|
143
|
+
names = contextNames(given);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const contexts = (0, project_1.findContexts)(project);
|
|
147
|
+
if (contexts.length === 1)
|
|
148
|
+
names = contextNames(contexts[0]);
|
|
149
|
+
else if (contexts.length === 0)
|
|
150
|
+
throw new errors_1.CliError('This project has no bounded context yet.', 'Create one first: ddd-express module <aggregate> --context <name> (or: ddd-express context <name>)');
|
|
151
|
+
else
|
|
152
|
+
throw new errors_1.CliError('Which bounded context? This project has several.', `Pass --context with one of: ${contexts.join(', ')}.`);
|
|
153
|
+
}
|
|
154
|
+
const context = (0, project_1.contextPaths)(project, names);
|
|
155
|
+
if (!writer.exists((0, project_1.ts)(context.module))) {
|
|
156
|
+
const known = (0, project_1.findContexts)(project);
|
|
157
|
+
throw new errors_1.CliError(`Bounded context "${names.kebab}" does not exist.`, known.length > 0
|
|
158
|
+
? `Existing contexts: ${known.join(', ')}. To create it: ddd-express context ${names.kebab}`
|
|
159
|
+
: `Create it first: ddd-express context ${names.kebab}`);
|
|
160
|
+
}
|
|
161
|
+
return context;
|
|
162
|
+
}
|
|
163
|
+
/** Adds an export line to a barrel file (creating the barrel when needed). */
|
|
164
|
+
function addToBarrel(writer, barrel, line) {
|
|
165
|
+
writer.update(barrel, (current) => (0, markers_1.ensureLine)(current, line), () => line);
|
|
166
|
+
}
|
|
167
|
+
/** Adds a row to the context glossary, unless the term is already there. */
|
|
168
|
+
function addGlossaryRow(writer, glossary, term, row) {
|
|
169
|
+
writer.update(glossary, (current) => (current.includes(`| **${term}** |`) ? null : (0, markers_1.ensureLine)(current, row)));
|
|
170
|
+
}
|
|
171
|
+
function newOutcome(title, writer) {
|
|
172
|
+
return { title, writer, notes: [], warnings: [] };
|
|
173
|
+
}
|