@domain-first/project-structure 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/dist/533.js +569 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +19 -584
- package/dist/lint.cjs +723 -0
- package/dist/lint.d.ts +2 -0
- package/dist/lint.js +285 -0
- package/dist/project-lint.d.ts +14 -0
- package/dist/project-lint.spec.d.ts +1 -0
- package/package.json +3 -2
- package/skills/df-ps-implement/SKILL.md +12 -3
- package/skills/df-ps-scaffold/SKILL.md +1 -1
- package/skills/df-ps-scaffold/references/choosing-components.md +2 -2
- package/skills/df-ps-scaffold/references/generators.md +1 -1
- package/skills/df-ps-implement/scripts/check-context-isolation.mjs +0 -204
package/dist/lint.cjs
ADDED
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __webpack_require__ = {};
|
|
4
|
+
(()=>{
|
|
5
|
+
__webpack_require__.n = (module)=>{
|
|
6
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
7
|
+
__webpack_require__.d(getter, {
|
|
8
|
+
a: getter
|
|
9
|
+
});
|
|
10
|
+
return getter;
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
(()=>{
|
|
14
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
15
|
+
var define = (defs, kind)=>{
|
|
16
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
[kind]: defs[key]
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
define(getters, "get");
|
|
22
|
+
define(values, "value");
|
|
23
|
+
};
|
|
24
|
+
})();
|
|
25
|
+
(()=>{
|
|
26
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
const external_node_util_namespaceObject = require("node:util");
|
|
30
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
31
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
32
|
+
const external_zod_namespaceObject = require("zod");
|
|
33
|
+
var external_zod_default = /*#__PURE__*/ __webpack_require__.n(external_zod_namespaceObject);
|
|
34
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
35
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
|
36
|
+
const pendingFiles = new Map();
|
|
37
|
+
new Set();
|
|
38
|
+
const pendingMoves = new Map();
|
|
39
|
+
const pendingDeletions = new Map();
|
|
40
|
+
class FileSystemItem {
|
|
41
|
+
path;
|
|
42
|
+
constructor(path){
|
|
43
|
+
this.path = path;
|
|
44
|
+
}
|
|
45
|
+
subitem = (relativePath)=>new this.constructor(external_node_path_default().resolve(this.path, ...relativePath));
|
|
46
|
+
get exists() {
|
|
47
|
+
return external_node_fs_default().existsSync(this.path);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class file_system_File extends FileSystemItem {
|
|
51
|
+
get exists() {
|
|
52
|
+
return pendingFiles.has(this.path) || pendingMoves.has(this.path) || external_node_fs_default().existsSync(this.path) && !pendingDeletions.has(this.path);
|
|
53
|
+
}
|
|
54
|
+
delete = (stopAt)=>{
|
|
55
|
+
pendingDeletions.set(this.path, stopAt);
|
|
56
|
+
};
|
|
57
|
+
moveTo = (targetPath, stopAt)=>{
|
|
58
|
+
pendingMoves.set(targetPath, {
|
|
59
|
+
sourcePath: this.path,
|
|
60
|
+
stopAt
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
get data() {
|
|
64
|
+
const pendingFile = pendingFiles.get(this.path);
|
|
65
|
+
if (pendingFile) return pendingFile.content;
|
|
66
|
+
if (!this.exists) throw new Error('File does not exist');
|
|
67
|
+
return external_node_fs_default().readFileSync(this.path, 'utf-8');
|
|
68
|
+
}
|
|
69
|
+
set data(content) {
|
|
70
|
+
pendingFiles.set(this.path, {
|
|
71
|
+
content,
|
|
72
|
+
replacesFile: true
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
update = (content)=>{
|
|
76
|
+
pendingFiles.set(this.path, {
|
|
77
|
+
content,
|
|
78
|
+
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
addLine = (newLine, separator = '\n')=>{
|
|
82
|
+
const content = this.exists ? this.data : '';
|
|
83
|
+
pendingFiles.set(this.path, {
|
|
84
|
+
content: [
|
|
85
|
+
content.trim(),
|
|
86
|
+
newLine.trim()
|
|
87
|
+
].filter((x)=>!!x).join(separator),
|
|
88
|
+
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
addLineIfMissing = (newLine, separator = '\n')=>{
|
|
92
|
+
const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
|
|
93
|
+
if (!alreadyAdded) this.addLine(newLine, separator);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
class ProjectRoot {
|
|
97
|
+
path;
|
|
98
|
+
constructor(){
|
|
99
|
+
let dir = process.cwd();
|
|
100
|
+
while(dir !== external_node_path_default().parse(dir).root){
|
|
101
|
+
if (external_node_fs_default().existsSync(external_node_path_default().join(dir, 'package.json'))) {
|
|
102
|
+
this.path = dir;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
dir = external_node_path_default().dirname(dir);
|
|
106
|
+
}
|
|
107
|
+
throw new Error('No package.json was found');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const configFileName = 'domain-first.project-structure.config.json';
|
|
111
|
+
const configSchema = external_zod_default().object({
|
|
112
|
+
rootFolder: external_zod_default().string(),
|
|
113
|
+
domainFirstPackages: external_zod_default().array(external_zod_default()["enum"]([
|
|
114
|
+
'@domain-first/types',
|
|
115
|
+
'@domain-first/errors',
|
|
116
|
+
'@domain-first/handlers',
|
|
117
|
+
'@domain-first/wire',
|
|
118
|
+
'@domain-first/handlers-rest'
|
|
119
|
+
])),
|
|
120
|
+
defaultPersistenceLayerImplementation: external_zod_default().string(),
|
|
121
|
+
withInMemoryDb: external_zod_default().boolean().optional(),
|
|
122
|
+
useReact: external_zod_default().boolean(),
|
|
123
|
+
storybookFramework: external_zod_default().string().optional(),
|
|
124
|
+
testingLibrary: external_zod_default().string().optional()
|
|
125
|
+
});
|
|
126
|
+
class ConfigFile {
|
|
127
|
+
data;
|
|
128
|
+
constructor(){
|
|
129
|
+
const filePath = ConfigFile.path;
|
|
130
|
+
const file = new file_system_File(filePath);
|
|
131
|
+
const configObject = JSON.parse(file.data);
|
|
132
|
+
this.data = configSchema.parse(configObject);
|
|
133
|
+
}
|
|
134
|
+
static get Instance() {
|
|
135
|
+
return new ConfigFile();
|
|
136
|
+
}
|
|
137
|
+
static get path() {
|
|
138
|
+
const { path: root } = new ProjectRoot();
|
|
139
|
+
return external_node_path_default().resolve(root, configFileName);
|
|
140
|
+
}
|
|
141
|
+
static get exists() {
|
|
142
|
+
return new file_system_File(ConfigFile.path).exists;
|
|
143
|
+
}
|
|
144
|
+
static write = (config)=>{
|
|
145
|
+
new file_system_File(ConfigFile.path).data = JSON.stringify(config, null, '\t');
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
const external_node_module_namespaceObject = require("node:module");
|
|
149
|
+
const stripJsonComments = (source)=>source.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, text)=>text ? text : '').replace(/,(\s*[}\]])/g, '$1');
|
|
150
|
+
const resolveExtends = (fromFile, target)=>{
|
|
151
|
+
if (target.startsWith('.') || external_node_path_default().isAbsolute(target)) {
|
|
152
|
+
const resolved = external_node_path_default().resolve(external_node_path_default().dirname(fromFile), target);
|
|
153
|
+
return resolved.endsWith('.json') ? resolved : `${resolved}.json`;
|
|
154
|
+
}
|
|
155
|
+
const require1 = (0, external_node_module_namespaceObject.createRequire)(fromFile);
|
|
156
|
+
for (const candidate of [
|
|
157
|
+
target,
|
|
158
|
+
`${target}/tsconfig.json`
|
|
159
|
+
])try {
|
|
160
|
+
return require1.resolve(candidate);
|
|
161
|
+
} catch {}
|
|
162
|
+
};
|
|
163
|
+
const compilerOption = (configFile, name, seen = new Set())=>{
|
|
164
|
+
if (seen.has(configFile) || !external_node_fs_default().existsSync(configFile)) return null;
|
|
165
|
+
seen.add(configFile);
|
|
166
|
+
let config;
|
|
167
|
+
try {
|
|
168
|
+
config = JSON.parse(stripJsonComments(external_node_fs_default().readFileSync(configFile, 'utf8')));
|
|
169
|
+
} catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if (config.compilerOptions && name in config.compilerOptions) return config.compilerOptions[name];
|
|
173
|
+
const parents = [
|
|
174
|
+
config.extends ?? []
|
|
175
|
+
].flat().reverse();
|
|
176
|
+
for (const parent of parents){
|
|
177
|
+
const parentFile = resolveExtends(configFile, parent);
|
|
178
|
+
const value = parentFile ? compilerOption(parentFile, name, seen) : null;
|
|
179
|
+
if (void 0 !== value) return value;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
class UnknownFormatNaming {
|
|
183
|
+
rawSource;
|
|
184
|
+
camelCase;
|
|
185
|
+
PascalCase;
|
|
186
|
+
constructor(rawSource){
|
|
187
|
+
this.rawSource = rawSource;
|
|
188
|
+
const source = rawSource.trim();
|
|
189
|
+
if (!source.includes('-')) {
|
|
190
|
+
this.camelCase = source.substring(0, 1).toLowerCase() + source.substring(1);
|
|
191
|
+
this.PascalCase = source.substring(0, 1).toUpperCase() + source.substring(1);
|
|
192
|
+
}
|
|
193
|
+
const words = source.split('-').filter((word)=>!!word);
|
|
194
|
+
this.camelCase = words.map((word, index)=>0 === index ? word.toLowerCase() : word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
195
|
+
this.PascalCase = words.map((word)=>word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
196
|
+
}
|
|
197
|
+
get fileName() {
|
|
198
|
+
return this.rawSource;
|
|
199
|
+
}
|
|
200
|
+
get variableName() {
|
|
201
|
+
return this.camelCase;
|
|
202
|
+
}
|
|
203
|
+
get ClassName() {
|
|
204
|
+
return this.PascalCase;
|
|
205
|
+
}
|
|
206
|
+
get withSpaces() {
|
|
207
|
+
return this.fileName.replaceAll('-', ' ');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const wiring_layers = [
|
|
211
|
+
'domain',
|
|
212
|
+
'application',
|
|
213
|
+
'infrastructure',
|
|
214
|
+
'presentation'
|
|
215
|
+
];
|
|
216
|
+
const visibleLayers = {
|
|
217
|
+
domain: [
|
|
218
|
+
'domain'
|
|
219
|
+
],
|
|
220
|
+
application: [
|
|
221
|
+
'domain',
|
|
222
|
+
'application'
|
|
223
|
+
],
|
|
224
|
+
infrastructure: [
|
|
225
|
+
'domain',
|
|
226
|
+
'application',
|
|
227
|
+
'infrastructure'
|
|
228
|
+
],
|
|
229
|
+
presentation: [
|
|
230
|
+
'application',
|
|
231
|
+
'presentation'
|
|
232
|
+
]
|
|
233
|
+
};
|
|
234
|
+
const branchedTypeFiles = {
|
|
235
|
+
repositories: (fileBase)=>[
|
|
236
|
+
'domain',
|
|
237
|
+
'aggregates',
|
|
238
|
+
fileBase.replace(/-repository$/, ''),
|
|
239
|
+
`${fileBase}.ts`
|
|
240
|
+
],
|
|
241
|
+
commands: (fileBase)=>[
|
|
242
|
+
'application',
|
|
243
|
+
'commands',
|
|
244
|
+
'execution',
|
|
245
|
+
`${fileBase}.ts`
|
|
246
|
+
],
|
|
247
|
+
queries: (fileBase)=>[
|
|
248
|
+
'application',
|
|
249
|
+
'queries',
|
|
250
|
+
'execution',
|
|
251
|
+
`${fileBase}.ts`
|
|
252
|
+
],
|
|
253
|
+
'application-ports': (fileBase)=>[
|
|
254
|
+
'application',
|
|
255
|
+
'ports',
|
|
256
|
+
`${fileBase}.ts`
|
|
257
|
+
]
|
|
258
|
+
};
|
|
259
|
+
const layerOf = (file, contextPath)=>{
|
|
260
|
+
const [firstSegment] = external_node_path_default().relative(contextPath, file).split(external_node_path_default().sep);
|
|
261
|
+
return wiring_layers.find((layer)=>layer === firstSegment);
|
|
262
|
+
};
|
|
263
|
+
const namedImports = (source, fromFile)=>{
|
|
264
|
+
const result = new Map();
|
|
265
|
+
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g))for (const name of names.split(',')){
|
|
266
|
+
const cleanName = name.replace(/^\s*type\s+/, '').trim();
|
|
267
|
+
if (cleanName) result.set(cleanName, `${external_node_path_default().resolve(external_node_path_default().dirname(fromFile), importPath)}.ts`);
|
|
268
|
+
}
|
|
269
|
+
return result;
|
|
270
|
+
};
|
|
271
|
+
const isEndpointWire = (wiringRelativePath)=>wiringRelativePath.startsWith([
|
|
272
|
+
'presentation',
|
|
273
|
+
'rest'
|
|
274
|
+
].join(external_node_path_default().sep));
|
|
275
|
+
const wireClassDefinitions = (source)=>[
|
|
276
|
+
...codeOnly(source).matchAll(/(?:export\s+)?const\s+(\w+)\s*=\s*(?:\w+\(\s*)*wireClass\(\s*(\w+)\s*,/g)
|
|
277
|
+
].map(([, wireName, className])=>({
|
|
278
|
+
wireName,
|
|
279
|
+
className
|
|
280
|
+
}));
|
|
281
|
+
const wiredClassNames = (source, wireName, initializerStart, branched)=>{
|
|
282
|
+
const definitions = wireClassDefinitions(source);
|
|
283
|
+
if (!branched) return definitions.filter((x)=>x.wireName === wireName).map((x)=>x.className);
|
|
284
|
+
const openIndex = source.indexOf('(', initializerStart);
|
|
285
|
+
const branches = codeOnly(source).slice(openIndex, closingIndex(source, openIndex));
|
|
286
|
+
const referenced = new Set(branches.match(/\w+/g));
|
|
287
|
+
return definitions.filter((x)=>referenced.has(x.wireName)).map((x)=>x.className);
|
|
288
|
+
};
|
|
289
|
+
const readWiredModule = (wireFile, contextPath)=>{
|
|
290
|
+
const wiringRelativePath = external_node_path_default().relative(external_node_path_default().join(contextPath, 'wiring'), wireFile);
|
|
291
|
+
if (isEndpointWire(wiringRelativePath)) return;
|
|
292
|
+
const source = new file_system_File(wireFile).data;
|
|
293
|
+
const wireName = `wire${new UnknownFormatNaming(external_node_path_default().basename(wireFile, '.ts').replace(/^wire-/, '')).ClassName}`;
|
|
294
|
+
const exportMatch = new RegExp(`export\\s+const\\s+${wireName}\\s*=\\s*`).exec(source);
|
|
295
|
+
if (!exportMatch) return;
|
|
296
|
+
const branched = source.slice(exportMatch.index + exportMatch[0].length).startsWith('envBranchedWire(');
|
|
297
|
+
const imports = namedImports(source, wireFile);
|
|
298
|
+
const implementations = wiredClassNames(source, wireName, exportMatch.index + exportMatch[0].length, branched).flatMap((className)=>{
|
|
299
|
+
const file = imports.get(className);
|
|
300
|
+
return file ? [
|
|
301
|
+
{
|
|
302
|
+
className,
|
|
303
|
+
file
|
|
304
|
+
}
|
|
305
|
+
] : [];
|
|
306
|
+
});
|
|
307
|
+
if (!implementations.length) return;
|
|
308
|
+
const typeName = wireName.slice(4);
|
|
309
|
+
const [wiringFolder] = wiringRelativePath.split(external_node_path_default().sep);
|
|
310
|
+
const typeFileSegments = branchedTypeFiles[wiringFolder]?.(external_node_path_default().basename(wireFile, '.ts').replace(/^wire-/, ''));
|
|
311
|
+
const typeFile = branched ? typeFileSegments && external_node_path_default().join(contextPath, ...typeFileSegments) : implementations[0].file;
|
|
312
|
+
const layer = typeFile && layerOf(typeFile, contextPath);
|
|
313
|
+
const implementationsLayer = layerOf(implementations[0].file, contextPath);
|
|
314
|
+
if (!typeFile || !layer || !implementationsLayer) return;
|
|
315
|
+
return {
|
|
316
|
+
wireName,
|
|
317
|
+
typeName,
|
|
318
|
+
wireFile,
|
|
319
|
+
typeFile,
|
|
320
|
+
layer,
|
|
321
|
+
implementations,
|
|
322
|
+
branched,
|
|
323
|
+
implementationsLayer,
|
|
324
|
+
kind: wiringFolder
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
const findWiredModules = (contextPath)=>{
|
|
328
|
+
const result = [];
|
|
329
|
+
const walk = (folderPath)=>{
|
|
330
|
+
if (!external_node_fs_default().existsSync(folderPath)) return;
|
|
331
|
+
for (const entry of external_node_fs_default().readdirSync(folderPath, {
|
|
332
|
+
withFileTypes: true
|
|
333
|
+
})){
|
|
334
|
+
const entryPath = external_node_path_default().join(folderPath, entry.name);
|
|
335
|
+
if (entry.isDirectory()) walk(entryPath);
|
|
336
|
+
else if (/^wire-.*(?<!\.spec)\.ts$/.test(entry.name)) {
|
|
337
|
+
const wiredModule = readWiredModule(entryPath, contextPath);
|
|
338
|
+
if (wiredModule) result.push(wiredModule);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
walk(external_node_path_default().join(contextPath, 'wiring'));
|
|
343
|
+
return result.sort((a, b)=>a.typeName.localeCompare(b.typeName));
|
|
344
|
+
};
|
|
345
|
+
const isCommandOrQuery = (module)=>'commands' === module.kind || 'queries' === module.kind;
|
|
346
|
+
const isExecution = (module)=>isCommandOrQuery(module) && 'execution' === external_node_path_default().basename(external_node_path_default().dirname(module.typeFile));
|
|
347
|
+
const commandOrQueryRule = (candidate, consumer)=>isExecution(consumer) ? !isCommandOrQuery(candidate) : 'application' !== candidate.layer || isExecution(candidate) && ('commands' === consumer.kind || 'queries' === candidate.kind);
|
|
348
|
+
const layerRules = {
|
|
349
|
+
infrastructure: (candidate)=>'application' !== candidate.layer || 'application-ports' === candidate.kind
|
|
350
|
+
};
|
|
351
|
+
const kindRules = {
|
|
352
|
+
'application-services': (candidate)=>'application' !== candidate.layer || 'application-ports' === candidate.kind,
|
|
353
|
+
'use-cases': (candidate)=>'use-cases' !== candidate.kind,
|
|
354
|
+
repositories: (candidate)=>'repositories' !== candidate.kind,
|
|
355
|
+
commands: commandOrQueryRule,
|
|
356
|
+
queries: commandOrQueryRule
|
|
357
|
+
};
|
|
358
|
+
const canDependOn = (consumer, candidate)=>visibleLayers[consumer.implementationsLayer].includes(candidate.layer) && (layerRules[consumer.implementationsLayer]?.(candidate) ?? true) && (kindRules[consumer.kind]?.(candidate, consumer) ?? true);
|
|
359
|
+
const codeOnly = (source)=>{
|
|
360
|
+
let result = '';
|
|
361
|
+
let index = 0;
|
|
362
|
+
const blank = (text)=>text.replace(/[^\n]/g, ' ');
|
|
363
|
+
while(index < source.length){
|
|
364
|
+
const pair = source.slice(index, index + 2);
|
|
365
|
+
const char = source[index];
|
|
366
|
+
let end = index + 1;
|
|
367
|
+
if ('//' === pair) {
|
|
368
|
+
const lineEnd = source.indexOf('\n', index);
|
|
369
|
+
end = -1 === lineEnd ? source.length : lineEnd;
|
|
370
|
+
result += blank(source.slice(index, end));
|
|
371
|
+
} else if ('/*' === pair) {
|
|
372
|
+
const commentEnd = source.indexOf('*/', index + 2);
|
|
373
|
+
end = -1 === commentEnd ? source.length : commentEnd + 2;
|
|
374
|
+
result += blank(source.slice(index, end));
|
|
375
|
+
} else if ('=>' === pair) {
|
|
376
|
+
end = index + 2;
|
|
377
|
+
result += ' ';
|
|
378
|
+
} else if ("'" === char || '"' === char || '`' === char) {
|
|
379
|
+
while(end < source.length && source[end] !== char)end += '\\' === source[end] ? 2 : 1;
|
|
380
|
+
end = Math.min(end + 1, source.length);
|
|
381
|
+
result += char + blank(source.slice(index + 1, end - 1));
|
|
382
|
+
result += end - 1 > index ? source[end - 1] : '';
|
|
383
|
+
} else result += char;
|
|
384
|
+
index = end;
|
|
385
|
+
}
|
|
386
|
+
return result;
|
|
387
|
+
};
|
|
388
|
+
const brackets = {
|
|
389
|
+
'(': ')',
|
|
390
|
+
'[': ']',
|
|
391
|
+
'{': '}',
|
|
392
|
+
'<': '>'
|
|
393
|
+
};
|
|
394
|
+
const closingIndex = (source, openIndex)=>{
|
|
395
|
+
const code = codeOnly(source);
|
|
396
|
+
let depth = 0;
|
|
397
|
+
for(let index = openIndex; index < code.length; index++){
|
|
398
|
+
const char = code[index];
|
|
399
|
+
if (char === code[openIndex]) depth++;
|
|
400
|
+
else if (char === brackets[code[openIndex]]) {
|
|
401
|
+
depth--;
|
|
402
|
+
if (0 === depth) return index;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return -1;
|
|
406
|
+
};
|
|
407
|
+
const splitTopLevel = (content)=>{
|
|
408
|
+
const code = codeOnly(content);
|
|
409
|
+
const items = [];
|
|
410
|
+
let depth = 0;
|
|
411
|
+
let itemStart = 0;
|
|
412
|
+
for(let index = 0; index <= code.length; index++){
|
|
413
|
+
const char = code[index];
|
|
414
|
+
if (void 0 !== char && char in brackets) depth++;
|
|
415
|
+
else if (Object.values(brackets).includes(char)) depth--;
|
|
416
|
+
if (index === code.length || ',' === char && 0 === depth) {
|
|
417
|
+
const item = content.slice(itemStart, index);
|
|
418
|
+
const itemCode = code.slice(itemStart, index);
|
|
419
|
+
const first = itemCode.search(/\S/);
|
|
420
|
+
const last = itemCode.search(/\S\s*$/);
|
|
421
|
+
if (-1 !== first) items.push(item.slice(first, last + 1));
|
|
422
|
+
itemStart = index + 1;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return items;
|
|
426
|
+
};
|
|
427
|
+
const wireListRange = (source, className)=>{
|
|
428
|
+
const match = new RegExp(`wireClass\\(\\s*${className}\\s*,\\s*\\[`).exec(codeOnly(source));
|
|
429
|
+
if (!match) throw new Error(`wireClass(${className}, [...]) not found`);
|
|
430
|
+
const openIndex = match.index + match[0].length - 1;
|
|
431
|
+
return {
|
|
432
|
+
openIndex,
|
|
433
|
+
closeIndex: closingIndex(source, openIndex)
|
|
434
|
+
};
|
|
435
|
+
};
|
|
436
|
+
const wiredDependencyNames = (source, className)=>{
|
|
437
|
+
const { openIndex, closeIndex } = wireListRange(source, className);
|
|
438
|
+
return splitTopLevel(source.slice(openIndex + 1, closeIndex));
|
|
439
|
+
};
|
|
440
|
+
const readAliases = (projectRoot)=>{
|
|
441
|
+
const tsconfig = external_node_path_default().join(projectRoot, 'tsconfig.json');
|
|
442
|
+
const paths = compilerOption(tsconfig, 'paths');
|
|
443
|
+
if (!paths || 'object' != typeof paths) return [];
|
|
444
|
+
const baseUrl = compilerOption(tsconfig, 'baseUrl');
|
|
445
|
+
const base = external_node_path_default().resolve(projectRoot, 'string' == typeof baseUrl ? baseUrl : '.');
|
|
446
|
+
return Object.entries(paths).filter(([, targets])=>targets.length).map(([key, [target]])=>{
|
|
447
|
+
const wildcard = key.endsWith('*');
|
|
448
|
+
return {
|
|
449
|
+
prefix: wildcard ? key.slice(0, -1) : key,
|
|
450
|
+
wildcard,
|
|
451
|
+
target: external_node_path_default().resolve(base, wildcard ? target.replace(/\*$/, '') : target)
|
|
452
|
+
};
|
|
453
|
+
}).sort((a, b)=>b.prefix.length - a.prefix.length);
|
|
454
|
+
};
|
|
455
|
+
const resolveSpecifier = (specifier, fromFile, aliases)=>{
|
|
456
|
+
if (specifier.startsWith('.')) return external_node_path_default().resolve(external_node_path_default().dirname(fromFile), specifier);
|
|
457
|
+
for (const { prefix, wildcard, target } of aliases)if (wildcard ? specifier.startsWith(prefix) : specifier === prefix) return external_node_path_default().join(target, wildcard ? specifier.slice(prefix.length) : '');
|
|
458
|
+
};
|
|
459
|
+
const inside = (folder, file)=>{
|
|
460
|
+
const relativePath = external_node_path_default().relative(folder, file);
|
|
461
|
+
return !relativePath || relativePath.startsWith('..') || external_node_path_default().isAbsolute(relativePath) ? void 0 : relativePath.split(external_node_path_default().sep);
|
|
462
|
+
};
|
|
463
|
+
const sourceFiles = (folder)=>{
|
|
464
|
+
if (!external_node_fs_default().existsSync(folder)) return [];
|
|
465
|
+
return external_node_fs_default().readdirSync(folder, {
|
|
466
|
+
withFileTypes: true
|
|
467
|
+
}).flatMap((entry)=>{
|
|
468
|
+
const entryPath = external_node_path_default().join(folder, entry.name);
|
|
469
|
+
if (entry.isDirectory()) return 'node_modules' === entry.name ? [] : sourceFiles(entryPath);
|
|
470
|
+
return /\.(c|m)?tsx?$/.test(entry.name) && !entry.name.endsWith('.d.ts') ? [
|
|
471
|
+
entryPath
|
|
472
|
+
] : [];
|
|
473
|
+
});
|
|
474
|
+
};
|
|
475
|
+
const importPattern = /\b(?:(import|export)\s+(type\s+)?(?:(?!\b(?:import|export)\b)[\w*{}\s,$])*?\s*from\s*|import\s*\(\s*|require\s*\(\s*|import\s*)(['"])([^'"\n]+)\3/g;
|
|
476
|
+
const lineAt = (source, index)=>source.slice(0, index).split('\n').length;
|
|
477
|
+
const importsOf = (source, file, aliases)=>[
|
|
478
|
+
...source.matchAll(importPattern)
|
|
479
|
+
].map((match)=>({
|
|
480
|
+
typeOnly: 'import' === match[1] && Boolean(match[2]),
|
|
481
|
+
target: resolveSpecifier(match[4], file, aliases),
|
|
482
|
+
line: lineAt(source, match.index),
|
|
483
|
+
statement: match[0].replace(/\s+/g, ' ')
|
|
484
|
+
}));
|
|
485
|
+
const isSpec = (file)=>/\.(spec|test)\.(c|m)?tsx?$/.test(file);
|
|
486
|
+
const layerImportRules = {
|
|
487
|
+
domain: {
|
|
488
|
+
allows: ([layer])=>'domain' === layer,
|
|
489
|
+
rule: 'domain imports outside the domain layer:'
|
|
490
|
+
},
|
|
491
|
+
application: {
|
|
492
|
+
allows: ([layer])=>'domain' === layer || 'application' === layer,
|
|
493
|
+
rule: 'application imports outside domain and application:'
|
|
494
|
+
},
|
|
495
|
+
infrastructure: {
|
|
496
|
+
allows: ([layer, part, mode])=>'domain' === layer || 'infrastructure' === layer || 'application' === layer && ('ports' === part || ('commands' === part || 'queries' === part) && 'execution' === mode),
|
|
497
|
+
rule: 'infrastructure imports outside domain, infrastructure, application ports and execution contracts:'
|
|
498
|
+
},
|
|
499
|
+
presentation: {
|
|
500
|
+
allows: ([layer])=>'infrastructure' !== layer,
|
|
501
|
+
rule: 'presentation imports infrastructure (only wiring knows concrete classes):'
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
const isKindFile = (segments, kind)=>Boolean(segments && [
|
|
505
|
+
'application',
|
|
506
|
+
'infrastructure',
|
|
507
|
+
'wiring'
|
|
508
|
+
].includes(segments[0]) && segments[1] === kind);
|
|
509
|
+
const importViolations = (projectRoot, rootFolder)=>{
|
|
510
|
+
const root = external_node_path_default().resolve(projectRoot, rootFolder);
|
|
511
|
+
const contextsFolder = external_node_path_default().join(root, 'bounded-contexts');
|
|
512
|
+
const sharedFolder = external_node_path_default().join(root, 'shared');
|
|
513
|
+
const reactFolder = external_node_path_default().join(root, 'presentation', 'react');
|
|
514
|
+
const uiKitFolder = external_node_path_default().join(reactFolder, 'ui-kit');
|
|
515
|
+
const widgetsFolder = external_node_path_default().join(reactFolder, 'widgets');
|
|
516
|
+
const hooksFolder = external_node_path_default().join(reactFolder, 'hooks');
|
|
517
|
+
const inMemoryDbFile = external_node_path_default().join(sharedFolder, 'infrastructure', 'services', 'in-memory-db-infrastructure-service.ts');
|
|
518
|
+
const aliases = readAliases(projectRoot);
|
|
519
|
+
const violations = [];
|
|
520
|
+
const locate = (file)=>{
|
|
521
|
+
const inContext = inside(contextsFolder, file);
|
|
522
|
+
if (inContext && inContext.length > 1) {
|
|
523
|
+
const [owner, ...segments] = inContext;
|
|
524
|
+
return {
|
|
525
|
+
owner,
|
|
526
|
+
segments
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
const inShared = inside(sharedFolder, file);
|
|
530
|
+
return inShared && {
|
|
531
|
+
owner: 'shared',
|
|
532
|
+
segments: inShared
|
|
533
|
+
};
|
|
534
|
+
};
|
|
535
|
+
const isDomainModel = ([layer, part], target)=>'domain' === layer && [
|
|
536
|
+
'aggregates',
|
|
537
|
+
'entities',
|
|
538
|
+
'value-objects'
|
|
539
|
+
].includes(part) && !/-repository(\.tsx?)?$/.test(external_node_path_default().basename(target));
|
|
540
|
+
const widgetFolders = new Map();
|
|
541
|
+
const isWidgetFolder = (folder)=>{
|
|
542
|
+
if (!widgetFolders.has(folder)) widgetFolders.set(folder, external_node_fs_default().existsSync(folder) && external_node_fs_default().statSync(folder).isDirectory() && external_node_fs_default().readdirSync(folder).some((name)=>name.endsWith('-widget.tsx')));
|
|
543
|
+
return widgetFolders.get(folder);
|
|
544
|
+
};
|
|
545
|
+
const widgetOf = (file)=>{
|
|
546
|
+
for(let folder = file; inside(widgetsFolder, folder); folder = external_node_path_default().dirname(folder))if (isWidgetFolder(folder)) return folder;
|
|
547
|
+
};
|
|
548
|
+
const files = [
|
|
549
|
+
contextsFolder,
|
|
550
|
+
sharedFolder,
|
|
551
|
+
reactFolder
|
|
552
|
+
].flatMap(sourceFiles);
|
|
553
|
+
for (const file of files){
|
|
554
|
+
const source = external_node_fs_default().readFileSync(file, 'utf8');
|
|
555
|
+
const from = locate(file);
|
|
556
|
+
const ownContext = inside(contextsFolder, file)?.[0];
|
|
557
|
+
const report = (rule, imported)=>violations.push({
|
|
558
|
+
rule,
|
|
559
|
+
file,
|
|
560
|
+
line: imported.line,
|
|
561
|
+
text: imported.statement
|
|
562
|
+
});
|
|
563
|
+
for (const imported of importsOf(source, file, aliases)){
|
|
564
|
+
const target = imported.target;
|
|
565
|
+
if (!target) continue;
|
|
566
|
+
const to = locate(target);
|
|
567
|
+
const targetContext = inside(contextsFolder, target)?.[0];
|
|
568
|
+
if (ownContext) {
|
|
569
|
+
if (targetContext && targetContext !== ownContext) report(`[${ownContext}] imports another context:`, imported);
|
|
570
|
+
} else if (inside(sharedFolder, file)) {
|
|
571
|
+
const allowed = !to || 'shared' === to.owner || 'domain' === to.segments[0] && 'errors' === to.segments[1] || file === inMemoryDbFile && isDomainModel(to.segments, target);
|
|
572
|
+
if (!allowed) report('[shared] imports a bounded context outside its domain/errors (and domain models for the in-memory DB):', imported);
|
|
573
|
+
} else if (inside(uiKitFolder, file) || inside(hooksFolder, file)) {
|
|
574
|
+
const [area] = inside(reactFolder, target) ?? [];
|
|
575
|
+
const forbidden = inside(uiKitFolder, file) ? [
|
|
576
|
+
'widgets',
|
|
577
|
+
'pages'
|
|
578
|
+
] : [
|
|
579
|
+
'ui-kit',
|
|
580
|
+
'widgets',
|
|
581
|
+
'pages'
|
|
582
|
+
];
|
|
583
|
+
if (targetContext || forbidden.includes(area)) report(inside(uiKitFolder, file) ? '[presentation/react/ui-kit] imports a bounded context, widget or page:' : '[presentation/react/hooks] a shared hook imports a bounded context, UI-kit, widget or page:', imported);
|
|
584
|
+
} else if (targetContext && !(imported.typeOnly && to?.segments[0] === 'presentation' && 'rest' === to.segments[1])) report('[presentation/react] imports a context other than `import type` from presentation/rest:', imported);
|
|
585
|
+
const ownWidget = widgetOf(file);
|
|
586
|
+
const targetWidget = widgetOf(target);
|
|
587
|
+
if (ownWidget && targetWidget && targetWidget !== ownWidget) report('[presentation/react/widgets] a widget imports another widget:', imported);
|
|
588
|
+
if (!from || !to) continue;
|
|
589
|
+
if (to.owner === from.owner && isKindFile(from.segments, 'queries') && isKindFile(to.segments, 'commands')) report(`[${from.owner}] a query imports a command (queries only read):`, imported);
|
|
590
|
+
const layerRule = layerImportRules[from.segments[0]];
|
|
591
|
+
if (layerRule && !isSpec(file) && !layerRule.allows(to.segments)) report(`[${from.owner}] ${layerRule.rule}`, imported);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return violations;
|
|
595
|
+
};
|
|
596
|
+
const wiringViolations = (projectRoot, rootFolder)=>{
|
|
597
|
+
const root = external_node_path_default().resolve(projectRoot, rootFolder);
|
|
598
|
+
const sharedFolder = external_node_path_default().join(root, 'shared');
|
|
599
|
+
const contextsFolder = external_node_path_default().join(root, 'bounded-contexts');
|
|
600
|
+
const sharedModules = findWiredModules(sharedFolder);
|
|
601
|
+
const owners = [
|
|
602
|
+
{
|
|
603
|
+
name: 'shared',
|
|
604
|
+
modules: sharedModules
|
|
605
|
+
},
|
|
606
|
+
...external_node_fs_default().existsSync(contextsFolder) ? external_node_fs_default().readdirSync(contextsFolder, {
|
|
607
|
+
withFileTypes: true
|
|
608
|
+
}).filter((entry)=>entry.isDirectory()).map((entry)=>({
|
|
609
|
+
name: entry.name,
|
|
610
|
+
modules: findWiredModules(external_node_path_default().join(contextsFolder, entry.name))
|
|
611
|
+
})) : []
|
|
612
|
+
];
|
|
613
|
+
const violations = [];
|
|
614
|
+
for (const { name, modules } of owners){
|
|
615
|
+
const visible = new Map([
|
|
616
|
+
...sharedModules,
|
|
617
|
+
...modules
|
|
618
|
+
].map((x)=>[
|
|
619
|
+
x.wireName,
|
|
620
|
+
x
|
|
621
|
+
]));
|
|
622
|
+
for (const consumer of modules){
|
|
623
|
+
const source = external_node_fs_default().readFileSync(consumer.wireFile, 'utf8');
|
|
624
|
+
for (const implementation of consumer.implementations){
|
|
625
|
+
let dependencies;
|
|
626
|
+
try {
|
|
627
|
+
dependencies = wiredDependencyNames(source, implementation.className);
|
|
628
|
+
} catch {
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
const listStart = source.search(new RegExp(`wireClass\\(\\s*${implementation.className}\\b`));
|
|
632
|
+
for (const dependency of dependencies){
|
|
633
|
+
const candidate = visible.get(dependency);
|
|
634
|
+
if (!candidate || canDependOn(consumer, candidate)) continue;
|
|
635
|
+
const index = source.indexOf(dependency, listStart);
|
|
636
|
+
violations.push({
|
|
637
|
+
rule: `[${name}] wiring: a dependency the CLI never offers (layer or kind rules):`,
|
|
638
|
+
file: consumer.wireFile,
|
|
639
|
+
line: lineAt(source, index),
|
|
640
|
+
text: `${implementation.className} ← ${candidate.typeName} (${candidate.kind})`
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return violations;
|
|
647
|
+
};
|
|
648
|
+
const clockViolations = (projectRoot, rootFolder)=>{
|
|
649
|
+
const root = external_node_path_default().resolve(projectRoot, rootFolder);
|
|
650
|
+
const folders = [
|
|
651
|
+
external_node_path_default().join(root, 'shared'),
|
|
652
|
+
...external_node_fs_default().existsSync(external_node_path_default().join(root, 'bounded-contexts')) ? external_node_fs_default().readdirSync(external_node_path_default().join(root, 'bounded-contexts')).map((name)=>external_node_path_default().join(root, 'bounded-contexts', name)) : []
|
|
653
|
+
].flatMap((folder)=>[
|
|
654
|
+
external_node_path_default().join(folder, 'domain'),
|
|
655
|
+
external_node_path_default().join(folder, 'application')
|
|
656
|
+
]);
|
|
657
|
+
return folders.flatMap(sourceFiles).filter((file)=>!isSpec(file)).flatMap((file)=>{
|
|
658
|
+
const source = external_node_fs_default().readFileSync(file, 'utf8');
|
|
659
|
+
return [
|
|
660
|
+
...source.matchAll(/\bnew\s+Date\s*\(\s*\)|\bDate\.now\s*\(/g)
|
|
661
|
+
].map((match)=>({
|
|
662
|
+
rule: 'domain or application reads the current time directly (use the clock port):',
|
|
663
|
+
file,
|
|
664
|
+
line: lineAt(source, match.index),
|
|
665
|
+
text: match[0]
|
|
666
|
+
}));
|
|
667
|
+
});
|
|
668
|
+
};
|
|
669
|
+
const lintProject = (projectRoot, rootFolder)=>[
|
|
670
|
+
...importViolations(projectRoot, rootFolder),
|
|
671
|
+
...wiringViolations(projectRoot, rootFolder),
|
|
672
|
+
...clockViolations(projectRoot, rootFolder)
|
|
673
|
+
];
|
|
674
|
+
const formatViolations = (violations, projectRoot)=>{
|
|
675
|
+
const groups = new Map();
|
|
676
|
+
for (const { rule, file, line, text } of violations)groups.set(rule, [
|
|
677
|
+
...groups.get(rule) ?? [],
|
|
678
|
+
` ${external_node_path_default().relative(projectRoot, file)}:${line}: ${text}`
|
|
679
|
+
]);
|
|
680
|
+
return [
|
|
681
|
+
...groups
|
|
682
|
+
].map(([rule, lines])=>[
|
|
683
|
+
rule,
|
|
684
|
+
...lines
|
|
685
|
+
].join('\n')).join('\n\n');
|
|
686
|
+
};
|
|
687
|
+
const help = `Usage: df-lint [rootFolder]
|
|
688
|
+
|
|
689
|
+
Reports what breaks the Domain-First rules in the project:
|
|
690
|
+
bounded context isolation, layers, wiring dependencies, React modules,
|
|
691
|
+
queries using commands, and reading the time without the clock port.
|
|
692
|
+
rootFolder defaults to the one in domain-first.project-structure.config.json.
|
|
693
|
+
Exits with 1 when something is found.
|
|
694
|
+
|
|
695
|
+
Options:
|
|
696
|
+
-h, --help show this help`;
|
|
697
|
+
const { values, positionals } = (0, external_node_util_namespaceObject.parseArgs)({
|
|
698
|
+
allowPositionals: true,
|
|
699
|
+
options: {
|
|
700
|
+
help: {
|
|
701
|
+
type: 'boolean',
|
|
702
|
+
short: 'h',
|
|
703
|
+
default: false
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
if (values.help) {
|
|
708
|
+
console.log(help);
|
|
709
|
+
process.exit(0);
|
|
710
|
+
}
|
|
711
|
+
const lint_projectRoot = new ProjectRoot().path;
|
|
712
|
+
const lint_rootFolder = positionals[0] ?? (ConfigFile.exists ? ConfigFile.Instance.data.rootFolder : 'src');
|
|
713
|
+
const lint_violations = lintProject(lint_projectRoot, lint_rootFolder);
|
|
714
|
+
if (lint_violations.length) {
|
|
715
|
+
console.log(formatViolations(lint_violations, lint_projectRoot));
|
|
716
|
+
console.log(`\n${lint_violations.length} problem(s) found.`);
|
|
717
|
+
process.exit(1);
|
|
718
|
+
}
|
|
719
|
+
console.log('OK: no Domain-First rule violations.');
|
|
720
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
721
|
+
Object.defineProperty(exports, '__esModule', {
|
|
722
|
+
value: true
|
|
723
|
+
});
|