create-backlist 1.3.1 → 1.3.3
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
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-backlist",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "An advanced, multi-language backend generator based on frontend analysis.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"bin": {
|
|
7
7
|
"create-backlist": "bin/index.js"
|
|
8
8
|
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
9
13
|
"scripts": {
|
|
10
14
|
"start": "node bin/index.js"
|
|
11
15
|
},
|
|
12
|
-
"author": "
|
|
16
|
+
"author": "W.A.H.ISHAN",
|
|
13
17
|
"license": "MIT",
|
|
14
18
|
"dependencies": {
|
|
15
19
|
"@babel/parser": "^7.22.7",
|
|
@@ -21,4 +25,4 @@
|
|
|
21
25
|
"glob": "^10.3.3",
|
|
22
26
|
"inquirer": "^8.2.4"
|
|
23
27
|
}
|
|
24
|
-
}
|
|
28
|
+
}
|
package/src/generators/node.js
CHANGED
|
@@ -8,55 +8,78 @@ const { renderAndWrite, getTemplatePath } = require('./template');
|
|
|
8
8
|
async function generateNodeProject(options) {
|
|
9
9
|
const { projectDir, projectName, frontendSrcDir } = options;
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
try {
|
|
12
|
+
// --- Step 1: Analyze Frontend ---
|
|
13
|
+
console.log(chalk.blue(' -> Analyzing frontend for API endpoints...'));
|
|
14
|
+
const endpoints = await analyzeFrontend(frontendSrcDir);
|
|
15
|
+
if (endpoints.length > 0) {
|
|
16
|
+
console.log(chalk.green(` -> Found ${endpoints.length} endpoints.`));
|
|
17
|
+
} else {
|
|
18
|
+
console.log(chalk.yellow(' -> No API endpoints found. A basic project will be created.'));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// --- Step 2: Scaffold Base Project ---
|
|
22
|
+
console.log(chalk.blue(' -> Scaffolding Node.js (Express + TS) project...'));
|
|
23
|
+
|
|
24
|
+
// Define paths clearly
|
|
25
|
+
const baseDir = getTemplatePath('node-ts-express/base');
|
|
26
|
+
const serverTemplatePath = path.join(baseDir, 'server.ts');
|
|
27
|
+
const tsconfigTemplatePath = path.join(baseDir, 'tsconfig.json');
|
|
28
|
+
|
|
29
|
+
const destSrcDir = path.join(projectDir, 'src');
|
|
30
|
+
const serverDestPath = path.join(destSrcDir, 'server.ts');
|
|
31
|
+
const tsconfigDestPath = path.join(projectDir, 'tsconfig.json');
|
|
32
|
+
|
|
33
|
+
// Ensure destination directory exists
|
|
34
|
+
await fs.ensureDir(destSrcDir);
|
|
35
|
+
|
|
36
|
+
// Copy base files individually for clarity
|
|
37
|
+
await fs.copy(serverTemplatePath, serverDestPath);
|
|
38
|
+
await fs.copy(tsconfigTemplatePath, tsconfigDestPath);
|
|
39
|
+
|
|
40
|
+
console.log(chalk.gray(' -> Base server.ts copied.'));
|
|
41
|
+
|
|
42
|
+
// --- Step 3: Generate Dynamic Files ---
|
|
43
|
+
await renderAndWrite(
|
|
44
|
+
getTemplatePath('node-ts-express/partials/package.json.ejs'),
|
|
45
|
+
path.join(projectDir, 'package.json'),
|
|
46
|
+
{ projectName }
|
|
47
|
+
);
|
|
48
|
+
await renderAndWrite(
|
|
49
|
+
getTemplatePath('node-ts-express/partials/routes.ts.ejs'),
|
|
50
|
+
path.join(destSrcDir, 'routes.ts'),
|
|
51
|
+
{ endpoints }
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
console.log(chalk.gray(' -> package.json and routes.ts generated.'));
|
|
55
|
+
|
|
56
|
+
// --- Step 4: Modify the copied server.ts ---
|
|
57
|
+
// Check if the file exists before reading
|
|
58
|
+
if (!await fs.pathExists(serverDestPath)) {
|
|
59
|
+
throw new Error(`Critical error: server.ts was not found at ${serverDestPath} after copy.`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let serverFileContent = await fs.readFile(serverDestPath, 'utf-8');
|
|
63
|
+
serverFileContent = serverFileContent.replace('// INJECT:ROUTES', `import apiRoutes from './routes';\napp.use(apiRoutes);`);
|
|
64
|
+
await fs.writeFile(serverDestPath, serverFileContent);
|
|
19
65
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
await renderAndWrite(
|
|
38
|
-
getTemplatePath('node-ts-express/partials/routes.ts.ejs'),
|
|
39
|
-
path.join(projectDir, 'src', 'routes.ts'),
|
|
40
|
-
{ endpoints }
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
// --- Step 4: Modify the copied server.ts to inject routes ---
|
|
44
|
-
// THIS IS THE FIX: We do this AFTER the base files are copied.
|
|
45
|
-
const serverFilePath = path.join(projectDir, 'src', 'server.ts');
|
|
46
|
-
let serverFileContent = await fs.readFile(serverFilePath, 'utf-8');
|
|
47
|
-
serverFileContent = serverFileContent.replace('// INJECT:ROUTES', `import apiRoutes from './routes';\napp.use(apiRoutes);`);
|
|
48
|
-
await fs.writeFile(serverFilePath, serverFileContent);
|
|
49
|
-
|
|
50
|
-
// --- Step 5: Install Dependencies ---
|
|
51
|
-
console.log(chalk.magenta(' -> Installing dependencies (npm install)...'));
|
|
52
|
-
await execa('npm', ['install'], { cwd: projectDir });
|
|
53
|
-
|
|
54
|
-
// --- Step 6: Generate README ---
|
|
55
|
-
await renderAndWrite(
|
|
56
|
-
getTemplatePath('node-ts-express/partials/README.md.ejs'),
|
|
57
|
-
path.join(projectDir, 'README.md'),
|
|
58
|
-
{ projectName }
|
|
59
|
-
);
|
|
66
|
+
console.log(chalk.gray(' -> server.ts modified successfully.'));
|
|
67
|
+
|
|
68
|
+
// --- Step 5: Install Dependencies ---
|
|
69
|
+
console.log(chalk.magenta(' -> Installing dependencies (npm install)...'));
|
|
70
|
+
await execa('npm', ['install'], { cwd: projectDir });
|
|
71
|
+
|
|
72
|
+
// --- Step 6: Generate README ---
|
|
73
|
+
await renderAndWrite(
|
|
74
|
+
getTemplatePath('node-ts-express/partials/README.md.ejs'),
|
|
75
|
+
path.join(projectDir, 'README.md'),
|
|
76
|
+
{ projectName }
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// Re-throw the error to be caught by the main CLI handler
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
60
83
|
}
|
|
61
84
|
|
|
62
85
|
module.exports = { generateNodeProject };
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import express from 'express';
|
|
1
|
+
import express, { Express, Request, Response } from 'express';
|
|
2
2
|
import cors from 'cors';
|
|
3
3
|
import dotenv from 'dotenv';
|
|
4
4
|
|
|
5
5
|
dotenv.config();
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
const app: Express = express(); // Added :Express type
|
|
8
|
+
|
|
7
9
|
app.use(cors());
|
|
8
10
|
app.use(express.json());
|
|
9
11
|
|
|
10
|
-
app.get('/', (req, res) =>
|
|
12
|
+
app.get('/', (req: Request, res: Response) => { // Added :Request and :Response types
|
|
13
|
+
res.send('Node.js Backend says Hello! Generated by Backlist.');
|
|
14
|
+
});
|
|
11
15
|
|
|
12
16
|
// INJECT:ROUTES
|
|
13
17
|
|
|
14
|
-
const PORT = process.env.PORT || 8000;
|
|
15
|
-
|
|
18
|
+
const PORT: number | string = process.env.PORT || 8000; // Added type for PORT
|
|
19
|
+
|
|
20
|
+
app.listen(PORT, () => {
|
|
21
|
+
console.log(`⚡️ Server running on http://localhost:${PORT}`);
|
|
22
|
+
});
|