create-backlist 6.0.9 → 6.1.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/package.json +1 -1
- package/src/generators/node.js +26 -15
package/package.json
CHANGED
package/src/generators/node.js
CHANGED
|
@@ -14,6 +14,8 @@ async function generateNodeProject(options) {
|
|
|
14
14
|
try {
|
|
15
15
|
// --- Step 1: Analyze Frontend ---
|
|
16
16
|
console.log(chalk.blue(' -> Analyzing frontend for API endpoints...'));
|
|
17
|
+
|
|
18
|
+
// NOTE: 'let' use kala api endpoints wenas karana nisa
|
|
17
19
|
let endpoints = await analyzeFrontend(frontendSrcDir);
|
|
18
20
|
|
|
19
21
|
if (endpoints.length > 0) {
|
|
@@ -23,18 +25,19 @@ async function generateNodeProject(options) {
|
|
|
23
25
|
// 🔥 FIX START: Sanitizing Endpoints Logic
|
|
24
26
|
// ============================================================
|
|
25
27
|
endpoints = endpoints.map(ep => {
|
|
26
|
-
// 1. Path
|
|
27
|
-
// 'api', 'v1',
|
|
28
|
+
// 1. Path eka sudda kirima (/api/v1/users -> ['users'])
|
|
29
|
+
// 'api', 'v1', histhan ain karanawa
|
|
28
30
|
const parts = ep.path.split('/').filter(part => part !== '' && part !== 'api' && part !== 'v1');
|
|
29
31
|
|
|
30
|
-
// Resource
|
|
32
|
+
// Resource eka hoyaganeema (e.g., 'users')
|
|
31
33
|
let resource = parts[0] || 'Default';
|
|
32
34
|
|
|
33
|
-
// 2. Controller Name
|
|
34
|
-
// Special Case: resource
|
|
35
|
+
// 2. Controller Name eka hadeema (CamelCase: 'users' -> 'Users')
|
|
36
|
+
// Special Case: resource eka 'auth' nam Controller eka 'Auth'
|
|
37
|
+
// 'V1' kiyana eka ain wenne methanin
|
|
35
38
|
let controllerName = resource.charAt(0).toUpperCase() + resource.slice(1);
|
|
36
39
|
|
|
37
|
-
// 3. Function Names
|
|
40
|
+
// 3. Function Names hariyatama map kirima
|
|
38
41
|
let functionName = '';
|
|
39
42
|
|
|
40
43
|
// --- AUTH LOGIC ---
|
|
@@ -45,8 +48,7 @@ async function generateNodeProject(options) {
|
|
|
45
48
|
}
|
|
46
49
|
// --- GENERAL RESOURCES LOGIC ---
|
|
47
50
|
else {
|
|
48
|
-
//
|
|
49
|
-
// 'users' -> 'Users' (Plural form for lists)
|
|
51
|
+
// Singular/Plural logic to avoid 'Userss'
|
|
50
52
|
const singularName = resource.endsWith('s') ? resource.slice(0, -1) : resource;
|
|
51
53
|
const pluralName = resource.endsWith('s') ? resource : resource + 's';
|
|
52
54
|
|
|
@@ -68,10 +70,11 @@ async function generateNodeProject(options) {
|
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
// Update the endpoint object
|
|
73
|
+
// meka ejs file ekedi <%= ep.functionName %> kiyala use karanna puluwan
|
|
71
74
|
return {
|
|
72
75
|
...ep,
|
|
73
76
|
controllerName,
|
|
74
|
-
functionName
|
|
77
|
+
functionName
|
|
75
78
|
};
|
|
76
79
|
});
|
|
77
80
|
// ============================================================
|
|
@@ -82,14 +85,22 @@ async function generateNodeProject(options) {
|
|
|
82
85
|
console.log(chalk.yellow(' -> No API endpoints found. A basic project will be created.'));
|
|
83
86
|
}
|
|
84
87
|
|
|
85
|
-
|
|
88
|
+
// --- Step 2: Identify Models to Generate ---
|
|
86
89
|
const modelsToGenerate = new Map();
|
|
87
90
|
endpoints.forEach(ep => {
|
|
88
|
-
//
|
|
89
|
-
|
|
91
|
+
// 🔥 FIX: 'ep.schemaFields' තිබ්බත් නැතත් Controller එක හදන්න ඕන.
|
|
92
|
+
// නැත්නම් Routes Import එකේදි Error එනවා.
|
|
93
|
+
if (ep.controllerName !== 'Default' && ep.controllerName !== 'Auth' && !modelsToGenerate.has(ep.controllerName)) {
|
|
94
|
+
|
|
95
|
+
// Schema Fields නැත්නම් හිස් Array එකක් ගන්න
|
|
96
|
+
let fields = [];
|
|
97
|
+
if (ep.schemaFields) {
|
|
98
|
+
fields = Object.entries(ep.schemaFields).map(([key, type]) => ({ name: key, type, isUnique: key === 'email' }));
|
|
99
|
+
}
|
|
100
|
+
|
|
90
101
|
modelsToGenerate.set(ep.controllerName, {
|
|
91
102
|
name: ep.controllerName,
|
|
92
|
-
fields:
|
|
103
|
+
fields: fields
|
|
93
104
|
});
|
|
94
105
|
}
|
|
95
106
|
});
|
|
@@ -160,7 +171,7 @@ async function generateNodeProject(options) {
|
|
|
160
171
|
console.log(chalk.blue(' -> Generating controllers...'));
|
|
161
172
|
for (const [modelName] of modelsToGenerate.entries()) {
|
|
162
173
|
const templateFile = dbType === 'mongoose' ? 'Controller.ts.ejs' : 'PrismaController.ts.ejs';
|
|
163
|
-
//
|
|
174
|
+
// Controller hadaddi Auth eka skip karanawa (mokada eka yatin wenama hadanawa)
|
|
164
175
|
if (modelName !== 'Auth') {
|
|
165
176
|
await renderAndWrite(getTemplatePath(`node-ts-express/partials/${templateFile}`), path.join(destSrcDir, 'controllers', `${modelName}.controller.ts`), { modelName, projectName });
|
|
166
177
|
}
|
|
@@ -217,7 +228,7 @@ async function generateNodeProject(options) {
|
|
|
217
228
|
}
|
|
218
229
|
|
|
219
230
|
// --- Step 9: Generate Main Route File & Inject Logic into Server ---
|
|
220
|
-
//
|
|
231
|
+
// IMPORTANT: 'endpoints' variable eka dan sanitized version eka.
|
|
221
232
|
await renderAndWrite(getTemplatePath('node-ts-express/partials/routes.ts.ejs'), path.join(destSrcDir, 'routes.ts'), { endpoints, addAuth, dbType });
|
|
222
233
|
|
|
223
234
|
let serverFileContent = await fs.readFile(path.join(destSrcDir, 'server.ts'), 'utf-8');
|