@domain-first/project-structure 2.10.0 → 2.10.2

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
@@ -263,7 +263,7 @@ src/bounded-contexts/sales/
263
263
 
264
264
  Use lowercase kebab-case for entered names, such as `place-order` and `payment-gateway`. Names are used in paths, and class and variable names are derived from their hyphen-separated words: `place-order` becomes `PlaceOrderUseCase` for a use case.
265
265
 
266
- Generation writes files directly and can overwrite matching filenames without confirmation. Use a new name for a new component, and review the generated changes before continuing implementation.
266
+ Nothing is written until a run finishes. If the run would replace existing files, the CLI lists them and asks `Overwrite them?` (default: No). Answering No, or interrupting the CLI at any prompt, leaves the project unchanged. Lines added to existing barrels, error files, and wiring indexes are not overwrites and are never duplicated. Use a new name for a new component, and review the generated changes before continuing implementation.
267
267
 
268
268
  ## License
269
269
 
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Existing files a full rewrite would replace, whatever their content.
3
+ */
4
+ export declare const filesToOverwrite: () => string[];
5
+ export declare const writePendingChanges: () => void;
1
6
  declare class FileSystemItem {
2
7
  readonly path: string;
3
8
  constructor(path: string);
@@ -15,6 +20,7 @@ export declare class Folder extends FileSystemItem {
15
20
  file: (name: string) => File;
16
21
  }
17
22
  export declare class File extends FileSystemItem {
23
+ get exists(): boolean;
18
24
  get data(): string;
19
25
  set data(content: string);
20
26
  addLine: (newLine: string, separator?: string) => void;
package/dist/index.cjs CHANGED
@@ -26,13 +26,31 @@ var __webpack_require__ = {};
26
26
  __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
27
27
  })();
28
28
  var __webpack_exports__ = {};
29
- const prompts_namespaceObject = require("@inquirer/prompts");
30
29
  const external_node_path_namespaceObject = require("node:path");
31
30
  var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
31
+ const prompts_namespaceObject = require("@inquirer/prompts");
32
32
  const external_zod_namespaceObject = require("zod");
33
33
  var external_zod_default = /*#__PURE__*/ __webpack_require__.n(external_zod_namespaceObject);
34
34
  const external_node_fs_namespaceObject = require("node:fs");
35
35
  var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
36
+ const pendingFiles = new Map();
37
+ const pendingFolders = new Set();
38
+ const filesToOverwrite = ()=>[
39
+ ...pendingFiles
40
+ ].filter(([filePath, { replacesFile }])=>replacesFile && external_node_fs_default().existsSync(filePath)).map(([filePath])=>filePath);
41
+ const writePendingChanges = ()=>{
42
+ for (const folderPath of pendingFolders)external_node_fs_default().mkdirSync(folderPath, {
43
+ recursive: true
44
+ });
45
+ for (const [filePath, { content }] of pendingFiles){
46
+ external_node_fs_default().mkdirSync(external_node_path_default().dirname(filePath), {
47
+ recursive: true
48
+ });
49
+ external_node_fs_default().writeFileSync(filePath, content, 'utf8');
50
+ }
51
+ pendingFolders.clear();
52
+ pendingFiles.clear();
53
+ };
36
54
  class FileSystemItem {
37
55
  path;
38
56
  constructor(path){
@@ -45,9 +63,7 @@ class FileSystemItem {
45
63
  }
46
64
  class Folder extends FileSystemItem {
47
65
  createIfNotExisted = ()=>{
48
- external_node_fs_default().mkdirSync(this.path, {
49
- recursive: true
50
- });
66
+ pendingFolders.add(this.path);
51
67
  };
52
68
  get content() {
53
69
  if (!this.exists) return {
@@ -82,22 +98,30 @@ class Folder extends FileSystemItem {
82
98
  };
83
99
  }
84
100
  class File extends FileSystemItem {
101
+ get exists() {
102
+ return pendingFiles.has(this.path) || external_node_fs_default().existsSync(this.path);
103
+ }
85
104
  get data() {
105
+ const pendingFile = pendingFiles.get(this.path);
106
+ if (pendingFile) return pendingFile.content;
86
107
  if (!this.exists) throw new Error('File does not exist');
87
108
  return external_node_fs_default().readFileSync(this.path, 'utf-8');
88
109
  }
89
110
  set data(content) {
90
- external_node_fs_default().writeFileSync(this.path, content);
111
+ pendingFiles.set(this.path, {
112
+ content,
113
+ replacesFile: true
114
+ });
91
115
  }
92
116
  addLine = (newLine, separator = '\n')=>{
93
- let content = '';
94
- try {
95
- content = external_node_fs_default().readFileSync(this.path, 'utf8');
96
- } catch {}
97
- external_node_fs_default().writeFileSync(this.path, [
98
- content.trim(),
99
- newLine.trim()
100
- ].filter((x)=>!!x).join(separator), 'utf8');
117
+ const content = this.exists ? this.data : '';
118
+ pendingFiles.set(this.path, {
119
+ content: [
120
+ content.trim(),
121
+ newLine.trim()
122
+ ].filter((x)=>!!x).join(separator),
123
+ replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
124
+ });
101
125
  };
102
126
  addLineIfMissing = (newLine, separator = '\n')=>{
103
127
  const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
@@ -212,7 +236,7 @@ import { branchedWire } from '@domain-first/wire'
212
236
  export const envBranchedWire = branchedWire(() => {
213
237
  return process.env.NODE_ENV ?? 'development' as 'test' | 'development' | 'production'
214
238
  });
215
- `);
239
+ `.trim());
216
240
  if (domainFirstPackages.includes('@domain-first/errors')) rootFolder.subitem([
217
241
  rootFolderName,
218
242
  'shared'
@@ -289,7 +313,7 @@ describe('${aggregateNaming.withSpaces}', () => {
289
313
  expect(${aggregateNaming.variableName}).toBeDefined()
290
314
  })
291
315
  })
292
- `);
316
+ `.trim());
293
317
  if (withRepository) {
294
318
  aggregatesFolder.createFile(`${aggregateNaming.fileName}-repository.ts`, `export abstract class ${aggregateNaming.ClassName}Repository { }`);
295
319
  barrelFile.addLine(`export * from './${aggregateNaming.fileName}-repository'`);
@@ -420,7 +444,7 @@ import { ${naming.ClassName} } from '../../../application/ports/${naming.fileNam
420
444
  export class ${currentNaming.ClassName} extends ${naming.ClassName} {
421
445
 
422
446
  }
423
- `);
447
+ `.trim());
424
448
  }
425
449
  if (scaffoldUnitTests) {
426
450
  const implementationNaming = new UnknownFormatNaming(implementationType);
@@ -959,7 +983,7 @@ export const wire${endpointNaming.ClassName} = wireClass(
959
983
  ${endpointNaming.ClassName},
960
984
  []
961
985
  )
962
- `);
986
+ `.trim());
963
987
  wiringFolder.file('index.ts').addLine(`export * from './${wireNaming.fileName}'`);
964
988
  }
965
989
  };
@@ -1680,7 +1704,22 @@ const main = async ()=>{
1680
1704
  return;
1681
1705
  }
1682
1706
  };
