@waqarcoding/saas-server 1.0.0 → 1.0.2
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 +34 -4
- package/lib/scaffold.js +29 -16
- package/package.json +2 -2
package/bin/cli.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import { fileURLToPath } from 'node:url';
|
|
5
6
|
import { spawnSync } from 'node:child_process';
|
|
@@ -18,7 +19,7 @@ ${ownPkg.name} v${ownPkg.version}
|
|
|
18
19
|
${ownPkg.description}
|
|
19
20
|
|
|
20
21
|
Usage:
|
|
21
|
-
npx
|
|
22
|
+
npx ${ownPkg.name} [project-name] [options]
|
|
22
23
|
|
|
23
24
|
Options:
|
|
24
25
|
--no-install Do not run "npm install" after creating the project
|
|
@@ -26,7 +27,7 @@ Options:
|
|
|
26
27
|
-h, --help Show this help
|
|
27
28
|
|
|
28
29
|
Example:
|
|
29
|
-
npx
|
|
30
|
+
npx ${ownPkg.name} my-backend
|
|
30
31
|
`);
|
|
31
32
|
process.exit(0);
|
|
32
33
|
}
|
|
@@ -43,8 +44,37 @@ if (major < 18) {
|
|
|
43
44
|
process.exit(1);
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
// ---- Safe working folder check ----
|
|
48
|
+
function getSafeCwd() {
|
|
49
|
+
let cwd;
|
|
50
|
+
try {
|
|
51
|
+
cwd = process.cwd(); // throws if the folder was deleted or moved
|
|
52
|
+
} catch {
|
|
53
|
+
console.error('❌ The current folder no longer exists.');
|
|
54
|
+
console.error(' Run "cd ~/Documents/projects" (or any normal folder) and try again.');
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const real = fs.realpathSync(cwd);
|
|
59
|
+
const home = os.homedir();
|
|
60
|
+
const trashDirs = [
|
|
61
|
+
path.join(home, '.Trash'), // macOS
|
|
62
|
+
path.join(home, '.local', 'share', 'Trash') // Linux
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
if (trashDirs.some((t) => real === t || real.startsWith(t + path.sep))) {
|
|
66
|
+
console.error(`❌ You are inside the Trash (${real}).`);
|
|
67
|
+
console.error(' Run "cd ~/Documents/projects" (or any normal folder) and try again.');
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return real;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const cwd = getSafeCwd();
|
|
75
|
+
|
|
46
76
|
const targetName = positional[0] || 'server';
|
|
47
|
-
const targetDir = path.resolve(
|
|
77
|
+
const targetDir = path.resolve(cwd, targetName);
|
|
48
78
|
|
|
49
79
|
if (fs.existsSync(targetDir) && fs.readdirSync(targetDir).length > 0) {
|
|
50
80
|
console.error(`❌ Folder "${targetName}" already exists and is not empty.`);
|
|
@@ -76,4 +106,4 @@ Next steps:
|
|
|
76
106
|
cd ${targetName}
|
|
77
107
|
${noInstall ? 'npm install\n ' : ''}# edit .env: MONGO_URI, Firebase and SMTP values
|
|
78
108
|
npm run dev
|
|
79
|
-
`);
|
|
109
|
+
`);
|
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
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
3
|
+
"version": "1.0.2",
|
|
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
|
+
}
|