@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.
Files changed (71) hide show
  1. package/README.md +128 -25
  2. package/dist/201.js +146 -342
  3. package/dist/287.js +1304 -568
  4. package/dist/561.js +518 -0
  5. package/dist/711.js +22 -0
  6. package/dist/870.js +438 -0
  7. package/dist/agent-cli.d.ts +378 -0
  8. package/dist/agent-cli.spec.d.ts +1 -0
  9. package/dist/agent-identifiers.d.ts +4 -0
  10. package/dist/agent-requests.d.ts +488 -0
  11. package/dist/backend-module-move.d.ts +1 -0
  12. package/dist/config-file.d.ts +231 -26
  13. package/dist/config-file.spec.d.ts +1 -0
  14. package/dist/context-dependencies.d.ts +16 -0
  15. package/dist/dependency-rules.d.ts +17 -17
  16. package/dist/file-system.d.ts +10 -1
  17. package/dist/implementation-choices.d.ts +8 -0
  18. package/dist/implementation-choices.spec.d.ts +1 -0
  19. package/dist/implementation-options-schema.d.ts +14 -0
  20. package/dist/implementations.d.ts +48 -0
  21. package/dist/index.cjs +3768 -574
  22. package/dist/index.js +25 -9
  23. package/dist/init.cjs +717 -122
  24. package/dist/init.js +24 -12
  25. package/dist/integration.cjs +3737 -186
  26. package/dist/integration.js +29 -17
  27. package/dist/integrations/rest-endpoint-generator.d.ts +4 -2
  28. package/dist/integrations/storybook.d.ts +1 -0
  29. package/dist/interaction.d.ts +41 -0
  30. package/dist/lint.cjs +173 -46
  31. package/dist/lint.js +22 -7
  32. package/dist/next-app.d.ts +7 -0
  33. package/dist/react.cjs +5128 -414
  34. package/dist/react.js +72 -60
  35. package/dist/system-implementations.d.ts +6 -0
  36. package/dist/upgrade-steps.d.ts +81 -0
  37. package/dist/upgrade-steps.spec.d.ts +1 -0
  38. package/dist/upgrade.cjs +593 -0
  39. package/dist/upgrade.d.ts +2 -0
  40. package/dist/upgrade.js +235 -0
  41. package/dist/use-cases/add-integration.d.ts +16 -1
  42. package/dist/use-cases/cross-context-dependencies.spec.d.ts +1 -0
  43. package/dist/use-cases/index.d.ts +1 -0
  44. package/dist/use-cases/initialize.d.ts +4 -4
  45. package/dist/use-cases/initialize.spec.d.ts +1 -0
  46. package/dist/use-cases/prompt-implementation-type.d.ts +20 -0
  47. package/dist/use-cases/scaffold-new-aggregate.d.ts +1 -1
  48. package/dist/use-cases/scaffold-new-implementation.d.ts +6 -0
  49. package/dist/use-cases/scaffold-new-implementation.spec.d.ts +1 -0
  50. package/dist/use-cases/scaffold-react-page.d.ts +3 -1
  51. package/dist/use-cases/wire-dependency.d.ts +5 -0
  52. package/dist/wire-branches.d.ts +26 -0
  53. package/dist/wiring.d.ts +24 -2
  54. package/package.json +3 -2
  55. package/skills/df-ps-implement/SKILL.md +9 -8
  56. package/skills/df-ps-plan-feature/SKILL.md +8 -7
  57. package/skills/df-ps-react/SKILL.md +13 -7
  58. package/skills/df-ps-react/references/generators.md +6 -2
  59. package/skills/df-ps-scaffold/SKILL.md +31 -19
  60. package/skills/df-ps-scaffold/references/agent-mode.md +72 -0
  61. package/skills/df-ps-scaffold/references/agent-operations.md +155 -0
  62. package/skills/df-ps-scaffold/references/choosing-components.md +14 -13
  63. package/skills/df-ps-scaffold/references/generators.md +20 -3
  64. package/skills/df-ps-scaffold/references/units/aggregate.md +4 -3
  65. package/skills/df-ps-scaffold/references/units/bounded-context.md +1 -1
  66. package/skills/df-ps-scaffold/references/units/command-query.md +12 -12
  67. package/skills/df-ps-scaffold/references/units/implementation.md +20 -0
  68. package/skills/df-ps-scaffold/references/units/move-or-rename.md +1 -1
  69. package/skills/df-ps-scaffold/references/units/port.md +7 -4
  70. package/skills/df-ps-scaffold/references/units/use-case.md +1 -1
  71. package/skills/df-ps-scaffold/scripts/drive.mjs +9 -1