1683
- main();
1707
+ const run = async ()=>{
1708
+ await main();
1709
+ const overwrittenFiles = filesToOverwrite();
1710
+ if (overwrittenFiles.length) {
1711
+ const root = new ProjectRoot().path;
1712
+ console.log('These files already exist and will be overwritten:');
1713
+ for (const filePath of overwrittenFiles)console.log(` ${external_node_path_default().relative(root, filePath)}`);
1714
+ const overwrite = await (0, prompts_namespaceObject.confirm)({
1715
+ message: 'Overwrite them?',
1716
+ default: false
1717
+ });
1718
+ if (!overwrite) return void console.log('Cancelled: nothing was written.');
1719
+ }
1720
+ writePendingChanges();
1721
+ };
1722
+ run();
1684
1723
  for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
1685
1724
  Object.defineProperty(exports, '__esModule', {
1686
1725
  value: true
package/dist/index.js CHANGED
@@ -1,8 +1,26 @@
1
1
  #!/usr/bin/env node
2
- import { checkbox as prompts_checkbox, confirm as prompts_confirm, input, select as prompts_select } from "@inquirer/prompts";
3
2
  import node_path from "node:path";
3
+ import { checkbox as prompts_checkbox, confirm as prompts_confirm, input, select as prompts_select } from "@inquirer/prompts";
4
4
  import zod from "zod";
5
5
  import node_fs from "node:fs";
6
+ const pendingFiles = new Map();
7
+ const pendingFolders = new Set();
8
+ const filesToOverwrite = ()=>[
9
+ ...pendingFiles
10
+ ].filter(([filePath, { replacesFile }])=>replacesFile && node_fs.existsSync(filePath)).map(([filePath])=>filePath);
11
+ const writePendingChanges = ()=>{
12
+ for (const folderPath of pendingFolders)node_fs.mkdirSync(folderPath, {
13
+ recursive: true
14
+ });
15
+ for (const [filePath, { content }] of pendingFiles){
16
+ node_fs.mkdirSync(node_path.dirname(filePath), {
17
+ recursive: true
18
+ });
19
+ node_fs.writeFileSync(filePath, content, 'utf8');
20
+ }
21
+ pendingFolders.clear();
22
+ pendingFiles.clear();
23
+ };
6
24
  class FileSystemItem {
7
25
  path;
8
26
  constructor(path){
@@ -15,9 +33,7 @@ class FileSystemItem {
15
33
  }
16
34
  class Folder extends FileSystemItem {
17
35
  createIfNotExisted = ()=>{
18
- node_fs.mkdirSync(this.path, {
19
- recursive: true
20
- });
36
+ pendingFolders.add(this.path);
21
37
  };
22
38
  get content() {
23
39
  if (!this.exists) return {
@@ -52,22 +68,30 @@ class Folder extends FileSystemItem {
52
68
  };
53
69
  }
54
70
  class File extends FileSystemItem {
71
+ get exists() {
72
+ return pendingFiles.has(this.path) || node_fs.existsSync(this.path);
73
+ }
55
74
  get data() {
75
+ const pendingFile = pendingFiles.get(this.path);
76
+ if (pendingFile) return pendingFile.content;
56
77
  if (!this.exists) throw new Error('File does not exist');
57
78
  return node_fs.readFileSync(this.path, 'utf-8');
58
79
  }
59
80
  set data(content) {
60
- node_fs.writeFileSync(this.path, content);
81
+ pendingFiles.set(this.path, {
82
+ content,
83
+ replacesFile: true
84
+ });
61
85
  }
62
86
  addLine = (newLine, separator = '\n')=>{
63
- let content = '';
64
- try {
65
- content = node_fs.readFileSync(this.path, 'utf8');
66
- } catch {}
67
- node_fs.writeFileSync(this.path, [
68
- content.trim(),
69
- newLine.trim()
70
- ].filter((x)=>!!x).join(separator), 'utf8');
87
+ const content = this.exists ? this.data : '';
88
+ pendingFiles.set(this.path, {
89
+ content: [
90
+ content.trim(),
91
+ newLine.trim()
92
+ ].filter((x)=>!!x).join(separator),
93
+ replacesFile: pendingFiles.get(this.path)?.replacesFile ?? false
94
+ });
71
95
  };
72
96
  addLineIfMissing = (newLine, separator = '\n')=>{
73
97
  const alreadyAdded = this.exists && this.data.split('\n').some((line)=>line.trim() === newLine.trim());
@@ -182,7 +206,7 @@ import { branchedWire } from '@domain-first/wire'
182
206
  export const envBranchedWire = branchedWire(() => {
183
207
  return process.env.NODE_ENV ?? 'development' as 'test' | 'development' | 'production'
184
208
  });
185
- `);
209
+ `.trim());
186
210
  if (domainFirstPackages.includes('@domain-first/errors')) rootFolder.subitem([
187
211
  rootFolderName,
188
212
  'shared'
@@ -259,7 +283,7 @@ describe('${aggregateNaming.withSpaces}', () => {
259
283
  expect(${aggregateNaming.variableName}).toBeDefined()
260
284
  })
261
285
  })
262
- `);
286
+ `.trim());
263
287
  if (withRepository) {
264
288
  aggregatesFolder.createFile(`${aggregateNaming.fileName}-repository.ts`, `export abstract class ${aggregateNaming.ClassName}Repository { }`);
265
289
  barrelFile.addLine(`export * from './${aggregateNaming.fileName}-repository'`);
@@ -390,7 +414,7 @@ import { ${naming.ClassName} } from '../../../application/ports/${naming.fileNam
390
414
  export class ${currentNaming.ClassName} extends ${naming.ClassName} {
391
415
 
392
416
  }
393
- `);
417
+ `.trim());
394
418
  }
395
419
  if (scaffoldUnitTests) {
396
420
  const implementationNaming = new UnknownFormatNaming(implementationType);
@@ -929,7 +953,7 @@ export const wire${endpointNaming.ClassName} = wireClass(
929
953
  ${endpointNaming.ClassName},
930
954
  []
931
955
  )
932
- `);
956
+ `.trim());
933
957
  wiringFolder.file('index.ts').addLine(`export * from './${wireNaming.fileName}'`);
934
958
  }
935
959
  };
@@ -1650,4 +1674,19 @@ const main = async ()=>{
1650
1674
  return;
1651
1675
  }
1652
1676
  };
1653
- main();
1677
+ const run = async ()=>{
1678
+ await main();
1679
+ const overwrittenFiles = filesToOverwrite();
1680
+ if (overwrittenFiles.length) {
1681
+ const root = new ProjectRoot().path;
1682
+ console.log('These files already exist and will be overwritten:');
1683
+ for (const filePath of overwrittenFiles)console.log(` ${node_path.relative(root, filePath)}`);
1684
+ const overwrite = await prompts_confirm({
1685
+ message: 'Overwrite them?',
1686
+ default: false
1687
+ });
1688
+ if (!overwrite) return void console.log('Cancelled: nothing was written.');
1689
+ }
1690
+ writePendingChanges();
1691
+ };
1692
+ run();
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": "2.10.0",
4
+ "version": "2.10.2",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",