Skip to content

Commit

Permalink
Updated: pm2 upstart - replaced service with pm2-windows-startup
Browse files Browse the repository at this point in the history
  • Loading branch information
vervallsweg committed Apr 15, 2019
1 parent c16e795 commit ed0f44b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
6 changes: 3 additions & 3 deletions castWebApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ prog
console.log(value);
})
.catch(error => {
if (error.stdout.includes("sudo env")) {
if (error.stdout && error.stdout.includes("sudo env")) {
spinner.fail("Permissions required. To do this, just copy/paste and run this command:");
} else {
spinner.fail(error.error.message);
console.error(error);
//console.error(error);
}
console.log(error.stdout);
});
Expand All @@ -99,7 +99,7 @@ prog
spinner.fail("Permissions required. To do this, just copy/paste and run this command:");
} else {
spinner.fail(error.error.message);
console.error(error);
// console.error(error);
}
console.log(error.stdout);
});
Expand Down
60 changes: 48 additions & 12 deletions manager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const pm2 = require('pm2');
const { exec } = require('child_process');

Expand Down Expand Up @@ -60,9 +61,17 @@ class Manager {
}

static startup() {
let windows = process.platform === "win32";
if (windows) return Manager.startupWin();
else return Manager.startupPm2();
return new Promise((resolve, reject) => {
let windows = process.platform === "win32";
Manager.save(windows)
.then(() => {
if (windows) resolve(Manager.startupWin());
else resolve(Manager.startupPm2());
})
.catch(error => {
reject({error: {message: "Couldn't save pm2 processes"}, stdout: "", stderr: error});
})
})
}

static startupPm2() {
Expand All @@ -81,13 +90,12 @@ class Manager {

static startupWin() {
return new Promise((resolve, reject) => {
let cmd = require.resolve('pm2-windows-service').replace('src/index.js', 'bin/pm2-service-install');
exec(`${cmd}`, (error, stdout, stderr) => {
if (error || stderr) {
reject({error: error, stdout: stdout, stderr: stderr});
}
resolve(stdout);
});
let pm2WindowsStartupPath = require.resolve('pm2-windows-startup');
Manager.fixWinResurrectBat(pm2WindowsStartupPath.replace('index.js', 'pm2_resurrect.cmd'))
.then(()=>{
reject({error: {message: "Windows, to auto start, just copy/paste and run the command below: \n"}, stdout: `node ${pm2WindowsStartupPath} install`, stderr: ""});
})
.catch(error => {reject(error)});
});
}

Expand All @@ -111,8 +119,16 @@ class Manager {

static unstartupWin() {
return new Promise((resolve, reject) => {
let cmd = require.resolve('pm2-windows-service').replace('src/index.js', 'bin/pm2-service-uninstall');
exec(`${cmd}`, (error, stdout, stderr) => {
let cmd = `${require.resolve('pm2-windows-startup')} uninstall`;
reject({error: {message: "Windows, to stop auto start, just copy/paste and run the command below: \n"}, stdout: `node ${cmd}`, stderr: ""});
});
}

static save(windows) {
return new Promise((resolve, reject) => {
let cmd = `${require.resolve('pm2').replace('index.js', 'bin/pm2')} save`;
if (windows) cmd = `node ${require.resolve('pm2').replace('index.js', 'bin\\pm2')} save`;
exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
reject({error: error, stdout: stdout, stderr: stderr});
}
Expand All @@ -121,6 +137,26 @@ class Manager {
});
}

static fixWinResurrectBat(resurrectBatPath) {
return new Promise((resolve, reject) => {
fs.readFile(resurrectBatPath, 'utf8', (err, data) => {
if (err) reject(err); //TODO: adapt to custom object format

if (!data.includes('\\pm2')) {
let newPM2Path = `node ${require.resolve('pm2').replace('index.js', 'bin\\pm2')}`;
let newResurrectBat = data.replace('pm2', newPM2Path);

fs.writeFile(resurrectBatPath, newResurrectBat, 'utf8', err => {
if (err) reject(err); //TODO: adapt to custom object format
else resolve(true);
});
} else {
resolve(true);
}
});
});
}

static getProcessDescriptionList() {
return new Promise((resolve, reject) => {
Manager.isReady().then(ready => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"optionalDependencies": {
"google-assistant": "^0.5.2",
"pm2-windows-service": "0.2.1"
"pm2-windows-startup": "1.0.3"
},
"repository": {
"type": "git",
Expand Down

1 comment on commit ed0f44b

@vervallsweg
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated: pm2 upstart - replaced service with pm2-windows-startup #102

Please sign in to comment.