@domain-first/project-structure 7.1.1 → 8.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/README.md +128 -25
- package/dist/201.js +146 -342
- package/dist/287.js +1304 -568
- package/dist/561.js +518 -0
- package/dist/711.js +22 -0
- package/dist/870.js +438 -0
- package/dist/agent-cli.d.ts +378 -0
- package/dist/agent-cli.spec.d.ts +1 -0
- package/dist/agent-identifiers.d.ts +4 -0
- package/dist/agent-requests.d.ts +488 -0
- package/dist/backend-module-move.d.ts +1 -0
- package/dist/config-file.d.ts +231 -26
- package/dist/config-file.spec.d.ts +1 -0
- package/dist/context-dependencies.d.ts +16 -0
- package/dist/dependency-rules.d.ts +17 -17
- package/dist/file-system.d.ts +10 -1
- package/dist/implementation-choices.d.ts +8 -0
- package/dist/implementation-choices.spec.d.ts +1 -0
- package/dist/implementation-options-schema.d.ts +14 -0
- package/dist/implementations.d.ts +48 -0
- package/dist/index.cjs +3768 -574
- package/dist/index.js +25 -9
- package/dist/init.cjs +717 -122
- package/dist/init.js +24 -12
- package/dist/integration.cjs +3737 -186
- package/dist/integration.js +29 -17
- package/dist/integrations/rest-endpoint-generator.d.ts +4 -2
- package/dist/integrations/storybook.d.ts +1 -0
- package/dist/interaction.d.ts +41 -0
- package/dist/lint.cjs +173 -46
- package/dist/lint.js +22 -7
- package/dist/next-app.d.ts +7 -0
- package/dist/react.cjs +5128 -414
- package/dist/react.js +72 -60
- package/dist/system-implementations.d.ts +6 -0
- package/dist/upgrade-steps.d.ts +81 -0
- package/dist/upgrade-steps.spec.d.ts +1 -0
- package/dist/upgrade.cjs +593 -0
- package/dist/upgrade.d.ts +2 -0
- package/dist/upgrade.js +235 -0
- package/dist/use-cases/add-integration.d.ts +16 -1
- package/dist/use-cases/cross-context-dependencies.spec.d.ts +1 -0
- package/dist/use-cases/index.d.ts +1 -0
- package/dist/use-cases/initialize.d.ts +4 -4
- package/dist/use-cases/initialize.spec.d.ts +1 -0
- package/dist/use-cases/prompt-implementation-type.d.ts +20 -0
- package/dist/use-cases/scaffold-new-aggregate.d.ts +1 -1
- package/dist/use-cases/scaffold-new-implementation.d.ts +6 -0
- package/dist/use-cases/scaffold-new-implementation.spec.d.ts +1 -0
- package/dist/use-cases/scaffold-react-page.d.ts +3 -1
- package/dist/use-cases/wire-dependency.d.ts +5 -0
- package/dist/wire-branches.d.ts +26 -0
- package/dist/wiring.d.ts +24 -2
- package/package.json +3 -2
- package/skills/df-ps-implement/SKILL.md +9 -8
- package/skills/df-ps-plan-feature/SKILL.md +8 -7
- package/skills/df-ps-react/SKILL.md +13 -7
- package/skills/df-ps-react/references/generators.md +6 -2
- package/skills/df-ps-scaffold/SKILL.md +31 -19
- package/skills/df-ps-scaffold/references/agent-mode.md +72 -0
- package/skills/df-ps-scaffold/references/agent-operations.md +155 -0
- package/skills/df-ps-scaffold/references/choosing-components.md +14 -13
- package/skills/df-ps-scaffold/references/generators.md +20 -3
- package/skills/df-ps-scaffold/references/units/aggregate.md +4 -3
- package/skills/df-ps-scaffold/references/units/bounded-context.md +1 -1
- package/skills/df-ps-scaffold/references/units/command-query.md +12 -12
- package/skills/df-ps-scaffold/references/units/implementation.md +20 -0
- package/skills/df-ps-scaffold/references/units/move-or-rename.md +1 -1
- package/skills/df-ps-scaffold/references/units/port.md +7 -4
- package/skills/df-ps-scaffold/references/units/use-case.md +1 -1
- package/skills/df-ps-scaffold/scripts/drive.mjs +9 -1
package/dist/201.js
CHANGED
|
@@ -1,332 +1,50 @@
|
|
|
1
|
-
import node_path from "node:path";
|
|
2
|
-
import zod from "zod";
|
|
3
1
|
import node_fs from "node:fs";
|
|
2
|
+
import node_path from "node:path";
|
|
4
3
|
import { createRequire } from "node:module";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
'
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
'queries',
|
|
13
|
-
'infrastructure-services',
|
|
14
|
-
'presentation-services'
|
|
15
|
-
];
|
|
16
|
-
const domain = [
|
|
17
|
-
'repositories',
|
|
18
|
-
'domain-services'
|
|
19
|
-
];
|
|
20
|
-
const commandsAndQueries = [
|
|
21
|
-
'commands',
|
|
22
|
-
'queries'
|
|
23
|
-
];
|
|
24
|
-
const infrastructure = [
|
|
25
|
-
...domain,
|
|
26
|
-
'application-ports',
|
|
27
|
-
'infrastructure-services'
|
|
28
|
-
];
|
|
29
|
-
const defaultDependencies = {
|
|
30
|
-
'domain-services': [
|
|
31
|
-
'domain-services'
|
|
32
|
-
],
|
|
33
|
-
repositories: [
|
|
34
|
-
'domain-services',
|
|
35
|
-
'application-ports',
|
|
36
|
-
'infrastructure-services'
|
|
37
|
-
],
|
|
38
|
-
'application-ports': infrastructure,
|
|
39
|
-
'application-services': [
|
|
40
|
-
...domain,
|
|
41
|
-
'application-ports',
|
|
42
|
-
...commandsAndQueries
|
|
43
|
-
],
|
|
44
|
-
'use-cases': [
|
|
45
|
-
...domain,
|
|
46
|
-
'application-ports',
|
|
47
|
-
'application-services',
|
|
48
|
-
...commandsAndQueries
|
|
49
|
-
],
|
|
50
|
-
commands: [
|
|
51
|
-
'infrastructure-services'
|
|
52
|
-
],
|
|
53
|
-
queries: [
|
|
54
|
-
'infrastructure-services'
|
|
55
|
-
],
|
|
56
|
-
'infrastructure-services': infrastructure,
|
|
57
|
-
'presentation-services': [
|
|
58
|
-
'application-ports',
|
|
59
|
-
'application-services',
|
|
60
|
-
'use-cases',
|
|
61
|
-
...commandsAndQueries,
|
|
62
|
-
'presentation-services'
|
|
63
|
-
]
|
|
4
|
+
import { defaultDependencyRules, configuredDependencyRules, wiredModuleKind, File } from "./870.js";
|
|
5
|
+
const crossContextImplementation = 'cross-context';
|
|
6
|
+
const hasNeighboringBoundedContext = (ownerPath)=>{
|
|
7
|
+
const contextsPath = node_path.dirname(ownerPath);
|
|
8
|
+
return 'bounded-contexts' === node_path.basename(contextsPath) && node_fs.existsSync(contextsPath) && node_fs.readdirSync(contextsPath, {
|
|
9
|
+
withFileTypes: true
|
|
10
|
+
}).some((entry)=>entry.isDirectory() && entry.name !== node_path.basename(ownerPath));
|
|
64
11
|
};
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const entries = moduleKinds.map((kind)=>{
|
|
75
|
-
const override = config[kind];
|
|
76
|
-
if (Array.isArray(override)) return [
|
|
77
|
-
kind,
|
|
78
|
-
new Set(override)
|
|
79
|
-
];
|
|
80
|
-
const kinds = new Set([
|
|
81
|
-
...defaultDependencies[kind],
|
|
82
|
-
...override?.add ?? []
|
|
83
|
-
]);
|
|
84
|
-
for (const removed of override?.remove ?? [])kinds.delete(removed);
|
|
85
|
-
return [
|
|
86
|
-
kind,
|
|
87
|
-
kinds
|
|
88
|
-
];
|
|
89
|
-
});
|
|
90
|
-
return Object.fromEntries(entries);
|
|
91
|
-
};
|
|
92
|
-
const defaultDependencyRules = resolveDependencyRules();
|
|
93
|
-
const isModuleKind = (value)=>moduleKinds.includes(value);
|
|
94
|
-
const wiredModuleKind = (wiringKind)=>isModuleKind(wiringKind) ? wiringKind : void 0;
|
|
95
|
-
const fileModuleKind = (segments)=>{
|
|
96
|
-
const [layer, folder] = segments;
|
|
97
|
-
const file = segments.at(-1) ?? '';
|
|
98
|
-
if (/^index(\.tsx?)?$/.test(file)) return;
|
|
99
|
-
if ('domain' === layer) {
|
|
100
|
-
if ('services' === folder) return 'domain-services';
|
|
101
|
-
return 'aggregates' === folder && /-repository(\.tsx?)?$/.test(file) ? 'repositories' : void 0;
|
|
102
|
-
}
|
|
103
|
-
if ('application' === layer) return ({
|
|
104
|
-
commands: 'commands',
|
|
105
|
-
queries: 'queries',
|
|
106
|
-
ports: 'application-ports',
|
|
107
|
-
services: 'application-services',
|
|
108
|
-
'use-cases': 'use-cases'
|
|
109
|
-
})[folder];
|
|
110
|
-
if ('infrastructure' === layer) return ({
|
|
111
|
-
repositories: 'repositories',
|
|
112
|
-
commands: 'commands',
|
|
113
|
-
queries: 'queries',
|
|
114
|
-
'application-adapters': 'application-ports',
|
|
115
|
-
services: 'infrastructure-services'
|
|
116
|
-
})[folder];
|
|
117
|
-
return 'presentation' === layer && 'services' === folder ? 'presentation-services' : void 0;
|
|
118
|
-
};
|
|
119
|
-
const pendingFiles = new Map();
|
|
120
|
-
const pendingFolders = new Set();
|
|
121
|
-
const pendingMoves = new Map();
|
|
122
|
-
const pendingDeletions = new Map();
|
|
123
|
-
const filesToOverwrite = ()=>[
|
|
124
|
-
...pendingFiles
|
|
125
|
-
].filter(([filePath, { replacesFile }])=>replacesFile && node_fs.existsSync(filePath)).map(([filePath])=>filePath);
|
|
126
|
-
const withoutOverwrites = (generate)=>{
|
|
127
|
-
const before = new Map(pendingFiles);
|
|
128
|
-
const result = generate();
|
|
129
|
-
for (const [filePath, change] of pendingFiles){
|
|
130
|
-
if (before.get(filePath) === change || !change.replacesFile || !node_fs.existsSync(filePath)) continue;
|
|
131
|
-
const previous = before.get(filePath);
|
|
132
|
-
if (previous) pendingFiles.set(filePath, previous);
|
|
133
|
-
else pendingFiles.delete(filePath);
|
|
134
|
-
}
|
|
135
|
-
return result;
|
|
12
|
+
const boundedContextLocation = (file)=>{
|
|
13
|
+
const parts = node_path.resolve(file).split(node_path.sep);
|
|
14
|
+
const index = parts.lastIndexOf('bounded-contexts');
|
|
15
|
+
if (-1 === index || parts.length < index + 3) return;
|
|
16
|
+
return {
|
|
17
|
+
root: parts.slice(0, index).join(node_path.sep),
|
|
18
|
+
name: parts[index + 1],
|
|
19
|
+
segments: parts.slice(index + 2)
|
|
20
|
+
};
|
|
136
21
|
};
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
22
|
+
const isCrossContextImplementation = (file)=>{
|
|
23
|
+
const location = boundedContextLocation(file);
|
|
24
|
+
const [layer, kind, label] = location?.segments ?? [];
|
|
25
|
+
return 'infrastructure' === layer && 'application-adapters' === kind && label === crossContextImplementation;
|
|
26
|
+
};
|
|
27
|
+
const crossContextImportAllowed = (from, to)=>{
|
|
28
|
+
const source = boundedContextLocation(from);
|
|
29
|
+
const target = boundedContextLocation(to);
|
|
30
|
+
if (!source || !target || source.name === target.name) return;
|
|
31
|
+
if (source.root !== target.root) return false;
|
|
32
|
+
const [layer, kind] = source.segments;
|
|
33
|
+
const [targetLayer, targetKind] = target.segments;
|
|
34
|
+
if ('wiring' === targetLayer && !node_path.basename(to).startsWith('wire-')) return false;
|
|
35
|
+
if (/\.(spec|test)\.[^.]+$/.test(to)) return false;
|
|
36
|
+
const suffix = {
|
|
37
|
+
queries: '-query',
|
|
38
|
+
commands: '-command',
|
|
39
|
+
'use-cases': '-use-case'
|
|
40
|
+
}[targetKind];
|
|
41
|
+
if (!suffix || !new RegExp(`${suffix}(\\.(?:c|m)?[jt]sx?)?$`).test(to)) return false;
|
|
42
|
+
return [
|
|
43
|
+
'queries',
|
|
44
|
+
'commands',
|
|
45
|
+
'use-cases'
|
|
46
|
+
].includes(targetKind) && (isCrossContextImplementation(from) && 'application' === targetLayer || 'wiring' === layer && 'application-ports' === kind && 'wiring' === targetLayer);
|
|
149
47
|
};
|
|
150
|
-
const writePendingChanges = ()=>{
|
|
151
|
-
for (const folderPath of pendingFolders)node_fs.mkdirSync(folderPath, {
|
|
152
|
-
recursive: true
|
|
153
|
-
});
|
|
154
|
-
for (const [filePath, { content }] of pendingFiles){
|
|
155
|
-
node_fs.mkdirSync(node_path.dirname(filePath), {
|
|
156
|
-
recursive: true
|
|
157
|
-
});
|
|
158
|
-
node_fs.writeFileSync(filePath, content, 'utf8');
|
|
159
|
-
}
|
|
160
|
-
for (const [targetPath, { sourcePath }] of pendingMoves){
|
|
161
|
-
node_fs.mkdirSync(node_path.dirname(targetPath), {
|
|
162
|
-
recursive: true
|
|
163
|
-
});
|
|
164
|
-
node_fs.renameSync(sourcePath, targetPath);
|
|
165
|
-
}
|
|
166
|
-
for (const filePath of pendingDeletions.keys())node_fs.rmSync(filePath, {
|
|
167
|
-
force: true
|
|
168
|
-
});
|
|
169
|
-
for (const [filePath, stopAt] of pendingDeletions)removeEmptyFolders(node_path.dirname(filePath), stopAt);
|
|
170
|
-
for (const { sourcePath, stopAt } of pendingMoves.values())removeEmptyFolders(node_path.dirname(sourcePath), stopAt);
|
|
171
|
-
discardPendingChanges();
|
|
172
|
-
};
|
|
173
|
-
class FileSystemItem {
|
|
174
|
-
path;
|
|
175
|
-
constructor(path){
|
|
176
|
-
this.path = path;
|
|
177
|
-
}
|
|
178
|
-
subitem = (relativePath)=>new this.constructor(node_path.resolve(this.path, ...relativePath));
|
|
179
|
-
get exists() {
|
|
180
|
-
return node_fs.existsSync(this.path);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
class Folder extends FileSystemItem {
|
|
184
|
-
createIfNotExisted = ()=>{
|
|
185
|
-
pendingFolders.add(this.path);
|
|
186
|
-
};
|
|
187
|
-
get content() {
|
|
188
|
-
if (!this.exists) return {
|
|
189
|
-
subfolderNames: [],
|
|
190
|
-
fileNames: []
|
|
191
|
-
};
|
|
192
|
-
const content = node_fs.readdirSync(this.path, {
|
|
193
|
-
withFileTypes: true
|
|
194
|
-
});
|
|
195
|
-
return {
|
|
196
|
-
subfolderNames: content.filter((x)=>x.isDirectory()).map((x)=>x.name),
|
|
197
|
-
fileNames: content.filter((x)=>!x.isDirectory()).map((x)=>node_path.parse(x.name).name)
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
get name() {
|
|
201
|
-
return node_path.basename(this.path);
|
|
202
|
-
}
|
|
203
|
-
createFile = (name, content)=>{
|
|
204
|
-
this.createIfNotExisted();
|
|
205
|
-
const file = new File(this.path).subitem([
|
|
206
|
-
name
|
|
207
|
-
]);
|
|
208
|
-
file.data = content;
|
|
209
|
-
return file;
|
|
210
|
-
};
|
|
211
|
-
file = (name)=>{
|
|
212
|
-
this.createIfNotExisted();
|
|
213
|
-
const file = new File(this.path).subitem([
|
|
214
|
-
name
|
|
215
|
-
]);
|
|
216
|
-
return file;
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
class File extends FileSystemItem {
|
|
220
|
-
get exists() {
|
|
221
|
-
return pendingFiles.has(this.path) || pendingMoves.has(this.path) || node_fs.existsSync(this.path) && !pendingDeletions.has(this.path);
|
|
222
|
-
}
|
|
223
|
-
delete = (stopAt)=>{
|
|
224
|
-
pendingDeletions.set(this.path, stopAt);
|
|
225
|
-
};
|
|
226
|
-
moveTo = (targetPath, stopAt)=>{
|
|
227
|
-
pendingMoves.set(targetPath, {
|
|
228
|
-
sourcePath: this.path,
|
|
229
|
-
stopAt
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
get data() {
|
|
233
|
-
const pendingFile = pendingFiles.get(this.path);
|
|
234
|
-
if (pendingFile) return pendingFile.content;
|
|
235
|
-
if (!this.exists) throw new Error('File does not exist');
|
|
236
|
-
return node_fs.readFileSync(this.path, 'utf-8');
|
|
237
|
-
}
|
|
238
|
-
set data(content) {
|
|
239
|
-
pendingFiles.set(this.path, {
|
|
240
|
-
content,
|
|
241
|
-
replacesFile: true
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
update = (content)=>{
|
|
245
|
-
pendingFiles.set(this.path, {
|
|
246
|
-
content,
|
|
247
|
-
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
248
|
-
});
|
|
249
|
-
};
|
|
250
|
-
addLine = (newLine, separator = '\n')=>{
|
|
251
|
-
const content = this.exists ? this.data : '';
|
|
252
|
-
pendingFiles.set(this.path, {
|
|
253
|
-
content: [
|
|
254
|
-
content.trim(),
|
|
255
|
-
newLine.trim()
|
|
256
|
-
].filter((x)=>!!x).join(separator),
|
|
257
|
-
replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
|
|
258
|
-
});
|
|
259
|
-
};
|
|
260
|
-
addLineIfMissing = (newLine, separator = '\n')=>{
|
|
261
|
-
const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
|
|
262
|
-
if (!alreadyAdded) this.addLine(newLine, separator);
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
class ProjectRoot {
|
|
266
|
-
path;
|
|
267
|
-
constructor(){
|
|
268
|
-
let dir = process.cwd();
|
|
269
|
-
while(dir !== node_path.parse(dir).root){
|
|
270
|
-
if (node_fs.existsSync(node_path.join(dir, 'package.json'))) {
|
|
271
|
-
this.path = dir;
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
dir = node_path.dirname(dir);
|
|
275
|
-
}
|
|
276
|
-
throw new Error('No package.json was found');
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
const lintChecks = [
|
|
280
|
-
'context-isolation',
|
|
281
|
-
'shared-kernel',
|
|
282
|
-
'layers',
|
|
283
|
-
'dependencies',
|
|
284
|
-
'react',
|
|
285
|
-
'clock'
|
|
286
|
-
];
|
|
287
|
-
const configFileName = 'domain-first.project-structure.config.json';
|
|
288
|
-
const configSchema = zod.object({
|
|
289
|
-
rootFolder: zod.string(),
|
|
290
|
-
domainFirstPackages: zod.array(zod["enum"]([
|
|
291
|
-
'@domain-first/types',
|
|
292
|
-
'@domain-first/errors',
|
|
293
|
-
'@domain-first/handlers',
|
|
294
|
-
'@domain-first/wire',
|
|
295
|
-
'@domain-first/handlers-rest'
|
|
296
|
-
])),
|
|
297
|
-
defaultPersistenceLayerImplementation: zod.string(),
|
|
298
|
-
withInMemoryDb: zod.boolean().optional(),
|
|
299
|
-
useReact: zod.boolean(),
|
|
300
|
-
storybookFramework: zod.string().optional(),
|
|
301
|
-
testingLibrary: zod.string().optional(),
|
|
302
|
-
rules: zod.object({
|
|
303
|
-
dependencies: dependencyRulesSchema.optional(),
|
|
304
|
-
lint: zod.partialRecord(zod["enum"](lintChecks), zod.boolean()).optional()
|
|
305
|
-
}).optional()
|
|
306
|
-
});
|
|
307
|
-
class ConfigFile {
|
|
308
|
-
data;
|
|
309
|
-
constructor(){
|
|
310
|
-
const filePath = ConfigFile.path;
|
|
311
|
-
const file = new File(filePath);
|
|
312
|
-
const configObject = JSON.parse(file.data);
|
|
313
|
-
this.data = configSchema.parse(configObject);
|
|
314
|
-
}
|
|
315
|
-
static get Instance() {
|
|
316
|
-
return new ConfigFile();
|
|
317
|
-
}
|
|
318
|
-
static get path() {
|
|
319
|
-
const { path: root } = new ProjectRoot();
|
|
320
|
-
return node_path.resolve(root, configFileName);
|
|
321
|
-
}
|
|
322
|
-
static get exists() {
|
|
323
|
-
return new File(ConfigFile.path).exists;
|
|
324
|
-
}
|
|
325
|
-
static write = (config)=>{
|
|
326
|
-
new File(ConfigFile.path).data = JSON.stringify(config, null, '\t');
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
const configuredDependencyRules = ()=>resolveDependencyRules(ConfigFile.exists ? ConfigFile.Instance.data.rules?.dependencies : void 0);
|
|
330
48
|
class UnknownFormatNaming {
|
|
331
49
|
rawSource;
|
|
332
50
|
camelCase;
|
|
@@ -406,11 +124,15 @@ const layerOf = (file, contextPath)=>{
|
|
|
406
124
|
const [firstSegment] = node_path.relative(contextPath, file).split(node_path.sep);
|
|
407
125
|
return wiring_layers.find((layer)=>layer === firstSegment);
|
|
408
126
|
};
|
|
409
|
-
const
|
|
127
|
+
const withoutScriptExtension = (file)=>file.replace(/\.(?:c|m)?[jt]s$/, '');
|
|
128
|
+
const namedImportFiles = (source, fromFile, resolve = (specifier, file)=>specifier.startsWith('.') ? node_path.resolve(node_path.dirname(file), specifier) : void 0)=>{
|
|
410
129
|
const result = new Map();
|
|
411
|
-
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g))
|
|
412
|
-
const
|
|
413
|
-
if (
|
|
130
|
+
for (const [, names, importPath] of source.matchAll(/import\s*(?:type\s*)?\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g)){
|
|
131
|
+
const target = resolve(importPath, fromFile);
|
|
132
|
+
if (target) for (const name of names.split(',')){
|
|
133
|
+
const cleanName = name.replace(/^\s*type\s+/, '').trim().split(/\s+as\s+/).at(-1);
|
|
134
|
+
if (cleanName) result.set(cleanName, `${withoutScriptExtension(target)}.ts`);
|
|
135
|
+
}
|
|
414
136
|
}
|
|
415
137
|
return result;
|
|
416
138
|
};
|
|
@@ -440,7 +162,7 @@ const readWiredModule = (wireFile, contextPath)=>{
|
|
|
440
162
|
const exportMatch = new RegExp(`export\\s+const\\s+${wireName}\\s*=\\s*`).exec(source);
|
|
441
163
|
if (!exportMatch) return;
|
|
442
164
|
const branched = source.slice(exportMatch.index + exportMatch[0].length).startsWith('envBranchedWire(');
|
|
443
|
-
const imports =
|
|
165
|
+
const imports = namedImportFiles(source, wireFile);
|
|
444
166
|
const implementations = wiredClassNames(source, wireName, exportMatch.index + exportMatch[0].length, branched).flatMap((className)=>{
|
|
445
167
|
const file = imports.get(className);
|
|
446
168
|
return file ? [
|
|
@@ -492,14 +214,19 @@ const moduleKindOf = (module)=>wiredModuleKind(module.kind);
|
|
|
492
214
|
const layerRule = (consumer, candidate)=>visibleLayers[consumer.implementationsLayer].includes(candidate.layer) && ('infrastructure' !== consumer.implementationsLayer || 'application' !== candidate.layer || 'application-ports' === candidate.kind);
|
|
493
215
|
const implementationType = (consumer, implementation)=>consumer.branched ? node_path.basename(node_path.dirname(implementation.file)) : void 0;
|
|
494
216
|
const isSuggestedFor = (candidate, implementation)=>'infrastructure' === candidate.layer && `-${node_path.basename(candidate.typeFile, '.ts')}-`.includes(`-${implementation}-`);
|
|
495
|
-
const canDependOn = (consumer, candidate, rules = defaultDependencyRules)=>{
|
|
217
|
+
const canDependOn = (consumer, candidate, rules = defaultDependencyRules, implementation = consumer.implementations[0])=>{
|
|
218
|
+
const crossContext = crossContextImportAllowed(consumer.wireFile, candidate.wireFile);
|
|
219
|
+
if (void 0 !== crossContext) return crossContext && 'infrastructure' === consumer.implementationsLayer && 'application' === candidate.layer && !!implementation && true === crossContextImportAllowed(implementation.file, candidate.typeFile);
|
|
496
220
|
const consumerKind = moduleKindOf(consumer);
|
|
497
221
|
const candidateKind = moduleKindOf(candidate);
|
|
498
222
|
return consumerKind && candidateKind ? rules[consumerKind].has(candidateKind) : layerRule(consumer, candidate);
|
|
499
223
|
};
|
|
500
224
|
const availableDependencies = (consumer, implementation, candidates, rules = configuredDependencyRules())=>{
|
|
501
|
-
const
|
|
502
|
-
|
|
225
|
+
const source = new File(consumer.wireFile).data;
|
|
226
|
+
const imported = namedImportFiles(source, consumer.wireFile);
|
|
227
|
+
const wired = wiredDependencyNames(source, implementation.className);
|
|
228
|
+
const wiredFiles = new Set(wired.map((name)=>imported.get(name)).filter(Boolean));
|
|
229
|
+
return candidates.filter((candidate)=>candidate.wireFile !== consumer.wireFile && canDependOn(consumer, candidate, rules, implementation) && !wiredFiles.has(candidate.wireFile) && !(wired.includes(candidate.wireName) && !imported.has(candidate.wireName)));
|
|
503
230
|
};
|
|
504
231
|
const relativeImport = (fromFile, toFile)=>{
|
|
505
232
|
const relativePath = node_path.relative(node_path.dirname(fromFile), toFile).replace(/\.ts$/, '').split(node_path.sep).join('/');
|
|
@@ -564,9 +291,9 @@ const closingIndex = (source, openIndex)=>{
|
|
|
564
291
|
}
|
|
565
292
|
return -1;
|
|
566
293
|
};
|
|
567
|
-
const
|
|
294
|
+
const topLevelItemRanges = (content)=>{
|
|
568
295
|
const code = codeOnly(content);
|
|
569
|
-
const
|
|
296
|
+
const ranges = [];
|
|
570
297
|
let depth = 0;
|
|
571
298
|
let itemStart = 0;
|
|
572
299
|
for(let index = 0; index <= code.length; index++){
|
|
@@ -574,16 +301,19 @@ const splitTopLevel = (content)=>{
|
|
|
574
301
|
if (void 0 !== char && char in brackets) depth++;
|
|
575
302
|
else if (Object.values(brackets).includes(char)) depth--;
|
|
576
303
|
if (index === code.length || ',' === char && 0 === depth) {
|
|
577
|
-
const item = content.slice(itemStart, index);
|
|
578
304
|
const itemCode = code.slice(itemStart, index);
|
|
579
305
|
const first = itemCode.search(/\S/);
|
|
580
306
|
const last = itemCode.search(/\S\s*$/);
|
|
581
|
-
if (-1 !== first)
|
|
307
|
+
if (-1 !== first) ranges.push({
|
|
308
|
+
start: itemStart + first,
|
|
309
|
+
end: itemStart + last + 1
|
|
310
|
+
});
|
|
582
311
|
itemStart = index + 1;
|
|
583
312
|
}
|
|
584
313
|
}
|
|
585
|
-
return
|
|
314
|
+
return ranges;
|
|
586
315
|
};
|
|
316
|
+
const splitTopLevel = (content)=>topLevelItemRanges(content).map(({ start, end })=>content.slice(start, end));
|
|
587
317
|
const appendItems = (content, items)=>{
|
|
588
318
|
const code = codeOnly(content);
|
|
589
319
|
const lastCode = code.search(/\S\s*$/);
|
|
@@ -652,15 +382,89 @@ const addWireDependencies = (source, className, wireNames)=>{
|
|
|
652
382
|
return source.slice(0, openIndex + 1) + appendItems(source.slice(openIndex + 1, closeIndex), wireNames) + source.slice(closeIndex);
|
|
653
383
|
};
|
|
654
384
|
const parameterName = (typeName)=>typeName.substring(0, 1).toLowerCase() + typeName.substring(1);
|
|
385
|
+
const dependencyBinding = (source, fromFile, targetFile, originalName, preferredName)=>{
|
|
386
|
+
const existing = [
|
|
387
|
+
...namedImportFiles(source, fromFile)
|
|
388
|
+
].find(([, file])=>file === targetFile);
|
|
389
|
+
if (existing) return existing[0];
|
|
390
|
+
let name = preferredName;
|
|
391
|
+
let suffix = 2;
|
|
392
|
+
while(new RegExp(`\\b${name}\\b`).test(codeOnly(source)))name = `${preferredName}${suffix++}`;
|
|
393
|
+
return preferredName === originalName ? originalName : name;
|
|
394
|
+
};
|
|
395
|
+
const importedAs = (name, local)=>name === local ? name : `${name} as ${local}`;
|
|
655
396
|
const wireDependencies = (consumer, implementation, dependencies)=>{
|
|
656
397
|
const wireFile = new File(consumer.wireFile);
|
|
657
398
|
let wireSource = wireFile.data;
|
|
658
|
-
for (const dependency of dependencies)wireSource = addImport(wireSource, `import { ${dependency.wireName} } from '${relativeImport(consumer.wireFile, dependency.wireFile)}'`);
|
|
659
|
-
wireFile.update(addWireDependencies(wireSource, implementation.className, dependencies.map((x)=>x.wireName)));
|
|
660
399
|
const classFile = new File(implementation.file);
|
|
661
400
|
let classSource = classFile.data;
|
|
662
|
-
|
|
663
|
-
|
|
401
|
+
const bindings = dependencies.map((dependency)=>{
|
|
402
|
+
const crossContext = crossContextImportAllowed(consumer.wireFile, dependency.wireFile);
|
|
403
|
+
const context = crossContext ? boundedContextLocation(dependency.wireFile) : void 0;
|
|
404
|
+
const prefix = context ? new UnknownFormatNaming(context.name).ClassName : '';
|
|
405
|
+
const wireName = dependencyBinding(wireSource, consumer.wireFile, dependency.wireFile, dependency.wireName, context ? `wire${prefix}${dependency.typeName}` : dependency.wireName);
|
|
406
|
+
const typeName = dependencyBinding(classSource, implementation.file, dependency.typeFile, dependency.typeName, `${prefix}${dependency.typeName}`);
|
|
407
|
+
wireSource = addImport(wireSource, `import { ${importedAs(dependency.wireName, wireName)} } from '${relativeImport(consumer.wireFile, dependency.wireFile)}'`);
|
|
408
|
+
classSource = addImport(classSource, `import type { ${importedAs(dependency.typeName, typeName)} } from '${relativeImport(implementation.file, dependency.typeFile)}'`);
|
|
409
|
+
return {
|
|
410
|
+
wireName,
|
|
411
|
+
typeName
|
|
412
|
+
};
|
|
413
|
+
});
|
|
414
|
+
wireFile.update(addWireDependencies(wireSource, implementation.className, bindings.map((x)=>x.wireName)));
|
|
415
|
+
classFile.update(addConstructorParameters(classSource, implementation.className, bindings.map((x)=>`private readonly ${parameterName(x.typeName)}: ${x.typeName}`)));
|
|
416
|
+
};
|
|
417
|
+
const branchesRange = (source, wireName)=>{
|
|
418
|
+
const match = new RegExp(`export\\s+const\\s+${wireName}\\s*=\\s*envBranchedWire\\(\\s*\\{`).exec(codeOnly(source));
|
|
419
|
+
if (!match) throw new Error(`${wireName} = envBranchedWire({...}) not found`);
|
|
420
|
+
const openIndex = match.index + match[0].length - 1;
|
|
421
|
+
return {
|
|
422
|
+
declarationIndex: match.index,
|
|
423
|
+
openIndex,
|
|
424
|
+
closeIndex: closingIndex(source, openIndex)
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
const branchItems = (content)=>topLevelItemRanges(content).map(({ start, end })=>{
|
|
428
|
+
const item = content.slice(start, end);
|
|
429
|
+
const colon = codeOnly(item).indexOf(':');
|
|
430
|
+
if (-1 === colon) return {
|
|
431
|
+
key: item,
|
|
432
|
+
valueStart: start,
|
|
433
|
+
end,
|
|
434
|
+
shorthand: true
|
|
435
|
+
};
|
|
436
|
+
const valueStart = start + colon + 1 + item.slice(colon + 1).search(/\S/);
|
|
437
|
+
return {
|
|
438
|
+
key: item.slice(0, colon).trim(),
|
|
439
|
+
valueStart,
|
|
440
|
+
end,
|
|
441
|
+
shorthand: false
|
|
442
|
+
};
|
|
443
|
+
});
|
|
444
|
+
const wireBranches = (source, wireName)=>{
|
|
445
|
+
const { openIndex, closeIndex } = branchesRange(source, wireName);
|
|
446
|
+
const content = source.slice(openIndex + 1, closeIndex);
|
|
447
|
+
return new Map(branchItems(content).map(({ key, valueStart, end })=>[
|
|
448
|
+
key,
|
|
449
|
+
content.slice(valueStart, end)
|
|
450
|
+
]));
|
|
451
|
+
};
|
|
452
|
+
const setWireBranches = (source, wireName, environments, branchWire)=>{
|
|
453
|
+
const { openIndex, closeIndex } = branchesRange(source, wireName);
|
|
454
|
+
let content = source.slice(openIndex + 1, closeIndex);
|
|
455
|
+
const items = branchItems(content);
|
|
456
|
+
for (const { key, valueStart, end, shorthand } of [
|
|
457
|
+
...items
|
|
458
|
+
].reverse())if (environments.includes(key)) content = content.slice(0, valueStart) + (shorthand ? `${key}: ${branchWire}` : branchWire) + content.slice(end);
|
|
459
|
+
const missing = environments.filter((environment)=>!items.some((x)=>x.key === environment));
|
|
460
|
+
if (missing.length) content = appendItems(content, missing.map((environment)=>`${environment}: ${branchWire}`));
|
|
461
|
+
return source.slice(0, openIndex + 1) + content + source.slice(closeIndex);
|
|
462
|
+
};
|
|
463
|
+
const addBranchedImplementation = (source, wireName, implementation)=>{
|
|
464
|
+
const withImport = addImport(source, implementation.importLine);
|
|
465
|
+
const { declarationIndex } = branchesRange(withImport, wireName);
|
|
466
|
+
const withWireClass = `${withImport.slice(0, declarationIndex)}const ${implementation.wireName} = wireClass(${implementation.className}, [])\n\n${withImport.slice(declarationIndex)}`;
|
|
467
|
+
return setWireBranches(withWireClass, wireName, implementation.environments, implementation.wireName);
|
|
664
468
|
};
|
|
665
469
|
const stripJsonComments = (source)=>source.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, text)=>text ? text : '').replace(/,(\s*[}\]])/g, '$1');
|
|
666
470
|
const resolveExtends = (fromFile, target)=>{
|
|
@@ -695,4 +499,4 @@ const compilerOption = (configFile, name, seen = new Set())=>{
|
|
|
695
499
|
if (void 0 !== value) return value;
|
|
696
500
|
}
|
|
697
501
|
};
|
|
698
|
-
export {
|
|
502
|
+
export { UnknownFormatNaming, addBranchedImplementation, addClassMember, addConstructorParameters, addImport, addWireDependencies, availableDependencies, boundedContextLocation, canDependOn, compilerOption, crossContextImplementation, crossContextImportAllowed, findWiredModules, hasNeighboringBoundedContext, implementationType, isCrossContextImplementation, isSuggestedFor, moduleKindOf, namedImportFiles, readWiredModule, relativeImport, stripJsonComments, wireBranches, wireDependencies, wiredDependencyNames };
|