Skip to content

Commit

Permalink
✨ feat [CLI]: --pm2 to offload EASY API to pm2` #2374
Browse files Browse the repository at this point in the history
  • Loading branch information
smashah committed Apr 12, 2022
1 parent 483f607 commit 339c40b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
41 changes: 39 additions & 2 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
#! /usr/bin/env node
// require('./index.js');
require('../dist/cli');
const pm2Index = process.argv.findIndex(arg => arg === "--pm2");
const sIdIndex = process.argv.findIndex(arg => arg === "--session-id");
const nameIndex = process.argv.findIndex(arg => arg === "--name");
const getVal = (index) => index !== -1 && process.argv[index + 1]
const getBool = (index) => !((index !== -1 && process.argv[index + 1]) === false)
const procName = getVal(sIdIndex || nameIndex) || "@OPEN-WA EASY API";
const CLI = '../dist/cli'
async function start() {
if (getBool(pm2Index)) {
const { spawn } = require("child_process");
try {
const pm2 = spawn('pm2');
await new Promise((resolve, reject) => {
pm2.on('error', reject);
pm2.stdout.on('data', () => resolve(true));
})
const pm2Flags = (getVal(pm2Index) || "").split(" ");
const cliFlags = (process.argv.slice(2) || []);
spawn("pm2", [
"start",
require.resolve(CLI),
'--name',
procName,
...pm2Flags,
'--',
...cliFlags.filter(x=>!pm2Flags.includes(x))
], {
stdio: "inherit",
detached: true
})
} catch (error) {
if (error.errorno === -2) console.error("pm2 not found. Please install with the following command: npm install -g pm2");
}
} else {
require(CLI);
}
}

start()
6 changes: 6 additions & 0 deletions src/cli/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ Merge<Merge<{
type: Boolean,
description: "Expose a tunnel to your EASY API session - this is for testing and it is unsecured."
},
{
name: 'pm2',
type: Boolean,
typeLabel: '{yellow {underline "--max-memory-restart 300M"}}',
description: "Offload the EASY API to local instance of pm2. You can add pm2 specific arguments also if you want."
},
{
name: 'help',
description: 'Print this usage guide.'
Expand Down

0 comments on commit 339c40b

Please sign in to comment.