@waqarcoding/saas-server 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/lib/scaffold.js +29 -16
  2. package/package.json +2 -2
package/lib/scaffold.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { execSync } from 'node:child_process';
3
4
  import { fileURLToPath } from 'node:url';
4
5
 
5
6
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -36,23 +37,31 @@ function mergePackageJson(targetDir, name, result) {
36
37
  const tpl = JSON.parse(fs.readFileSync(tplPath, 'utf8'));
37
38
  const pkgPath = path.join(targetDir, 'package.json');
38
39
 
39
- // No package.json yet: write the template one.
40
+ // No package.json in THIS folder: run "npm init -y" here.
41
+ // (npm would otherwise use a package.json from a parent folder.)
40
42
  if (!fs.existsSync(pkgPath)) {
41
- tpl.name = safeName(name || path.basename(targetDir));
42
- fs.writeFileSync(pkgPath, JSON.stringify(tpl, null, 2) + '\n');
43
- result.created.push('package.json');
44
- return;
43
+ try {
44
+ execSync('npm init -y', { cwd: targetDir, stdio: 'ignore' });
45
+ result.created.push('package.json (npm init -y)');
46
+ } catch {
47
+ // npm not available or failed: write the template one instead
48
+ tpl.name = safeName(name || path.basename(targetDir));
49
+ fs.writeFileSync(pkgPath, JSON.stringify(tpl, null, 2) + '\n');
50
+ result.created.push('package.json');
51
+ return;
52
+ }
45
53
  }
46
54
 
47
- // Existing package.json: add what is missing, never overwrite.
48
55
  const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
49
56
  let changed = false;
50
57
 
51
- if (!pkg.type) {
58
+ // The template uses "import", so the type must be "module"
59
+ if (pkg.type !== 'module') {
60
+ if (pkg.type === 'commonjs' && fs.existsSync(path.join(targetDir, 'node_modules'))) {
61
+ result.warnings.push('"type" changed from commonjs to module. Old require() files will need updating.');
62
+ }
52
63
  pkg.type = 'module';
53
64
  changed = true;
54
- } else if (pkg.type !== 'module') {
55
- result.warnings.push('package.json has "type": "' + pkg.type + '". The template needs "type": "module".');
56
65
  }
57
66
 
58
67
  pkg.scripts = pkg.scripts || {};
@@ -73,14 +82,18 @@ function mergePackageJson(targetDir, name, result) {
73
82
  }
74
83
  }
75
84
 
76
- if (!pkg.main) {
85
+ // npm init -y sets main to index.js; use the template's entry file instead
86
+ const defaultMain = !pkg.main || (pkg.main === 'index.js' && !fs.existsSync(path.join(targetDir, 'index.js')));
87
+ if (defaultMain && tpl.main) {
77
88
  pkg.main = tpl.main;
78
89
  changed = true;
79
90
  }
80
91
 
81
92
  if (changed) {
82
93
  fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
83
- result.created.push('package.json (updated)');
94
+ if (!result.created.some((c) => c.startsWith('package.json'))) {
95
+ result.created.push('package.json (updated)');
96
+ }
84
97
  }
85
98
  }
86
99
 
@@ -98,10 +111,7 @@ export function scaffold(targetDir, { name } = {}) {
98
111
  const destName = parts.length === 0 && ROOT_RENAMES[fileName] ? ROOT_RENAMES[fileName] : fileName;
99
112
  const destRel = path.join(...parts, destName);
100
113
 
101
- if (destRel === 'package.json') {
102
- mergePackageJson(targetDir, name, result);
103
- continue;
104
- }
114
+ if (destRel === 'package.json') continue; // handled last, see below
105
115
 
106
116
  const dest = path.join(targetDir, destRel);
107
117
  if (fs.existsSync(dest)) {
@@ -113,5 +123,8 @@ export function scaffold(targetDir, { name } = {}) {
113
123
  result.created.push(destRel);
114
124
  }
115
125
 
126
+ // package.json is merged last, after every other file is in place
127
+ mergePackageJson(targetDir, name, result);
128
+
116
129
  return result;
117
- }
130
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waqarcoding/saas-server",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "It creates Express, MongoDB & Firebase Backend Template Ready",
5
5
  "type": "module",
6
6
  "bin": {
@@ -44,4 +44,4 @@
44
44
  "nodemailer": "^6.9.0",
45
45
  "socket.io": "^4.8.0"
46
46
  }
47
- }
47
+ }