@@ -2,7 +2,7 @@ import path from "path";
2
2
import { writeFile } from "../utils/fileSystem.js" ;
3
3
import { logSuccess } from "../utils/logger.js" ;
4
4
5
- export async function configureIndex ( projectName , language , framework ) {
5
+ export async function configureIndex ( projectName , language , framework , database ) {
6
6
try {
7
7
const isTS = language === "TypeScript" ;
8
8
const extension = isTS ? "ts" : "js" ;
@@ -13,41 +13,39 @@ export async function configureIndex(projectName, language, framework) {
13
13
indexContent = isTS
14
14
? `
15
15
import express, { Request, Response } from 'express';
16
- import dotenv from 'dotenv';
17
-
18
- dotenv.config();
16
+ import ENV from "./config/env.config";
17
+ ${ database ? `import connectDB from './config/db.config';` : '' }
19
18
20
19
const app = express();
21
20
22
21
app.get('/', (req: Request, res: Response) => {
23
22
res.send('Hello from ${ projectName } backend!');
24
23
});
25
24
26
- const port = process.env.PORT || 3000;
27
- app.listen(port, () =>
28
- console.log('🚀 Server running on http://localhost:' + port)
29
- );
25
+ const port: number = ENV.PORT || 3000;
26
+ {{APP_LISTEN}};
30
27
`
31
28
: `
32
29
import express from 'express';
33
- import dotenv from 'dotenv';
34
-
35
- dotenv.config();
30
+ import ENV from "./config/env.config.js";
31
+ ${ database ? `import connectDB from './config/db.config.js';` : '' }
36
32
37
33
const app = express();
38
34
app.use(express.json());
39
35
40
36
app.get('/', (req, res) => res.send('Hello from ${ projectName } backend!'));
41
37
42
- const port = process.env .PORT || 3000;
43
- app.listen(port, () => console.log('🚀 Server running on http://localhost:' + port)) ;
38
+ const port = ENV .PORT || 3000;
39
+ {{APP_LISTEN}} ;
44
40
` ;
45
41
} else {
46
42
indexContent = isTS
47
43
? `
48
44
import http, { IncomingMessage, ServerResponse } from 'http';
45
+ import ENV from "./config/env.config";
46
+ ${ database ? `import connectDB from './config/db.config';` : '' }
49
47
50
- const server = http.createServer((req: IncomingMessage, res: ServerResponse) => {
48
+ const app = http.createServer((req: IncomingMessage, res: ServerResponse) => {
51
49
if (req.url === '/' && req.method === 'GET') {
52
50
res.writeHead(200, { 'Content-Type': 'text/plain' });
53
51
res.end('Hello from ${ projectName } backend!');
@@ -57,15 +55,15 @@ const server = http.createServer((req: IncomingMessage, res: ServerResponse) =>
57
55
}
58
56
});
59
57
60
- const PORT = process.env.PORT || 3000;
61
- server.listen(PORT, () => {
62
- console.log(\`🚀 Server running on http://localhost:\${PORT}\`);
63
- });
58
+ const PORT: number = ENV.PORT || 3000;
59
+ {{APP_LISTEN}};
64
60
`
65
61
: `
66
62
import http from 'http';
63
+ import ENV from "./config/env.config.js";
64
+ ${ database ? `import connectDB from './config/db.config.js';` : '' }
67
65
68
- const server = http.createServer((req, res) => {
66
+ const app = http.createServer((req, res) => {
69
67
if (req.url === '/' && req.method === 'GET') {
70
68
res.writeHead(200, { 'Content-Type': 'text/plain' });
71
69
res.end('Hello from ${ projectName } backend!');
@@ -75,15 +73,64 @@ const server = http.createServer((req, res) => {
75
73
}
76
74
});
77
75
78
- const PORT = process.env.PORT || 3000;
79
- server.listen(PORT, () => {
80
- console.log(\`🚀 Server running on http://localhost:\${PORT}\`);
76
+ const PORT = ENV.PORT || 3000;
77
+ {{APP_LISTEN}}
78
+ ` ;
79
+ }
80
+
81
+ // 🔥 NOW: inject the APP_LISTEN logic
82
+ let appListenLogic = '' ;
83
+
84
+ if ( database ) {
85
+ if ( framework === 'Express' ) {
86
+ appListenLogic = isTS
87
+ ? `
88
+ connectDB().then(() => {
89
+ app.listen(port, () => console.log(\`🚀 Server running on port \${port}\`));
90
+ }).catch((err: any) => {
91
+ console.error('❌ Failed to connect to DB:', err);
92
+ });
93
+ `
94
+ : `
95
+ connectDB().then(() => {
96
+ app.listen(port, () => console.log(\`🚀 Server running on port \${port}\`));
97
+ }).catch((err) => {
98
+ console.error('❌ Failed to connect to DB:', err);
81
99
});
100
+ ` ;
101
+ } else {
102
+ appListenLogic = isTS
103
+ ? `
104
+ connectDB().then(() => {
105
+ app.listen(PORT, () => console.log(\`🚀 Server running on port \${PORT}\`));
106
+ }).catch((err: any) => {
107
+ console.error('❌ Failed to connect to DB:', err);
108
+ });
109
+ `
110
+ : `
111
+ connectDB().then(() => {
112
+ app.listen(PORT, () => console.log(\`🚀 Server running on port \${PORT}\`));
113
+ }).catch((err) => {
114
+ console.error('❌ Failed to connect to DB:', err);
115
+ });
116
+ ` ;
117
+ }
118
+ } else {
119
+ if ( framework === 'Express' ) {
120
+ appListenLogic = `
121
+ app.listen(port, () => console.log(\`🚀 Server running on port \${port}\`));
82
122
` ;
123
+ } else {
124
+ appListenLogic = `
125
+ app.listen(PORT, () => console.log(\`🚀 Server running on port \${PORT}\`));
126
+ ` ;
127
+ }
83
128
}
84
129
85
- const indexPath = path . join ( process . cwd ( ) , "src" , `index.${ extension } ` ) ;
130
+ // 🪄 Replace the placeholder with the actual logic
131
+ indexContent = indexContent . replace ( '{{APP_LISTEN}}' , appListenLogic . trim ( ) ) ;
86
132
133
+ const indexPath = path . join ( process . cwd ( ) , "src" , `index.${ extension } ` ) ;
87
134
await writeFile ( indexPath , indexContent . trim ( ) , "utf-8" ) ;
88
135
89
136
logSuccess ( `✅ Created src/index.${ extension } ` ) ;
0 commit comments