@drocketxx/pm2me 1.0.0 → 1.1.0
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/pm2me.js +58 -2
- package/package.json +1 -1
package/bin/pm2me.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
3
|
* pm2me — CLI entry point
|
|
4
|
-
* Usage:
|
|
4
|
+
* Usage:
|
|
5
|
+
* pm2me (Run in foreground)
|
|
6
|
+
* pm2me service install (Run in background via PM2 + set boot startup)
|
|
7
|
+
* pm2me service uninstall (Remove from PM2 background)
|
|
5
8
|
*/
|
|
6
9
|
import path from 'path';
|
|
7
10
|
import { fileURLToPath } from 'url';
|
|
8
|
-
import { spawn } from 'child_process';
|
|
11
|
+
import { spawn, execSync } from 'child_process';
|
|
9
12
|
import fs from 'fs';
|
|
10
13
|
import os from 'os';
|
|
11
14
|
|
|
@@ -15,6 +18,59 @@ const backendDir = path.join(pkgDir, 'backend');
|
|
|
15
18
|
|
|
16
19
|
// ── Parse CLI args ────────────────────────────────────────────────────────────
|
|
17
20
|
const args = process.argv.slice(2);
|
|
21
|
+
|
|
22
|
+
// ── Handle Service Commands ───────────────────────────────────────────────────
|
|
23
|
+
if (args[0] === 'service') {
|
|
24
|
+
const action = args[1];
|
|
25
|
+
if (action === 'install') {
|
|
26
|
+
console.log('[pm2me] Installing background service...');
|
|
27
|
+
try {
|
|
28
|
+
// 1. Start pm2me via PM2
|
|
29
|
+
// We use the full path to the backend app.js
|
|
30
|
+
const appPath = path.join(backendDir, 'app.js');
|
|
31
|
+
const homeDir = path.join(os.homedir(), '.pm2me');
|
|
32
|
+
const dbPath = path.join(homeDir, 'database.json');
|
|
33
|
+
|
|
34
|
+
console.log(`[pm2me] Starting via PM2 as 'pm2me-server'...`);
|
|
35
|
+
execSync(`pm2 start "${appPath}" --name pm2me-server --env PM2ME_DB_PATH="${dbPath}"`, { stdio: 'inherit' });
|
|
36
|
+
|
|
37
|
+
// 2. Save PM2 list
|
|
38
|
+
console.log(`[pm2me] Saving PM2 process list...`);
|
|
39
|
+
execSync(`pm2 save`, { stdio: 'inherit' });
|
|
40
|
+
|
|
41
|
+
// 3. Optional: Startup (User needs to run this with sudo/admin usually, but we try)
|
|
42
|
+
console.log(`[pm2me] Setting up boot startup...`);
|
|
43
|
+
try {
|
|
44
|
+
execSync(`pm2 startup`, { stdio: 'inherit' });
|
|
45
|
+
console.log(`[pm2me] Done! If you see a command above, please copy and run it to finalize startup.`);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.log(`[pm2me] 'pm2 startup' might need manual execution. Check instructions above.`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
console.log(`[pm2me] Service installed successfully. Access your UI at http://localhost:12345`);
|
|
51
|
+
process.exit(0);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error(`[pm2me] Failed to install service:`, err.message);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
} else if (action === 'uninstall') {
|
|
57
|
+
console.log('[pm2me] Uninstalling background service...');
|
|
58
|
+
try {
|
|
59
|
+
execSync(`pm2 delete pm2me-server`, { stdio: 'inherit' });
|
|
60
|
+
execSync(`pm2 save`, { stdio: 'inherit' });
|
|
61
|
+
console.log(`[pm2me] Service uninstalled successfully.`);
|
|
62
|
+
process.exit(0);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.error(`[pm2me] Failed to uninstall service:`, err.message);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
console.log(`Usage: pm2me service [install|uninstall]`);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ── Standard Foreground Execution ─────────────────────────────────────────────
|
|
18
74
|
const portArg = args.indexOf('--port');
|
|
19
75
|
const PORT = portArg !== -1 ? args[portArg + 1] : (process.env.PORT || 12345);
|
|
20
76
|
|