package/dist/870.js ADDED
@@ -0,0 +1,438 @@
1
+ import zod from "zod";
2
+ import node_path from "node:path";
3
+ import node_fs from "node:fs";
4
+ const implementationKinds = [
5
+ 'repositories',
6
+ 'commands',
7
+ 'queries',
8
+ 'ports'
9
+ ];
10
+ const implementationLabelSchema = zod.string().regex(/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/);
11
+ const implementationOptionsSchema = zod.partialRecord(zod["enum"](implementationKinds), zod.object({
12
+ primary: zod.array(implementationLabelSchema).default([]),
13
+ test: zod.array(implementationLabelSchema).default([])
14
+ }).strict());
15
+ const moduleKinds = [
16
+ 'repositories',
17
+ 'domain-services',
18
+ 'application-ports',
19
+ 'application-services',
20
+ 'use-cases',
21
+ 'commands',
22
+ 'queries',
23
+ 'infrastructure-services',
24
+ 'presentation-services'
25
+ ];
26
+ const domain = [
27
+ 'repositories',
28
+ 'domain-services'
29
+ ];
30
+ const commandsAndQueries = [
31
+ 'commands',
32
+ 'queries'
33
+ ];
34
+ const infrastructure = [
35
+ ...domain,
36
+ 'application-ports',
37
+ 'infrastructure-services'
38
+ ];
39
+ const defaultDependencies = {
40
+ 'domain-services': [
41
+ 'domain-services'
42
+ ],
43
+ repositories: [
44
+ 'domain-services',
45
+ 'application-ports',
46
+ 'infrastructure-services'
47
+ ],
48
+ 'application-ports': infrastructure,
49
+ 'application-services': [
50
+ ...domain,
51
+ 'application-ports',
52
+ ...commandsAndQueries
53
+ ],
54
+ 'use-cases': [
55
+ ...domain,
56
+ 'application-ports',
57
+ 'application-services',
58
+ ...commandsAndQueries
59
+ ],
60
+ commands: [
61
+ 'infrastructure-services'
62
+ ],
63
+ queries: [
64
+ 'infrastructure-services'
65
+ ],
66
+ 'infrastructure-services': infrastructure,
67
+ 'presentation-services': [
68
+ 'application-ports',
69
+ 'application-services',
70
+ 'use-cases',
71
+ ...commandsAndQueries,
72
+ 'presentation-services'
73
+ ]
74
+ };
75
+ const kindSchema = zod["enum"](moduleKinds);
76
+ const dependencyRulesSchema = zod.partialRecord(kindSchema, zod.union([
77
+ zod.array(kindSchema),
78
+ zod.object({
79
+ add: zod.array(kindSchema).optional(),
80
+ remove: zod.array(kindSchema).optional()
81
+ })
82
+ ]));
83
+ const resolveDependencyRules = (config = {})=>{
84
+ const entries = moduleKinds.map((kind)=>{
85
+ const override = config[kind];
86
+ if (Array.isArray(override)) return [
87
+ kind,
88
+ new Set(override)
89
+ ];
90
+ const kinds = new Set([
91
+ ...defaultDependencies[kind],
92
+ ...override?.add ?? []
93
+ ]);
94
+ for (const removed of override?.remove ?? [])kinds.delete(removed);
95
+ return [
96
+ kind,
97
+ kinds
98
+ ];
99
+ });
100
+ return Object.fromEntries(entries);
101
+ };
102
+ const defaultDependencyRules = resolveDependencyRules();
103
+ const isModuleKind = (value)=>moduleKinds.includes(value);
104
+ const wiredModuleKind = (wiringKind)=>isModuleKind(wiringKind) ? wiringKind : void 0;
105
+ const fileModuleKind = (segments)=>{
106
+ const [layer, folder] = segments;
107
+ const file = segments.at(-1) ?? '';
108
+ if (/^index(\.tsx?)?$/.test(file)) return;
109
+ if ('domain' === layer) {
110
+ if ('services' === folder) return 'domain-services';
111
+ return 'aggregates' === folder && /-repository(\.tsx?)?$/.test(file) ? 'repositories' : void 0;
112
+ }
113
+ if ('application' === layer) return ({
114
+ commands: 'commands',
115
+ queries: 'queries',
116
+ ports: 'application-ports',
117
+ services: 'application-services',
118
+ 'use-cases': 'use-cases'
119
+ })[folder];
120
+ if ('infrastructure' === layer) return ({
121
+ repositories: 'repositories',
122
+ commands: 'commands',
123
+ queries: 'queries',
124
+ 'application-adapters': 'application-ports',
125
+ services: 'infrastructure-services'
126
+ })[folder];
127
+ return 'presentation' === layer && 'services' === folder ? 'presentation-services' : void 0;
128
+ };
129
+ const pendingFiles = new Map();
130
+ const pendingFolders = new Set();
131
+ const pendingMoves = new Map();
132
+ const pendingDeletions = new Map();
133
+ const filesToOverwrite = ({ changedOnly = false } = {})=>[
134
+ ...pendingFiles
135
+ ].filter(([filePath, { replacesFile, content }])=>replacesFile && node_fs.existsSync(filePath) && (!changedOnly || node_fs.readFileSync(filePath, 'utf8') !== content)).map(([filePath])=>filePath);
136
+ const pendingChanges = ()=>({
137
+ created: [
138
+ ...new Set([
139
+ ...[
140
+ ...pendingFiles.keys()
141
+ ].filter((file)=>!node_fs.existsSync(file)),
142
+ ...pendingMoves.keys()
143
+ ])
144
+ ].sort(),
145
+ updated: [
146
+ ...pendingFiles
147
+ ].filter(([file, change])=>node_fs.existsSync(file) && node_fs.readFileSync(file, 'utf8') !== change.content).map(([file])=>file).sort(),
148
+ deleted: [
149
+ ...new Set([
150
+ ...pendingDeletions.keys(),
151
+ ...[
152
+ ...pendingMoves.values()
153
+ ].map((move)=>move.sourcePath)
154
+ ])
155
+ ].sort(),
156
+ directories: [
157
+ ...pendingFolders
158
+ ].filter((folder)=>!node_fs.existsSync(folder)).sort()
159
+ });
160
+ const withoutOverwrites = (generate)=>{
161
+ const before = new Map(pendingFiles);
162
+ const result = generate();
163
+ for (const [filePath, change] of pendingFiles){
164
+ if (before.get(filePath) === change || !change.replacesFile || !node_fs.existsSync(filePath)) continue;
165
+ const previous = before.get(filePath);
166
+ if (previous) pendingFiles.set(filePath, previous);
167
+ else pendingFiles.delete(filePath);
168
+ }
169
+ return result;
170
+ };
171
+ const discardPendingChanges = ()=>{
172
+ pendingFolders.clear();
173
+ pendingFiles.clear();
174
+ pendingMoves.clear();
175
+ pendingDeletions.clear();
176
+ };
177
+ const removeEmptyFolders = (folderPath, stopAt)=>{
178
+ let current = folderPath;
179
+ while(current.startsWith(`${stopAt}${node_path.sep}`) && node_fs.existsSync(current) && !node_fs.readdirSync(current).length){
180
+ node_fs.rmdirSync(current);
181
+ current = node_path.dirname(current);
182
+ }
183
+ };
184
+ const writePendingChanges = ()=>{
185
+ for (const folderPath of pendingFolders)node_fs.mkdirSync(folderPath, {
186
+ recursive: true
187
+ });
188
+ for (const [filePath, { content }] of pendingFiles){
189
+ node_fs.mkdirSync(node_path.dirname(filePath), {
190
+ recursive: true
191
+ });
192
+ node_fs.writeFileSync(filePath, content, 'utf8');
193
+ }
194
+ for (const [targetPath, { sourcePath }] of pendingMoves){
195
+ node_fs.mkdirSync(node_path.dirname(targetPath), {
196
+ recursive: true
197
+ });
198
+ node_fs.renameSync(sourcePath, targetPath);
199
+ }
200
+ for (const filePath of pendingDeletions.keys())node_fs.rmSync(filePath, {
201
+ force: true
202
+ });
203
+ for (const [filePath, stopAt] of pendingDeletions)removeEmptyFolders(node_path.dirname(filePath), stopAt);
204
+ for (const { sourcePath, stopAt } of pendingMoves.values())removeEmptyFolders(node_path.dirname(sourcePath), stopAt);
205
+ discardPendingChanges();
206
+ };
207
+ class FileSystemItem {
208
+ path;
209
+ constructor(path){
210
+ this.path = path;
211
+ }
212
+ subitem = (relativePath)=>new this.constructor(node_path.resolve(this.path, ...relativePath));
213
+ get exists() {
214
+ return node_fs.existsSync(this.path);
215
+ }
216
+ }
217
+ class Folder extends FileSystemItem {
218
+ createIfNotExisted = ()=>{
219
+ pendingFolders.add(this.path);
220
+ };
221
+ get content() {
222
+ if (!this.exists) return {
223
+ subfolderNames: [],
224
+ fileNames: []
225
+ };
226
+ const content = node_fs.readdirSync(this.path, {
227
+ withFileTypes: true
228
+ });
229
+ return {
230
+ subfolderNames: content.filter((x)=>x.isDirectory()).map((x)=>x.name),
231
+ fileNames: content.filter((x)=>!x.isDirectory()).map((x)=>node_path.parse(x.name).name)
232
+ };
233
+ }
234
+ get name() {
235
+ return node_path.basename(this.path);
236
+ }
237
+ createFile = (name, content)=>{
238
+ this.createIfNotExisted();
239
+ const file = new File(this.path).subitem([
240
+ name
241
+ ]);
242
+ file.data = content;
243
+ return file;
244
+ };
245
+ file = (name)=>{
246
+ this.createIfNotExisted();
247
+ const file = new File(this.path).subitem([
248
+ name
249
+ ]);
250
+ return file;
251
+ };
252
+ }
253
+ class File extends FileSystemItem {
254
+ get exists() {
255
+ return pendingFiles.has(this.path) || pendingMoves.has(this.path) || node_fs.existsSync(this.path) && !pendingDeletions.has(this.path);
256
+ }
257
+ delete = (stopAt)=>{
258
+ pendingDeletions.set(this.path, stopAt);
259
+ };
260
+ moveTo = (targetPath, stopAt)=>{
261
+ pendingMoves.set(targetPath, {
262
+ sourcePath: this.path,
263
+ stopAt
264
+ });
265
+ };
266
+ get data() {
267
+ const pendingFile = pendingFiles.get(this.path);
268
+ if (pendingFile) return pendingFile.content;
269
+ if (!this.exists) throw new Error('File does not exist');
270
+ return node_fs.readFileSync(this.path, 'utf-8');
271
+ }
272
+ set data(content) {
273
+ pendingFiles.set(this.path, {
274
+ content,
275
+ replacesFile: true
276
+ });
277
+ }
278
+ update = (content)=>{
279
+ pendingFiles.set(this.path, {
280
+ content,
281
+ replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
282
+ });
283
+ };
284
+ addLine = (newLine, separator = '\n')=>{
285
+ const content = this.exists ? this.data : '';
286
+ pendingFiles.set(this.path, {
287
+ content: [
288
+ content.trim(),
289
+ newLine.trim()
290
+ ].filter((x)=>!!x).join(separator),
291
+ replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
292
+ });
293
+ };
294
+ addLineIfMissing = (newLine, separator = '\n')=>{
295
+ const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
296
+ if (!alreadyAdded) this.addLine(newLine, separator);
297
+ };
298
+ }
299
+ class ProjectRoot {
300
+ path;
301
+ constructor(){
302
+ let dir = process.cwd();
303
+ while(dir !== node_path.parse(dir).root){
304
+ if (node_fs.existsSync(node_path.join(dir, 'package.json'))) {
305
+ this.path = dir;
306
+ return;
307
+ }
308
+ dir = node_path.dirname(dir);
309
+ }
310
+ throw new Error('No package.json was found');
311
+ }
312
+ }
313
+ const lintChecks = [
314
+ 'context-isolation',
315
+ 'shared-kernel',
316
+ 'layers',
317
+ 'dependencies',
318
+ 'react',
319
+ 'clock'
320
+ ];
321
+ const configFileName = 'domain-first.project-structure.config.json';
322
+ const projectConfigSchema = zod.object({
323
+ rootFolder: zod.string().min(1)
324
+ }).passthrough();
325
+ const domainFirstPackages = [
326
+ '@domain-first/types',
327
+ '@domain-first/errors',
328
+ '@domain-first/handlers',
329
+ '@domain-first/wire',
330
+ '@domain-first/handlers-rest'
331
+ ];
332
+ const rulesSchema = zod.object({
333
+ dependencies: dependencyRulesSchema.optional(),
334
+ lint: zod.partialRecord(zod["enum"](lintChecks), zod.boolean()).optional()
335
+ });
336
+ const branchNameSchema = zod.string().regex(/^[a-z][a-z0-9]*$/, 'Use lowercase letters and digits, e.g. staging.');
337
+ const envBranchesSchema = zod.object({
338
+ names: zod.array(branchNameSchema).min(1),
339
+ test: branchNameSchema.optional()
340
+ }).refine(({ names })=>new Set(names).size === names.length, {
341
+ message: 'Branch names must be unique.'
342
+ }).refine(({ names, test })=>void 0 === test || names.includes(test), {
343
+ message: 'The test branch must be one of the branches.'
344
+ }).refine(({ names, test })=>void 0 === test || names.length > 1, {
345
+ message: 'The primary implementation needs a branch besides the test one.'
346
+ });
347
+ const backendConfigSchema = zod.object({
348
+ domainFirstPackages: zod.array(zod["enum"](domainFirstPackages)),
349
+ envBranches: envBranchesSchema.optional(),
350
+ withInMemoryDb: zod.boolean().optional(),
351
+ testingLibrary: zod.string().optional(),
352
+ implementations: implementationOptionsSchema.optional(),
353
+ rules: rulesSchema.optional()
354
+ }).passthrough();
355
+ const reactConfigSchema = zod.object({
356
+ framework: zod["enum"]([
357
+ 'react',
358
+ 'next'
359
+ ]).optional(),
360
+ storybookFramework: zod.string().optional()
361
+ }).passthrough();
362
+ const frontendConfigSchema = zod.object({
363
+ react: reactConfigSchema.optional()
364
+ }).passthrough();
365
+ const configSchema = zod.object({
366
+ project: projectConfigSchema,
367
+ backend: backendConfigSchema.optional(),
368
+ frontend: frontendConfigSchema.optional()
369
+ }).passthrough();
370
+ const scaffoldBackendRules = (rules = {})=>({
371
+ dependencies: {
372
+ ...Object.fromEntries(Object.entries(defaultDependencies).map(([kind, dependencies])=>[
373
+ kind,
374
+ [
375
+ ...dependencies
376
+ ]
377
+ ])),
378
+ ...rules.dependencies
379
+ },
380
+ lint: {
381
+ ...Object.fromEntries(lintChecks.map((check)=>[
382
+ check,
383
+ true
384
+ ])),
385
+ ...rules.lint
386
+ }
387
+ });
388
+ const isOutdatedConfig = (value)=>'object' == typeof value && null !== value && !Array.isArray(value) && !('project' in value);
389
+ const outdatedConfigMessage = `${configFileName} uses the format of an earlier major version: run npx df-upgrade.`;
390
+ class ConfigOutdatedError extends Error {
391
+ constructor(){
392
+ super(outdatedConfigMessage);
393
+ }
394
+ }
395
+ class ConfigFile {
396
+ data;
397
+ constructor(){
398
+ const raw = JSON.parse(new File(ConfigFile.path).data);
399
+ if (isOutdatedConfig(raw)) throw new ConfigOutdatedError();
400
+ this.data = configSchema.parse(raw);
401
+ }
402
+ static get Instance() {
403
+ return new ConfigFile();
404
+ }
405
+ static get backend() {
406
+ const backend = ConfigFile.Instance.data.backend;
407
+ if (!backend) throw new Error('Backend is not configured: run df-init --section backend.');
408
+ return backend;
409
+ }
410
+ static get path() {
411
+ return node_path.resolve(new ProjectRoot().path, configFileName);
412
+ }
413
+ static get exists() {
414
+ return new File(ConfigFile.path).exists;
415
+ }
416
+ static get outdated() {
417
+ if (!ConfigFile.exists) return false;
418
+ try {
419
+ return isOutdatedConfig(JSON.parse(new File(ConfigFile.path).data));
420
+ } catch {
421
+ return false;
422
+ }
423
+ }
424
+ static write = (config)=>{
425
+ ConfigFile.stage(configSchema.parse(config));
426
+ };
427
+ static update = (sections)=>{
428
+ ConfigFile.stage(configSchema.parse({
429
+ ...ConfigFile.Instance.data,
430
+ ...sections
431
+ }));
432
+ };
433
+ static stage(config) {
434
+ new File(ConfigFile.path).update(`${JSON.stringify(config, null, 4)}\n`);
435
+ }
436
+ }
437
+ const configuredDependencyRules = ()=>resolveDependencyRules(ConfigFile.exists ? ConfigFile.Instance.data.backend?.rules?.dependencies : void 0);
438
+ export { ConfigFile, File, Folder, ProjectRoot, branchNameSchema, configFileName, configSchema, configuredDependencyRules, defaultDependencyRules, discardPendingChanges, domainFirstPackages, fileModuleKind, filesToOverwrite, implementationKinds, implementationLabelSchema, isOutdatedConfig, outdatedConfigMessage, pendingChanges, rulesSchema, scaffoldBackendRules, wiredModuleKind, withoutOverwrites, writePendingChanges };