@ifecodes/backend-template 1.1.0 → 1.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/bin/lib/prompts.js +6 -0
- package/bin/lib/service-setup.js +28 -12
- package/package.json +1 -1
package/bin/lib/prompts.js
CHANGED
|
@@ -137,6 +137,12 @@ export const getProjectConfig = async () => {
|
|
|
137
137
|
},
|
|
138
138
|
]);
|
|
139
139
|
|
|
140
|
+
// Handle cancelled prompts (user pressed Ctrl+C or closed the prompt)
|
|
141
|
+
if (!res || !res.name) {
|
|
142
|
+
console.log(pc.yellow("\n❌ Operation cancelled by user."));
|
|
143
|
+
process.exit(0);
|
|
144
|
+
}
|
|
145
|
+
|
|
140
146
|
// Set defaults for CI/non-interactive mode
|
|
141
147
|
if (isCI) {
|
|
142
148
|
res.features = res.features || [];
|
package/bin/lib/service-setup.js
CHANGED
|
@@ -5,6 +5,19 @@ import pc from "picocolors";
|
|
|
5
5
|
import { execSync } from "child_process";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
7
|
|
|
8
|
+
// Helper function to get the correct file extension (.ts or .js)
|
|
9
|
+
function getFileExtension(dir) {
|
|
10
|
+
// Check if .ts files exist, otherwise use .js
|
|
11
|
+
const sampleFiles = ['src/app.ts', 'src/server.ts', 'src/routes.ts'];
|
|
12
|
+
for (const file of sampleFiles) {
|
|
13
|
+
const tsPath = path.join(dir, file);
|
|
14
|
+
if (fs.existsSync(tsPath)) return 'ts';
|
|
15
|
+
const jsPath = path.join(dir, file.replace('.ts', '.js'));
|
|
16
|
+
if (fs.existsSync(jsPath)) return 'js';
|
|
17
|
+
}
|
|
18
|
+
return 'ts'; // default to ts
|
|
19
|
+
}
|
|
20
|
+
|
|
8
21
|
export const setupService = async (
|
|
9
22
|
res,
|
|
10
23
|
serviceName,
|
|
@@ -18,6 +31,9 @@ export const setupService = async (
|
|
|
18
31
|
let devDeps = [];
|
|
19
32
|
let v1Imports = [];
|
|
20
33
|
let v1Routes = [];
|
|
34
|
+
|
|
35
|
+
// Detect file extension (ts or js)
|
|
36
|
+
const ext = getFileExtension(serviceRoot);
|
|
21
37
|
|
|
22
38
|
// Special handling for gateway service
|
|
23
39
|
if (serviceName === "gateway") {
|
|
@@ -25,8 +41,8 @@ export const setupService = async (
|
|
|
25
41
|
deps.push(...gatewayModule.gatewayDeps);
|
|
26
42
|
|
|
27
43
|
// Copy gateway-specific files
|
|
28
|
-
const gatewayAppPath = path.join(serviceRoot,
|
|
29
|
-
const gatewayServerPath = path.join(serviceRoot,
|
|
44
|
+
const gatewayAppPath = path.join(serviceRoot, `src/app.${ext}`);
|
|
45
|
+
const gatewayServerPath = path.join(serviceRoot, `src/server.${ext}`);
|
|
30
46
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
31
47
|
|
|
32
48
|
const gatewayAppContent = fs.readFileSync(
|
|
@@ -46,7 +62,7 @@ export const setupService = async (
|
|
|
46
62
|
fs.writeFileSync(gatewayServerPath, gatewayServerContent);
|
|
47
63
|
|
|
48
64
|
// Remove unnecessary files for gateway
|
|
49
|
-
const routesPath = path.join(serviceRoot,
|
|
65
|
+
const routesPath = path.join(serviceRoot, `src/routes.${ext}`);
|
|
50
66
|
const modulesPath = path.join(serviceRoot, "src/modules");
|
|
51
67
|
const middlewaresPath = path.join(serviceRoot, "src/middlewares");
|
|
52
68
|
|
|
@@ -125,15 +141,15 @@ export const setupService = async (
|
|
|
125
141
|
v1Routes.push(baseAuth.middleware);
|
|
126
142
|
}
|
|
127
143
|
|
|
128
|
-
// Update app
|
|
129
|
-
const appPath = path.join(serviceRoot,
|
|
144
|
+
// Update app file
|
|
145
|
+
const appPath = path.join(serviceRoot, `src/app.${ext}`);
|
|
130
146
|
let content = fs.readFileSync(appPath, "utf8");
|
|
131
147
|
content = content.replace("/*__IMPORTS__*/", imports.join("\n"));
|
|
132
148
|
content = content.replace("/*__MIDDLEWARE__*/", middlewares.join("\n"));
|
|
133
149
|
fs.writeFileSync(appPath, content);
|
|
134
150
|
|
|
135
151
|
// Update root endpoint middleware with project info
|
|
136
|
-
const rootMiddlewarePath = path.join(serviceRoot,
|
|
152
|
+
const rootMiddlewarePath = path.join(serviceRoot, `src/middlewares/root.middleware.${ext}`);
|
|
137
153
|
if (fs.existsSync(rootMiddlewarePath)) {
|
|
138
154
|
let rootContent = fs.readFileSync(rootMiddlewarePath, "utf8");
|
|
139
155
|
rootContent = rootContent.replace("/*__PROJECT_NAME__*/", serviceName || res.sanitizedName);
|
|
@@ -152,9 +168,9 @@ export const setupService = async (
|
|
|
152
168
|
fs.writeFileSync(rootMiddlewarePath, rootContent);
|
|
153
169
|
}
|
|
154
170
|
|
|
155
|
-
// Update v1 index
|
|
171
|
+
// Update v1 index file if needed
|
|
156
172
|
if (v1Imports.length || v1Routes.length) {
|
|
157
|
-
const v1IndexPath = path.join(serviceRoot,
|
|
173
|
+
const v1IndexPath = path.join(serviceRoot, `src/modules/v1/index.${ext}`);
|
|
158
174
|
let v1Content = fs.readFileSync(v1IndexPath, "utf8");
|
|
159
175
|
|
|
160
176
|
const lastImportIndex = v1Content.lastIndexOf("import");
|
|
@@ -175,8 +191,8 @@ export const setupService = async (
|
|
|
175
191
|
fs.writeFileSync(v1IndexPath, v1Content);
|
|
176
192
|
}
|
|
177
193
|
|
|
178
|
-
// Update env
|
|
179
|
-
const envPath = path.join(serviceRoot,
|
|
194
|
+
// Update env file to conditionally include ALLOWED_ORIGIN and MONGO_URI
|
|
195
|
+
const envPath = path.join(serviceRoot, `src/config/env.${ext}`);
|
|
180
196
|
if (fs.existsSync(envPath)) {
|
|
181
197
|
let envContent = fs.readFileSync(envPath, "utf8");
|
|
182
198
|
|
|
@@ -220,8 +236,8 @@ export const setupService = async (
|
|
|
220
236
|
fs.writeFileSync(envPath, envContent);
|
|
221
237
|
}
|
|
222
238
|
|
|
223
|
-
// Update server
|
|
224
|
-
const serverPath = path.join(serviceRoot,
|
|
239
|
+
// Update server file to connect to DB if auth is enabled
|
|
240
|
+
const serverPath = path.join(serviceRoot, `src/server.${ext}`);
|
|
225
241
|
if (fs.existsSync(serverPath)) {
|
|
226
242
|
let serverContent = fs.readFileSync(serverPath, "utf8");
|
|
227
243
|
|
package/package.json
CHANGED