create-backlist 6.0.8 → 6.0.9
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 +79 -6
package/package.json
CHANGED
package/src/generators/node.js
CHANGED
|
@@ -14,17 +14,86 @@ 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
|
-
|
|
19
|
-
|
|
17
|
+
let endpoints = await analyzeFrontend(frontendSrcDir);
|
|
18
|
+
|
|
19
|
+
if (endpoints.length > 0) {
|
|
20
|
+
console.log(chalk.green(` -> Found ${endpoints.length} endpoints.`));
|
|
21
|
+
|
|
22
|
+
// ============================================================
|
|
23
|
+
// 🔥 FIX START: Sanitizing Endpoints Logic
|
|
24
|
+
// ============================================================
|
|
25
|
+
endpoints = endpoints.map(ep => {
|
|
26
|
+
// 1. Path එක සුද්ද කිරීම (/api/v1/users -> ['users'])
|
|
27
|
+
// 'api', 'v1', හිස්තැන් අයින් කරනවා
|
|
28
|
+
const parts = ep.path.split('/').filter(part => part !== '' && part !== 'api' && part !== 'v1');
|
|
29
|
+
|
|
30
|
+
// Resource එක හොයාගැනීම (e.g., 'users')
|
|
31
|
+
let resource = parts[0] || 'Default';
|
|
32
|
+
|
|
33
|
+
// 2. Controller Name එක හැදීම (CamelCase: 'users' -> 'Users')
|
|
34
|
+
// Special Case: resource එක 'auth' නම් Controller එක 'Auth'
|
|
35
|
+
let controllerName = resource.charAt(0).toUpperCase() + resource.slice(1);
|
|
36
|
+
|
|
37
|
+
// 3. Function Names හරියටම මැප් කිරීම
|
|
38
|
+
let functionName = '';
|
|
39
|
+
|
|
40
|
+
// --- AUTH LOGIC ---
|
|
41
|
+
if (controllerName.toLowerCase() === 'auth') {
|
|
42
|
+
if (ep.path.includes('login')) functionName = 'loginUser';
|
|
43
|
+
else if (ep.path.includes('register')) functionName = 'registerUser';
|
|
44
|
+
else functionName = 'authAction'; // fallback
|
|
45
|
+
}
|
|
46
|
+
// --- GENERAL RESOURCES LOGIC ---
|
|
47
|
+
else {
|
|
48
|
+
// 'users' -> 'User' (Singular form for function names like createUser)
|
|
49
|
+
// 'users' -> 'Users' (Plural form for lists)
|
|
50
|
+
const singularName = resource.endsWith('s') ? resource.slice(0, -1) : resource;
|
|
51
|
+
const pluralName = resource.endsWith('s') ? resource : resource + 's';
|
|
52
|
+
|
|
53
|
+
const pascalSingular = singularName.charAt(0).toUpperCase() + singularName.slice(1);
|
|
54
|
+
const pascalPlural = pluralName.charAt(0).toUpperCase() + pluralName.slice(1);
|
|
55
|
+
|
|
56
|
+
if (ep.method === 'GET') {
|
|
57
|
+
if (ep.path.includes(':id')) functionName = `get${pascalSingular}ById`;
|
|
58
|
+
else functionName = `getAll${pascalPlural}`; // Fixes 'getAllUserss'
|
|
59
|
+
} else if (ep.method === 'POST') {
|
|
60
|
+
functionName = `create${pascalSingular}`;
|
|
61
|
+
} else if (ep.method === 'PUT') {
|
|
62
|
+
functionName = `update${pascalSingular}ById`;
|
|
63
|
+
} else if (ep.method === 'DELETE') {
|
|
64
|
+
functionName = `delete${pascalSingular}ById`;
|
|
65
|
+
} else {
|
|
66
|
+
functionName = `${ep.method.toLowerCase()}${pascalPlural}`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Update the endpoint object
|
|
71
|
+
return {
|
|
72
|
+
...ep,
|
|
73
|
+
controllerName,
|
|
74
|
+
functionName // ejs file එකේදි <%= ep.functionName %> කියලා පාවිච්චි කරන්න පුළුවන් දැන්
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
// ============================================================
|
|
78
|
+
// 🔥 FIX END
|
|
79
|
+
// ============================================================
|
|
80
|
+
|
|
81
|
+
} else {
|
|
82
|
+
console.log(chalk.yellow(' -> No API endpoints found. A basic project will be created.'));
|
|
83
|
+
}
|
|
20
84
|
|
|
21
85
|
// --- Step 2: Identify Models to Generate ---
|
|
22
86
|
const modelsToGenerate = new Map();
|
|
23
87
|
endpoints.forEach(ep => {
|
|
24
|
-
|
|
25
|
-
|
|
88
|
+
// Default සහ Auth වලට Model හදන්න ඕන නෑ (Auth එකට User Model එක යටින් හදනවා)
|
|
89
|
+
if (ep.schemaFields && ep.controllerName !== 'Default' && ep.controllerName !== 'Auth' && !modelsToGenerate.has(ep.controllerName)) {
|
|
90
|
+
modelsToGenerate.set(ep.controllerName, {
|
|
91
|
+
name: ep.controllerName,
|
|
92
|
+
fields: Object.entries(ep.schemaFields).map(([key, type]) => ({ name: key, type, isUnique: key === 'email' }))
|
|
93
|
+
});
|
|
26
94
|
}
|
|
27
95
|
});
|
|
96
|
+
|
|
28
97
|
if (addAuth && !modelsToGenerate.has('User')) {
|
|
29
98
|
console.log(chalk.yellow(' -> Authentication requires a "User" model. Creating a default one.'));
|
|
30
99
|
modelsToGenerate.set('User', { name: 'User', fields: [{ name: 'name', type: 'String' }, { name: 'email', type: 'String', isUnique: true }, { name: 'password', type: 'String' }] });
|
|
@@ -91,7 +160,10 @@ async function generateNodeProject(options) {
|
|
|
91
160
|
console.log(chalk.blue(' -> Generating controllers...'));
|
|
92
161
|
for (const [modelName] of modelsToGenerate.entries()) {
|
|
93
162
|
const templateFile = dbType === 'mongoose' ? 'Controller.ts.ejs' : 'PrismaController.ts.ejs';
|
|
94
|
-
|
|
163
|
+
// මෙතන Controller එක හදද්දි Auth එක Skip කරනවා (මොකද ඒක පහළ වෙනම හදනවා)
|
|
164
|
+
if (modelName !== 'Auth') {
|
|
165
|
+
await renderAndWrite(getTemplatePath(`node-ts-express/partials/${templateFile}`), path.join(destSrcDir, 'controllers', `${modelName}.controller.ts`), { modelName, projectName });
|
|
166
|
+
}
|
|
95
167
|
}
|
|
96
168
|
}
|
|
97
169
|
|
|
@@ -145,6 +217,7 @@ async function generateNodeProject(options) {
|
|
|
145
217
|
}
|
|
146
218
|
|
|
147
219
|
// --- Step 9: Generate Main Route File & Inject Logic into Server ---
|
|
220
|
+
// Updated endpoints passed here
|
|
148
221
|
await renderAndWrite(getTemplatePath('node-ts-express/partials/routes.ts.ejs'), path.join(destSrcDir, 'routes.ts'), { endpoints, addAuth, dbType });
|
|
149
222
|
|
|
150
223
|
let serverFileContent = await fs.readFile(path.join(destSrcDir, 'server.ts'), 'utf-8');
|