Skip to content

Commit 339c40b

Browse files
committed
✨ feat [CLI]: --pm2 to offload EASY API to pm2` #2374
1 parent 483f607 commit 339c40b

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

bin/server.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
11
#! /usr/bin/env node
2-
// require('./index.js');
3-
require('../dist/cli');
2+
const pm2Index = process.argv.findIndex(arg => arg === "--pm2");
3+
const sIdIndex = process.argv.findIndex(arg => arg === "--session-id");
4+
const nameIndex = process.argv.findIndex(arg => arg === "--name");
5+
const getVal = (index) => index !== -1 && process.argv[index + 1]
6+
const getBool = (index) => !((index !== -1 && process.argv[index + 1]) === false)
7+
const procName = getVal(sIdIndex || nameIndex) || "@OPEN-WA EASY API";
8+
const CLI = '../dist/cli'
9+
async function start() {
10+
if (getBool(pm2Index)) {
11+
const { spawn } = require("child_process");
12+
try {
13+
const pm2 = spawn('pm2');
14+
await new Promise((resolve, reject) => {
15+
pm2.on('error', reject);
16+
pm2.stdout.on('data', () => resolve(true));
17+
})
18+
const pm2Flags = (getVal(pm2Index) || "").split(" ");
19+
const cliFlags = (process.argv.slice(2) || []);
20+
spawn("pm2", [
21+
"start",
22+
require.resolve(CLI),
23+
'--name',
24+
procName,
25+
...pm2Flags,
26+
'--',
27+
...cliFlags.filter(x=>!pm2Flags.includes(x))
28+
], {
29+
stdio: "inherit",
30+
detached: true
31+
})
32+
} catch (error) {
33+
if (error.errorno === -2) console.error("pm2 not found. Please install with the following command: npm install -g pm2");
34+
}
35+
} else {
36+
require(CLI);
37+
}
38+
}
39+
40+
start()

src/cli/cli-options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ Merge<Merge<{
241241
type: Boolean,
242242
description: "Expose a tunnel to your EASY API session - this is for testing and it is unsecured."
243243
},
244+
{
245+
name: 'pm2',
246+
type: Boolean,
247+
typeLabel: '{yellow {underline "--max-memory-restart 300M"}}',
248+
description: "Offload the EASY API to local instance of pm2. You can add pm2 specific arguments also if you want."
249+
},
244250
{
245251
name: 'help',
246252
description: 'Print this usage guide.'

0 commit comments

Comments
 (0)