@domain-first/project-structure 6.0.0 → 7.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 +20 -19
- package/dist/201.js +46 -35
- package/dist/287.js +1762 -1250
- package/dist/backend-module-move.d.ts +53 -0
- package/dist/backend-module-move.spec.d.ts +1 -0
- package/dist/config-file.d.ts +2 -2
- package/dist/dependency-rules.d.ts +1 -1
- package/dist/file-system.d.ts +5 -0
- package/dist/in-memory-db.d.ts +9 -0
- package/dist/index.cjs +534 -804
- package/dist/index.js +9 -47
- package/dist/init.cjs +913 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.js +36 -0
- package/dist/integration.cjs +457 -33
- package/dist/integration.js +4 -4
- package/dist/integrations/in-memory-db.d.ts +6 -0
- package/dist/integrations/index.d.ts +2 -0
- package/dist/integrations/missing-domain-first-packages.d.ts +8 -0
- package/dist/integrations/rest-endpoint-generator.d.ts +18 -0
- package/dist/integrations/rest-endpoint-generator.spec.d.ts +1 -0
- package/dist/lint.cjs +8 -8
- package/dist/lint.js +7 -7
- package/dist/module-move.d.ts +58 -0
- package/dist/print-next-steps.d.ts +2 -2
- package/dist/react-module-move.d.ts +2 -6
- package/dist/react.cjs +986 -0
- package/dist/react.d.ts +2 -0
- package/dist/react.js +73 -0
- package/dist/use-cases/index.d.ts +1 -0
- package/dist/use-cases/move-backend-unit.d.ts +7 -0
- package/dist/use-cases/rest-endpoint-generator.d.ts +1 -1
- package/package.json +3 -1
- package/skills/df-ps-implement/SKILL.md +11 -56
- package/skills/df-ps-plan-feature/SKILL.md +6 -6
- package/skills/df-ps-project-scripts/SKILL.md +1 -1
- package/skills/df-ps-react/SKILL.md +82 -0
- package/skills/df-ps-react/references/generators.md +80 -0
- package/skills/df-ps-scaffold/SKILL.md +39 -20
- package/skills/df-ps-scaffold/references/choosing-components.md +7 -28
- package/skills/df-ps-scaffold/references/generators.md +6 -179
- package/skills/df-ps-scaffold/references/units/aggregate.md +47 -0
- package/skills/df-ps-scaffold/references/units/bounded-context.md +13 -0
- package/skills/df-ps-scaffold/references/units/command-query.md +32 -0
- package/skills/df-ps-scaffold/references/units/endpoint.md +28 -0
- package/skills/df-ps-scaffold/references/units/entity-value-object.md +27 -0
- package/skills/df-ps-scaffold/references/units/errors.md +26 -0
- package/skills/df-ps-scaffold/references/units/move-or-rename.md +21 -0
- package/skills/df-ps-scaffold/references/units/port.md +23 -0
- package/skills/df-ps-scaffold/references/units/services.md +28 -0
- package/skills/df-ps-scaffold/references/units/use-case.md +25 -0
- package/skills/df-ps-scaffold/references/units/wire-dependency.md +15 -0
- package/skills/df-ps-scaffold/scripts/drive.mjs +12 -4
package/dist/init.d.ts
ADDED
package/dist/init.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { ConfigFile } from "./201.js";
|
|
4
|
+
import { printNextSteps, initializeUseCase, savePendingChanges } from "./287.js";
|
|
5
|
+
const help = `Usage: df-init
|
|
6
|
+
|
|
7
|
+
Initializes a Domain-First project: asks for the source folder, the
|
|
8
|
+
@domain-first packages, the default persistence implementation, the shared
|
|
9
|
+
in-memory DB, React, Storybook and the testing library, writes
|
|
10
|
+
domain-first.project-structure.config.json and scaffolds the Shared Kernel
|
|
11
|
+
files they need. Prints the install command; nothing is installed.
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
-h, --help show this help`;
|
|
15
|
+
const { values: values } = parseArgs({
|
|
16
|
+
options: {
|
|
17
|
+
help: {
|
|
18
|
+
type: 'boolean',
|
|
19
|
+
short: 'h',
|
|
20
|
+
default: false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
if (values.help) {
|
|
25
|
+
console.log(help);
|
|
26
|
+
process.exit(0);
|
|
27
|
+
}
|
|
28
|
+
if (ConfigFile.exists) {
|
|
29
|
+
console.log('domain-first.project-structure.config.json already exists: edit it or add Shared Kernel files with df-integration.');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
const run = async ()=>{
|
|
33
|
+
const nextSteps = await initializeUseCase();
|
|
34
|
+
if (await savePendingChanges()) printNextSteps(nextSteps);
|
|
35
|
+
};
|
|
36
|
+
run();
|
package/dist/integration.cjs
CHANGED
|
@@ -128,6 +128,17 @@ const pendingDeletions = new Map();
|
|
|
128
128
|
const filesToOverwrite = ()=>[
|
|
129
129
|
...pendingFiles
|
|
130
130
|
].filter(([filePath, { replacesFile }])=>replacesFile && external_node_fs_default().existsSync(filePath)).map(([filePath])=>filePath);
|
|
131
|
+
const withoutOverwrites = (generate)=>{
|
|
132
|
+
const before = new Map(pendingFiles);
|
|
133
|
+
const result = generate();
|
|
134
|
+
for (const [filePath, change] of pendingFiles){
|
|
135
|
+
if (before.get(filePath) === change || !change.replacesFile || !external_node_fs_default().existsSync(filePath)) continue;
|
|
136
|
+
const previous = before.get(filePath);
|
|
137
|
+
if (previous) pendingFiles.set(filePath, previous);
|
|
138
|
+
else pendingFiles.delete(filePath);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
};
|
|
131
142
|
const file_system_discardPendingChanges = ()=>{
|
|
132
143
|
pendingFolders.clear();
|
|
133
144
|
pendingFiles.clear();
|
|
@@ -272,7 +283,7 @@ class project_root_ProjectRoot {
|
|
|
272
283
|
}
|
|
273
284
|
const lintChecks = [
|
|
274
285
|
'context-isolation',
|
|
275
|
-
'shared-
|
|
286
|
+
'shared-kernel',
|
|
276
287
|
'layers',
|
|
277
288
|
'dependencies',
|
|
278
289
|
'react',
|
|
@@ -321,17 +332,198 @@ class config_file_ConfigFile {
|
|
|
321
332
|
};
|
|
322
333
|
}
|
|
323
334
|
const prompts_namespaceObject = require("@inquirer/prompts");
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
335
|
+
const wiring_relativeImport = (fromFile, toFile)=>{
|
|
336
|
+
const relativePath = external_node_path_default().relative(external_node_path_default().dirname(fromFile), toFile).replace(/\.ts$/, '').split(external_node_path_default().sep).join('/');
|
|
337
|
+
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
338
|
+
};
|
|
339
|
+
const inMemoryDbService = `type Entry = {
|
|
340
|
+
id: unknown;
|
|
341
|
+
item: any;
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
declare global {
|
|
345
|
+
var ___SHARED_IN_MEMORY_DB___: Record<string, Array<Entry>> | undefined;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
type Section<T, TId> = {
|
|
349
|
+
all: (predicate?: (x: T) => boolean) => T[];
|
|
350
|
+
first: (predicate: (x: T) => boolean) => T | null;
|
|
351
|
+
withId: (id: TId) => T | null;
|
|
352
|
+
lastAddedItem: () => T | null;
|
|
353
|
+
save: (payload: T extends { id: TId } ? T : [TId, T]) => void;
|
|
354
|
+
deleteById: (id: TId) => void;
|
|
355
|
+
delete: (predicate: (x: T) => boolean) => void;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
type IdsAreEqual<TId> = (a: TId, b: TId) => boolean;
|
|
359
|
+
|
|
360
|
+
const comparisonMethodNames = ['equals', 'same', 'isSame', 'isEqual'] as const;
|
|
361
|
+
|
|
362
|
+
const defaultIdsAreEqual = (a: unknown, b: unknown): boolean => {
|
|
363
|
+
if (typeof a !== 'object' || a === null) {
|
|
364
|
+
return Object.is(a, b);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
for (const methodName of comparisonMethodNames) {
|
|
368
|
+
const method: unknown = (a as Record<string, unknown>)[methodName];
|
|
369
|
+
if (typeof method === 'function') {
|
|
370
|
+
return Boolean(method.call(a, b));
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return Object.is(a, b);
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const hasId = (x: unknown): x is { id: unknown } =>
|
|
378
|
+
typeof x === 'object' && x !== null && 'id' in x;
|
|
379
|
+
|
|
380
|
+
export class InMemoryDbInfrastructureService {
|
|
381
|
+
protected section<T extends { id: unknown }>(
|
|
382
|
+
key: string,
|
|
383
|
+
idsAreEqual?: IdsAreEqual<T['id']>
|
|
384
|
+
): Section<T, T['id']>;
|
|
385
|
+
protected section<T, TId>(
|
|
386
|
+
key: string,
|
|
387
|
+
idsAreEqual?: IdsAreEqual<TId>
|
|
388
|
+
): Section<T, TId>;
|
|
389
|
+
protected section<T, TId>(
|
|
390
|
+
key: string,
|
|
391
|
+
idsAreEqual: IdsAreEqual<TId> = defaultIdsAreEqual
|
|
392
|
+
): Section<T, TId> {
|
|
393
|
+
if (!globalThis.___SHARED_IN_MEMORY_DB___) {
|
|
394
|
+
globalThis.___SHARED_IN_MEMORY_DB___ = {};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (!globalThis.___SHARED_IN_MEMORY_DB___[key]) {
|
|
398
|
+
globalThis.___SHARED_IN_MEMORY_DB___[key] = [];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const items = (): T[] =>
|
|
402
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key].map(
|
|
403
|
+
(entry) => entry.item
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
return {
|
|
407
|
+
all: (predicate: (x: T) => boolean = () => true): T[] =>
|
|
408
|
+
items().filter(predicate),
|
|
409
|
+
first: (predicate: (x: T) => boolean): T | null =>
|
|
410
|
+
items().find(predicate) ?? null,
|
|
411
|
+
withId: (id: TId): T | null =>
|
|
412
|
+
items().find(
|
|
413
|
+
(x: T) => hasId(x) && idsAreEqual(x.id as TId, id)
|
|
414
|
+
) ?? null,
|
|
415
|
+
lastAddedItem: (): T | null =>
|
|
416
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key].at(-1)?.item ?? null,
|
|
417
|
+
save: (payload: T extends { id: TId } ? T : [TId, T]) => {
|
|
418
|
+
const id = Array.isArray(payload) ? payload[0] : payload.id;
|
|
419
|
+
const item = Array.isArray(payload) ? payload[1] : payload;
|
|
420
|
+
const entries = globalThis.___SHARED_IN_MEMORY_DB___![key];
|
|
421
|
+
const existing = entries.find((entry) =>
|
|
422
|
+
idsAreEqual(entry.id as TId, id)
|
|
423
|
+
);
|
|
424
|
+
if (existing) {
|
|
425
|
+
existing.item = item;
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
entries.push({ id, item });
|
|
429
|
+
},
|
|
430
|
+
deleteById: (id: TId) => {
|
|
431
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key] =
|
|
432
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key].filter(
|
|
433
|
+
(entry) => !idsAreEqual(entry.id as TId, id)
|
|
434
|
+
);
|
|
435
|
+
},
|
|
436
|
+
delete: (predicate: (x: T) => boolean) => {
|
|
437
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key] =
|
|
438
|
+
globalThis.___SHARED_IN_MEMORY_DB___![key].filter(
|
|
439
|
+
(entry) => !predicate(entry.item)
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
`;
|
|
446
|
+
const inMemoryDbWiring = `import { wireClass } from '@domain-first/wire';
|
|
447
|
+
import { InMemoryDbInfrastructureService } from '../../infrastructure/services/in-memory-db-infrastructure-service';
|
|
448
|
+
|
|
449
|
+
export const wireInMemoryDbInfrastructureService = wireClass(
|
|
450
|
+
InMemoryDbInfrastructureService,
|
|
451
|
+
[]
|
|
452
|
+
);
|
|
453
|
+
`;
|
|
454
|
+
const inMemoryDbSpec = (testingLibrary, withWiring)=>`
|
|
455
|
+
import { describe, test, expect, beforeEach } from '${testingLibrary}'
|
|
456
|
+
${withWiring ? `import { type InMemoryDbInfrastructureService } from './in-memory-db-infrastructure-service'
|
|
457
|
+
import { wireInMemoryDbInfrastructureService } from '../../wiring/infrastructure-services/wire-in-memory-db-infrastructure-service'` : "import { InMemoryDbInfrastructureService } from './in-memory-db-infrastructure-service'"}
|
|
458
|
+
|
|
459
|
+
let inMemoryDbInfrastructureService: InMemoryDbInfrastructureService
|
|
460
|
+
|
|
461
|
+
beforeEach(() => {
|
|
462
|
+
inMemoryDbInfrastructureService = ${withWiring ? 'wireInMemoryDbInfrastructureService()' : 'new InMemoryDbInfrastructureService()'}
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
describe('in memory db', () => {
|
|
466
|
+
test('can be ${withWiring ? 'wired' : 'created'}', () => {
|
|
467
|
+
expect(inMemoryDbInfrastructureService).toBeDefined()
|
|
468
|
+
})
|
|
469
|
+
})
|
|
470
|
+
`.trim();
|
|
471
|
+
const in_memory_db_withWiring = ()=>config_file_ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
|
|
472
|
+
const in_memory_db_sharedFolder = ()=>new file_system_Folder(new project_root_ProjectRoot().path).subitem([
|
|
473
|
+
config_file_ConfigFile.Instance.data.rootFolder,
|
|
474
|
+
'shared-kernel'
|
|
475
|
+
]);
|
|
476
|
+
const serviceFile = ()=>new file_system_File(in_memory_db_sharedFolder().path).subitem([
|
|
477
|
+
'infrastructure',
|
|
478
|
+
'services',
|
|
479
|
+
'in-memory-db-infrastructure-service.ts'
|
|
480
|
+
]);
|
|
481
|
+
const in_memory_db_wireFile = ()=>new file_system_File(in_memory_db_sharedFolder().path).subitem([
|
|
482
|
+
'wiring',
|
|
483
|
+
'infrastructure-services',
|
|
484
|
+
'wire-in-memory-db-infrastructure-service.ts'
|
|
485
|
+
]);
|
|
486
|
+
const in_memory_db_scaffoldInMemoryDb = (shared)=>{
|
|
487
|
+
const { testingLibrary } = config_file_ConfigFile.Instance.data;
|
|
488
|
+
const servicesFolder = shared.subitem([
|
|
489
|
+
'infrastructure',
|
|
490
|
+
'services'
|
|
491
|
+
]);
|
|
492
|
+
servicesFolder.createFile('in-memory-db-infrastructure-service.ts', inMemoryDbService);
|
|
493
|
+
if (testingLibrary) servicesFolder.createFile('in-memory-db-infrastructure-service.spec.ts', inMemoryDbSpec(testingLibrary, in_memory_db_withWiring()));
|
|
494
|
+
if (in_memory_db_withWiring()) shared.subitem([
|
|
495
|
+
'wiring',
|
|
496
|
+
'infrastructure-services'
|
|
497
|
+
]).createFile('wire-in-memory-db-infrastructure-service.ts', inMemoryDbWiring);
|
|
498
|
+
};
|
|
499
|
+
const inMemoryDbScaffolded = ()=>serviceFile().exists && (!in_memory_db_withWiring() || in_memory_db_wireFile().exists);
|
|
500
|
+
const addInMemoryDb = ()=>{
|
|
501
|
+
const config = config_file_ConfigFile.Instance.data;
|
|
502
|
+
if (!config.withInMemoryDb) config_file_ConfigFile.write({
|
|
503
|
+
...config,
|
|
504
|
+
withInMemoryDb: true
|
|
505
|
+
});
|
|
506
|
+
withoutOverwrites(()=>in_memory_db_scaffoldInMemoryDb(new file_system_Folder(new project_root_ProjectRoot().path).subitem([
|
|
507
|
+
config.rootFolder,
|
|
508
|
+
'shared-kernel'
|
|
509
|
+
])));
|
|
510
|
+
return [];
|
|
511
|
+
};
|
|
512
|
+
const inMemoryDbApplied = ()=>Boolean(config_file_ConfigFile.Instance.data.withInMemoryDb) && inMemoryDbScaffolded();
|
|
513
|
+
const hasDependency = (name)=>{
|
|
514
|
+
const manifestFile = new file_system_File(external_node_path_default().join(new project_root_ProjectRoot().path, 'package.json'));
|
|
515
|
+
if (!manifestFile.exists) return false;
|
|
516
|
+
const manifest = JSON.parse(manifestFile.data);
|
|
517
|
+
return [
|
|
518
|
+
manifest.dependencies,
|
|
519
|
+
manifest.devDependencies,
|
|
520
|
+
manifest.optionalDependencies
|
|
521
|
+
].some((dependencies)=>dependencies && Object.hasOwn(dependencies, name));
|
|
522
|
+
};
|
|
523
|
+
const missingPackages = (packages)=>{
|
|
332
524
|
const missing = [
|
|
333
525
|
...new Set(packages)
|
|
334
|
-
].filter((name)=>!
|
|
526
|
+
].filter((name)=>!hasDependency(name));
|
|
335
527
|
return missing.length ? [
|
|
336
528
|
`npm i ${missing.join(' ')}`
|
|
337
529
|
] : [];
|
|
@@ -1019,7 +1211,7 @@ const developmentPackages = [
|
|
|
1019
1211
|
'@prisma/cli-engine'
|
|
1020
1212
|
];
|
|
1021
1213
|
const prismaScripts = (rootFolder)=>{
|
|
1022
|
-
const config = JSON.stringify(external_node_path_default().posix.join(rootFolder.replaceAll('\\', '/'), 'shared/infrastructure/prisma/prisma.config.ts'));
|
|
1214
|
+
const config = JSON.stringify(external_node_path_default().posix.join(rootFolder.replaceAll('\\', '/'), 'shared-kernel/infrastructure/prisma/prisma.config.ts'));
|
|
1023
1215
|
const emit = `prisma contract emit --config ${config}`;
|
|
1024
1216
|
return {
|
|
1025
1217
|
'prisma:emit': emit,
|
|
@@ -1086,14 +1278,14 @@ const addPrisma = async ()=>{
|
|
|
1086
1278
|
...manifest.dependencies,
|
|
1087
1279
|
...manifest.devDependencies
|
|
1088
1280
|
};
|
|
1089
|
-
const shared = external_node_path_default().resolve(root, config.rootFolder, 'shared');
|
|
1281
|
+
const shared = external_node_path_default().resolve(root, config.rootFolder, 'shared-kernel');
|
|
1090
1282
|
const prismaFolder = external_node_path_default().join(shared, 'infrastructure/prisma');
|
|
1091
1283
|
if (dependencies['@prisma/client'] || dependencies.prisma && !/^[~^]?8\./.test(dependencies.prisma) || dependencies['@prisma/orm-mongo'] || dependencies['@prisma/orm-sqlite'] || [
|
|
1092
1284
|
external_node_path_default().join(root, 'prisma/schema.prisma'),
|
|
1093
1285
|
external_node_path_default().join(prismaFolder, 'schema.prisma')
|
|
1094
1286
|
].some((filePath)=>new file_system_File(filePath).exists)) throw new Error('This preset adds Prisma 8 for PostgreSQL. Migrate the existing Prisma setup separately before using it.');
|
|
1095
1287
|
const withService = await (0, prompts_namespaceObject.confirm)({
|
|
1096
|
-
message: 'Create
|
|
1288
|
+
message: 'Create the Prisma infrastructure service in the Shared Kernel?',
|
|
1097
1289
|
default: true
|
|
1098
1290
|
});
|
|
1099
1291
|
const useByDefault = 'prisma' !== config.defaultPersistenceLayerImplementation && await (0, prompts_namespaceObject.confirm)({
|
|
@@ -1130,6 +1322,186 @@ const addPrisma = async ()=>{
|
|
|
1130
1322
|
})
|
|
1131
1323
|
];
|
|
1132
1324
|
};
|
|
1325
|
+
class unknown_format_naming_UnknownFormatNaming {
|
|
1326
|
+
rawSource;
|
|
1327
|
+
camelCase;
|
|
1328
|
+
PascalCase;
|
|
1329
|
+
constructor(rawSource){
|
|
1330
|
+
this.rawSource = rawSource;
|
|
1331
|
+
const source = rawSource.trim();
|
|
1332
|
+
if (!source.includes('-')) {
|
|
1333
|
+
this.camelCase = source.substring(0, 1).toLowerCase() + source.substring(1);
|
|
1334
|
+
this.PascalCase = source.substring(0, 1).toUpperCase() + source.substring(1);
|
|
1335
|
+
}
|
|
1336
|
+
const words = source.split('-').filter((word)=>!!word);
|
|
1337
|
+
this.camelCase = words.map((word, index)=>0 === index ? word.toLowerCase() : word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
1338
|
+
this.PascalCase = words.map((word)=>word[0].toUpperCase() + word.slice(1).toLowerCase()).join('');
|
|
1339
|
+
}
|
|
1340
|
+
get fileName() {
|
|
1341
|
+
return this.rawSource;
|
|
1342
|
+
}
|
|
1343
|
+
get variableName() {
|
|
1344
|
+
return this.camelCase;
|
|
1345
|
+
}
|
|
1346
|
+
get ClassName() {
|
|
1347
|
+
return this.PascalCase;
|
|
1348
|
+
}
|
|
1349
|
+
get withSpaces() {
|
|
1350
|
+
return this.fileName.replaceAll('-', ' ');
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
const createPresentationService = (boundedContextFolder, name, classFile)=>{
|
|
1354
|
+
const naming = new unknown_format_naming_UnknownFormatNaming(name);
|
|
1355
|
+
const className = `${naming.ClassName}PresentationService`;
|
|
1356
|
+
const scaffoldUnitTests = Boolean(config_file_ConfigFile.Instance.data.testingLibrary);
|
|
1357
|
+
const withWiring = config_file_ConfigFile.Instance.data.domainFirstPackages.includes('@domain-first/wire');
|
|
1358
|
+
const wiringFolder = boundedContextFolder.subitem([
|
|
1359
|
+
'wiring',
|
|
1360
|
+
'presentation-services'
|
|
1361
|
+
]);
|
|
1362
|
+
const presentationServicesFolder = boundedContextFolder.subitem([
|
|
1363
|
+
'presentation',
|
|
1364
|
+
'services'
|
|
1365
|
+
]);
|
|
1366
|
+
presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.ts`, classFile?.(className) ?? `
|
|
1367
|
+
export class ${className} {
|
|
1368
|
+
|
|
1369
|
+
}
|
|
1370
|
+
`.trim());
|
|
1371
|
+
if (scaffoldUnitTests) presentationServicesFolder.createFile(`${naming.fileName}-presentation-service.spec.ts`, `
|
|
1372
|
+
import { describe, test, expect, beforeEach } from '${config_file_ConfigFile.Instance.data.testingLibrary}'
|
|
1373
|
+
${withWiring ? `import { type ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'
|
|
1374
|
+
import { wire${naming.ClassName}PresentationService } from '../../wiring/presentation-services/wire-${naming.fileName}-presentation-service'` : `import { ${naming.ClassName}PresentationService } from './${naming.fileName}-presentation-service'`}
|
|
1375
|
+
|
|
1376
|
+
let ${naming.variableName}PresentationService: ${naming.ClassName}PresentationService
|
|
1377
|
+
|
|
1378
|
+
beforeEach(() => {
|
|
1379
|
+
${naming.variableName}PresentationService = ${withWiring ? `wire${naming.ClassName}PresentationService()` : `new ${naming.ClassName}PresentationService()`}
|
|
1380
|
+
})
|
|
1381
|
+
|
|
1382
|
+
describe('${naming.withSpaces}', () => {
|
|
1383
|
+
test('can be ${withWiring ? 'wired' : 'created'}', () => {
|
|
1384
|
+
expect(${naming.variableName}PresentationService).toBeDefined()
|
|
1385
|
+
})
|
|
1386
|
+
})
|
|
1387
|
+
`.trim());
|
|
1388
|
+
if (withWiring) return wiringFolder.createFile(`wire-${naming.fileName}-presentation-service.ts`, `
|
|
1389
|
+
import { wireClass } from '@domain-first/wire'
|
|
1390
|
+
import { ${naming.ClassName}PresentationService } from '../../presentation/services/${naming.fileName}-presentation-service'
|
|
1391
|
+
|
|
1392
|
+
export const wire${naming.ClassName}PresentationService = wireClass(
|
|
1393
|
+
${naming.ClassName}PresentationService,
|
|
1394
|
+
[]
|
|
1395
|
+
)
|
|
1396
|
+
`.trim());
|
|
1397
|
+
};
|
|
1398
|
+
const rest_endpoint_generator_name = 'rest-endpoint-generator';
|
|
1399
|
+
const scaffoldRestEndpointGenerator = (sharedFolder, { withNextAdapter = false } = {})=>createPresentationService(sharedFolder, rest_endpoint_generator_name, (className)=>{
|
|
1400
|
+
const adapter = withNextAdapter ? 'nextAdapter' : 'adapter';
|
|
1401
|
+
return `
|
|
1402
|
+
import {
|
|
1403
|
+
${withNextAdapter ? '' : ' type Adapter,\n'} type EndpointGenerator,
|
|
1404
|
+
createEndpoint,
|
|
1405
|
+
invalidRequest
|
|
1406
|
+
} from '@domain-first/handlers-rest'
|
|
1407
|
+
${withNextAdapter ? "import { nextAdapter } from '@domain-first/handlers-next'" : `
|
|
1408
|
+
/**
|
|
1409
|
+
* The adapter of your HTTP framework.
|
|
1410
|
+
*/
|
|
1411
|
+
const adapter = undefined as unknown as Adapter<any[], any>`}
|
|
1412
|
+
|
|
1413
|
+
export class ${className} {
|
|
1414
|
+
get withoutGuards(): EndpointGenerator {
|
|
1415
|
+
return createEndpoint({
|
|
1416
|
+
errors: [invalidRequest()]
|
|
1417
|
+
})(${adapter}) as EndpointGenerator
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
`.trim();
|
|
1421
|
+
});
|
|
1422
|
+
const rest_endpoint_generator_findRestEndpointGenerator = (sharedFolder)=>{
|
|
1423
|
+
const typeFile = external_node_path_default().join(sharedFolder.path, 'presentation', 'services', `${rest_endpoint_generator_name}-presentation-service.ts`);
|
|
1424
|
+
if (!new file_system_File(typeFile).exists) return;
|
|
1425
|
+
return {
|
|
1426
|
+
typeName: 'RestEndpointGeneratorPresentationService',
|
|
1427
|
+
typeFile,
|
|
1428
|
+
wireName: 'wireRestEndpointGeneratorPresentationService',
|
|
1429
|
+
wireFile: external_node_path_default().join(sharedFolder.path, 'wiring', 'presentation-services', `wire-${rest_endpoint_generator_name}-presentation-service.ts`)
|
|
1430
|
+
};
|
|
1431
|
+
};
|
|
1432
|
+
const nextAppFolder = (root)=>[
|
|
1433
|
+
'app',
|
|
1434
|
+
'src/app'
|
|
1435
|
+
].find((folder)=>new file_system_Folder(external_node_path_default().join(root, folder)).exists) ?? 'app';
|
|
1436
|
+
const nextRoutePath = (root)=>external_node_path_default().posix.join(nextAppFolder(root), 'api/[...path]/route.ts');
|
|
1437
|
+
const nextRoute = (restHandlersImport)=>`
|
|
1438
|
+
import { nextEndpoints } from '@domain-first/handlers-next'
|
|
1439
|
+
${restHandlersImport ? `import { restHandlers } from '${restHandlersImport}'
|
|
1440
|
+
|
|
1441
|
+
const handler = nextEndpoints(restHandlers)` : `
|
|
1442
|
+
/**
|
|
1443
|
+
* Pass the endpoints' handle here; their paths are relative to this route's folder.
|
|
1444
|
+
*/
|
|
1445
|
+
const handler = nextEndpoints([])`}
|
|
1446
|
+
|
|
1447
|
+
export const GET = handler
|
|
1448
|
+
export const POST = handler
|
|
1449
|
+
export const PUT = handler
|
|
1450
|
+
export const PATCH = handler
|
|
1451
|
+
export const DELETE = handler
|
|
1452
|
+
`.trim();
|
|
1453
|
+
const scaffoldNextRoute = (root)=>{
|
|
1454
|
+
const { rootFolder, domainFirstPackages } = config_file_ConfigFile.Instance.data;
|
|
1455
|
+
const routePath = nextRoutePath(root);
|
|
1456
|
+
const routeFile = external_node_path_default().join(root, routePath);
|
|
1457
|
+
if (new file_system_File(routeFile).exists) return [];
|
|
1458
|
+
if (!domainFirstPackages.includes('@domain-first/wire')) {
|
|
1459
|
+
new file_system_File(routeFile).data = nextRoute(void 0);
|
|
1460
|
+
return [];
|
|
1461
|
+
}
|
|
1462
|
+
const restFolder = new file_system_Folder(root).subitem([
|
|
1463
|
+
rootFolder,
|
|
1464
|
+
'presentation',
|
|
1465
|
+
'rest'
|
|
1466
|
+
]);
|
|
1467
|
+
new file_system_File(routeFile).data = nextRoute(wiring_relativeImport(routeFile, restFolder.path));
|
|
1468
|
+
return new file_system_File(external_node_path_default().join(restFolder.path, 'index.ts')).exists ? [] : [
|
|
1469
|
+
`${routePath} serves restHandlers from ${external_node_path_default().posix.join(rootFolder.replaceAll('\\', '/'), 'presentation/rest/index.ts')}, which appears with the first REST endpoint — generate one with df-scaffold (Presentation → New Handlers-REST Endpoint) and the route compiles`
|
|
1470
|
+
];
|
|
1471
|
+
};
|
|
1472
|
+
const rest_endpoint_generator_askNextAdapter = async ()=>hasDependency('next') && await (0, prompts_namespaceObject.confirm)({
|
|
1473
|
+
message: 'Use Next adapter for REST endpoints'
|
|
1474
|
+
});
|
|
1475
|
+
const rest_endpoint_generator_scaffoldRestEndpointGeneratorFiles = ({ withNextAdapter })=>{
|
|
1476
|
+
const root = new project_root_ProjectRoot().path;
|
|
1477
|
+
const generatorExisted = restEndpointGeneratorApplied();
|
|
1478
|
+
const routeNotes = withoutOverwrites(()=>{
|
|
1479
|
+
scaffoldRestEndpointGenerator(new file_system_Folder(root).subitem([
|
|
1480
|
+
config_file_ConfigFile.Instance.data.rootFolder,
|
|
1481
|
+
'shared-kernel'
|
|
1482
|
+
]), {
|
|
1483
|
+
withNextAdapter
|
|
1484
|
+
});
|
|
1485
|
+
return withNextAdapter ? scaffoldNextRoute(root) : [];
|
|
1486
|
+
});
|
|
1487
|
+
if (!withNextAdapter) return [];
|
|
1488
|
+
return [
|
|
1489
|
+
...missingPackages([
|
|
1490
|
+
'@domain-first/handlers-next'
|
|
1491
|
+
]),
|
|
1492
|
+
...routeNotes,
|
|
1493
|
+
...generatorExisted ? [
|
|
1494
|
+
'the existing endpoint generator was kept — pass nextAdapter from @domain-first/handlers-next to its createEndpoint calls'
|
|
1495
|
+
] : []
|
|
1496
|
+
];
|
|
1497
|
+
};
|
|
1498
|
+
const addRestEndpointGenerator = async ()=>rest_endpoint_generator_scaffoldRestEndpointGeneratorFiles({
|
|
1499
|
+
withNextAdapter: await rest_endpoint_generator_askNextAdapter()
|
|
1500
|
+
});
|
|
1501
|
+
const restEndpointGeneratorApplied = ()=>Boolean(rest_endpoint_generator_findRestEndpointGenerator(new file_system_Folder(new project_root_ProjectRoot().path).subitem([
|
|
1502
|
+
config_file_ConfigFile.Instance.data.rootFolder,
|
|
1503
|
+
'shared-kernel'
|
|
1504
|
+
])));
|
|
1133
1505
|
const storybookFiles = (relativeProjectRoot)=>({
|
|
1134
1506
|
'main.ts': `import { resolve } from 'node:path';
|
|
1135
1507
|
import react from '@vitejs/plugin-react';
|
|
@@ -1373,7 +1745,7 @@ const addTestcontainers = async ()=>{
|
|
|
1373
1745
|
...manifest.devDependencies
|
|
1374
1746
|
};
|
|
1375
1747
|
const selected = await addPackageScripts(manifestFile, testcontainersScripts);
|
|
1376
|
-
const withPrisma = new file_system_File(external_node_path_default().resolve(root, config.rootFolder, 'shared/infrastructure/prisma/prisma.config.ts')).exists;
|
|
1748
|
+
const withPrisma = new file_system_File(external_node_path_default().resolve(root, config.rootFolder, 'shared-kernel/infrastructure/prisma/prisma.config.ts')).exists;
|
|
1377
1749
|
const migrationCommand = withPrisma ? `npx ${prismaScripts(config.rootFolder)['prisma:migration:apply'].replace(' --advance-ref db', '')}` : void 0;
|
|
1378
1750
|
const folder = external_node_path_default().join(root, integrationFolder);
|
|
1379
1751
|
testcontainers_stageFile(external_node_path_default().join(folder, 'vitest.config.ts'), testcontainersVitestConfig(Boolean(dependencies['@domain-first/handlers-next'])));
|
|
@@ -1436,13 +1808,13 @@ export default defineConfig({
|
|
|
1436
1808
|
exclude: [...configDefaults.exclude, 'specs/e2e/**', 'specs/integration/**'],
|
|
1437
1809
|
coverage: {
|
|
1438
1810
|
provider: 'v8',
|
|
1439
|
-
include: [${folder('bounded-contexts/**/*.ts')}, ${folder('shared/**/*.ts')}],
|
|
1811
|
+
include: [${folder('bounded-contexts/**/*.ts')}, ${folder('shared-kernel/**/*.ts')}],
|
|
1440
1812
|
exclude: [
|
|
1441
1813
|
'**/*.spec.ts',
|
|
1442
|
-
${folder('shared/infrastructure/**')},
|
|
1814
|
+
${folder('shared-kernel/infrastructure/**')},
|
|
1443
1815
|
${folder('bounded-contexts/*/infrastructure/**')},
|
|
1444
1816
|
${folder('bounded-contexts/*/presentation/rest/**')},
|
|
1445
|
-
${folder('shared/wiring/**')},
|
|
1817
|
+
${folder('shared-kernel/wiring/**')},
|
|
1446
1818
|
${folder('bounded-contexts/*/wiring/**')}
|
|
1447
1819
|
],
|
|
1448
1820
|
reporter: [['text', { skipFull: true }], 'html']
|
|
@@ -1521,11 +1893,11 @@ const printNextSteps = (nextSteps)=>{
|
|
|
1521
1893
|
const notes = nextSteps.filter((line)=>!isCommand(line));
|
|
1522
1894
|
const sections = [];
|
|
1523
1895
|
if (commands.length) sections.push([
|
|
1524
|
-
'Run
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1896
|
+
'Run this command to install the required dependencies:',
|
|
1897
|
+
(0, external_node_util_namespaceObject.styleText)([
|
|
1898
|
+
'bold',
|
|
1899
|
+
'cyan'
|
|
1900
|
+
], commands.join(' && '))
|
|
1529
1901
|
].join('\n'));
|
|
1530
1902
|
if (notes.length) sections.push([
|
|
1531
1903
|
'Additional information:',
|
|
@@ -1565,11 +1937,24 @@ const integrations = [
|
|
|
1565
1937
|
run: addStorybook,
|
|
1566
1938
|
applied: ()=>createdInRoot('presentation/react/.storybook/main.ts')
|
|
1567
1939
|
},
|
|
1940
|
+
{
|
|
1941
|
+
name: 'REST endpoint generator',
|
|
1942
|
+
group: 'backend',
|
|
1943
|
+
run: addRestEndpointGenerator,
|
|
1944
|
+
available: ()=>withHandlersRest(),
|
|
1945
|
+
applied: restEndpointGeneratorApplied
|
|
1946
|
+
},
|
|
1947
|
+
{
|
|
1948
|
+
name: 'In-memory DB',
|
|
1949
|
+
group: 'backend',
|
|
1950
|
+
run: addInMemoryDb,
|
|
1951
|
+
applied: inMemoryDbApplied
|
|
1952
|
+
},
|
|
1568
1953
|
{
|
|
1569
1954
|
name: 'Prisma 8 (PostgreSQL, RC)',
|
|
1570
1955
|
group: 'backend',
|
|
1571
1956
|
run: addPrisma,
|
|
1572
|
-
applied: ()=>createdInRoot('shared/infrastructure/prisma/prisma.config.ts')
|
|
1957
|
+
applied: ()=>createdInRoot('shared-kernel/infrastructure/prisma/prisma.config.ts')
|
|
1573
1958
|
},
|
|
1574
1959
|
{
|
|
1575
1960
|
name: 'Vitest',
|
|
@@ -1602,12 +1987,6 @@ const integrations = [
|
|
|
1602
1987
|
run: addOrval,
|
|
1603
1988
|
available: ()=>withHandlersRest(),
|
|
1604
1989
|
applied: ()=>createdInRoot('presentation/react/data-layer/orval.config.ts') || createdInRoot('presentation/data-layer/orval.config.ts')
|
|
1605
|
-
},
|
|
1606
|
-
{
|
|
1607
|
-
name: 'Missing domain-first packages',
|
|
1608
|
-
group: 'backend',
|
|
1609
|
-
run: missingDomainFirstPackages,
|
|
1610
|
-
applied: ()=>!missingDomainFirstPackages().length
|
|
1611
1990
|
}
|
|
1612
1991
|
];
|
|
1613
1992
|
const addIntegration = async ()=>{
|
|
@@ -1651,11 +2030,56 @@ new Set([
|
|
|
1651
2030
|
'coverage',
|
|
1652
2031
|
'storybook-static'
|
|
1653
2032
|
]);
|
|
2033
|
+
const serviceLayout = (layer)=>({
|
|
2034
|
+
title: `${layer[0].toUpperCase()}${layer.slice(1)} services`,
|
|
2035
|
+
core: (ClassName)=>`${ClassName}${layer[0].toUpperCase()}${layer.slice(1)}Service`,
|
|
2036
|
+
files: ({ name })=>[
|
|
2037
|
+
`${layer}/services/${name}-${layer}-service.ts`,
|
|
2038
|
+
`${layer}/services/${name}-${layer}-service.spec.ts`,
|
|
2039
|
+
`wiring/${layer}-services/wire-${name}-${layer}-service.ts`
|
|
2040
|
+
],
|
|
2041
|
+
wired: true,
|
|
2042
|
+
sharedKernel: true
|
|
2043
|
+
});
|
|
2044
|
+
const handlerLayout = (kind)=>{
|
|
2045
|
+
const folder = 'command' === kind ? 'commands' : 'queries';
|
|
2046
|
+
const Suffix = 'command' === kind ? 'Command' : 'Query';
|
|
2047
|
+
return {
|
|
2048
|
+
title: 'command' === kind ? 'Commands' : 'Queries',
|
|
2049
|
+
core: (ClassName)=>`${ClassName}${Suffix}`,
|
|
2050
|
+
files: ({ name }, labels)=>[
|
|
2051
|
+
`application/${folder}/${name}-${kind}.ts`,
|
|
2052
|
+
`application/${folder}/${name}-${kind}.spec.ts`,
|
|
2053
|
+
...labels.map((label)=>`infrastructure/${folder}/${label}/${label}-${name}-${kind}.ts`),
|
|
2054
|
+
`wiring/${folder}/wire-${name}-${kind}.ts`
|
|
2055
|
+
],
|
|
2056
|
+
implementations: `infrastructure/${folder}`,
|
|
2057
|
+
wired: true,
|
|
2058
|
+
sharedKernel: false
|
|
2059
|
+
};
|
|
2060
|
+
};
|
|
2061
|
+
const domainModelLayout = (kind)=>{
|
|
2062
|
+
const folder = 'entity' === kind ? 'entities' : 'value-objects';
|
|
2063
|
+
return {
|
|
2064
|
+
title: 'entity' === kind ? 'Entities' : 'Value objects',
|
|
2065
|
+
core: (ClassName)=>ClassName,
|
|
2066
|
+
files: ({ name, aggregate })=>{
|
|
2067
|
+
const base = aggregate ? `domain/aggregates/${aggregate}/${folder}` : `domain/${folder}`;
|
|
2068
|
+
return [
|
|
2069
|
+
`${base}/${name}.${kind}.ts`,
|
|
2070
|
+
`${base}/${name}.${kind}.spec.ts`
|
|
2071
|
+
];
|
|
2072
|
+
},
|
|
2073
|
+
wired: false,
|
|
2074
|
+
sharedKernel: true
|
|
2075
|
+
};
|
|
2076
|
+
};
|
|
2077
|
+
domainModelLayout('entity'), domainModelLayout('value-object'), serviceLayout('domain'), serviceLayout('application'), serviceLayout('infrastructure'), serviceLayout('presentation'), handlerLayout('command'), handlerLayout('query');
|
|
1654
2078
|
const help = `Usage: df-integration
|
|
1655
2079
|
|
|
1656
|
-
Adds integrations to a Domain-First project:
|
|
1657
|
-
|
|
1658
|
-
|
|
2080
|
+
Adds integrations to a Domain-First project: the REST endpoint generator,
|
|
2081
|
+
the in-memory DB, Storybook, Prisma, Vitest, Playwright, integration tests,
|
|
2082
|
+
OpenAPI and Orval. Several can be selected at once. Changes project files and prints
|
|
1659
2083
|
the install commands; nothing is installed or run.
|
|
1660
2084
|
|
|
1661
2085
|
Options:
|
|
@@ -1674,7 +2098,7 @@ if (values.help) {
|
|
|
1674
2098
|
process.exit(0);
|
|
1675
2099
|
}
|
|
1676
2100
|
if (!config_file_ConfigFile.exists) {
|
|
1677
|
-
console.log('No domain-first.project-structure.config.json found: run df-
|
|
2101
|
+
console.log('No domain-first.project-structure.config.json found: run df-init first to initialize the project.');
|
|
1678
2102
|
process.exit(1);
|
|
1679
2103
|
}
|
|
1680
2104
|
addIntegration();
|
package/dist/integration.js
CHANGED
|
@@ -4,9 +4,9 @@ import { ConfigFile } from "./201.js";
|
|
|
4
4
|
import { addIntegration } from "./287.js";
|
|
5
5
|
const help = `Usage: df-integration
|
|
6
6
|
|
|
7
|
-
Adds integrations to a Domain-First project:
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
Adds integrations to a Domain-First project: the REST endpoint generator,
|
|
8
|
+
the in-memory DB, Storybook, Prisma, Vitest, Playwright, integration tests,
|
|
9
|
+
OpenAPI and Orval. Several can be selected at once. Changes project files and prints
|
|
10
10
|
the install commands; nothing is installed or run.
|
|
11
11
|
|
|
12
12
|
Options:
|
|
@@ -25,7 +25,7 @@ if (values.help) {
|
|
|
25
25
|
process.exit(0);
|
|
26
26
|
}
|
|
27
27
|
if (!ConfigFile.exists) {
|
|
28
|
-
console.log('No domain-first.project-structure.config.json found: run df-
|
|
28
|
+
console.log('No domain-first.project-structure.config.json found: run df-init first to initialize the project.');
|
|
29
29
|
process.exit(1);
|
|
30
30
|
}
|
|
31
31
|
addIntegration();
|