@ifecodes/backend-template 1.0.7 → 1.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/cli.js CHANGED
@@ -123,21 +123,30 @@ if (isInMicroserviceProject || config.projectType === "microservice") {
123
123
  .filter((f) => fs.statSync(path.join(servicesDir, f)).isDirectory())
124
124
  : servicesToCreate;
125
125
 
126
+ // Track if all installs succeeded for Husky setup
127
+ let allInstallsSucceeded = true;
128
+
126
129
  // Now setup each service with knowledge of all services
127
130
  for (const serviceName of servicesToCreate) {
128
131
  const serviceRoot = path.join(target, "services", serviceName);
129
132
  const shouldIncludeAuth = isInMicroserviceProject
130
133
  ? config.auth
131
134
  : serviceName === "auth-service";
132
- await setupService(
135
+ const result = await setupService(
133
136
  config,
134
137
  serviceName,
135
138
  serviceRoot,
136
139
  shouldIncludeAuth,
137
140
  allServices
138
141
  );
142
+ if (!result.installSucceeded) {
143
+ allInstallsSucceeded = false;
144
+ }
139
145
  }
140
146
 
147
+ // Store for later use
148
+ config.allInstallsSucceeded = allInstallsSucceeded;
149
+
141
150
  if (mode === "docker") {
142
151
  generateDockerCompose(target, allServices);
143
152
  copyDockerfile(target, servicesToCreate);
@@ -165,7 +174,8 @@ if (isInMicroserviceProject || config.projectType === "microservice") {
165
174
  );
166
175
  }
167
176
  } else {
168
- await setupService(config, null, target, true);
177
+ const result = await setupService(config, null, target, true);
178
+ config.installSucceeded = result.installSucceeded;
169
179
  }
170
180
 
171
181
  // Generate README.md
@@ -205,17 +215,20 @@ if (!isInMicroserviceProject) {
205
215
  // Install husky and setup at root level
206
216
  if (config.projectType === "microservice") {
207
217
  console.log("\n📦 Installing Husky at root level...\n");
208
- try {
209
- execSync("npm install", { cwd: target, stdio: "inherit" });
210
- console.log("\n🔧 Setting up Husky...\n");
211
- execSync("npm run prepare", { cwd: target, stdio: "inherit" });
212
- } catch (error) {
213
- console.log("\n⚠️ Husky setup skipped (dependencies not installed)\n");
218
+ if (config.allInstallsSucceeded) {
219
+ try {
220
+ execSync("npm install", { cwd: target, stdio: "inherit" });
221
+ console.log("\n🔧 Setting up Husky...\n");
222
+ execSync("npm run prepare", { cwd: target, stdio: "inherit" });
223
+ } catch (error) {
224
+ console.log("\n⚠️ Husky setup failed\n");
225
+ }
226
+ } else {
227
+ console.log("\n⚠️ Husky setup skipped (run 'npm install && npm run prepare' after fixing service dependencies)\n");
214
228
  }
215
229
  } else if (config.projectType === "monolith") {
216
- // Only setup Husky if node_modules exists (dependencies installed successfully)
217
- const nodeModulesPath = path.join(target, "node_modules");
218
- if (fs.existsSync(nodeModulesPath)) {
230
+ // Only setup Husky if installation succeeded
231
+ if (config.installSucceeded) {
219
232
  console.log("\n🔧 Setting up Husky...\n");
220
233
  try {
221
234
  execSync("npm run prepare", { cwd: target, stdio: "inherit" });
@@ -197,8 +197,13 @@ export const setupService = async (
197
197
  "/*__MONGO_URI__*/",
198
198
  'MONGO_URI: process.env.MONGO_URI!,'
199
199
  );
200
+ envContent = envContent.replace(
201
+ "/*__JWT_SECRET__*/",
202
+ 'JWT_SECRET: process.env.JWT_SECRET!,'
203
+ );
200
204
  } else {
201
205
  envContent = envContent.replace("/*__MONGO_URI__*/", "");
206
+ envContent = envContent.replace("/*__JWT_SECRET__*/", "");
202
207
  }
203
208
 
204
209
  fs.writeFileSync(envPath, envContent);
@@ -297,6 +302,8 @@ await connectDB();`
297
302
  `\n📦 Installing dependencies for ${serviceName || "project"}...\n`
298
303
  );
299
304
 
305
+ let installSucceeded = false;
306
+
300
307
  try {
301
308
  if (deps.length) {
302
309
  execSync(`npm install ${deps.join(" ")}`, {
@@ -305,17 +312,27 @@ await connectDB();`
305
312
  });
306
313
  }
307
314
  execSync("npm install", { cwd: serviceRoot, stdio: "inherit" });
315
+ installSucceeded = true;
316
+
317
+ // Run format after successful install
318
+ console.log("\n🎨 Formatting code...\n");
319
+ try {
320
+ execSync("npm run format", { cwd: serviceRoot, stdio: "inherit" });
321
+ } catch (formatError) {
322
+ console.warn("⚠️ Warning: Code formatting failed. You can run it manually later with: npm run format\n");
323
+ }
308
324
  } catch (error) {
309
325
  console.error("\n⚠️ Warning: Some dependencies failed to install.");
310
326
  console.error("This is usually due to native modules (like argon2) requiring build tools.\n");
311
327
  console.error("💡 Solutions:");
312
328
  console.error(" 1. Install build tools: npm install --global windows-build-tools");
313
329
  console.error(" 2. Or switch to bcrypt (works better on Windows)");
314
- console.error(" 3. Or manually install later: cd " + (serviceName || res.sanitizedName) + " && npm install\n");
330
+ console.error(" 3. Or manually install later: cd " + (serviceName || res.sanitizedName) + " && npm install");
331
+ console.error(" 4. Then run: npm run format\n");
315
332
 
316
333
  // Don't exit - let the project be created anyway
317
334
  console.log("⏭️ Continuing with project creation...\n");
318
335
  }
319
336
 
320
- return deps;
337
+ return { deps, installSucceeded };
321
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ifecodes/backend-template",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Production-ready Express + TypeScript backend generator with optional features and microservice support",
5
5
  "bin": {
6
6
  "ifecodes-template": "bin/cli.js"
@@ -6,4 +6,5 @@ export const ENV = {
6
6
  /*__ALLOWED_ORIGIN__*/
7
7
  NODE_ENV: process.env.NODE_ENV!,
8
8
  /*__MONGO_URI__*/
9
+ /*__JWT_SECRET__*/
9
10
  };
@@ -1,11 +1,19 @@
1
1
  import app from "./app";
2
2
  import { ENV } from "./config";
3
+ import { logger } from "@/utils";
3
4
  /*__DB_IMPORT__*/
4
5
 
5
6
  const PORT = ENV.PORT || 3000;
6
7
 
7
- /*__DB_CONNECT__*/
8
+ const startServer = async () => {
9
+ /*__DB_CONNECT__*/
8
10
 
9
- app.listen(PORT, () => {
10
- console.log(`Server is running on port ${PORT}`);
11
- });
11
+ app.listen(PORT, () => {
12
+ logger.info("Server", `Server is running on port ${PORT}`);
13
+ });
14
+ };
15
+
16
+ startServer().catch((error) => {
17
+ logger.error("Server", "Failed to start server", error as Error);
18
+ process.exit(1);
19
+ });