netcore-blueprint 0.0.28 → 0.0.29

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.
@@ -2,169 +2,111 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
- const { execSync } = require('child_process');
6
-
7
- // =====================================================
8
- // Param = Source Module Name
9
- // Example: copy-module __MODULE__
10
- // Will create: Modules\__MODULE__Module\*
11
- // =====================================================
12
-
13
- const sourceModuleName = process.argv[2];
14
- const force = process.argv.includes('--force');
15
-
16
- if (!sourceModuleName) {
17
- console.error("❌ Usage: copy-module <SourceModuleName>");
18
- console.error(" Example: copy-module __MODULE__");
5
+ const { buildReplaceMap, applyReplacements } = require('../lib/replacer');
6
+
7
+ // =======================
8
+ // Args
9
+ // =======================
10
+ // Usage:
11
+ // create-module <NewModuleName> <NewEntityName>
12
+ // Example:
13
+ // create-module AiAssistant Prompt
14
+ // =======================
15
+
16
+ const newModuleName = process.argv[2];
17
+ const newEntityName = process.argv[3];
18
+
19
+ if (!newModuleName || !newEntityName) {
20
+ console.error("❌ Usage: create-module <NewModuleName> <NewEntityName>");
21
+ console.error(" Example: create-module AiAssistant Prompt");
19
22
  process.exit(1);
20
23
  }
21
24
 
22
- // Assume current working directory is Modules/
23
- const modulesPath = process.cwd();
25
+ // =======================
26
+ // Template Source (Module mẫu)
27
+ // =======================
24
28
 
25
- const templateRoot = path.join(modulesPath, sourceModuleName);
26
- const targetModuleName = `${sourceModuleName}Module`;
27
- const targetModuleRoot = path.join(modulesPath, targetModuleName);
29
+ const sourceModuleName = "ItemModule"; // module mẫu
30
+ const sourceEntityName = "Item"; // entity mẫu
28
31
 
29
- const solutionFile = findSolutionFileUpwards(modulesPath);
32
+ const modulesTemplateRoot = path.join(__dirname, "..", "Modules");
33
+ const sourceModulePath = path.join(modulesTemplateRoot, sourceModuleName);
30
34
 
31
- if (!fs.existsSync(templateRoot)) {
32
- console.error(`❌ Source module folder not found: ${templateRoot}`);
33
- process.exit(1);
34
- }
35
+ // =======================
36
+ // Destination (nơi user đang đứng)
37
+ // =======================
35
38
 
36
- if (fs.existsSync(targetModuleRoot)) {
37
- if (!force) {
38
- console.error(`❌ Target module already exists: ${targetModuleRoot}`);
39
- console.error(` Use --force to overwrite.`);
40
- process.exit(1);
41
- }
39
+ const destinationRoot = process.cwd(); // thường là thư mục Modules
40
+ const destinationModulePath = path.join(destinationRoot, newModuleName);
42
41
 
43
- console.log(`🔥 --force detected. Removing existing module: ${targetModuleRoot}`);
44
- fs.rmSync(targetModuleRoot, { recursive: true, force: true });
45
- }
42
+ // =======================
43
+ // Guards
44
+ // =======================
46
45
 
47
- if (!solutionFile) {
48
- console.error("❌ Could not find .sln or .slnx file.");
46
+ if (!fs.existsSync(sourceModulePath)) {
47
+ console.error(`❌ Source module template not found: ${sourceModulePath}`);
49
48
  process.exit(1);
50
49
  }
51
50
 
52
- console.log(`🚀 Cloning module template:`);
53
- console.log(` From: ${templateRoot}`);
54
- console.log(` To: ${targetModuleRoot}`);
55
- console.log(`🧩 Solution: ${solutionFile}`);
56
-
57
- // =====================================================
58
- // 1. Create module root folder
59
- // =====================================================
60
-
61
- fs.mkdirSync(targetModuleRoot, { recursive: true });
62
-
63
- // =====================================================
64
- // 2. Create projects (ALL classlib)
65
- // =====================================================
66
-
67
- const projects = [
68
- { name: `${targetModuleName}.API`, type: 'classlib' },
69
- { name: `${targetModuleName}.Core`, type: 'classlib' },
70
- { name: `${targetModuleName}.Infrastructure`, type: 'classlib' },
71
- ];
72
-
73
- projects.forEach(p => {
74
- console.log(`📦 Creating project: ${p.name}`);
75
-
76
- execSync(`dotnet new ${p.type} -n ${p.name}`, {
77
- cwd: targetModuleRoot,
78
- stdio: 'inherit'
79
- });
80
-
81
- const projectPath = path.join(targetModuleRoot, p.name);
82
-
83
- // =====================================================
84
- // 3. Copy template files (NO csproj)
85
- // =====================================================
86
-
87
- const templateSubFolder = getTemplateSubFolder(p.name, templateRoot);
51
+ if (fs.existsSync(destinationModulePath)) {
52
+ console.error(`❌ Destination module already exists: ${destinationModulePath}`);
53
+ process.exit(1);
54
+ }
88
55
 
89
- if (!templateSubFolder || !fs.existsSync(templateSubFolder)) {
90
- console.warn(`⚠️ Template subfolder not found: ${templateSubFolder}`);
91
- return;
92
- }
56
+ // =======================
57
+ // Replace Map
58
+ // =======================
93
59
 
94
- console.log(`📄 Copying template from: ${templateSubFolder}`);
95
- copyRecursive(templateSubFolder, projectPath);
60
+ const replaceMap = buildReplaceMap({
61
+ sourceModuleName,
62
+ newModuleName,
63
+ sourceEntityName,
64
+ newEntityName
96
65
  });
97
66
 
98
- // =====================================================
99
- // 4. Add Solution Folder + add projects under it
100
- // =====================================================
101
-
102
- console.log(`📁 Creating solution folder: Modules\\${targetModuleName}`);
103
- execSync(
104
- `dotnet sln "${solutionFile}" add --solution-folder "Modules\\${targetModuleName}" "${targetModuleRoot}\\*\\*.csproj"`,
105
- { stdio: 'inherit' }
106
- );
67
+ // =======================
68
+ // Logs
69
+ // =======================
107
70
 
108
- console.log(`🎉 Module "${targetModuleName}" created successfully!`);
71
+ console.log(`🚀 Creating module from template`);
72
+ console.log(`📦 New Module: ${newModuleName}`);
73
+ console.log(`🧩 New Entity: ${newEntityName}`);
74
+ console.log(`📁 Template: ${sourceModulePath}`);
75
+ console.log(`📁 Target: ${destinationModulePath}`);
109
76
 
110
- // =====================================================
111
- // Helpers
112
- // =====================================================
77
+ // =======================
78
+ // Run
79
+ // =======================
113
80
 
114
- function getTemplateSubFolder(projectName, templateRoot) {
115
- if (projectName.endsWith('.API')) {
116
- return path.join(templateRoot, 'API');
117
- }
118
- if (projectName.endsWith('.Core')) {
119
- return path.join(templateRoot, 'Core');
120
- }
121
- if (projectName.endsWith('.Infrastructure')) {
122
- return path.join(templateRoot, 'Infrastructure');
123
- }
124
- return null;
125
- }
81
+ copyAndRenameFiles(sourceModulePath, destinationModulePath);
126
82
 
127
- function copyRecursive(src, dest) {
128
- if (!fs.existsSync(dest)) {
129
- fs.mkdirSync(dest, { recursive: true });
130
- }
83
+ console.log(`🎉 Module "${newModuleName}" created with Entity "${newEntityName}" successfully!`);
131
84
 
132
- const entries = fs.readdirSync(src, { withFileTypes: true });
85
+ // =======================
86
+ // Core Logic
87
+ // =======================
133
88
 
134
- for (const entry of entries) {
135
- const srcPath = path.join(src, entry.name);
136
- const destPath = path.join(dest, entry.name);
89
+ function copyAndRenameFiles(src, dest) {
90
+ const stat = fs.lstatSync(src);
137
91
 
138
- // Do NOT overwrite .csproj
139
- if (entry.isFile() && entry.name.endsWith('.csproj')) {
140
- continue;
141
- }
92
+ if (stat.isDirectory()) {
93
+ fs.mkdirSync(dest, { recursive: true });
142
94
 
143
- if (entry.isDirectory()) {
144
- copyRecursive(srcPath, destPath);
145
- } else {
146
- fs.copyFileSync(srcPath, destPath);
147
- }
148
- }
149
- }
95
+ const entries = fs.readdirSync(src, { withFileTypes: true });
150
96
 
151
- function findSolutionFileUpwards(startDir) {
152
- let current = startDir;
97
+ for (const entry of entries) {
98
+ const srcPath = path.join(src, entry.name);
153
99
 
154
- while (true) {
155
- const files = fs.readdirSync(current);
156
- let sln = files.find(f => f.endsWith('.sln') || f.endsWith('.slnx'));
157
- if (sln) return path.join(current, sln);
100
+ // Rename folder/file name
101
+ const renamedName = applyReplacements(entry.name, replaceMap);
102
+ const destPath = path.join(dest, renamedName);
158
103
 
159
- const blueprintTemplate = path.join(current, 'BlueprintTemplate');
160
- if (fs.existsSync(blueprintTemplate)) {
161
- const btFiles = fs.readdirSync(blueprintTemplate);
162
- sln = btFiles.find(f => f.endsWith('.sln') || f.endsWith('.slnx'));
163
- if (sln) return path.join(blueprintTemplate, sln);
104
+ copyAndRenameFiles(srcPath, destPath);
164
105
  }
165
-
166
- const parent = path.dirname(current);
167
- if (parent === current) return null;
168
- current = parent;
106
+ } else {
107
+ // Copy + replace file content
108
+ let content = fs.readFileSync(src, 'utf8');
109
+ content = applyReplacements(content, replaceMap);
110
+ fs.writeFileSync(dest, content, 'utf8');
169
111
  }
170
112
  }
@@ -8,35 +8,33 @@ const { buildReplaceMap, applyReplacements } = require('../lib/replacer');
8
8
  // Args
9
9
  // =======================
10
10
  // Usage:
11
- // create-module <NewModuleName> <NewEntityName>
12
- // Example:
13
11
  // create-module AiAssistant Prompt
14
- // =======================
12
+ //
15
13
 
16
14
  const newModuleName = process.argv[2];
17
15
  const newEntityName = process.argv[3];
18
16
 
19
17
  if (!newModuleName || !newEntityName) {
20
- console.error("❌ Usage: create-module <NewModuleName> <NewEntityName>");
18
+ console.error("❌ Usage: create-module <ModuleName> <EntityName>");
21
19
  console.error(" Example: create-module AiAssistant Prompt");
22
20
  process.exit(1);
23
21
  }
24
22
 
25
23
  // =======================
26
- // Template Source (Module mẫu)
24
+ // Template source
27
25
  // =======================
28
26
 
29
- const sourceModuleName = "ItemModule"; // module mẫu
30
- const sourceEntityName = "Item"; // entity mẫu
27
+ const sourceModuleName = "ItemModule";
28
+ const sourceEntityName = "Item";
31
29
 
32
30
  const modulesTemplateRoot = path.join(__dirname, "..", "Modules");
33
31
  const sourceModulePath = path.join(modulesTemplateRoot, sourceModuleName);
34
32
 
35
33
  // =======================
36
- // Destination (nơi user đang đứng)
34
+ // Destination
37
35
  // =======================
38
36
 
39
- const destinationRoot = process.cwd(); // thường là thư mục Modules
37
+ const destinationRoot = process.cwd();
40
38
  const destinationModulePath = path.join(destinationRoot, newModuleName);
41
39
 
42
40
  // =======================
@@ -44,7 +42,7 @@ const destinationModulePath = path.join(destinationRoot, newModuleName);
44
42
  // =======================
45
43
 
46
44
  if (!fs.existsSync(sourceModulePath)) {
47
- console.error(`❌ Source module template not found: ${sourceModulePath}`);
45
+ console.error(`❌ Source module not found: ${sourceModulePath}`);
48
46
  process.exit(1);
49
47
  }
50
48
 
@@ -64,15 +62,10 @@ const replaceMap = buildReplaceMap({
64
62
  newEntityName
65
63
  });
66
64
 
67
- // =======================
68
- // Logs
69
- // =======================
70
-
71
- console.log(`🚀 Creating module from template`);
72
- console.log(`📦 New Module: ${newModuleName}`);
73
- console.log(`🧩 New Entity: ${newEntityName}`);
74
- console.log(`📁 Template: ${sourceModulePath}`);
75
- console.log(`📁 Target: ${destinationModulePath}`);
65
+ console.log(`📦 Creating module: ${newModuleName}`);
66
+ console.log(`🧩 Entity name: ${newEntityName}`);
67
+ console.log(`📁 From template: ${sourceModulePath}`);
68
+ console.log(`📁 To: ${destinationModulePath}`);
76
69
 
77
70
  // =======================
78
71
  // Run
@@ -80,7 +73,7 @@ console.log(`📁 Target: ${destinationModulePath}`);
80
73
 
81
74
  copyAndRenameFiles(sourceModulePath, destinationModulePath);
82
75
 
83
- console.log(`🎉 Module "${newModuleName}" created with Entity "${newEntityName}" successfully!`);
76
+ console.log(`🎉 Module "${newModuleName}" with Entity "${newEntityName}" created successfully!`);
84
77
 
85
78
  // =======================
86
79
  // Core Logic
@@ -97,14 +90,12 @@ function copyAndRenameFiles(src, dest) {
97
90
  for (const entry of entries) {
98
91
  const srcPath = path.join(src, entry.name);
99
92
 
100
- // Rename folder/file name
101
- const renamedName = applyReplacements(entry.name, replaceMap);
102
- const destPath = path.join(dest, renamedName);
93
+ let renamed = applyReplacements(entry.name, replaceMap);
94
+ const destPath = path.join(dest, renamed);
103
95
 
104
96
  copyAndRenameFiles(srcPath, destPath);
105
97
  }
106
98
  } else {
107
- // Copy + replace file content
108
99
  let content = fs.readFileSync(src, 'utf8');
109
100
  content = applyReplacements(content, replaceMap);
110
101
  fs.writeFileSync(dest, content, 'utf8');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "netcore-blueprint",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "A custom project blueprint",
5
5
  "main": "create.js",
6
6
  "bin": {