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,135 @@
|
|
|
1
|
+
import { type Names } from './names';
|
|
2
|
+
export type TestRunner = 'vitest' | 'jest' | 'node';
|
|
3
|
+
export type ModuleSystem = 'esm' | 'commonjs';
|
|
4
|
+
export declare const TEST_RUNNERS: readonly TestRunner[];
|
|
5
|
+
export declare const MODULE_SYSTEMS: readonly ModuleSystem[];
|
|
6
|
+
export declare const CONFIG_FILE = "ddd.config.json";
|
|
7
|
+
/** Stored in ddd.config.json at the project root. */
|
|
8
|
+
export interface DddConfig {
|
|
9
|
+
/** Folder that holds the bounded contexts and `shared/` (relative to the project root). */
|
|
10
|
+
srcDir: string;
|
|
11
|
+
/** Decides the imports of the generated tests. */
|
|
12
|
+
testRunner: TestRunner;
|
|
13
|
+
/** "esm" adds ".js" to relative imports (required by Node ESM); "commonjs" does not. */
|
|
14
|
+
moduleSystem: ModuleSystem;
|
|
15
|
+
}
|
|
16
|
+
export interface Project {
|
|
17
|
+
root: string;
|
|
18
|
+
config: DddConfig;
|
|
19
|
+
/** true when ddd.config.json exists (false = detected from package.json). */
|
|
20
|
+
hasConfigFile: boolean;
|
|
21
|
+
/** Absolute path of srcDir. */
|
|
22
|
+
src: string;
|
|
23
|
+
/** Absolute path of srcDir/shared. */
|
|
24
|
+
shared: string;
|
|
25
|
+
packageJson: PackageJson | null;
|
|
26
|
+
}
|
|
27
|
+
export interface PackageJson {
|
|
28
|
+
name?: string;
|
|
29
|
+
type?: string;
|
|
30
|
+
dependencies?: Record<string, string>;
|
|
31
|
+
devDependencies?: Record<string, string>;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
export declare function readJson<T>(file: string): T | null;
|
|
35
|
+
/** Walks up from `cwd` to the closest folder that has a package.json. */
|
|
36
|
+
export declare function findProjectRoot(cwd: string): string | null;
|
|
37
|
+
export declare function hasDependency(pkg: PackageJson | null, name: string): boolean;
|
|
38
|
+
export declare function detectTestRunner(pkg: PackageJson | null): TestRunner;
|
|
39
|
+
export declare function detectModuleSystem(pkg: PackageJson | null): ModuleSystem;
|
|
40
|
+
/** Loads the project around `cwd`: ddd.config.json if present, otherwise detected defaults. */
|
|
41
|
+
export declare function loadProject(cwd: string, overrides?: Partial<DddConfig>): Project;
|
|
42
|
+
export declare function projectAt(root: string, overrides?: Partial<DddConfig>): Project;
|
|
43
|
+
/**
|
|
44
|
+
* The import specifier from `fromFile` (a .ts path) to `target` (a path without extension).
|
|
45
|
+
* ESM gets ".js" (Node ESM needs it; TypeScript, tsx, Vitest and Jest resolve it to the .ts file).
|
|
46
|
+
* A target ending in "/index" is a barrel: CommonJS imports the folder, ESM the file.
|
|
47
|
+
*/
|
|
48
|
+
export declare function importPath(project: Project, fromFile: string, target: string): string;
|
|
49
|
+
export declare const ts: (fileWithoutExt: string) => string;
|
|
50
|
+
export declare function sharedPaths(project: Project): {
|
|
51
|
+
entity: string;
|
|
52
|
+
aggregateRoot: string;
|
|
53
|
+
valueObject: string;
|
|
54
|
+
domainEvent: string;
|
|
55
|
+
domainError: string;
|
|
56
|
+
guard: string;
|
|
57
|
+
id: string;
|
|
58
|
+
pagination: string;
|
|
59
|
+
object: string;
|
|
60
|
+
eventBus: string;
|
|
61
|
+
inMemoryEventBus: string;
|
|
62
|
+
asyncHandler: string;
|
|
63
|
+
routeParam: string;
|
|
64
|
+
httpError: string;
|
|
65
|
+
errorHandler: string;
|
|
66
|
+
requestBody: string;
|
|
67
|
+
httpPagination: string;
|
|
68
|
+
testServer: string;
|
|
69
|
+
recordingEventBus: string;
|
|
70
|
+
};
|
|
71
|
+
export type SharedPaths = ReturnType<typeof sharedPaths>;
|
|
72
|
+
/** The registry that mounts every bounded context (src/contexts.ts). */
|
|
73
|
+
export declare const contextsFile: (project: Project) => string;
|
|
74
|
+
export declare function contextPaths(project: Project, names: Names): {
|
|
75
|
+
names: Names;
|
|
76
|
+
dir: string;
|
|
77
|
+
module: string;
|
|
78
|
+
index: string;
|
|
79
|
+
glossary: string;
|
|
80
|
+
domain: string;
|
|
81
|
+
events: string;
|
|
82
|
+
eventsIndex: string;
|
|
83
|
+
ports: string;
|
|
84
|
+
portsIndex: string;
|
|
85
|
+
valueObjects: string;
|
|
86
|
+
valueObjectsIndex: string;
|
|
87
|
+
services: string;
|
|
88
|
+
application: string;
|
|
89
|
+
http: string;
|
|
90
|
+
persistence: string;
|
|
91
|
+
};
|
|
92
|
+
export type ContextPaths = ReturnType<typeof contextPaths>;
|
|
93
|
+
export declare function aggregatePaths(context: ContextPaths, names: Names): {
|
|
94
|
+
names: Names;
|
|
95
|
+
plural: Names;
|
|
96
|
+
dir: string;
|
|
97
|
+
file: string;
|
|
98
|
+
index: string;
|
|
99
|
+
spec: string;
|
|
100
|
+
createdEvent: string;
|
|
101
|
+
updatedEvent: string;
|
|
102
|
+
deletedEvent: string;
|
|
103
|
+
port: string;
|
|
104
|
+
dto: string;
|
|
105
|
+
createUseCase: string;
|
|
106
|
+
getUseCase: string;
|
|
107
|
+
listUseCase: string;
|
|
108
|
+
updateUseCase: string;
|
|
109
|
+
deleteUseCase: string;
|
|
110
|
+
useCases: string;
|
|
111
|
+
useCasesSpec: string;
|
|
112
|
+
mapper: string;
|
|
113
|
+
repository: string;
|
|
114
|
+
router: string;
|
|
115
|
+
request: string;
|
|
116
|
+
routerSpec: string;
|
|
117
|
+
};
|
|
118
|
+
export type AggregatePaths = ReturnType<typeof aggregatePaths>;
|
|
119
|
+
/** Files of one command (behaviour) of an aggregate. */
|
|
120
|
+
export declare function commandPaths(context: ContextPaths, aggregate: AggregatePaths, command: Names): {
|
|
121
|
+
useCase: string;
|
|
122
|
+
useCaseSpec: string;
|
|
123
|
+
request: string;
|
|
124
|
+
routerSpec: string;
|
|
125
|
+
};
|
|
126
|
+
export type CommandPaths = ReturnType<typeof commandPaths>;
|
|
127
|
+
/** Bounded contexts = folders in srcDir that have a "<name>.module.ts". */
|
|
128
|
+
export declare function findContexts(project: Project, extra?: string[]): string[];
|
|
129
|
+
/** Aggregates of a context = folders in domain/ that have a "<name>.ts". */
|
|
130
|
+
export declare function findAggregates(context: ContextPaths, exists: (file: string) => boolean): string[];
|
|
131
|
+
/**
|
|
132
|
+
* Picks the bounded context for a command: --context when given; otherwise the only context
|
|
133
|
+
* of the project. With several contexts the user has to choose.
|
|
134
|
+
*/
|
|
135
|
+
export declare function resolveContext(project: Project, given: string | undefined, knownExtra?: string[]): Names;
|
|
@@ -0,0 +1,287 @@
|
|
|
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.contextsFile = exports.ts = exports.CONFIG_FILE = exports.MODULE_SYSTEMS = exports.TEST_RUNNERS = void 0;
|
|
37
|
+
exports.readJson = readJson;
|
|
38
|
+
exports.findProjectRoot = findProjectRoot;
|
|
39
|
+
exports.hasDependency = hasDependency;
|
|
40
|
+
exports.detectTestRunner = detectTestRunner;
|
|
41
|
+
exports.detectModuleSystem = detectModuleSystem;
|
|
42
|
+
exports.loadProject = loadProject;
|
|
43
|
+
exports.projectAt = projectAt;
|
|
44
|
+
exports.importPath = importPath;
|
|
45
|
+
exports.sharedPaths = sharedPaths;
|
|
46
|
+
exports.contextPaths = contextPaths;
|
|
47
|
+
exports.aggregatePaths = aggregatePaths;
|
|
48
|
+
exports.commandPaths = commandPaths;
|
|
49
|
+
exports.findContexts = findContexts;
|
|
50
|
+
exports.findAggregates = findAggregates;
|
|
51
|
+
exports.resolveContext = resolveContext;
|
|
52
|
+
const fs = __importStar(require("node:fs"));
|
|
53
|
+
const path = __importStar(require("node:path"));
|
|
54
|
+
const errors_1 = require("./errors");
|
|
55
|
+
const names_1 = require("./names");
|
|
56
|
+
exports.TEST_RUNNERS = ['vitest', 'jest', 'node'];
|
|
57
|
+
exports.MODULE_SYSTEMS = ['esm', 'commonjs'];
|
|
58
|
+
exports.CONFIG_FILE = 'ddd.config.json';
|
|
59
|
+
function readJson(file) {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Walks up from `cwd` to the closest folder that has a package.json. */
|
|
68
|
+
function findProjectRoot(cwd) {
|
|
69
|
+
let dir = path.resolve(cwd);
|
|
70
|
+
for (;;) {
|
|
71
|
+
if (fs.existsSync(path.join(dir, 'package.json')))
|
|
72
|
+
return dir;
|
|
73
|
+
const parent = path.dirname(dir);
|
|
74
|
+
if (parent === dir)
|
|
75
|
+
return null;
|
|
76
|
+
dir = parent;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function hasDependency(pkg, name) {
|
|
80
|
+
return Boolean(pkg?.dependencies?.[name] ?? pkg?.devDependencies?.[name]);
|
|
81
|
+
}
|
|
82
|
+
function detectTestRunner(pkg) {
|
|
83
|
+
if (hasDependency(pkg, 'vitest'))
|
|
84
|
+
return 'vitest';
|
|
85
|
+
if (hasDependency(pkg, 'jest'))
|
|
86
|
+
return 'jest';
|
|
87
|
+
return 'node';
|
|
88
|
+
}
|
|
89
|
+
function detectModuleSystem(pkg) {
|
|
90
|
+
return pkg?.type === 'module' ? 'esm' : 'commonjs';
|
|
91
|
+
}
|
|
92
|
+
function validateConfig(config, source) {
|
|
93
|
+
if (config.testRunner !== undefined && !exports.TEST_RUNNERS.includes(config.testRunner)) {
|
|
94
|
+
throw new errors_1.CliError(`Invalid testRunner "${config.testRunner}" in ${source}.`, `Use one of: ${exports.TEST_RUNNERS.join(', ')}.`);
|
|
95
|
+
}
|
|
96
|
+
if (config.moduleSystem !== undefined && !exports.MODULE_SYSTEMS.includes(config.moduleSystem)) {
|
|
97
|
+
throw new errors_1.CliError(`Invalid moduleSystem "${config.moduleSystem}" in ${source}.`, `Use one of: ${exports.MODULE_SYSTEMS.join(', ')}.`);
|
|
98
|
+
}
|
|
99
|
+
if (config.srcDir !== undefined && (typeof config.srcDir !== 'string' || config.srcDir.trim() === '')) {
|
|
100
|
+
throw new errors_1.CliError(`Invalid srcDir in ${source}.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Loads the project around `cwd`: ddd.config.json if present, otherwise detected defaults. */
|
|
104
|
+
function loadProject(cwd, overrides = {}) {
|
|
105
|
+
const root = findProjectRoot(cwd);
|
|
106
|
+
if (!root) {
|
|
107
|
+
throw new errors_1.CliError('No package.json found in this folder or any parent.', 'Run the command inside your Express project, or create one with: npx ddd-express new my-app');
|
|
108
|
+
}
|
|
109
|
+
return projectAt(root, overrides);
|
|
110
|
+
}
|
|
111
|
+
function projectAt(root, overrides = {}) {
|
|
112
|
+
const packageJson = readJson(path.join(root, 'package.json'));
|
|
113
|
+
const configFile = path.join(root, exports.CONFIG_FILE);
|
|
114
|
+
const hasConfigFile = fs.existsSync(configFile);
|
|
115
|
+
let fromFile = {};
|
|
116
|
+
if (hasConfigFile) {
|
|
117
|
+
const parsed = readJson(configFile);
|
|
118
|
+
if (!parsed || typeof parsed !== 'object')
|
|
119
|
+
throw new errors_1.CliError(`${exports.CONFIG_FILE} is not valid JSON.`);
|
|
120
|
+
validateConfig(parsed, exports.CONFIG_FILE);
|
|
121
|
+
fromFile = parsed;
|
|
122
|
+
}
|
|
123
|
+
validateConfig(overrides, 'the command line');
|
|
124
|
+
const config = {
|
|
125
|
+
srcDir: overrides.srcDir ?? fromFile.srcDir ?? 'src',
|
|
126
|
+
testRunner: overrides.testRunner ?? fromFile.testRunner ?? detectTestRunner(packageJson),
|
|
127
|
+
moduleSystem: overrides.moduleSystem ?? fromFile.moduleSystem ?? detectModuleSystem(packageJson),
|
|
128
|
+
};
|
|
129
|
+
const src = path.resolve(root, config.srcDir);
|
|
130
|
+
return { root, config, hasConfigFile, src, shared: path.join(src, 'shared'), packageJson };
|
|
131
|
+
}
|
|
132
|
+
// ---------------------------------------------------------------------------------------------
|
|
133
|
+
// Import specifiers
|
|
134
|
+
// ---------------------------------------------------------------------------------------------
|
|
135
|
+
/**
|
|
136
|
+
* The import specifier from `fromFile` (a .ts path) to `target` (a path without extension).
|
|
137
|
+
* ESM gets ".js" (Node ESM needs it; TypeScript, tsx, Vitest and Jest resolve it to the .ts file).
|
|
138
|
+
* A target ending in "/index" is a barrel: CommonJS imports the folder, ESM the file.
|
|
139
|
+
*/
|
|
140
|
+
function importPath(project, fromFile, target) {
|
|
141
|
+
let to = target;
|
|
142
|
+
const isIndex = path.basename(to) === 'index';
|
|
143
|
+
if (isIndex && project.config.moduleSystem === 'commonjs')
|
|
144
|
+
to = path.dirname(to);
|
|
145
|
+
let rel = path.relative(path.dirname(fromFile), to).split(path.sep).join('/');
|
|
146
|
+
if (rel === '')
|
|
147
|
+
rel = './index';
|
|
148
|
+
if (!rel.startsWith('.'))
|
|
149
|
+
rel = `./${rel}`;
|
|
150
|
+
const ext = project.config.moduleSystem === 'esm' ? '.js' : '';
|
|
151
|
+
return `${rel}${ext}`;
|
|
152
|
+
}
|
|
153
|
+
const ts = (fileWithoutExt) => `${fileWithoutExt}.ts`;
|
|
154
|
+
exports.ts = ts;
|
|
155
|
+
// ---------------------------------------------------------------------------------------------
|
|
156
|
+
// Where things live
|
|
157
|
+
// ---------------------------------------------------------------------------------------------
|
|
158
|
+
function sharedPaths(project) {
|
|
159
|
+
const at = (rel) => path.join(project.shared, rel);
|
|
160
|
+
return {
|
|
161
|
+
entity: at('domain/entity'),
|
|
162
|
+
aggregateRoot: at('domain/aggregate-root'),
|
|
163
|
+
valueObject: at('domain/value-object'),
|
|
164
|
+
domainEvent: at('domain/domain-event'),
|
|
165
|
+
domainError: at('domain/domain.error'),
|
|
166
|
+
guard: at('domain/guard'),
|
|
167
|
+
id: at('domain/id'),
|
|
168
|
+
pagination: at('domain/pagination'),
|
|
169
|
+
object: at('domain/object'),
|
|
170
|
+
eventBus: at('application/event-bus'),
|
|
171
|
+
inMemoryEventBus: at('infrastructure/in-memory-event-bus'),
|
|
172
|
+
asyncHandler: at('http/async-handler'),
|
|
173
|
+
routeParam: at('http/route-param'),
|
|
174
|
+
httpError: at('http/http-error'),
|
|
175
|
+
errorHandler: at('http/error-handler'),
|
|
176
|
+
requestBody: at('http/request-body'),
|
|
177
|
+
httpPagination: at('http/pagination'),
|
|
178
|
+
testServer: at('testing/test-server'),
|
|
179
|
+
recordingEventBus: at('testing/recording-event-bus'),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** The registry that mounts every bounded context (src/contexts.ts). */
|
|
183
|
+
const contextsFile = (project) => path.join(project.src, 'contexts');
|
|
184
|
+
exports.contextsFile = contextsFile;
|
|
185
|
+
function contextPaths(project, names) {
|
|
186
|
+
const dir = path.join(project.src, names.kebab);
|
|
187
|
+
return {
|
|
188
|
+
names,
|
|
189
|
+
dir,
|
|
190
|
+
module: path.join(dir, `${names.kebab}.module`),
|
|
191
|
+
index: path.join(dir, 'index'),
|
|
192
|
+
glossary: path.join(dir, 'GLOSSARY.md'),
|
|
193
|
+
domain: path.join(dir, 'domain'),
|
|
194
|
+
events: path.join(dir, 'domain/events'),
|
|
195
|
+
eventsIndex: path.join(dir, 'domain/events/index'),
|
|
196
|
+
ports: path.join(dir, 'domain/ports'),
|
|
197
|
+
portsIndex: path.join(dir, 'domain/ports/index'),
|
|
198
|
+
valueObjects: path.join(dir, 'domain/value-objects'),
|
|
199
|
+
valueObjectsIndex: path.join(dir, 'domain/value-objects/index'),
|
|
200
|
+
services: path.join(dir, 'domain/services'),
|
|
201
|
+
application: path.join(dir, 'application'),
|
|
202
|
+
http: path.join(dir, 'infrastructure/http'),
|
|
203
|
+
persistence: path.join(dir, 'infrastructure/persistence'),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function aggregatePaths(context, names) {
|
|
207
|
+
const plural = (0, names_1.pluralize)(names);
|
|
208
|
+
const dir = path.join(context.domain, names.kebab);
|
|
209
|
+
return {
|
|
210
|
+
names,
|
|
211
|
+
plural,
|
|
212
|
+
dir,
|
|
213
|
+
file: path.join(dir, names.kebab),
|
|
214
|
+
index: path.join(dir, 'index'),
|
|
215
|
+
spec: path.join(dir, `${names.kebab}.spec`),
|
|
216
|
+
createdEvent: path.join(context.events, `${names.kebab}-created.event`),
|
|
217
|
+
updatedEvent: path.join(context.events, `${names.kebab}-updated.event`),
|
|
218
|
+
deletedEvent: path.join(context.events, `${names.kebab}-deleted.event`),
|
|
219
|
+
port: path.join(context.ports, `${names.kebab}.repository`),
|
|
220
|
+
dto: path.join(context.application, `${names.kebab}.dto`),
|
|
221
|
+
createUseCase: path.join(context.application, `create-${names.kebab}.use-case`),
|
|
222
|
+
getUseCase: path.join(context.application, `get-${names.kebab}.use-case`),
|
|
223
|
+
listUseCase: path.join(context.application, `list-${plural.kebab}.use-case`),
|
|
224
|
+
updateUseCase: path.join(context.application, `update-${names.kebab}.use-case`),
|
|
225
|
+
deleteUseCase: path.join(context.application, `delete-${names.kebab}.use-case`),
|
|
226
|
+
useCases: path.join(context.application, `${names.kebab}.use-cases`),
|
|
227
|
+
useCasesSpec: path.join(context.application, `${names.kebab}.use-cases.spec`),
|
|
228
|
+
mapper: path.join(context.persistence, `${names.kebab}.mapper`),
|
|
229
|
+
repository: path.join(context.persistence, `in-memory-${names.kebab}.repository`),
|
|
230
|
+
router: path.join(context.http, `${plural.kebab}.router`),
|
|
231
|
+
request: path.join(context.http, `${names.kebab}.request`),
|
|
232
|
+
routerSpec: path.join(context.http, `${plural.kebab}.router.spec`),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/** Files of one command (behaviour) of an aggregate. */
|
|
236
|
+
function commandPaths(context, aggregate, command) {
|
|
237
|
+
const base = `${command.kebab}-${aggregate.names.kebab}`;
|
|
238
|
+
return {
|
|
239
|
+
useCase: path.join(context.application, `${base}.use-case`),
|
|
240
|
+
useCaseSpec: path.join(context.application, `${base}.use-case.spec`),
|
|
241
|
+
request: path.join(context.http, `${base}.request`),
|
|
242
|
+
routerSpec: path.join(context.http, `${base}.router.spec`),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
// ---------------------------------------------------------------------------------------------
|
|
246
|
+
// Discovery
|
|
247
|
+
// ---------------------------------------------------------------------------------------------
|
|
248
|
+
/** Bounded contexts = folders in srcDir that have a "<name>.module.ts". */
|
|
249
|
+
function findContexts(project, extra = []) {
|
|
250
|
+
const found = new Set(extra);
|
|
251
|
+
if (fs.existsSync(project.src)) {
|
|
252
|
+
for (const entry of fs.readdirSync(project.src, { withFileTypes: true })) {
|
|
253
|
+
if (!entry.isDirectory() || entry.name === 'shared')
|
|
254
|
+
continue;
|
|
255
|
+
if (fs.existsSync(path.join(project.src, entry.name, `${entry.name}.module.ts`)))
|
|
256
|
+
found.add(entry.name);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return [...found].sort();
|
|
260
|
+
}
|
|
261
|
+
const DOMAIN_FOLDERS = new Set(['events', 'ports', 'services', 'value-objects']);
|
|
262
|
+
/** Aggregates of a context = folders in domain/ that have a "<name>.ts". */
|
|
263
|
+
function findAggregates(context, exists) {
|
|
264
|
+
if (!fs.existsSync(context.domain))
|
|
265
|
+
return [];
|
|
266
|
+
return fs
|
|
267
|
+
.readdirSync(context.domain, { withFileTypes: true })
|
|
268
|
+
.filter((entry) => entry.isDirectory() && !DOMAIN_FOLDERS.has(entry.name))
|
|
269
|
+
.map((entry) => entry.name)
|
|
270
|
+
.filter((name) => exists(path.join(context.domain, name, `${name}.ts`)))
|
|
271
|
+
.sort();
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Picks the bounded context for a command: --context when given; otherwise the only context
|
|
275
|
+
* of the project. With several contexts the user has to choose.
|
|
276
|
+
*/
|
|
277
|
+
function resolveContext(project, given, knownExtra = []) {
|
|
278
|
+
if (given)
|
|
279
|
+
return (0, names_1.toNames)(given, 'context name');
|
|
280
|
+
const contexts = findContexts(project, knownExtra);
|
|
281
|
+
if (contexts.length === 1)
|
|
282
|
+
return (0, names_1.toNames)(contexts[0], 'context name');
|
|
283
|
+
if (contexts.length === 0) {
|
|
284
|
+
throw new errors_1.CliError('This project has no bounded context yet.', 'Create one with "ddd-express module <name>" or "ddd-express context <name>", or pass --context <name>.');
|
|
285
|
+
}
|
|
286
|
+
throw new errors_1.CliError('Which bounded context? This project has several.', `Pass --context with one of: ${contexts.join(', ')}.`);
|
|
287
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Io } from './output';
|
|
2
|
+
export type Action = 'create' | 'overwrite' | 'update' | 'skip';
|
|
3
|
+
export interface Change {
|
|
4
|
+
action: Action;
|
|
5
|
+
file: string;
|
|
6
|
+
note?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface WriterOptions {
|
|
9
|
+
root: string;
|
|
10
|
+
dryRun: boolean;
|
|
11
|
+
force: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Writes generated files. Never overwrites an existing file unless --force is given.
|
|
15
|
+
* In --dry-run nothing touches the disk, but later steps still "see" the files created
|
|
16
|
+
* earlier in the same run (an in-memory overlay), so the report is exactly what would happen.
|
|
17
|
+
*/
|
|
18
|
+
export declare class Writer {
|
|
19
|
+
private readonly options;
|
|
20
|
+
readonly changes: Change[];
|
|
21
|
+
private readonly overlay;
|
|
22
|
+
constructor(options: WriterOptions);
|
|
23
|
+
get dryRun(): boolean;
|
|
24
|
+
exists(file: string): boolean;
|
|
25
|
+
read(file: string): string | null;
|
|
26
|
+
/** Creates a new file. An existing file is skipped (or overwritten with --force). */
|
|
27
|
+
create(file: string, content: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Changes an existing file. `transform` returns the new content, or null to leave it alone.
|
|
30
|
+
* When the file does not exist, `whenMissing` (if given) is created instead.
|
|
31
|
+
*/
|
|
32
|
+
update(file: string, transform: (current: string) => string | null, whenMissing?: () => string): void;
|
|
33
|
+
/** One line per file in the report: a file created and then updated in the same run shows as created. */
|
|
34
|
+
private record;
|
|
35
|
+
private write;
|
|
36
|
+
relative(file: string): string;
|
|
37
|
+
report(io: Io): void;
|
|
38
|
+
count(action: Action): number;
|
|
39
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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.Writer = void 0;
|
|
37
|
+
const fs = __importStar(require("node:fs"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const output_1 = require("./output");
|
|
40
|
+
/**
|
|
41
|
+
* Writes generated files. Never overwrites an existing file unless --force is given.
|
|
42
|
+
* In --dry-run nothing touches the disk, but later steps still "see" the files created
|
|
43
|
+
* earlier in the same run (an in-memory overlay), so the report is exactly what would happen.
|
|
44
|
+
*/
|
|
45
|
+
class Writer {
|
|
46
|
+
options;
|
|
47
|
+
changes = [];
|
|
48
|
+
overlay = new Map();
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this.options = options;
|
|
51
|
+
}
|
|
52
|
+
get dryRun() {
|
|
53
|
+
return this.options.dryRun;
|
|
54
|
+
}
|
|
55
|
+
exists(file) {
|
|
56
|
+
return this.overlay.has(file) || fs.existsSync(file);
|
|
57
|
+
}
|
|
58
|
+
read(file) {
|
|
59
|
+
if (this.overlay.has(file))
|
|
60
|
+
return this.overlay.get(file) ?? null;
|
|
61
|
+
return fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : null;
|
|
62
|
+
}
|
|
63
|
+
/** Creates a new file. An existing file is skipped (or overwritten with --force). */
|
|
64
|
+
create(file, content) {
|
|
65
|
+
const normalized = content.endsWith('\n') ? content : `${content}\n`;
|
|
66
|
+
if (this.exists(file)) {
|
|
67
|
+
if (!this.options.force) {
|
|
68
|
+
this.changes.push({ action: 'skip', file, note: 'already exists' });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (this.read(file) === normalized) {
|
|
72
|
+
this.changes.push({ action: 'skip', file, note: 'identical' });
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.write(file, normalized);
|
|
76
|
+
this.record({ action: 'overwrite', file });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.write(file, normalized);
|
|
80
|
+
this.record({ action: 'create', file });
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Changes an existing file. `transform` returns the new content, or null to leave it alone.
|
|
84
|
+
* When the file does not exist, `whenMissing` (if given) is created instead.
|
|
85
|
+
*/
|
|
86
|
+
update(file, transform, whenMissing) {
|
|
87
|
+
const current = this.read(file);
|
|
88
|
+
if (current === null) {
|
|
89
|
+
if (whenMissing)
|
|
90
|
+
this.create(file, whenMissing());
|
|
91
|
+
else
|
|
92
|
+
this.changes.push({ action: 'skip', file, note: 'not found' });
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const next = transform(current);
|
|
96
|
+
if (next === null || next === current)
|
|
97
|
+
return;
|
|
98
|
+
this.write(file, next);
|
|
99
|
+
this.record({ action: 'update', file });
|
|
100
|
+
}
|
|
101
|
+
/** One line per file in the report: a file created and then updated in the same run shows as created. */
|
|
102
|
+
record(change) {
|
|
103
|
+
const previous = this.changes.find((item) => item.file === change.file && item.action !== 'skip');
|
|
104
|
+
if (previous)
|
|
105
|
+
return;
|
|
106
|
+
this.changes.push(change);
|
|
107
|
+
}
|
|
108
|
+
write(file, content) {
|
|
109
|
+
this.overlay.set(file, content);
|
|
110
|
+
if (this.options.dryRun)
|
|
111
|
+
return;
|
|
112
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
113
|
+
fs.writeFileSync(file, content, 'utf8');
|
|
114
|
+
}
|
|
115
|
+
relative(file) {
|
|
116
|
+
return path.relative(this.options.root, file).split(path.sep).join('/');
|
|
117
|
+
}
|
|
118
|
+
report(io) {
|
|
119
|
+
const styles = {
|
|
120
|
+
create: 'green',
|
|
121
|
+
overwrite: 'magenta',
|
|
122
|
+
update: 'cyan',
|
|
123
|
+
skip: 'yellow',
|
|
124
|
+
};
|
|
125
|
+
for (const change of this.changes) {
|
|
126
|
+
const label = (0, output_1.paint)(io, styles[change.action], change.action.padEnd(9));
|
|
127
|
+
const note = change.note ? (0, output_1.paint)(io, 'gray', ` (${change.note})`) : '';
|
|
128
|
+
io.out(` ${label} ${this.relative(change.file)}${note}`);
|
|
129
|
+
}
|
|
130
|
+
if (this.options.dryRun) {
|
|
131
|
+
io.out((0, output_1.paint)(io, 'yellow', '\n --dry-run: nothing was written.'));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
count(action) {
|
|
135
|
+
return this.changes.filter((change) => change.action === action).length;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
exports.Writer = Writer;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Io } from '../core/output';
|
|
2
|
+
import { type CommonOptions, type Outcome } from './common';
|
|
3
|
+
export interface OutputOptions extends CommonOptions {
|
|
4
|
+
out?: string;
|
|
5
|
+
format?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function generateMap(options: OutputOptions, io: Io): Outcome | null;
|
|
8
|
+
export declare function generateOpenApi(options: OutputOptions, io: Io): Outcome | null;
|
|
9
|
+
export interface CheckOptions extends CommonOptions {
|
|
10
|
+
json: boolean;
|
|
11
|
+
fix: boolean;
|
|
12
|
+
strict: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Prints the findings; returns the exit code. */
|
|
15
|
+
export declare function runCheck(options: CheckOptions, io: Io): number;
|
|
16
|
+
export declare function rulesHelp(): string[];
|