@domain-first/project-structure 4.0.0 → 4.2.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 +27 -2
- package/dist/201.js +699 -0
- package/dist/config-file.d.ts +71 -0
- package/dist/dependency-rules.d.ts +82 -0
- package/dist/dependency-rules.spec.d.ts +1 -0
- package/dist/index.cjs +122 -17
- package/dist/index.js +19 -584
- package/dist/lint.cjs +864 -0
- package/dist/lint.d.ts +2 -0
- package/dist/lint.js +296 -0
- package/dist/project-lint.d.ts +27 -0
- package/dist/project-lint.spec.d.ts +1 -0
- package/dist/wiring.d.ts +10 -5
- package/package.json +3 -2
- package/skills/df-ps-implement/SKILL.md +12 -3
- package/skills/df-ps-scaffold/SKILL.md +3 -3
- package/skills/df-ps-scaffold/references/choosing-components.md +3 -3
- package/skills/df-ps-scaffold/references/generators.md +1 -1
- package/skills/df-ps-implement/scripts/check-context-isolation.mjs +0 -204
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Reports imports that cross bounded-context boundaries outside the allowed places.
|
|
4
|
-
*
|
|
5
|
-
* Usage (from the project root):
|
|
6
|
-
* node check-context-isolation.mjs [rootFolder]
|
|
7
|
-
* rootFolder defaults to `rootFolder` in domain-first.project-structure.config.json.
|
|
8
|
-
*
|
|
9
|
-
* Bounded contexts never import each other (no exceptions).
|
|
10
|
-
* Allowed: shared/** may import any context's domain/errors; the shared in-memory DB
|
|
11
|
-
* (shared/infrastructure/services/in-memory-db-infrastructure-service.ts) may also import any
|
|
12
|
-
* context's domain models: domain/aggregates/** (barrels included, `*-repository` files not),
|
|
13
|
-
* domain/entities/**, domain/value-objects/**; presentation/react/** may only
|
|
14
|
-
* `import type` from a context's presentation/rest/; presentation/react/ui-kit/** imports
|
|
15
|
-
* nothing from bounded contexts, widgets or pages.
|
|
16
|
-
* Widgets never import each other: a file of one widget (the nearest folder under widgets/
|
|
17
|
-
* that has a `*-widget.tsx`) must not import anything of another widget.
|
|
18
|
-
* Everything else that imports another bounded context is reported.
|
|
19
|
-
*
|
|
20
|
-
* Checked: `import … from`, `export … from`, side-effect `import '…'`, `import('…')`,
|
|
21
|
-
* `require('…')`; specifiers are resolved as relative paths or tsconfig `paths` aliases.
|
|
22
|
-
* Exit code 1 when violations are found.
|
|
23
|
-
*/
|
|
24
|
-
import fs from 'node:fs';
|
|
25
|
-
import path from 'node:path';
|
|
26
|
-
import { createRequire } from 'node:module';
|
|
27
|
-
|
|
28
|
-
const cwd = process.cwd();
|
|
29
|
-
|
|
30
|
-
const readRootFolder = () => {
|
|
31
|
-
const configPath = path.join(cwd, 'domain-first.project-structure.config.json');
|
|
32
|
-
if (!fs.existsSync(configPath)) return 'src';
|
|
33
|
-
return JSON.parse(fs.readFileSync(configPath, 'utf8')).rootFolder ?? 'src';
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const root = path.resolve(cwd, process.argv[2] ?? readRootFolder());
|
|
37
|
-
const contextsDir = path.join(root, 'bounded-contexts');
|
|
38
|
-
const sharedDir = path.join(root, 'shared');
|
|
39
|
-
const reactDir = path.join(root, 'presentation', 'react');
|
|
40
|
-
const uiKitDir = path.join(reactDir, 'ui-kit');
|
|
41
|
-
const widgetsDir = path.join(reactDir, 'widgets');
|
|
42
|
-
const inMemoryDbFile = path.join(sharedDir, 'infrastructure', 'services', 'in-memory-db-infrastructure-service.ts');
|
|
43
|
-
|
|
44
|
-
// Domain models of a context: aggregate roots, entities and value objects, not repository contracts.
|
|
45
|
-
const isDomainModel = (target, layer, part) =>
|
|
46
|
-
layer === 'domain' &&
|
|
47
|
-
['aggregates', 'entities', 'value-objects'].includes(part) &&
|
|
48
|
-
!/-repository(\.tsx?)?$/.test(path.basename(target));
|
|
49
|
-
|
|
50
|
-
// tsconfig `paths` → [{ prefix, wildcard, targets }]
|
|
51
|
-
const readTsconfig = (file) => {
|
|
52
|
-
try {
|
|
53
|
-
const ts = createRequire(path.join(cwd, 'package.json'))('typescript');
|
|
54
|
-
const { config, error } = ts.readConfigFile(file, ts.sys.readFile);
|
|
55
|
-
if (!error) return config;
|
|
56
|
-
} catch {}
|
|
57
|
-
const text = fs
|
|
58
|
-
.readFileSync(file, 'utf8')
|
|
59
|
-
.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, str) => str ?? '')
|
|
60
|
-
.replace(/,(\s*[}\]])/g, '$1');
|
|
61
|
-
return JSON.parse(text);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const readAliases = () => {
|
|
65
|
-
const file = path.join(cwd, 'tsconfig.json');
|
|
66
|
-
if (!fs.existsSync(file)) return [];
|
|
67
|
-
let options;
|
|
68
|
-
try {
|
|
69
|
-
options = readTsconfig(file).compilerOptions ?? {};
|
|
70
|
-
} catch (e) {
|
|
71
|
-
console.warn(`WARNING: cannot parse tsconfig.json (${e.message}); aliases are not checked\n`);
|
|
72
|
-
return [];
|
|
73
|
-
}
|
|
74
|
-
const base = path.resolve(cwd, options.baseUrl ?? '.');
|
|
75
|
-
return Object.entries(options.paths ?? {})
|
|
76
|
-
.map(([key, targets]) => {
|
|
77
|
-
const wildcard = key.endsWith('*');
|
|
78
|
-
return {
|
|
79
|
-
prefix: wildcard ? key.slice(0, -1) : key,
|
|
80
|
-
wildcard,
|
|
81
|
-
targets: targets.map((t) => path.resolve(base, wildcard ? t.replace(/\*$/, '') : t)),
|
|
82
|
-
};
|
|
83
|
-
})
|
|
84
|
-
.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const aliases = readAliases();
|
|
88
|
-
|
|
89
|
-
// Absolute path a specifier points to, or null for a package.
|
|
90
|
-
const resolveSpecifier = (spec, fromFile) => {
|
|
91
|
-
if (spec.startsWith('.')) return path.resolve(path.dirname(fromFile), spec);
|
|
92
|
-
for (const { prefix, wildcard, targets } of aliases) {
|
|
93
|
-
if (wildcard ? spec.startsWith(prefix) : spec === prefix) {
|
|
94
|
-
return path.join(targets[0], wildcard ? spec.slice(prefix.length) : '');
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return null;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const inside = (dir, file) => {
|
|
101
|
-
const rel = path.relative(dir, file);
|
|
102
|
-
return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel) ? rel.split(path.sep) : null;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const sourceFiles = (dir) => {
|
|
106
|
-
if (!fs.existsSync(dir)) return [];
|
|
107
|
-
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
108
|
-
const full = path.join(dir, entry.name);
|
|
109
|
-
if (entry.isDirectory()) return entry.name === 'node_modules' ? [] : sourceFiles(full);
|
|
110
|
-
return /\.(c|m)?tsx?$/.test(entry.name) && !entry.name.endsWith('.d.ts') ? [full] : [];
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// One alternation, scanned left to right, so a match never spans two statements.
|
|
115
|
-
const importPattern =
|
|
116
|
-
/\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;
|
|
117
|
-
|
|
118
|
-
const importsOf = (file) => {
|
|
119
|
-
const text = fs.readFileSync(file, 'utf8');
|
|
120
|
-
return [...text.matchAll(importPattern)].map((m) => ({
|
|
121
|
-
typeOnly: m[1] === 'import' && Boolean(m[2]),
|
|
122
|
-
target: resolveSpecifier(m[4], file),
|
|
123
|
-
line: text.slice(0, m.index).split('\n').length,
|
|
124
|
-
statement: m[0].replace(/\s+/g, ' '),
|
|
125
|
-
}));
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const contexts = fs.existsSync(contextsDir)
|
|
129
|
-
? fs
|
|
130
|
-
.readdirSync(contextsDir, { withFileTypes: true })
|
|
131
|
-
.filter((e) => e.isDirectory())
|
|
132
|
-
.map((e) => e.name)
|
|
133
|
-
.sort()
|
|
134
|
-
: [];
|
|
135
|
-
|
|
136
|
-
// Folder of the widget a path belongs to: the nearest folder under widgets/ with a `*-widget.tsx`.
|
|
137
|
-
const widgetCache = new Map();
|
|
138
|
-
const isWidgetFolder = (dir) => {
|
|
139
|
-
if (!widgetCache.has(dir)) {
|
|
140
|
-
widgetCache.set(
|
|
141
|
-
dir,
|
|
142
|
-
fs.existsSync(dir) &&
|
|
143
|
-
fs.statSync(dir).isDirectory() &&
|
|
144
|
-
fs.readdirSync(dir).some((name) => name.endsWith('-widget.tsx')),
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
return widgetCache.get(dir);
|
|
148
|
-
};
|
|
149
|
-
const widgetOf = (target) => {
|
|
150
|
-
if (!inside(widgetsDir, target)) return null;
|
|
151
|
-
for (let dir = target; inside(widgetsDir, dir); dir = path.dirname(dir)) {
|
|
152
|
-
if (isWidgetFolder(dir)) return dir;
|
|
153
|
-
}
|
|
154
|
-
return null;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const violations = new Map();
|
|
158
|
-
const report = (title, file, imp) => {
|
|
159
|
-
if (!violations.has(title)) violations.set(title, []);
|
|
160
|
-
violations.get(title).push(`${path.relative(cwd, file)}:${imp.line}: ${imp.statement}`);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const scanned = [contextsDir, sharedDir, reactDir].flatMap(sourceFiles);
|
|
164
|
-
|
|
165
|
-
for (const file of scanned) {
|
|
166
|
-
const ownContext = inside(contextsDir, file)?.[0];
|
|
167
|
-
for (const imp of importsOf(file)) {
|
|
168
|
-
if (!imp.target) continue;
|
|
169
|
-
const [targetContext, layer, part] = inside(contextsDir, imp.target) ?? [];
|
|
170
|
-
const allowedInMemoryDbImport = file === inMemoryDbFile && isDomainModel(imp.target, layer, part);
|
|
171
|
-
|
|
172
|
-
if (ownContext) {
|
|
173
|
-
if (targetContext && targetContext !== ownContext) report(`[${ownContext}] imports another context:`, file, imp);
|
|
174
|
-
} else if (inside(sharedDir, file)) {
|
|
175
|
-
if (targetContext && !(layer === 'domain' && part === 'errors') && !allowedInMemoryDbImport) {
|
|
176
|
-
report('[shared] imports a bounded context outside its domain/errors (and domain models for the in-memory DB):', file, imp);
|
|
177
|
-
}
|
|
178
|
-
} else if (inside(uiKitDir, file)) {
|
|
179
|
-
const [area] = inside(reactDir, imp.target) ?? [];
|
|
180
|
-
if (targetContext || area === 'widgets' || area === 'pages') {
|
|
181
|
-
report('[presentation/react/ui-kit] imports a bounded context, widget or page:', file, imp);
|
|
182
|
-
}
|
|
183
|
-
} else if (targetContext && !(imp.typeOnly && layer === 'presentation' && part === 'rest')) {
|
|
184
|
-
report('[presentation/react] imports a context other than `import type` from presentation/rest:', file, imp);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const ownWidget = widgetOf(file);
|
|
188
|
-
const targetWidget = widgetOf(imp.target);
|
|
189
|
-
if (ownWidget && targetWidget && targetWidget !== ownWidget) {
|
|
190
|
-
report('[presentation/react/widgets] a widget imports another widget:', file, imp);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
for (const [title, lines] of violations) {
|
|
196
|
-
console.log(title);
|
|
197
|
-
for (const line of lines) console.log(` ${line}`);
|
|
198
|
-
console.log();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (violations.size === 0) {
|
|
202
|
-
console.log(`OK: no cross-context or cross-widget import violations (${contexts.length} contexts)`);
|
|
203
|
-
}
|
|
204
|
-
process.exit(violations.size === 0 ? 0 : 1);
|