diff --git a/README.md b/README.md index 7458562c..625fbfd5 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,10 @@ By default, `hotel` uses the following configuration values: "tld": 'localhost', // If you're behind a corporate proxy, replace this with your network proxy IP (example: "1.2.3.4:5000") - "proxy": false + "proxy": false, + + // Change to false to prevent hotel from starting itself up automatically on login + "autostart": true } ``` diff --git a/src/cli/daemon.js b/src/cli/daemon.js index 4716d320..31abfbc2 100644 --- a/src/cli/daemon.js +++ b/src/cli/daemon.js @@ -1,9 +1,11 @@ +const cp = require('child_process') const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') const startup = require('user-startup') const common = require('../common') const conf = require('../conf') +const pidFile = require('../pid-file') const uninstall = require('../scripts/uninstall') module.exports = { @@ -15,14 +17,33 @@ module.exports = { function start() { const node = process.execPath const daemonFile = path.join(__dirname, '../daemon') - const startupFile = startup.getFile('hotel') - startup.create('hotel', node, [daemonFile], common.logFile) + const pid = pidFile.read() + if (pid) { + console.log(`Already running (process ${pid})`) + return + } - // Save startup file path in ~/.hotel - // Will be used later by uninstall script - mkdirp.sync(common.hotelDir) - fs.writeFileSync(common.startupFile, startupFile) + if (conf.autostart) { + const startupFile = startup.getFile('hotel') + startup.create('hotel', node, [daemonFile], common.logFile) + + // Save startup file path in ~/.hotel + // Will be used later by uninstall script + mkdirp.sync(common.hotelDir) + fs.writeFileSync(common.startupFile, startupFile) + } else { + const fd = fs.openSync(common.logFile, 'w') + const opts = { + detached: true, // needed to unref() below + stdio: ['ignore', fd, fd] + } + + cp + .spawn(node, [daemonFile], opts) + .on('error', console.log) + .unref() + } console.log(`Started http://localhost:${conf.port}`) } diff --git a/src/conf.js b/src/conf.js index 717d150d..36b9995c 100644 --- a/src/conf.js +++ b/src/conf.js @@ -13,7 +13,9 @@ const defaults = { tld: 'localhost', // Replace with your network proxy IP (1.2.3.4:5000) if any // For example, if you're behind a corporate proxy - proxy: false + proxy: false, + // Set to false to disable automatic running on startup. + autostart: true } // Create empty conf it it doesn't exist