@domain-first/project-structure 3.3.0 → 3.3.1

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 CHANGED
@@ -138,10 +138,11 @@ Only the dependencies that respect the layers are offered: a use case can't take
138
138
  Choose **Domain → Errors** to add an error to the context namespace, create a nested namespace, or add an error to it.
139
139
 
140
140
  ```ts
141
- export const OutOfStockSalesError =
142
- SalesErrors.define("out-of-stock");
141
+ export class OutOfStock extends SalesErrors.define('out-of-stock') {}
143
142
  ```
144
143
 
144
+ Names don't repeat the context: an error of the `payment` namespace in `sales` is `PaymentDeclined`. In the Shared Layer they end with `SharedError`: `TimeoutSharedError`.
145
+
145
146
  ### How to share code between contexts
146
147
 
147
148
  Bounded contexts don't import each other. Put cross-context errors, entities, value objects, ports and services into the **Shared Layer** (`src/shared`). Its modules are offered as dependencies in every context.
package/dist/index.cjs CHANGED
@@ -3022,11 +3022,25 @@ export const wire${naming.ClassName}DomainService = wireClass(
3022
3022
  await wireNewModuleDependencies(boundedContextFolder, wireFile);
3023
3023
  }
3024
3024
  };
3025
+ const errorNames = (boundedContextFolder)=>{
3026
+ const isShared = boundedContextFolder.path === new Folder(new ProjectRoot().path).subitem([
3027
+ ConfigFile.Instance.data.rootFolder,
3028
+ 'shared'
3029
+ ]).path;
3030
+ return {
3031
+ namespace: (namespaceName)=>`${namespaceName}${isShared ? 'Shared' : ''}Errors`,
3032
+ error: (errorName)=>`${errorName}${isShared ? 'SharedError' : ''}`
3033
+ };
3034
+ };
3035
+ const exportedNamespace = (file, fallback)=>/export\s+const\s+(\w+)\s*=\s*errorNamespace\b/.exec(file.data)?.[1] ?? fallback;
3036
+ const errorClass = (className, namespace, code)=>`export class ${className} extends ${namespace}.define('${code}') {}`;
3025
3037
  const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
3026
3038
  const currentErrorFolder = boundedContextFolder.subitem([
3027
3039
  'domain',
3028
3040
  'errors'
3029
3041
  ]);
3042
+ const names = errorNames(boundedContextFolder);
3043
+ const contextNaming = new UnknownFormatNaming(boundedContextFolder.name);
3030
3044
  const currentErrorNamespaces = currentErrorFolder.content.fileNames.filter((x)=>'index' !== x);
3031
3045
  const variants = [
3032
3046
  'New error without namespace',
@@ -3042,7 +3056,8 @@ const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
3042
3056
  message: 'Error name:'
3043
3057
  });
3044
3058
  const naming = new UnknownFormatNaming(errorName);
3045
- currentErrorFolder.file('index.ts').addLineIfMissing(`export const ${naming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Error = ${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors.define('${naming.fileName}')`, '\n\n');
3059
+ const indexFile = currentErrorFolder.file('index.ts');
3060
+ indexFile.addLineIfMissing(errorClass(names.error(naming.ClassName), exportedNamespace(indexFile, `${contextNaming.ClassName}Errors`), naming.fileName), '\n\n');
3046
3061
  return;
3047
3062
  }
3048
3063
  if ('New namespace' === selectedVariant) {
@@ -3053,7 +3068,7 @@ const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
3053
3068
  if (!currentErrorFolder.file(`${fileName}.ts`).exists) currentErrorFolder.createFile(`${fileName}.ts`, `
3054
3069
  import { errorNamespace } from '@domain-first/errors'
3055
3070
 
3056
- export const ${ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors = errorNamespace('${boundedContextFolder.name}').subnamespace('${fileName}')
3071
+ export const ${names.namespace(ClassName)} = errorNamespace('${boundedContextFolder.name}').subnamespace('${fileName}')
3057
3072
  `.trim());
3058
3073
  currentErrorFolder.file('index.ts').addLineIfMissing(`export * from './${fileName}'`, '\n\n');
3059
3074
  return;
@@ -3064,7 +3079,8 @@ export const ${ClassName}${new UnknownFormatNaming(boundedContextFolder.name).Cl
3064
3079
  message: 'Error name:'
3065
3080
  });
3066
3081
  const { ClassName, fileName } = new UnknownFormatNaming(errorName);
3067
- currentErrorFolder.file(`${namespace}.ts`).addLineIfMissing(`export const ${ClassName}${namespaceNaming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Error = ${namespaceNaming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors.define('${fileName}')`, '\n\n');
3082
+ const namespaceFile = currentErrorFolder.file(`${namespace}.ts`);
3083
+ namespaceFile.addLineIfMissing(errorClass(names.error(`${namespaceNaming.ClassName}${ClassName}`), exportedNamespace(namespaceFile, names.namespace(namespaceNaming.ClassName)), fileName), '\n\n');
3068
3084
  };
