@seip/bluebird-js 0.0.0-stage → 0.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.
- package/.env_example +54 -0
- package/AGENTS.md +403 -0
- package/LICENSE +21 -0
- package/README.md +284 -2
- package/app/routes/api.js +50 -0
- package/app/routes/web.js +53 -0
- package/app/views/about.html +161 -0
- package/app/views/dashboard.html +140 -0
- package/app/views/index.html +150 -0
- package/app/views/login.html +144 -0
- package/core/app.d.ts +18 -0
- package/core/app.js +644 -0
- package/core/auth.d.ts +13 -0
- package/core/auth.js +326 -0
- package/core/cache.d.ts +17 -0
- package/core/cache.js +475 -0
- package/core/cli/doctor.js +287 -0
- package/core/cli/ecosystem.js +162 -0
- package/core/cli/init.js +779 -0
- package/core/cli/migrate.js +541 -0
- package/core/cli/model.js +66 -0
- package/core/cli/nginx.js +199 -0
- package/core/cli/route.js +121 -0
- package/core/cli/swagger.js +45 -0
- package/core/cli/view.js +77 -0
- package/core/config.d.ts +3 -0
- package/core/config.js +111 -0
- package/core/csrf.d.ts +3 -0
- package/core/csrf.js +114 -0
- package/core/database.d.ts +15 -0
- package/core/database.js +636 -0
- package/core/debug.d.ts +3 -0
- package/core/debug.js +250 -0
- package/core/hash.d.ts +3 -0
- package/core/hash.js +194 -0
- package/core/index.d.ts +1071 -0
- package/core/logger.d.ts +3 -0
- package/core/logger.js +124 -0
- package/core/middleware.d.ts +3 -0
- package/core/middleware.js +28 -0
- package/core/model.d.ts +19 -0
- package/core/model.js +600 -0
- package/core/queue.d.ts +13 -0
- package/core/queue.js +172 -0
- package/core/ratelimit.d.ts +3 -0
- package/core/ratelimit.js +145 -0
- package/core/redis.d.ts +21 -0
- package/core/redis.js +230 -0
- package/core/render.d.ts +3 -0
- package/core/render.js +676 -0
- package/core/router.d.ts +3 -0
- package/core/router.js +171 -0
- package/core/seo.d.ts +3 -0
- package/core/seo.js +106 -0
- package/core/swagger.d.ts +3 -0
- package/core/swagger.js +60 -0
- package/core/upload.d.ts +13 -0
- package/core/upload.js +102 -0
- package/core/util/colors.js +82 -0
- package/core/validate.d.ts +15 -0
- package/core/validate.js +364 -0
- package/core/vite.d.ts +3 -0
- package/core/vite.js +145 -0
- package/core/ws.d.ts +13 -0
- package/core/ws.js +245 -0
- package/index.js +18 -0
- package/jsconfig.json +15 -0
- package/package.json +84 -3
- package/production.md +273 -0
- package/public/css/bluebird.min.css +2 -0
- package/public/images/favicon.ico +0 -0
- package/public/js/bluebird-client.js +1663 -0
- package/public/js/bluebird.min.js +1073 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
import colors from "../util/colors.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Parses .env file into key-value map.
|
|
10
|
+
* @returns {Record<string, string>}
|
|
11
|
+
*/
|
|
12
|
+
function parseEnv() {
|
|
13
|
+
const envPath = path.resolve(process.cwd(), ".env");
|
|
14
|
+
const env = {};
|
|
15
|
+
if (fs.existsSync(envPath)) {
|
|
16
|
+
const lines = fs.readFileSync(envPath, "utf-8").split("\n");
|
|
17
|
+
for (const line of lines) {
|
|
18
|
+
const trimmed = line.trim();
|
|
19
|
+
if (trimmed && !trimmed.startsWith("#") && trimmed.includes("=")) {
|
|
20
|
+
const idx = trimmed.indexOf("=");
|
|
21
|
+
const key = trimmed.substring(0, idx).trim();
|
|
22
|
+
let val = trimmed.substring(idx + 1).trim();
|
|
23
|
+
if (
|
|
24
|
+
(val.startsWith('"') && val.endsWith('"')) ||
|
|
25
|
+
(val.startsWith("'") && val.endsWith("'"))
|
|
26
|
+
) {
|
|
27
|
+
val = val.slice(1, -1);
|
|
28
|
+
}
|
|
29
|
+
env[key] = val;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return env;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a TCP port is currently open for binding on localhost.
|
|
38
|
+
* @param {number} port
|
|
39
|
+
* @returns {Promise<boolean>} Resolves to true if available, false if in use.
|
|
40
|
+
*/
|
|
41
|
+
function isPortAvailable(port) {
|
|
42
|
+
return new Promise((resolve) => {
|
|
43
|
+
const server = net.createServer();
|
|
44
|
+
server.once("error", () => {
|
|
45
|
+
resolve(false);
|
|
46
|
+
});
|
|
47
|
+
server.once("listening", () => {
|
|
48
|
+
server.close(() => resolve(true));
|
|
49
|
+
});
|
|
50
|
+
server.listen(port, "127.0.0.1");
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Finds a representative static file in public/ directory.
|
|
56
|
+
* @returns {string|null}
|
|
57
|
+
*/
|
|
58
|
+
function findSampleStaticAsset() {
|
|
59
|
+
const publicDir = path.resolve(process.cwd(), "public");
|
|
60
|
+
if (!fs.existsSync(publicDir)) return null;
|
|
61
|
+
|
|
62
|
+
const candidates = [
|
|
63
|
+
"css/style.css",
|
|
64
|
+
"js/index.js",
|
|
65
|
+
"favicon.ico",
|
|
66
|
+
"images/logo.png",
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
for (const candidate of candidates) {
|
|
70
|
+
if (fs.existsSync(path.join(publicDir, candidate))) {
|
|
71
|
+
return candidate;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const files = fs.readdirSync(publicDir, { recursive: true });
|
|
77
|
+
for (const f of files) {
|
|
78
|
+
const full = path.join(publicDir, f);
|
|
79
|
+
if (fs.statSync(full).isFile()) {
|
|
80
|
+
return f.replace(/\\/g, "/");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch {}
|
|
84
|
+
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Formats file permissions mode (e.g. 0600, 0755).
|
|
90
|
+
* @param {string} filePath
|
|
91
|
+
* @returns {string}
|
|
92
|
+
*/
|
|
93
|
+
function getOctalPermissions(filePath) {
|
|
94
|
+
try {
|
|
95
|
+
const stats = fs.statSync(filePath);
|
|
96
|
+
return "0" + (stats.mode & 0o777).toString(8);
|
|
97
|
+
} catch {
|
|
98
|
+
return "unknown";
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Runs the Blue Bird Doctor diagnostic suite.
|
|
104
|
+
*/
|
|
105
|
+
export async function runDoctor() {
|
|
106
|
+
console.log(colors.bold.cyan("============================================================="));
|
|
107
|
+
console.log(colors.bold.cyan(" Blue Bird System & Security Health Diagnostic (Doctor)"));
|
|
108
|
+
console.log(colors.bold.cyan("============================================================="));
|
|
109
|
+
console.log("");
|
|
110
|
+
|
|
111
|
+
const env = parseEnv();
|
|
112
|
+
const envPath = path.resolve(process.cwd(), ".env");
|
|
113
|
+
const projectDir = process.cwd();
|
|
114
|
+
const isLinux = process.platform === "linux";
|
|
115
|
+
const port = parseInt(env.PORT || "3000", 10);
|
|
116
|
+
let appUrl = (env.APP_URL || `http://localhost:${port}`).replace(/\/$/, "");
|
|
117
|
+
if (
|
|
118
|
+
(appUrl === "http://localhost" || appUrl === "http://127.0.0.1") &&
|
|
119
|
+
port !== 80
|
|
120
|
+
) {
|
|
121
|
+
appUrl = `http://localhost:${port}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let issuesFound = 0;
|
|
125
|
+
let warningsFound = 0;
|
|
126
|
+
|
|
127
|
+
console.log(colors.bold("[1/5] Environment & Configuration (.env):"));
|
|
128
|
+
if (!fs.existsSync(envPath)) {
|
|
129
|
+
console.log(colors.red(" [FAIL] .env file is missing in project root."));
|
|
130
|
+
console.log(colors.gray(" Run 'npx bluebird-js' or copy .env_example to .env."));
|
|
131
|
+
issuesFound++;
|
|
132
|
+
} else {
|
|
133
|
+
console.log(colors.green(" [PASS] .env file exists."));
|
|
134
|
+
|
|
135
|
+
if (isLinux) {
|
|
136
|
+
const envMode = getOctalPermissions(envPath);
|
|
137
|
+
if (envMode !== "0600" && envMode !== "0400") {
|
|
138
|
+
console.log(colors.yellow(` [WARN] .env permissions are '${envMode}'. Recommended mode is '0600' (chmod 600 .env).`));
|
|
139
|
+
warningsFound++;
|
|
140
|
+
} else {
|
|
141
|
+
console.log(colors.green(` [PASS] .env file permissions are strictly isolated (${envMode}).`));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!env.JWT_SECRET || env.JWT_SECRET.length < 16) {
|
|
146
|
+
console.log(colors.yellow(" [WARN] JWT_SECRET is missing or shorter than 16 characters."));
|
|
147
|
+
warningsFound++;
|
|
148
|
+
} else {
|
|
149
|
+
console.log(colors.green(" [PASS] JWT_SECRET is configured."));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
console.log("");
|
|
153
|
+
|
|
154
|
+
console.log(colors.bold("[2/5] Filesystem Hierarchy Standard & Static Asset Structure:"));
|
|
155
|
+
if (isLinux) {
|
|
156
|
+
if (projectDir.startsWith("/home/")) {
|
|
157
|
+
console.log(colors.yellow(" [WARN] Project is deployed inside '/home/user/'."));
|
|
158
|
+
console.log(colors.gray(" Recommended FHS production standard is '/var/www/<project>' or '/srv/<project>'"));
|
|
159
|
+
console.log(colors.gray(" to prevent path traversal restrictions for unprivileged containers (nginx UID 101)."));
|
|
160
|
+
warningsFound++;
|
|
161
|
+
} else {
|
|
162
|
+
console.log(colors.green(" [PASS] Project directory follows recommended FHS location."));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const publicDir = path.resolve(process.cwd(), "public");
|
|
167
|
+
if (!fs.existsSync(publicDir)) {
|
|
168
|
+
console.log(colors.yellow(" [WARN] 'public/' directory is missing. Static assets will not be served by Nginx."));
|
|
169
|
+
warningsFound++;
|
|
170
|
+
} else {
|
|
171
|
+
console.log(colors.green(" [PASS] 'public/' directory exists."));
|
|
172
|
+
const sampleAsset = findSampleStaticAsset();
|
|
173
|
+
if (sampleAsset) {
|
|
174
|
+
console.log(colors.green(` [PASS] Detected sample static asset: 'public/${sampleAsset}'`));
|
|
175
|
+
} else {
|
|
176
|
+
console.log(colors.yellow(" [WARN] No static files found inside 'public/'."));
|
|
177
|
+
warningsFound++;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const appDir = path.resolve(process.cwd(), "app");
|
|
182
|
+
if (!fs.existsSync(appDir)) {
|
|
183
|
+
console.log(colors.red(" [FAIL] 'app/' directory is missing."));
|
|
184
|
+
issuesFound++;
|
|
185
|
+
} else {
|
|
186
|
+
console.log(colors.green(" [PASS] 'app/' directory exists."));
|
|
187
|
+
}
|
|
188
|
+
console.log("");
|
|
189
|
+
|
|
190
|
+
console.log(colors.bold("[3/5] Port Availability & Binding:"));
|
|
191
|
+
const portFree = await isPortAvailable(port);
|
|
192
|
+
if (portFree) {
|
|
193
|
+
console.log(colors.green(` [INFO] Port ${port} is currently free and available for binding.`));
|
|
194
|
+
} else {
|
|
195
|
+
console.log(colors.cyan(` [INFO] Port ${port} is currently in use (application server or container is active).`));
|
|
196
|
+
}
|
|
197
|
+
console.log("");
|
|
198
|
+
|
|
199
|
+
console.log(colors.bold("[4/5] Live HTTP Smoke Test (Health & Static Delivery):"));
|
|
200
|
+
const sampleAsset = findSampleStaticAsset();
|
|
201
|
+
const testUrls = [
|
|
202
|
+
{ name: "API Health Endpoint", url: `${appUrl}/api/health`, isApi: true },
|
|
203
|
+
];
|
|
204
|
+
if (sampleAsset) {
|
|
205
|
+
testUrls.push({
|
|
206
|
+
name: `Static Asset (/${sampleAsset})`,
|
|
207
|
+
url: `${appUrl}/${sampleAsset}`,
|
|
208
|
+
isApi: false,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let serverReachable = false;
|
|
213
|
+
|
|
214
|
+
for (const item of testUrls) {
|
|
215
|
+
try {
|
|
216
|
+
const controller = new AbortController();
|
|
217
|
+
const timeoutId = setTimeout(() => controller.abort(), 3000);
|
|
218
|
+
const res = await fetch(item.url, { signal: controller.signal });
|
|
219
|
+
clearTimeout(timeoutId);
|
|
220
|
+
|
|
221
|
+
serverReachable = true;
|
|
222
|
+
|
|
223
|
+
if (res.status === 200) {
|
|
224
|
+
console.log(colors.green(` [PASS] ${item.name} returned HTTP 200 OK.`));
|
|
225
|
+
} else if (res.status === 403) {
|
|
226
|
+
console.log(colors.red(` [FAIL] ${item.name} returned HTTP 403 Forbidden.`));
|
|
227
|
+
console.log(colors.yellow(" Cause: Unprivileged Nginx worker (UID 101) lacks path traversal (+x) or read (+r) permissions."));
|
|
228
|
+
console.log(colors.yellow(" Remediation: Run the following commands on your host:"));
|
|
229
|
+
console.log(colors.white(` chmod 755 ${projectDir}`));
|
|
230
|
+
console.log(colors.white(" find public -type d -exec chmod 755 {} +"));
|
|
231
|
+
console.log(colors.white(" find public -type f -exec chmod 644 {} +"));
|
|
232
|
+
issuesFound++;
|
|
233
|
+
} else if (res.status === 404) {
|
|
234
|
+
console.log(colors.red(` [FAIL] ${item.name} returned HTTP 404 Not Found.`));
|
|
235
|
+
console.log(colors.yellow(" Cause: Resource or route not found."));
|
|
236
|
+
issuesFound++;
|
|
237
|
+
} else {
|
|
238
|
+
console.log(colors.yellow(` [WARN] ${item.name} returned HTTP ${res.status}.`));
|
|
239
|
+
warningsFound++;
|
|
240
|
+
}
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if (err.name === "AbortError") {
|
|
243
|
+
console.log(colors.yellow(` [WARN] Request to ${item.name} (${item.url}) timed out after 3s.`));
|
|
244
|
+
} else {
|
|
245
|
+
console.log(colors.gray(` [INFO] Cannot connect to ${item.name} at ${item.url} (${err.code || err.message}).`));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!serverReachable) {
|
|
251
|
+
console.log(colors.gray(" [INFO] Application server is not running locally. Start it with:"));
|
|
252
|
+
console.log(colors.gray(" Development: npm run dev"));
|
|
253
|
+
console.log(colors.gray(" Production: npm start (or 'pm2 start ecosystem.config.cjs')"));
|
|
254
|
+
}
|
|
255
|
+
console.log("");
|
|
256
|
+
|
|
257
|
+
console.log(colors.bold("[5/5] Database & Cache Architecture:"));
|
|
258
|
+
const rawDbType = (env.DB_TYPE || "").toLowerCase().trim();
|
|
259
|
+
let dbType = "sqlite";
|
|
260
|
+
if (["sqlite", "sqlite3", "sql", "better-sqlite3", "better-sqlite", "lite"].includes(rawDbType)) {
|
|
261
|
+
dbType = "sqlite";
|
|
262
|
+
} else if (["postgres", "postgresql", "pg", "psql", "pgsql", "postgre", "postgr", "psgr"].includes(rawDbType)) {
|
|
263
|
+
dbType = "postgres";
|
|
264
|
+
} else if (["mysql", "mariadb", "maria", "my"].includes(rawDbType)) {
|
|
265
|
+
dbType = "mysql";
|
|
266
|
+
} else if (["none", "no", "false", "null", "0"].includes(rawDbType)) {
|
|
267
|
+
dbType = "none";
|
|
268
|
+
}
|
|
269
|
+
console.log(colors.green(` [INFO] Database Type: ${dbType.toUpperCase()}`));
|
|
270
|
+
const cacheMode = (env.CACHE_MODE || "memory").toLowerCase();
|
|
271
|
+
console.log(colors.green(` [INFO] Cache Mode: ${cacheMode.toUpperCase()}`));
|
|
272
|
+
console.log("");
|
|
273
|
+
|
|
274
|
+
console.log(colors.bold.cyan("============================================================="));
|
|
275
|
+
if (issuesFound === 0 && warningsFound === 0) {
|
|
276
|
+
console.log(colors.bold.green(" Diagnostic Summary: All checks passed with zero issues!"));
|
|
277
|
+
} else if (issuesFound === 0) {
|
|
278
|
+
console.log(colors.bold.yellow(` Diagnostic Summary: System is operational with ${warningsFound} warning(s).`));
|
|
279
|
+
} else {
|
|
280
|
+
console.log(colors.bold.red(` Diagnostic Summary: Found ${issuesFound} error(s) and ${warningsFound} warning(s).`));
|
|
281
|
+
}
|
|
282
|
+
console.log(colors.bold.cyan("============================================================="));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("doctor.js") || process.argv[2] === "doctor") {
|
|
286
|
+
runDoctor();
|
|
287
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
import colors from "../util/colors.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Parses .env file into a key-value map.
|
|
10
|
+
* @param {string} envPath
|
|
11
|
+
* @returns {Record<string, string>}
|
|
12
|
+
*/
|
|
13
|
+
function parseEnv(envPath) {
|
|
14
|
+
const env = {};
|
|
15
|
+
if (!fs.existsSync(envPath)) return env;
|
|
16
|
+
const lines = fs.readFileSync(envPath, "utf-8").split("\n");
|
|
17
|
+
for (const line of lines) {
|
|
18
|
+
const trimmed = line.trim();
|
|
19
|
+
if (!trimmed || trimmed.startsWith("#") || !trimmed.includes("=")) continue;
|
|
20
|
+
const idx = trimmed.indexOf("=");
|
|
21
|
+
const key = trimmed.substring(0, idx).trim();
|
|
22
|
+
let val = trimmed.substring(idx + 1).trim();
|
|
23
|
+
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
|
|
24
|
+
val = val.slice(1, -1);
|
|
25
|
+
}
|
|
26
|
+
env[key] = val;
|
|
27
|
+
}
|
|
28
|
+
return env;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Detects job files inside app/jobs/ directory.
|
|
33
|
+
* @param {string} jobsDir
|
|
34
|
+
* @returns {string[]} Relative paths to job files.
|
|
35
|
+
*/
|
|
36
|
+
function detectJobs(jobsDir) {
|
|
37
|
+
if (!fs.existsSync(jobsDir)) return [];
|
|
38
|
+
try {
|
|
39
|
+
return fs
|
|
40
|
+
.readdirSync(jobsDir)
|
|
41
|
+
.filter((f) => f.endsWith(".js") || f.endsWith(".mjs"))
|
|
42
|
+
.map((f) => path.join("app/jobs", f).replace(/\\/g, "/"));
|
|
43
|
+
} catch {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Generates the PM2 ecosystem.config.cjs file for standalone deployments.
|
|
50
|
+
*/
|
|
51
|
+
function makeEcosystem() {
|
|
52
|
+
const cwd = process.cwd();
|
|
53
|
+
const envPath = path.join(cwd, ".env");
|
|
54
|
+
const outputPath = path.join(cwd, "ecosystem.config.cjs");
|
|
55
|
+
|
|
56
|
+
const env = parseEnv(envPath);
|
|
57
|
+
const instances = parseInt(env.PM2_INSTANCES || "1", 10);
|
|
58
|
+
const execMode = instances > 1 ? "cluster" : "fork";
|
|
59
|
+
const appName = (env.PM2_APP_NAME || env.TITLE || path.basename(cwd) || "bluebird-app")
|
|
60
|
+
.toLowerCase()
|
|
61
|
+
.replace(/[^a-z0-9_-]/g, "-");
|
|
62
|
+
|
|
63
|
+
const entryScript = "index.js";
|
|
64
|
+
|
|
65
|
+
const jobsDir = path.join(cwd, "app/jobs");
|
|
66
|
+
const jobs = detectJobs(jobsDir);
|
|
67
|
+
|
|
68
|
+
const mainApp = {
|
|
69
|
+
name: appName,
|
|
70
|
+
script: entryScript,
|
|
71
|
+
instances,
|
|
72
|
+
exec_mode: execMode,
|
|
73
|
+
node_args: "--env-file=.env",
|
|
74
|
+
env: {
|
|
75
|
+
NODE_ENV: "production",
|
|
76
|
+
DEBUG: "false",
|
|
77
|
+
},
|
|
78
|
+
max_memory_restart: env.PM2_MAX_MEMORY || "512M",
|
|
79
|
+
error_file: "logs/pm2-error.log",
|
|
80
|
+
out_file: "logs/pm2-out.log",
|
|
81
|
+
log_date_format: "YYYY-MM-DD HH:mm:ss",
|
|
82
|
+
autorestart: true,
|
|
83
|
+
watch: false,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const workerApps = jobs.map((jobFile) => {
|
|
87
|
+
const jobName = path.basename(jobFile, path.extname(jobFile));
|
|
88
|
+
return {
|
|
89
|
+
name: `${appName}-worker-${jobName}`,
|
|
90
|
+
script: jobFile,
|
|
91
|
+
instances: 1,
|
|
92
|
+
exec_mode: "fork",
|
|
93
|
+
node_args: "--env-file=.env",
|
|
94
|
+
env: {
|
|
95
|
+
NODE_ENV: "production",
|
|
96
|
+
DEBUG: "false",
|
|
97
|
+
},
|
|
98
|
+
max_memory_restart: "256M",
|
|
99
|
+
error_file: `logs/pm2-worker-${jobName}-error.log`,
|
|
100
|
+
out_file: `logs/pm2-worker-${jobName}-out.log`,
|
|
101
|
+
log_date_format: "YYYY-MM-DD HH:mm:ss",
|
|
102
|
+
autorestart: true,
|
|
103
|
+
watch: false,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const allApps = [mainApp, ...workerApps];
|
|
108
|
+
|
|
109
|
+
const fileContent = `/**
|
|
110
|
+
* Generated by: npx bluebird-js make:ecosystem
|
|
111
|
+
* Deployment mode: ${execMode} (PM2_INSTANCES=${instances})
|
|
112
|
+
*
|
|
113
|
+
* Usage:
|
|
114
|
+
* pm2 start ecosystem.config.cjs
|
|
115
|
+
* pm2 reload ecosystem.config.cjs --update-env
|
|
116
|
+
* pm2 delete ecosystem.config.cjs
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
module.exports = {
|
|
120
|
+
apps: ${JSON.stringify(allApps, null, 4)}
|
|
121
|
+
};
|
|
122
|
+
`;
|
|
123
|
+
|
|
124
|
+
if (fs.existsSync(outputPath)) {
|
|
125
|
+
console.log(colors.yellow("[WARN] ecosystem.config.cjs already exists. Overwriting."));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
fs.mkdirSync(path.join(cwd, "logs"), { recursive: true });
|
|
130
|
+
fs.writeFileSync(outputPath, fileContent, "utf-8");
|
|
131
|
+
console.log(colors.green(`[OK] Generated ecosystem.config.cjs (${instances} instance(s), mode: ${execMode}, app: ${appName}).`));
|
|
132
|
+
|
|
133
|
+
if (jobs.length > 0) {
|
|
134
|
+
console.log(colors.cyan(`[INFO] Detected ${jobs.length} worker job(s):`));
|
|
135
|
+
jobs.forEach((j) => console.log(colors.gray(` - ${j}`)));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const shouldStart = process.argv.includes("--start");
|
|
139
|
+
if (shouldStart) {
|
|
140
|
+
console.log(colors.cyan("[INFO] Starting application with PM2 and saving process list..."));
|
|
141
|
+
try {
|
|
142
|
+
execSync("pm2 start ecosystem.config.cjs", { stdio: "inherit", cwd });
|
|
143
|
+
execSync("pm2 save", { stdio: "inherit", cwd });
|
|
144
|
+
console.log(colors.green(`[OK] Successfully started '${appName}' in PM2 and saved process state.`));
|
|
145
|
+
} catch (pm2Err) {
|
|
146
|
+
console.error(colors.red("[ERROR] Failed to run PM2 commands. Make sure PM2 is installed: npm install -g pm2"), pm2Err.message);
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
console.log("");
|
|
150
|
+
console.log(colors.bold("Next steps for production:"));
|
|
151
|
+
console.log(colors.gray(" pm2 start ecosystem.config.cjs"));
|
|
152
|
+
console.log(colors.gray(" pm2 save"));
|
|
153
|
+
console.log(colors.gray(" pm2 startup"));
|
|
154
|
+
console.log("");
|
|
155
|
+
console.log(colors.gray("Tip: Run 'npx bluebird-js make:ecosystem --start' to generate and launch immediately."));
|
|
156
|
+
}
|
|
157
|
+
} catch (err) {
|
|
158
|
+
console.error(colors.red("[ERROR] Failed to generate ecosystem.config.cjs:"), err.message);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
makeEcosystem();
|