@domain-first/project-structure 8.9.2 → 8.10.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/dist/upgrade.cjs CHANGED
@@ -368,6 +368,55 @@ const hasDependency = (name)=>{
368
368
  manifest.optionalDependencies
369
369
  ].some((dependencies)=>dependencies && Object.hasOwn(dependencies, name));
370
370
  };
371
+ const bounded_contexts_boundedContextsFolderName = 'bounded-contexts';
372
+ const contextLayerFolders = [
373
+ 'domain',
374
+ 'application',
375
+ 'infrastructure',
376
+ 'presentation',
377
+ 'wiring'
378
+ ];
379
+ const subfolders = (folder)=>external_node_fs_default().existsSync(folder) ? external_node_fs_default().readdirSync(folder, {
380
+ withFileTypes: true
381
+ }).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name) : [];
382
+ const isBoundedContextFolder = (folder)=>{
383
+ const names = subfolders(folder);
384
+ return !names.length || names.some((name)=>contextLayerFolders.includes(name));
385
+ };
386
+ const bounded_contexts_boundedContextNames = (contextsFolder)=>{
387
+ const walk = (folder, prefix)=>subfolders(folder).flatMap((name)=>{
388
+ const child = external_node_path_default().join(folder, name);
389
+ return isBoundedContextFolder(child) ? [
390
+ [
391
+ ...prefix,
392
+ name
393
+ ].join('/')
394
+ ] : walk(child, [
395
+ ...prefix,
396
+ name
397
+ ]);
398
+ });
399
+ return walk(contextsFolder, []).sort();
400
+ };
401
+ const boundedContextPath = (contextsFolder, name)=>external_node_path_default().join(contextsFolder, ...name.split('/'));
402
+ const splitContextSegments = (segments)=>{
403
+ const layer = segments.findIndex((segment)=>contextLayerFolders.includes(segment));
404
+ const length = layer > 0 ? layer : 1;
405
+ return {
406
+ name: segments.slice(0, length).join('/'),
407
+ segments: segments.slice(length)
408
+ };
409
+ };
410
+ const context_dependencies_boundedContextLocation = (file)=>{
411
+ const parts = external_node_path_default().resolve(file).split(external_node_path_default().sep);
412
+ const index = parts.lastIndexOf(bounded_contexts_boundedContextsFolderName);
413
+ if (-1 === index || parts.length < index + 3) return;
414
+ return {
415
+ root: parts.slice(0, index).join(external_node_path_default().sep),
416
+ ...splitContextSegments(parts.slice(index + 1))
417
+ };
418
+ };
419
+ const boundedContextName = (folder)=>context_dependencies_boundedContextLocation(external_node_path_default().join(folder, 'domain'))?.name;
371
420
  new Map([
372
421
  [
373
422
  'prisma',
@@ -454,7 +503,7 @@ const legacyEndpointFileLookup = (rootFolder)=>` // The controller, i
454
503
  )
455
504
  );
456
505
  `;
457
- const endpointFileLookup = (rootFolder)=>` const [currentBoundedContext, ...relativePath] = routeMetadata.path;
506
+ const rootEndpointFileLookup = (rootFolder)=>` const [currentBoundedContext, ...relativePath] = routeMetadata.path;
458
507
 
459
508
  if (!currentBoundedContext) {
460
509
  return undefined;
@@ -478,6 +527,37 @@ const endpointFileLookup = (rootFolder)=>` const [currentBoundedConte
478
527
  path.join(restFolder, '(root)', ...endpointPath)
479
528
  );
480
529
  `;
530
+ const endpointFileLookup = (rootFolder)=>` // A context is the start of the route, nested ones included
531
+ // (/sales/orders/...). A controller is the next folder; other
532
+ // routes belong to the context root in presentation/rest/(root).
533
+ const contextsFolder = path.join(
534
+ ${rootFolderSegments(rootFolder, 16)}'bounded-contexts'
535
+ );
536
+ const endpointFile = Array.from(
537
+ { length: routeMetadata.path.length },
538
+ (_, index) => index + 1
539
+ )
540
+ .flatMap((contextLength) => {
541
+ const restFolder = path.join(
542
+ contextsFolder,
543
+ ...routeMetadata.path.slice(0, contextLength),
544
+ 'presentation',
545
+ 'rest'
546
+ );
547
+ const endpointPath = [
548
+ ...routeMetadata.path
549
+ .slice(contextLength)
550
+ .map(pathParameterAsName),
551
+ \`\${routeMetadata.method.toUpperCase()}.ts\`
552
+ ];
553
+ return [
554
+ path.join(restFolder, ...endpointPath),
555
+ path.join(restFolder, '(root)', ...endpointPath)
556
+ ];
557
+ })
558
+ .map((file) => project.getSourceFile(file))
559
+ .find((file) => !!file);
560
+ `;
481
561
  require("node:module");
482
562
  const stripJsonComments = (source)=>source.replace(/("(?:\\.|[^"\\])*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g, (_, text)=>text ? text : '').replace(/,(\s*[}\]])/g, '$1');
483
563
  const ignoredFolders = new Set([
@@ -631,7 +711,7 @@ const endpointFileNames = new Set([
631
711
  const rest_layout_entries = (folder)=>external_node_fs_default().existsSync(folder) ? external_node_fs_default().readdirSync(folder, {
632
712
  withFileTypes: true
633
713
  }) : [];
634
- const subfolders = (folder)=>rest_layout_entries(folder).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name);
714
+ const rest_layout_subfolders = (folder)=>rest_layout_entries(folder).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name);
635
715
  const restFolders = (boundedContextFolder)=>({
636
716
  presentation: external_node_path_default().join(boundedContextFolder, 'presentation', 'rest'),
637
717
  wiring: external_node_path_default().join(boundedContextFolder, 'wiring', 'presentation', 'rest')
@@ -640,7 +720,7 @@ const legacyRootEndpoints = (boundedContextFolder)=>{
640
720
  const { presentation, wiring } = restFolders(boundedContextFolder);
641
721
  return {
642
722
  endpointFiles: rest_layout_entries(presentation).filter((entry)=>entry.isFile() && endpointFileNames.has(entry.name)).map((entry)=>external_node_path_default().join(presentation, entry.name)),
643
- parameterFolders: subfolders(presentation).filter(isPathParameterName).map((name)=>external_node_path_default().join(presentation, name)),
723
+ parameterFolders: rest_layout_subfolders(presentation).filter(isPathParameterName).map((name)=>external_node_path_default().join(presentation, name)),
644
724
  wireFiles: rest_layout_entries(wiring).filter((entry)=>entry.isFile() && 'index.ts' !== entry.name && /\.ts$/.test(entry.name)).map((entry)=>external_node_path_default().join(wiring, entry.name))
645
725
  };
646
726
  };
@@ -654,9 +734,7 @@ const contextFolders = (context)=>{
654
734
  const rootFolder = rootFolderOf(context);
655
735
  if (!rootFolder) return [];
656
736
  const contexts = external_node_path_default().join(new project_root_ProjectRoot().path, rootFolder, 'bounded-contexts');
657
- return external_node_fs_default().existsSync(contexts) ? external_node_fs_default().readdirSync(contexts, {
658
- withFileTypes: true
659
- }).filter((entry)=>entry.isDirectory()).map((entry)=>external_node_path_default().join(contexts, entry.name)) : [];
737
+ return bounded_contexts_boundedContextNames(contexts).map((name)=>boundedContextPath(contexts, name));
660
738
  };
661
739
  const legacyContexts = (context)=>contextFolders(context).filter(hasLegacyRootEndpoints);
662
740
  const upgrade_rest_root_relative = (file)=>toPosix(external_node_path_default().relative(new project_root_ProjectRoot().path, file));
@@ -706,7 +784,7 @@ const restRootControllerStep = {
706
784
  title: "Move context-root REST endpoints to presentation/rest/(root)/",
707
785
  detect: (context)=>legacyContexts(context).length > 0,
708
786
  plan: (context)=>legacyContexts(context).flatMap((contextFolder)=>{
709
- const name = external_node_path_default().basename(contextFolder);
787
+ const name = boundedContextName(contextFolder) ?? external_node_path_default().basename(contextFolder);
710
788
  const { wiring } = restFolders(contextFolder);
711
789
  const { files } = rootMoves(contextFolder);
712
790
  const wired = files.some(([file])=>file.startsWith(wiring));
@@ -753,7 +831,7 @@ const restRootControllerStep = {
753
831
  });
754
832
  const sharedWires = new file_system_File(external_node_path_default().join(projectRoot, rootFolderOf(context), 'presentation', 'rest', 'wires.ts'));
755
833
  for (const contextFolder of contexts){
756
- const name = external_node_path_default().basename(contextFolder);
834
+ const name = boundedContextName(contextFolder) ?? external_node_path_default().basename(contextFolder);
757
835
  const { wiring } = restFolders(contextFolder);
758
836
  const movedWires = legacyRootEndpoints(contextFolder).wireFiles;
759
837
  if (!movedWires.length) continue;
@@ -787,6 +865,24 @@ const openapiExtractorRootStep = {
787
865
  file.update(file.data.replace(legacyEndpointFileLookup(rootFolder), endpointFileLookup(rootFolder)));
788
866
  }
789
867
  };
868
+ const openapiExtractorNestedStep = {
869
+ id: 'openapi-extractor-nested',
870
+ kind: 'optional',
871
+ title: 'Let the OpenAPI extractor find endpoints of nested bounded contexts',
872
+ detect: (context)=>{
873
+ const rootFolder = rootFolderOf(context);
874
+ const file = extractorFile();
875
+ return !!rootFolder && file.exists && file.data.includes(rootEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>external_node_path_default().relative(external_node_path_default().join(new project_root_ProjectRoot().path, rootFolder, 'bounded-contexts'), folder).includes(external_node_path_default().sep));
876
+ },
877
+ plan: ()=>[
878
+ `${openapiExtractorPath}: the context of an endpoint is looked up by the start of its route, nested contexts included`
879
+ ],
880
+ apply: (context)=>{
881
+ const rootFolder = rootFolderOf(context);
882
+ const file = extractorFile();
883
+ file.update(file.data.replace(rootEndpointFileLookup(rootFolder), endpointFileLookup(rootFolder)));
884
+ }
885
+ };
790
886
  const openapiExtractorManualStep = {
791
887
  id: 'openapi-extractor-manual',
792
888
  kind: 'manual',
@@ -796,7 +892,7 @@ const openapiExtractorManualStep = {
796
892
  const file = extractorFile();
797
893
  if (!rootFolder || !file.exists) return false;
798
894
  const data = file.data;
799
- return !data.includes(endpointFileLookup(rootFolder)) && !data.includes(legacyEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>hasLegacyRootEndpoints(folder) || external_node_fs_default().existsSync(external_node_path_default().join(restFolders(folder).presentation, "(root)")));
895
+ return !data.includes(endpointFileLookup(rootFolder)) && !data.includes(rootEndpointFileLookup(rootFolder)) && !data.includes(legacyEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>hasLegacyRootEndpoints(folder) || external_node_fs_default().existsSync(external_node_path_default().join(restFolders(folder).presentation, "(root)")));
800
896
  },
801
897
  plan: ()=>[
802
898
  `${openapiExtractorPath} doesn't look up endpoints in presentation/rest/(root)/: compare it with the one npx df-integration scaffolds for openapi`
@@ -921,6 +1017,7 @@ const upgradeSteps = [
921
1017
  persistenceStep,
922
1018
  restRootControllerStep,
923
1019
  openapiExtractorRootStep,
1020
+ openapiExtractorNestedStep,
924
1021
  openapiExtractorManualStep,
925
1022
  inMemoryDbWithIdStep
926
1023
  ];
package/dist/upgrade.js CHANGED
@@ -4,8 +4,8 @@ import { parseArgs } from "node:util";
4
4
  import { confirm as prompts_confirm } from "@inquirer/prompts";
5
5
  import zod from "zod";
6
6
  import node_fs from "node:fs";
7
- import { implementationLabelSchema, domainFirstPackages as config_file_domainFirstPackages, discardPendingChanges, configSchema, rulesSchema, ConfigFile, isOutdatedConfig, writePendingChanges, File, configFileName, ProjectRoot } from "./201.js";
8
- import { restFolders, withIdLookup, openapiExtractorPath, legacyEndpointFileLookup, walk, legacyHasId, legacyWithIdLookup, hasLegacyRootEndpoints, endpointFileLookup, hasDependency, stageModuleMove, legacyRootEndpoints, toPosix, codeExtension } from "./115.js";
7
+ import { boundedContextName, implementationLabelSchema, domainFirstPackages as config_file_domainFirstPackages, discardPendingChanges, configSchema, rulesSchema, ConfigFile, boundedContextPath, isOutdatedConfig, writePendingChanges, File, ProjectRoot, configFileName, boundedContextNames } from "./201.js";
8
+ import { restFolders, withIdLookup, endpointFileLookup, walk, legacyWithIdLookup, hasLegacyRootEndpoints, stageModuleMove, legacyRootEndpoints, toPosix, openapiExtractorPath, rootEndpointFileLookup, legacyEndpointFileLookup, legacyHasId, hasDependency, codeExtension } from "./115.js";
9
9
  const serviceFile = ({ config })=>{
10
10
  const project = config.project;
11
11
  const rootFolder = 'object' == typeof project && null !== project ? project.rootFolder : void 0;
@@ -36,9 +36,7 @@ const contextFolders = (context)=>{
36
36
  const rootFolder = rootFolderOf(context);
37
37
  if (!rootFolder) return [];
38
38
  const contexts = node_path.join(new ProjectRoot().path, rootFolder, 'bounded-contexts');
39
- return node_fs.existsSync(contexts) ? node_fs.readdirSync(contexts, {
40
- withFileTypes: true
41
- }).filter((entry)=>entry.isDirectory()).map((entry)=>node_path.join(contexts, entry.name)) : [];
39
+ return boundedContextNames(contexts).map((name)=>boundedContextPath(contexts, name));
42
40
  };
43
41
  const legacyContexts = (context)=>contextFolders(context).filter(hasLegacyRootEndpoints);
44
42
  const relative = (file)=>toPosix(node_path.relative(new ProjectRoot().path, file));
@@ -88,7 +86,7 @@ const restRootControllerStep = {
88
86
  title: "Move context-root REST endpoints to presentation/rest/(root)/",
89
87
  detect: (context)=>legacyContexts(context).length > 0,
90
88
  plan: (context)=>legacyContexts(context).flatMap((contextFolder)=>{
91
- const name = node_path.basename(contextFolder);
89
+ const name = boundedContextName(contextFolder) ?? node_path.basename(contextFolder);
92
90
  const { wiring } = restFolders(contextFolder);
93
91
  const { files } = rootMoves(contextFolder);
94
92
  const wired = files.some(([file])=>file.startsWith(wiring));
@@ -135,7 +133,7 @@ const restRootControllerStep = {
135
133
  });
136
134
  const sharedWires = new File(node_path.join(projectRoot, rootFolderOf(context), 'presentation', 'rest', 'wires.ts'));
137
135
  for (const contextFolder of contexts){
138
- const name = node_path.basename(contextFolder);
136
+ const name = boundedContextName(contextFolder) ?? node_path.basename(contextFolder);
139
137
  const { wiring } = restFolders(contextFolder);
140
138
  const movedWires = legacyRootEndpoints(contextFolder).wireFiles;
141
139
  if (!movedWires.length) continue;
@@ -169,6 +167,24 @@ const openapiExtractorRootStep = {
169
167
  file.update(file.data.replace(legacyEndpointFileLookup(rootFolder), endpointFileLookup(rootFolder)));
170
168
  }
171
169
  };
170
+ const openapiExtractorNestedStep = {
171
+ id: 'openapi-extractor-nested',
172
+ kind: 'optional',
173
+ title: 'Let the OpenAPI extractor find endpoints of nested bounded contexts',
174
+ detect: (context)=>{
175
+ const rootFolder = rootFolderOf(context);
176
+ const file = extractorFile();
177
+ return !!rootFolder && file.exists && file.data.includes(rootEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>node_path.relative(node_path.join(new ProjectRoot().path, rootFolder, 'bounded-contexts'), folder).includes(node_path.sep));
178
+ },
179
+ plan: ()=>[
180
+ `${openapiExtractorPath}: the context of an endpoint is looked up by the start of its route, nested contexts included`
181
+ ],
182
+ apply: (context)=>{
183
+ const rootFolder = rootFolderOf(context);
184
+ const file = extractorFile();
185
+ file.update(file.data.replace(rootEndpointFileLookup(rootFolder), endpointFileLookup(rootFolder)));
186
+ }
187
+ };
172
188
  const openapiExtractorManualStep = {
173
189
  id: 'openapi-extractor-manual',
174
190
  kind: 'manual',
@@ -178,7 +194,7 @@ const openapiExtractorManualStep = {
178
194
  const file = extractorFile();
179
195
  if (!rootFolder || !file.exists) return false;
180
196
  const data = file.data;
181
- return !data.includes(endpointFileLookup(rootFolder)) && !data.includes(legacyEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>hasLegacyRootEndpoints(folder) || node_fs.existsSync(node_path.join(restFolders(folder).presentation, "(root)")));
197
+ return !data.includes(endpointFileLookup(rootFolder)) && !data.includes(rootEndpointFileLookup(rootFolder)) && !data.includes(legacyEndpointFileLookup(rootFolder)) && contextFolders(context).some((folder)=>hasLegacyRootEndpoints(folder) || node_fs.existsSync(node_path.join(restFolders(folder).presentation, "(root)")));
182
198
  },
183
199
  plan: ()=>[
184
200
  `${openapiExtractorPath} doesn't look up endpoints in presentation/rest/(root)/: compare it with the one npx df-integration scaffolds for openapi`
@@ -303,6 +319,7 @@ const upgradeSteps = [
303
319
  persistenceStep,
304
320
  restRootControllerStep,
305
321
  openapiExtractorRootStep,
322
+ openapiExtractorNestedStep,
306
323
  openapiExtractorManualStep,
307
324
  inMemoryDbWithIdStep
308
325
  ];
@@ -1,4 +1,4 @@
1
- import { Folder } from '../file-system';
1
+ import { type Folder } from '../file-system';
2
2
  /**
3
3
  * Renames a unit of a bounded context or the Shared Kernel, or moves it
4
4
  * to another context or the Shared Kernel, with every file, name and
@@ -1 +1,5 @@
1
+ /**
2
+ * Folders before the name group contexts, like the folders of React
3
+ * pages and widgets: `sales/orders` → `bounded-contexts/sales/orders`.
4
+ */
1
5
  export declare const scaffoldNewBoundedContextUseCase: () => Promise<void>;
@@ -1,4 +1,4 @@
1
- import type { Folder } from '../file-system';
1
+ import { Folder } from '../file-system';
2
2
  /**
3
3
  * The answer value of the context-root controller, kept from 8.5 so
4
4
  * recorded answers still select it.
@@ -22,5 +22,10 @@ export declare const offersTestImplementation: () => boolean;
22
22
  * others in their configured order.
23
23
  */
24
24
  export declare const envBranchedWireArgument: (testWire: string, primaryWire: string) => string;
25
+ /**
26
+ * The import of the Shared Kernel `envBranchedWire` from a wire in the
27
+ * folder, however deep its bounded context is nested.
28
+ */
29
+ export declare const envBranchedWireImport: (wireFolder: string) => string;
25
30
  /** `env-branched-wire.ts` of the Shared Kernel for the configured branches. */
26
31
  export declare const envBranchedWireSource: () => string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@domain-first/project-structure",
3
3
  "description": "Build and manage your Domain-First project structure with CLI",
4
- "version": "8.9.2",
4
+ "version": "8.10.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -6,11 +6,11 @@ One invocation accepts one operation, not an array or a batch. Each invocation s
6
6
 
7
7
  ## Backend operations: df-scaffold
8
8
 
9
- All operations support `dryRun` and `overwrite`. Except `bounded-context`, they require `context`, using a discovered context name or reserved `shared-kernel`. Names are lowercase kebab-case. The backend must be configured by the owner.
9
+ All operations support `dryRun` and `overwrite`. Except `bounded-context`, they require `context`, using a discovered context name (a nested context by its path, e.g. `commerce/orders`) or reserved `shared-kernel`. Names are lowercase kebab-case. The backend must be configured by the owner.
10
10
 
11
11
  | Operation | Required fields beyond operation/context | Optional fields and conditions |
12
12
  | --- | --- | --- |
13
- | `bounded-context` | `name`; no `context` | Creates an empty context. `shared-kernel` is a reserved name. |
13
+ | `bounded-context` | `name`; no `context` | Creates an empty context. Folders split by `/` group contexts (`commerce/orders`); a segment can't be a layer folder, a context can't be nested in another, and a grouping folder isn't a context. `shared-kernel` is a reserved name. |
14
14
  | `aggregate` | `name` | `withRepository` defaults to false. True requires `implementation`; may also use `testImplementation` and `dependencies`. These repository fields do not apply when `withRepository` is false. Bounded contexts only. |
15
15
  | `entity` | `name` | `aggregate` selects an existing aggregate in the same context; omitted means a standalone domain entity. |
16
16
  | `value-object` | `name` | Same aggregate rules as entity. |