3069
3085
  const scaffoldNewHandlersRestEndpoint = async (boundedContextFolder)=>{
3070
3086
  const restPresentationFolder = boundedContextFolder.subitem([
package/dist/index.js CHANGED
@@ -2992,11 +2992,25 @@ export const wire${naming.ClassName}DomainService = wireClass(
2992
2992
  await wireNewModuleDependencies(boundedContextFolder, wireFile);
2993
2993
  }
2994
2994
  };
2995
+ const errorNames = (boundedContextFolder)=>{
2996
+ const isShared = boundedContextFolder.path === new Folder(new ProjectRoot().path).subitem([
2997
+ ConfigFile.Instance.data.rootFolder,
2998
+ 'shared'
2999
+ ]).path;
3000
+ return {
3001
+ namespace: (namespaceName)=>`${namespaceName}${isShared ? 'Shared' : ''}Errors`,
3002
+ error: (errorName)=>`${errorName}${isShared ? 'SharedError' : ''}`
3003
+ };
3004
+ };
3005
+ const exportedNamespace = (file, fallback)=>/export\s+const\s+(\w+)\s*=\s*errorNamespace\b/.exec(file.data)?.[1] ?? fallback;
3006
+ const errorClass = (className, namespace, code)=>`export class ${className} extends ${namespace}.define('${code}') {}`;
2995
3007
  const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
2996
3008
  const currentErrorFolder = boundedContextFolder.subitem([
2997
3009
  'domain',
2998
3010
  'errors'
2999
3011
  ]);
3012
+ const names = errorNames(boundedContextFolder);
3013
+ const contextNaming = new UnknownFormatNaming(boundedContextFolder.name);
3000
3014
  const currentErrorNamespaces = currentErrorFolder.content.fileNames.filter((x)=>'index' !== x);
3001
3015
  const variants = [
3002
3016
  'New error without namespace',
@@ -3012,7 +3026,8 @@ const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
3012
3026
  message: 'Error name:'
3013
3027
  });
3014
3028
  const naming = new UnknownFormatNaming(errorName);
3015
- currentErrorFolder.file('index.ts').addLineIfMissing(`export const ${naming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Error = ${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors.define('${naming.fileName}')`, '\n\n');
3029
+ const indexFile = currentErrorFolder.file('index.ts');
3030
+ indexFile.addLineIfMissing(errorClass(names.error(naming.ClassName), exportedNamespace(indexFile, `${contextNaming.ClassName}Errors`), naming.fileName), '\n\n');
3016
3031
  return;
3017
3032
  }
3018
3033
  if ('New namespace' === selectedVariant) {
@@ -3023,7 +3038,7 @@ const scaffoldNewErrorUseCase = async (boundedContextFolder)=>{
3023
3038
  if (!currentErrorFolder.file(`${fileName}.ts`).exists) currentErrorFolder.createFile(`${fileName}.ts`, `
3024
3039
  import { errorNamespace } from '@domain-first/errors'
3025
3040
 
3026
- export const ${ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors = errorNamespace('${boundedContextFolder.name}').subnamespace('${fileName}')
3041
+ export const ${names.namespace(ClassName)} = errorNamespace('${boundedContextFolder.name}').subnamespace('${fileName}')
3027
3042
  `.trim());
3028
3043
  currentErrorFolder.file('index.ts').addLineIfMissing(`export * from './${fileName}'`, '\n\n');
3029
3044
  return;
@@ -3034,7 +3049,8 @@ export const ${ClassName}${new UnknownFormatNaming(boundedContextFolder.name).Cl
3034
3049
  message: 'Error name:'
3035
3050
  });
3036
3051
  const { ClassName, fileName } = new UnknownFormatNaming(errorName);
3037
- currentErrorFolder.file(`${namespace}.ts`).addLineIfMissing(`export const ${ClassName}${namespaceNaming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Error = ${namespaceNaming.ClassName}${new UnknownFormatNaming(boundedContextFolder.name).ClassName}Errors.define('${fileName}')`, '\n\n');
3052
+ const namespaceFile = currentErrorFolder.file(`${namespace}.ts`);
3053
+ namespaceFile.addLineIfMissing(errorClass(names.error(`${namespaceNaming.ClassName}${ClassName}`), exportedNamespace(namespaceFile, names.namespace(namespaceNaming.ClassName)), fileName), '\n\n');
3038
3054
  };
3039
3055
  const scaffoldNewHandlersRestEndpoint = async (boundedContextFolder)=>{
3040
3056
  const restPresentationFolder = boundedContextFolder.subitem([
@@ -1,2 +1,2 @@
1
- import type { Folder } from '../file-system';
1
+ import { Folder } from '../file-system';
2
2
  export declare const scaffoldNewErrorUseCase: (boundedContextFolder: Folder) => Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
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": "3.3.0",
4
+ "version": "3.3.1",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -42,9 +42,10 @@ First prompt (select): `New error without namespace` | `New namespace` | `New er
42
42
  `… pick:Domain pick:Errors "pick:New error in namespace order" text:not-found`
43
43
  `… pick:Domain pick:Errors "pick:New error without" text:order-closed`
44
44
 
45
- - Without namespace: appends `export const NCtxError = CtxErrors.define('n')` to `<C>/domain/errors/index.ts`
46
- - New namespace: `<C>/domain/errors/ns.ts` (`NsCtxErrors = errorNamespace('ctx').subnamespace('ns')`) + `export * from './ns'` in `index.ts`
47
- - In namespace: appends `NNsCtxError = NsCtxErrors.define('n')` to `ns.ts`
45
+ - Without namespace: appends `export class N extends CtxErrors.define('n') {}` to `<C>/domain/errors/index.ts`
46
+ - New namespace: `<C>/domain/errors/ns.ts` (`NsErrors = errorNamespace('ctx').subnamespace('ns')`) + `export * from './ns'` in `index.ts`
47
+ - In namespace: appends `export class NsN extends NsErrors.define('n') {}` to `ns.ts` (namespace first), extending the namespace the file exports
48
+ - Shared Layer: errors end with `SharedError`, namespaces with `SharedErrors`: `NSharedError`, `NsSharedErrors`, `NsNSharedError`
48
49
 
49
50
  Re-running is safe: an existing namespace file is kept, and a line already in the file isn't added again.
50
51