create-backlist 6.0.6 → 6.0.8
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/bin/index.js +141 -0
- package/package.json +4 -10
- package/src/analyzer.js +105 -308
- package/src/generators/dotnet.js +94 -120
- package/src/generators/java.js +109 -157
- package/src/generators/node.js +85 -262
- package/src/generators/template.js +2 -38
- package/src/templates/dotnet/partials/Controller.cs.ejs +14 -7
- package/src/templates/java-spring/partials/ApplicationSeeder.java.ejs +2 -7
- package/src/templates/java-spring/partials/AuthController.java.ejs +10 -23
- package/src/templates/java-spring/partials/Controller.java.ejs +6 -17
- package/src/templates/java-spring/partials/Dockerfile.ejs +1 -6
- package/src/templates/java-spring/partials/Entity.java.ejs +5 -15
- package/src/templates/java-spring/partials/JwtAuthFilter.java.ejs +7 -30
- package/src/templates/java-spring/partials/JwtService.java.ejs +10 -38
- package/src/templates/java-spring/partials/Repository.java.ejs +1 -10
- package/src/templates/java-spring/partials/Service.java.ejs +7 -45
- package/src/templates/java-spring/partials/User.java.ejs +4 -17
- package/src/templates/java-spring/partials/UserDetailsServiceImpl.java.ejs +4 -10
- package/src/templates/java-spring/partials/UserRepository.java.ejs +0 -8
- package/src/templates/java-spring/partials/docker-compose.yml.ejs +8 -16
- package/src/templates/node-ts-express/base/server.ts +6 -13
- package/src/templates/node-ts-express/base/tsconfig.json +3 -13
- package/src/templates/node-ts-express/partials/ApiDocs.ts.ejs +7 -17
- package/src/templates/node-ts-express/partials/App.test.ts.ejs +26 -49
- package/src/templates/node-ts-express/partials/Auth.controller.ts.ejs +62 -56
- package/src/templates/node-ts-express/partials/Auth.middleware.ts.ejs +10 -21
- package/src/templates/node-ts-express/partials/Controller.ts.ejs +40 -40
- package/src/templates/node-ts-express/partials/DbContext.cs.ejs +3 -3
- package/src/templates/node-ts-express/partials/Dockerfile.ejs +11 -9
- package/src/templates/node-ts-express/partials/Model.cs.ejs +7 -25
- package/src/templates/node-ts-express/partials/Model.ts.ejs +12 -20
- package/src/templates/node-ts-express/partials/PrismaController.ts.ejs +55 -72
- package/src/templates/node-ts-express/partials/PrismaSchema.prisma.ejs +12 -27
- package/src/templates/node-ts-express/partials/README.md.ejs +12 -9
- package/src/templates/node-ts-express/partials/Seeder.ts.ejs +64 -44
- package/src/templates/node-ts-express/partials/docker-compose.yml.ejs +16 -31
- package/src/templates/node-ts-express/partials/package.json.ejs +1 -3
- package/src/templates/node-ts-express/partials/routes.ts.ejs +24 -35
- package/src/utils.js +4 -19
- package/bin/backlist.js +0 -227
- package/src/db/prisma.ts +0 -4
- package/src/scanner/analyzeFrontend.js +0 -146
- package/src/scanner/index.js +0 -99
- package/src/templates/dotnet/partials/Dto.cs.ejs +0 -8
- package/src/templates/node-ts-express/partials/prismaClient.ts.ejs +0 -4
package/bin/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const inquirer = require('inquirer');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const fs = require('fs-extra');
|
|
6
|
+
const path = require('path'); // FIX: Correctly require the 'path' module
|
|
7
|
+
const { isCommandAvailable } = require('../src/utils');
|
|
8
|
+
|
|
9
|
+
// Import ALL generators
|
|
10
|
+
const { generateNodeProject } = require('../src/generators/node');
|
|
11
|
+
const { generateDotnetProject } = require('../src/generators/dotnet');
|
|
12
|
+
const { generateJavaProject } = require('../src/generators/java');
|
|
13
|
+
const { generatePythonProject } = require('../src/generators/python');
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
console.log(chalk.cyan.bold('🚀 Welcome to Backlist! The Polyglot Backend Generator.'));
|
|
17
|
+
|
|
18
|
+
const answers = await inquirer.prompt([
|
|
19
|
+
// --- General Questions ---
|
|
20
|
+
{
|
|
21
|
+
type: 'input',
|
|
22
|
+
name: 'projectName',
|
|
23
|
+
message: 'Enter a name for your backend directory:',
|
|
24
|
+
default: 'backend',
|
|
25
|
+
validate: input => input ? true : 'Project name cannot be empty.'
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: 'list',
|
|
29
|
+
name: 'stack',
|
|
30
|
+
message: 'Select the backend stack:',
|
|
31
|
+
choices: [
|
|
32
|
+
{ name: 'Node.js (TypeScript, Express)', value: 'node-ts-express' },
|
|
33
|
+
{ name: 'C# (ASP.NET Core Web API)', value: 'dotnet-webapi' },
|
|
34
|
+
{ name: 'Java (Spring Boot)', value: 'java-spring' },
|
|
35
|
+
{ name: 'Python (FastAPI)', value: 'python-fastapi' },
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: 'input',
|
|
40
|
+
name: 'srcPath',
|
|
41
|
+
message: 'Enter the path to your frontend `src` directory:',
|
|
42
|
+
default: 'src',
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
// --- Node.js Specific Questions ---
|
|
46
|
+
{
|
|
47
|
+
type: 'list',
|
|
48
|
+
name: 'dbType',
|
|
49
|
+
message: 'Select your database type for Node.js:',
|
|
50
|
+
choices: [
|
|
51
|
+
{ name: 'NoSQL (MongoDB with Mongoose)', value: 'mongoose' },
|
|
52
|
+
{ name: 'SQL (PostgreSQL/MySQL with Prisma)', value: 'prisma' },
|
|
53
|
+
],
|
|
54
|
+
when: (answers) => answers.stack === 'node-ts-express'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
type: 'confirm',
|
|
58
|
+
name: 'addAuth',
|
|
59
|
+
message: 'Add JWT authentication boilerplate?',
|
|
60
|
+
default: true,
|
|
61
|
+
when: (answers) => answers.stack === 'node-ts-express'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: 'confirm',
|
|
65
|
+
name: 'addSeeder',
|
|
66
|
+
message: 'Add a database seeder with sample data?',
|
|
67
|
+
default: true,
|
|
68
|
+
// Seeder only makes sense if there's an auth/user model to seed
|
|
69
|
+
when: (answers) => answers.stack === 'node-ts-express' && answers.addAuth
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: 'checkbox',
|
|
73
|
+
name: 'extraFeatures',
|
|
74
|
+
message: 'Select additional features for Node.js:',
|
|
75
|
+
choices: [
|
|
76
|
+
{ name: 'Docker Support (Dockerfile & docker-compose.yml)', value: 'docker', checked: true },
|
|
77
|
+
{ name: 'API Testing Boilerplate (Jest & Supertest)', value: 'testing', checked: true },
|
|
78
|
+
{ name: 'API Documentation (Swagger UI)', value: 'swagger', checked: true },
|
|
79
|
+
],
|
|
80
|
+
when: (answers) => answers.stack === 'node-ts-express'
|
|
81
|
+
}
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
const options = {
|
|
85
|
+
...answers,
|
|
86
|
+
projectDir: path.resolve(process.cwd(), answers.projectName),
|
|
87
|
+
frontendSrcDir: path.resolve(process.cwd(), answers.srcPath),
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
console.log(chalk.blue(`\n✨ Starting backend generation for: ${chalk.bold(options.stack)}`));
|
|
92
|
+
|
|
93
|
+
// --- Dispatcher Logic for ALL Stacks ---
|
|
94
|
+
switch (options.stack) {
|
|
95
|
+
case 'node-ts-express':
|
|
96
|
+
await generateNodeProject(options);
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case 'dotnet-webapi':
|
|
100
|
+
if (!await isCommandAvailable('dotnet')) {
|
|
101
|
+
throw new Error('.NET SDK is not installed. Please install it from https://dotnet.microsoft.com/download');
|
|
102
|
+
}
|
|
103
|
+
await generateDotnetProject(options);
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
case 'java-spring':
|
|
107
|
+
if (!await isCommandAvailable('java')) {
|
|
108
|
+
throw new Error('Java (JDK 17 or newer) is not installed. Please install a JDK to continue.');
|
|
109
|
+
}
|
|
110
|
+
await generateJavaProject(options);
|
|
111
|
+
break;
|
|
112
|
+
|
|
113
|
+
case 'python-fastapi':
|
|
114
|
+
if (!await isCommandAvailable('python')) {
|
|
115
|
+
throw new Error('Python is not installed. Please install Python (3.8+) and pip to continue.');
|
|
116
|
+
}
|
|
117
|
+
await generatePythonProject(options);
|
|
118
|
+
break;
|
|
119
|
+
|
|
120
|
+
default:
|
|
121
|
+
throw new Error(`The selected stack '${options.stack}' is not supported yet.`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.log(chalk.green.bold('\n✅ Backend generation complete!'));
|
|
125
|
+
console.log('\nNext Steps:');
|
|
126
|
+
console.log(chalk.cyan(` cd ${options.projectName}`));
|
|
127
|
+
console.log(chalk.cyan(' (Check the generated README.md for instructions)'));
|
|
128
|
+
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error(chalk.red.bold('\n❌ An error occurred during generation:'));
|
|
131
|
+
console.error(error);
|
|
132
|
+
|
|
133
|
+
if (fs.existsSync(options.projectDir)) {
|
|
134
|
+
console.log(chalk.yellow(' -> Cleaning up failed installation...'));
|
|
135
|
+
fs.removeSync(options.projectDir);
|
|
136
|
+
}
|
|
137
|
+
process.exit(1);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backlist",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.8",
|
|
4
4
|
"description": "An advanced, multi-language backend generator based on frontend analysis.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"bin": {
|
|
7
|
-
"create-backlist": "bin/index.js"
|
|
8
|
-
"backlist": "bin/index.js"
|
|
7
|
+
"create-backlist": "bin/index.js"
|
|
9
8
|
},
|
|
10
9
|
"files": [
|
|
11
10
|
"bin",
|
|
12
|
-
"src"
|
|
13
|
-
"templates"
|
|
11
|
+
"src"
|
|
14
12
|
],
|
|
15
13
|
"scripts": {
|
|
16
14
|
"start": "node bin/index.js"
|
|
@@ -22,15 +20,11 @@
|
|
|
22
20
|
"@babel/traverse": "^7.22.8",
|
|
23
21
|
"axios": "^1.13.1",
|
|
24
22
|
"chalk": "^4.1.2",
|
|
25
|
-
"chokidar": "^5.0.0",
|
|
26
|
-
"commander": "^14.0.2",
|
|
27
23
|
"ejs": "^3.1.9",
|
|
28
24
|
"execa": "^6.1.0",
|
|
29
|
-
"fast-glob": "^3.3.3",
|
|
30
25
|
"fs-extra": "^11.1.1",
|
|
31
26
|
"glob": "^10.3.3",
|
|
32
27
|
"inquirer": "^8.2.4",
|
|
33
|
-
"ts-morph": "^27.0.2",
|
|
34
28
|
"unzipper": "^0.12.3"
|
|
35
29
|
}
|
|
36
|
-
}
|
|
30
|
+
}
|
package/src/analyzer.js
CHANGED
|
@@ -1,328 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// Small utils
|
|
13
|
-
// -------------------------
|
|
14
|
-
function normalizeSlashes(p) {
|
|
15
|
-
return String(p || "").replace(/\\/g, "/");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function readJSONSafe(p) {
|
|
19
|
-
try {
|
|
20
|
-
return fs.readJsonSync(p);
|
|
21
|
-
} catch {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// -------------------------
|
|
27
|
-
// AUTH detection (for addAuth)
|
|
28
|
-
// -------------------------
|
|
29
|
-
function findAuthUsageInRepo(rootDir) {
|
|
30
|
-
const pkgPath = path.join(rootDir, "package.json");
|
|
31
|
-
const pkg = readJSONSafe(pkgPath) || {};
|
|
32
|
-
const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
|
|
33
|
-
|
|
34
|
-
const authDeps = [
|
|
35
|
-
"next-auth",
|
|
36
|
-
"@auth/core",
|
|
37
|
-
"@clerk/nextjs",
|
|
38
|
-
"@supabase/auth-helpers-nextjs",
|
|
39
|
-
"@supabase/supabase-js",
|
|
40
|
-
"firebase",
|
|
41
|
-
"lucia",
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
if (authDeps.some((d) => deps[d])) return true;
|
|
45
|
-
|
|
46
|
-
const scanDirs = ["src", "app", "pages", "components", "lib", "utils"]
|
|
47
|
-
.map((d) => path.join(rootDir, d))
|
|
48
|
-
.filter((d) => fs.existsSync(d));
|
|
49
|
-
|
|
50
|
-
const patterns = [
|
|
51
|
-
"next-auth",
|
|
52
|
-
"getServerSession",
|
|
53
|
-
"useSession",
|
|
54
|
-
"SessionProvider",
|
|
55
|
-
"@clerk/nextjs",
|
|
56
|
-
"ClerkProvider",
|
|
57
|
-
"auth()",
|
|
58
|
-
"currentUser",
|
|
59
|
-
"createServerClient",
|
|
60
|
-
];
|
|
61
|
-
|
|
62
|
-
for (const dir of scanDirs) {
|
|
63
|
-
const files = glob.sync(`${normalizeSlashes(dir)}/**/*.{js,ts,jsx,tsx}`, {
|
|
64
|
-
ignore: [
|
|
65
|
-
"**/node_modules/**",
|
|
66
|
-
"**/dist/**",
|
|
67
|
-
"**/build/**",
|
|
68
|
-
"**/.next/**",
|
|
69
|
-
"**/coverage/**",
|
|
70
|
-
],
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
for (const f of files) {
|
|
74
|
-
try {
|
|
75
|
-
const content = fs.readFileSync(f, "utf8");
|
|
76
|
-
if (patterns.some((p) => content.includes(p))) return true;
|
|
77
|
-
} catch {
|
|
78
|
-
// ignore read errors
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// -------------------------
|
|
87
|
-
// Helper functions for frontend API scan
|
|
88
|
-
// -------------------------
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const { glob } = require('glob');
|
|
3
|
+
const parser = require('@babel/parser');
|
|
4
|
+
const traverse = require('@babel/traverse').default;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a string to TitleCase, which is suitable for model and controller names.
|
|
8
|
+
* e.g., 'user-orders' -> 'UserOrders'
|
|
9
|
+
* @param {string} str The input string.
|
|
10
|
+
* @returns {string} The TitleCased string.
|
|
11
|
+
*/
|
|
89
12
|
function toTitleCase(str) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
.replace(/[^a-zA-Z0-9]/g, "");
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function normalizeRouteForBackend(urlValue) {
|
|
98
|
-
return urlValue.replace(/\{(\w+)\}/g, ":$1");
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function extractPathParams(route) {
|
|
102
|
-
const params = [];
|
|
103
|
-
const re = /[:{]([a-zA-Z0-9_]+)[}]/g;
|
|
104
|
-
let m;
|
|
105
|
-
while ((m = re.exec(route))) params.push(m[1]);
|
|
106
|
-
return Array.from(new Set(params));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function extractQueryParamsFromUrl(urlValue) {
|
|
110
|
-
try {
|
|
111
|
-
const qIndex = urlValue.indexOf("?");
|
|
112
|
-
if (qIndex === -1) return [];
|
|
113
|
-
const qs = urlValue.slice(qIndex + 1);
|
|
114
|
-
return qs
|
|
115
|
-
.split("&")
|
|
116
|
-
.map((p) => p.split("=")[0])
|
|
117
|
-
.filter(Boolean);
|
|
118
|
-
} catch {
|
|
119
|
-
return [];
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function deriveControllerNameFromUrl(urlValue) {
|
|
124
|
-
const parts = String(urlValue || "").split("/").filter(Boolean);
|
|
125
|
-
const apiIndex = parts.indexOf("api");
|
|
126
|
-
let seg = null;
|
|
127
|
-
if (apiIndex >= 0) {
|
|
128
|
-
seg = parts[apiIndex + 1] || null;
|
|
129
|
-
if (seg && /^v\d+$/i.test(seg)) {
|
|
130
|
-
seg = parts[apiIndex + 2] || seg;
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
seg = parts[0] || null;
|
|
134
|
-
}
|
|
135
|
-
return toTitleCase(seg);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function deriveActionName(method, route) {
|
|
139
|
-
const cleaned = String(route).replace(/^\/api\//, "/").replace(/[/:{}-]/g, " ");
|
|
140
|
-
const last = cleaned.trim().split(/\s+/).filter(Boolean).pop() || "Action";
|
|
141
|
-
return `${String(method).toLowerCase()}${toTitleCase(last)}`;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// -------------------------
|
|
145
|
-
// Axios/Fetch detection
|
|
146
|
-
// -------------------------
|
|
147
|
-
function detectAxiosLikeMethod(node) {
|
|
148
|
-
if (!node.callee || node.callee.type !== "MemberExpression") return null;
|
|
149
|
-
const prop = node.callee.property;
|
|
150
|
-
if (!prop || prop.type !== "Identifier") return null;
|
|
151
|
-
const name = prop.name.toLowerCase();
|
|
152
|
-
if (!HTTP_METHODS.has(name)) return null;
|
|
153
|
-
return name.toUpperCase();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function getUrlValue(urlNode) {
|
|
157
|
-
if (!urlNode) return null;
|
|
158
|
-
if (urlNode.type === "StringLiteral") return urlNode.value;
|
|
159
|
-
if (urlNode.type === "TemplateLiteral") {
|
|
160
|
-
const quasis = urlNode.quasis || [];
|
|
161
|
-
const exprs = urlNode.expressions || [];
|
|
162
|
-
let out = "";
|
|
163
|
-
for (let i = 0; i < quasis.length; i++) {
|
|
164
|
-
out += quasis[i].value.raw;
|
|
165
|
-
if (exprs[i]) {
|
|
166
|
-
if (exprs[i].type === "Identifier") out += `{${exprs[i].name}}`;
|
|
167
|
-
else out += `{param${i + 1}}`;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
return out;
|
|
171
|
-
}
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// -------------------------
|
|
176
|
-
// Extract request schema (simple)
|
|
177
|
-
function inferTypeFromNode(node) {
|
|
178
|
-
if (!node) return "String";
|
|
179
|
-
switch (node.type) {
|
|
180
|
-
case "StringLiteral": return "String";
|
|
181
|
-
case "NumericLiteral": return "Number";
|
|
182
|
-
case "BooleanLiteral": return "Boolean";
|
|
183
|
-
case "NullLiteral": return "String";
|
|
184
|
-
default: return "String";
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function extractObjectSchema(objExpr) {
|
|
189
|
-
const schema = {};
|
|
190
|
-
if (!objExpr || objExpr.type !== "ObjectExpression") return null;
|
|
191
|
-
for (const prop of objExpr.properties) {
|
|
192
|
-
if (prop.type !== "ObjectProperty") continue;
|
|
193
|
-
const key = prop.key.type === "Identifier" ? prop.key.name : prop.key.value;
|
|
194
|
-
schema[key] = inferTypeFromNode(prop.value);
|
|
195
|
-
}
|
|
196
|
-
return schema;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function resolveIdentifierToInit(pathObj, identifierName) {
|
|
200
|
-
try {
|
|
201
|
-
const binding = pathObj.scope.getBinding(identifierName);
|
|
202
|
-
if (!binding) return null;
|
|
203
|
-
const decl = binding.path.node;
|
|
204
|
-
if (!decl) return null;
|
|
205
|
-
if (decl.type === "VariableDeclarator") return decl.init || null;
|
|
206
|
-
return null;
|
|
207
|
-
} catch {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
13
|
+
if (!str) return 'Default';
|
|
14
|
+
return str.replace(/-_(\w)/g, g => g[1].toUpperCase()) // handle snake_case and kebab-case
|
|
15
|
+
.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())
|
|
16
|
+
.replace(/[^a-zA-Z0-9]/g, '');
|
|
210
17
|
}
|
|
211
18
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Analyzes frontend source files to find API endpoints and their details.
|
|
21
|
+
* @param {string} srcPath The path to the frontend source directory.
|
|
22
|
+
* @returns {Promise<Array<object>>} A promise that resolves to an array of endpoint objects.
|
|
23
|
+
*/
|
|
215
24
|
async function analyzeFrontend(srcPath) {
|
|
216
|
-
if (!srcPath)
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const files = await glob(`${normalizeSlashes(srcPath)}/**/*.{js,ts,jsx,tsx}`, {
|
|
220
|
-
ignore: ["**/node_modules/**", "**/dist/**", "**/build/**", "**/.next/**", "**/coverage/**"],
|
|
221
|
-
});
|
|
25
|
+
if (!fs.existsSync(srcPath)) {
|
|
26
|
+
throw new Error(`The source directory '${srcPath}' does not exist.`);
|
|
27
|
+
}
|
|
222
28
|
|
|
29
|
+
const files = await glob(`${srcPath}/**/*.{js,ts,jsx,tsx}`, { ignore: 'node_modules/**' });
|
|
223
30
|
const endpoints = new Map();
|
|
224
31
|
|
|
225
32
|
for (const file of files) {
|
|
226
|
-
|
|
227
|
-
try {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
33
|
+
const code = await fs.readFile(file, 'utf-8');
|
|
34
|
+
try {
|
|
35
|
+
const ast = parser.parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
36
|
+
|
|
37
|
+
traverse(ast, {
|
|
38
|
+
CallExpression(path) {
|
|
39
|
+
// We are only interested in 'fetch' calls
|
|
40
|
+
if (path.node.callee.name !== 'fetch') return;
|
|
41
|
+
|
|
42
|
+
const urlNode = path.node.arguments[0];
|
|
43
|
+
|
|
44
|
+
let urlValue;
|
|
45
|
+
if (urlNode.type === 'StringLiteral') {
|
|
46
|
+
urlValue = urlNode.value;
|
|
47
|
+
} else if (urlNode.type === 'TemplateLiteral' && urlNode.quasis.length > 0) {
|
|
48
|
+
// Reconstruct path for dynamic URLs like `/api/users/${id}` -> `/api/users/{id}`
|
|
49
|
+
urlValue = urlNode.quasis.map((q, i) => {
|
|
50
|
+
return q.value.raw + (urlNode.expressions[i] ? `{${urlNode.expressions[i].name || 'id'}}` : '');
|
|
51
|
+
}).join('');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Only process API calls that start with '/api/'
|
|
55
|
+
if (!urlValue || !urlValue.startsWith('/api/')) return;
|
|
56
|
+
|
|
57
|
+
let method = 'GET';
|
|
58
|
+
let schemaFields = null;
|
|
59
|
+
|
|
60
|
+
const optionsNode = path.node.arguments[1];
|
|
61
|
+
if (optionsNode && optionsNode.type === 'ObjectExpression') {
|
|
62
|
+
// Find the HTTP method
|
|
63
|
+
const methodProp = optionsNode.properties.find(p => p.key.name === 'method');
|
|
64
|
+
if (methodProp && methodProp.value.type === 'StringLiteral') {
|
|
65
|
+
method = methodProp.value.value.toUpperCase();
|
|
66
|
+
}
|
|
241
67
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
68
|
+
// --- NEW LOGIC: Analyze the 'body' for POST/PUT requests ---
|
|
69
|
+
if (method === 'POST' || method === 'PUT') {
|
|
70
|
+
const bodyProp = optionsNode.properties.find(p => p.key.name === 'body');
|
|
71
|
+
|
|
72
|
+
// Check if body is wrapped in JSON.stringify
|
|
73
|
+
if (bodyProp && bodyProp.value.callee && bodyProp.value.callee.name === 'JSON.stringify') {
|
|
74
|
+
const dataObjectNode = bodyProp.value.arguments[0];
|
|
75
|
+
|
|
76
|
+
// This is a simplified analysis assuming the object is defined inline.
|
|
77
|
+
// A more robust solution would trace variables back to their definition.
|
|
78
|
+
if (dataObjectNode.type === 'ObjectExpression') {
|
|
79
|
+
schemaFields = {};
|
|
80
|
+
dataObjectNode.properties.forEach(prop => {
|
|
81
|
+
const key = prop.key.name;
|
|
82
|
+
const valueNode = prop.value;
|
|
83
|
+
|
|
84
|
+
// Infer Mongoose schema type based on the value's literal type
|
|
85
|
+
if (valueNode.type === 'StringLiteral') {
|
|
86
|
+
schemaFields[key] = 'String';
|
|
87
|
+
} else if (valueNode.type === 'NumericLiteral') {
|
|
88
|
+
schemaFields[key] = 'Number';
|
|
89
|
+
} else if (valueNode.type === 'BooleanLiteral') {
|
|
90
|
+
schemaFields[key] = 'Boolean';
|
|
91
|
+
} else {
|
|
92
|
+
// Default to String if the type is complex or a variable
|
|
93
|
+
schemaFields[key] = 'String';
|
|
94
|
+
}
|
|
95
|
+
});
|
|
259
96
|
}
|
|
260
97
|
}
|
|
261
98
|
}
|
|
262
99
|
}
|
|
263
|
-
}
|
|
264
100
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
101
|
+
// Generate a clean controller name (e.g., /api/user-orders -> UserOrders)
|
|
102
|
+
const controllerName = toTitleCase(urlValue.split('/')[2]);
|
|
103
|
+
const key = `${method}:${urlValue}`;
|
|
104
|
+
|
|
105
|
+
// Avoid adding duplicate endpoints
|
|
106
|
+
if (!endpoints.has(key)) {
|
|
107
|
+
endpoints.set(key, {
|
|
108
|
+
path: urlValue,
|
|
109
|
+
method,
|
|
110
|
+
controllerName,
|
|
111
|
+
schemaFields // This will be null for GET/DELETE, and an object for POST/PUT
|
|
112
|
+
});
|
|
275
113
|
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const route = normalizeRouteForBackend(apiPath.split("?")[0]);
|
|
282
|
-
const controllerName = deriveControllerNameFromUrl(apiPath);
|
|
283
|
-
const actionName = deriveActionName(method, route);
|
|
284
|
-
const key = `${method}:${route}`;
|
|
285
|
-
|
|
286
|
-
if (!endpoints.has(key)) {
|
|
287
|
-
endpoints.set(key, {
|
|
288
|
-
path: apiPath,
|
|
289
|
-
route,
|
|
290
|
-
method,
|
|
291
|
-
controllerName,
|
|
292
|
-
actionName,
|
|
293
|
-
pathParams: extractPathParams(route),
|
|
294
|
-
queryParams: extractQueryParamsFromUrl(apiPath),
|
|
295
|
-
requestBody: schemaFields ? { fields: schemaFields } : null,
|
|
296
|
-
sourceFile: normalizeSlashes(file),
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
});
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
} catch (e) {
|
|
117
|
+
// Ignore files that babel can't parse (e.g., CSS-in-JS files)
|
|
118
|
+
}
|
|
301
119
|
}
|
|
302
120
|
|
|
121
|
+
// Return all found endpoints as an array
|
|
303
122
|
return Array.from(endpoints.values());
|
|
304
123
|
}
|
|
305
124
|
|
|
306
|
-
|
|
307
|
-
// Main analyze() for CLI
|
|
308
|
-
// -------------------------
|
|
309
|
-
async function analyze(projectRoot = process.cwd()) {
|
|
310
|
-
const rootDir = path.resolve(projectRoot);
|
|
311
|
-
const frontendSrc = ["src", "app", "pages"]
|
|
312
|
-
.map(d => path.join(rootDir,d))
|
|
313
|
-
.find(d => fs.existsSync(d));
|
|
314
|
-
|
|
315
|
-
const endpoints = frontendSrc ? await analyzeFrontend(frontendSrc) : [];
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
rootDir: normalizeSlashes(rootDir),
|
|
319
|
-
hasAuth: findAuthUsageInRepo(rootDir),
|
|
320
|
-
addAuth: findAuthUsageInRepo(rootDir),
|
|
321
|
-
endpoints
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
module.exports = {
|
|
326
|
-
analyze,
|
|
327
|
-
analyzeFrontend
|
|
328
|
-
};
|
|
125
|
+
module.exports = { analyzeFrontend };
|