netcore-blueprint 0.0.28 → 0.0.30

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.
@@ -5,31 +5,38 @@ const path = require('path');
5
5
  const { execSync } = require('child_process');
6
6
 
7
7
  // =====================================================
8
- // Param = Source Module Name
9
- // Example: copy-module __MODULE__
10
- // Will create: Modules\__MODULE__Module\*
8
+ // Usage:
9
+ // copy-module __MODULE__ [--force]
10
+ //
11
+ // Template structure:
12
+ // Modules\__MODULE__Module\
13
+ // __MODULE__Module.API\
14
+ // __MODULE__Module.Core\
15
+ // __MODULE__Module.Infrastructure\
11
16
  // =====================================================
12
17
 
13
18
  const sourceModuleName = process.argv[2];
14
19
  const force = process.argv.includes('--force');
15
20
 
16
21
  if (!sourceModuleName) {
17
- console.error("❌ Usage: copy-module <SourceModuleName>");
22
+ console.error("❌ Usage: copy-module <SourceModuleName> [--force]");
18
23
  console.error(" Example: copy-module __MODULE__");
19
24
  process.exit(1);
20
25
  }
21
26
 
22
- // Assume current working directory is Modules/
27
+ // Assume cwd = Modules/
23
28
  const modulesPath = process.cwd();
24
29
 
25
- const templateRoot = path.join(modulesPath, sourceModuleName);
30
+ const templateModuleRoot = path.join(modulesPath, `${sourceModuleName}Module`);
26
31
  const targetModuleName = `${sourceModuleName}Module`;
27
32
  const targetModuleRoot = path.join(modulesPath, targetModuleName);
28
33
 
29
34
  const solutionFile = findSolutionFileUpwards(modulesPath);
30
35
 
31
- if (!fs.existsSync(templateRoot)) {
32
- console.error(`❌ Source module folder not found: ${templateRoot}`);
36
+ // ================= Guards =================
37
+
38
+ if (!fs.existsSync(templateModuleRoot)) {
39
+ console.error(`❌ Template module not found: ${templateModuleRoot}`);
33
40
  process.exit(1);
34
41
  }
35
42
 
@@ -40,7 +47,7 @@ if (fs.existsSync(targetModuleRoot)) {
40
47
  process.exit(1);
41
48
  }
42
49
 
43
- console.log(`🔥 --force detected. Removing existing module: ${targetModuleRoot}`);
50
+ console.log(`🔥 --force detected. Removing: ${targetModuleRoot}`);
44
51
  fs.rmSync(targetModuleRoot, { recursive: true, force: true });
45
52
  }
46
53
 
@@ -49,103 +56,73 @@ if (!solutionFile) {
49
56
  process.exit(1);
50
57
  }
51
58
 
52
- console.log(`🚀 Cloning module template:`);
53
- console.log(` From: ${templateRoot}`);
54
- console.log(` To: ${targetModuleRoot}`);
59
+ console.log(`🚀 Cloning module template`);
60
+ console.log(`📁 From: ${templateModuleRoot}`);
61
+ console.log(`📁 To: ${targetModuleRoot}`);
55
62
  console.log(`🧩 Solution: ${solutionFile}`);
56
63
 
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
- ];
64
+ // ================= 1. Copy whole module folder =================
72
65
 
73
- projects.forEach(p => {
74
- console.log(`📦 Creating project: ${p.name}`);
66
+ copyRecursive(templateModuleRoot, targetModuleRoot);
75
67
 
76
- execSync(`dotnet new ${p.type} -n ${p.name}`, {
77
- cwd: targetModuleRoot,
78
- stdio: 'inherit'
79
- });
68
+ // ================= 2. Collect csproj files =================
80
69
 
81
- const projectPath = path.join(targetModuleRoot, p.name);
70
+ const csprojFiles = findCsprojFiles(targetModuleRoot);
82
71
 
83
- // =====================================================
84
- // 3. Copy template files (NO csproj)
85
- // =====================================================
72
+ if (csprojFiles.length === 0) {
73
+ console.error("❌ No .csproj files found in cloned module.");
74
+ process.exit(1);
75
+ }
86
76
 
87
- const templateSubFolder = getTemplateSubFolder(p.name, templateRoot);
77
+ // ================= 3. Add to solution (NO wildcard) =================
88
78
 
89
- if (!templateSubFolder || !fs.existsSync(templateSubFolder)) {
90
- console.warn(`⚠️ Template subfolder not found: ${templateSubFolder}`);
91
- return;
92
- }
79
+ console.log(`📁 Creating solution folder: Modules\\${targetModuleName}`);
93
80
 
94
- console.log(`📄 Copying template from: ${templateSubFolder}`);
95
- copyRecursive(templateSubFolder, projectPath);
81
+ csprojFiles.forEach(csproj => {
82
+ console.log(`➕ Adding to solution: ${csproj}`);
83
+ execSync(
84
+ `dotnet sln "${solutionFile}" add --solution-folder "Modules\\${targetModuleName}" "${csproj}"`,
85
+ { stdio: 'inherit' }
86
+ );
96
87
  });
97
88
 
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
- );
107
-
108
- console.log(`🎉 Module "${targetModuleName}" created successfully!`);
89
+ console.log(`🎉 Module "${targetModuleName}" cloned successfully!`);
109
90
 
110
91
  // =====================================================
111
92
  // Helpers
112
93
  // =====================================================
113
94
 
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
- }
126
-
127
95
  function copyRecursive(src, dest) {
128
- if (!fs.existsSync(dest)) {
96
+ const stat = fs.lstatSync(src);
97
+
98
+ if (stat.isDirectory()) {
129
99
  fs.mkdirSync(dest, { recursive: true });
100
+ const entries = fs.readdirSync(src, { withFileTypes: true });
101
+
102
+ for (const entry of entries) {
103
+ const srcPath = path.join(src, entry.name);
104
+ const destPath = path.join(dest, entry.name);
105
+ copyRecursive(srcPath, destPath);
106
+ }
107
+ } else {
108
+ fs.copyFileSync(src, dest);
130
109
  }
110
+ }
131
111
 
132
- const entries = fs.readdirSync(src, { withFileTypes: true });
112
+ function findCsprojFiles(dir, results = []) {
113
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
133
114
 
134
115
  for (const entry of entries) {
135
- const srcPath = path.join(src, entry.name);
136
- const destPath = path.join(dest, entry.name);
137
-
138
- // Do NOT overwrite .csproj
139
- if (entry.isFile() && entry.name.endsWith('.csproj')) {
140
- continue;
141
- }
116
+ const fullPath = path.join(dir, entry.name);
142
117
 
143
118
  if (entry.isDirectory()) {
144
- copyRecursive(srcPath, destPath);
145
- } else {
146
- fs.copyFileSync(srcPath, destPath);
119
+ findCsprojFiles(fullPath, results);
120
+ } else if (entry.isFile() && entry.name.endsWith('.csproj')) {
121
+ results.push(fullPath);
147
122
  }
148
123
  }
124
+
125
+ return results;
149
126
  }
150
127
 
151
128
  function findSolutionFileUpwards(startDir) {
@@ -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.30",
4
4
  "description": "A custom project blueprint",
5
5
  "main": "create.js",
6
6
  "bin": {