diff --git a/lib/forever.js b/lib/forever.js index 26201081..986afc40 100755 --- a/lib/forever.js +++ b/lib/forever.js @@ -17,7 +17,8 @@ var fs = require('fs'), utile = require('utile'), winston = require('winston'), mkdirp = utile.mkdirp, - async = utile.async; + async = utile.async, + configUtils = require('./util/config-utils'); var forever = exports; @@ -49,7 +50,7 @@ forever.initialized = false; forever.kill = require('forever-monitor').kill; forever.checkProcess = require('forever-monitor').checkProcess; forever.root = process.env.FOREVER_ROOT || path.join(process.env.HOME || process.env.USERPROFILE || '/root', '.forever'); -forever.config = new nconf.File({ file: path.join(forever.root, 'config.json') }); +forever.config = configUtils.initConfigFile(forever.root); forever.Forever = forever.Monitor = require('forever-monitor').Monitor; forever.Worker = require('./forever/worker').Worker; forever.cli = require('./forever/cli'); @@ -334,22 +335,9 @@ forever.load = function (options) { forever._debug(); } - // - // Syncronously create the `root` directory - // and the `pid` directory for forever. Although there is - // an additional overhead here of the sync action. It simplifies - // the setup of forever dramatically. - // - function tryCreate(dir) { - try { - fs.mkdirSync(dir, '0755'); - } - catch (ex) { } - } - - tryCreate(forever.config.get('root')); - tryCreate(forever.config.get('pidPath')); - tryCreate(forever.config.get('sockPath')); + configUtils.tryCreateDir(forever.config.get('root')); + configUtils.tryCreateDir(forever.config.get('pidPath')); + configUtils.tryCreateDir(forever.config.get('sockPath')); // // Attempt to save the new `config.json` for forever diff --git a/lib/util/config-utils.js b/lib/util/config-utils.js new file mode 100644 index 00000000..4bc35a43 --- /dev/null +++ b/lib/util/config-utils.js @@ -0,0 +1,29 @@ +var path = require('path'); +var fs = require('fs'); +var nconf = require('nconf'); + +function initConfigFile(foreverRoot) { + return new nconf.File({file: path.join(foreverRoot, 'config.json')}); +} + +// +// Synchronously create the `root` directory +// and the `pid` directory for forever. Although there is +// an additional overhead here of the sync action. It simplifies +// the setup of forever dramatically. +// +function tryCreateDir(dir) { + try { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, '0755'); + } + } + catch (error) { + throw new Error('Failed to create directory '+dir+":" +error.message); + } +} + +module.exports = { + initConfigFile: initConfigFile, + tryCreateDir: tryCreateDir +}; diff --git a/package.json b/package.json index a221bf05..cb7e7568 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,9 @@ }, "devDependencies": { "broadway": "~0.3.6", + "chai": "^4.2.0", "eventemitter2": "0.4.x", + "mocha": "^3.5.3", "moment": "^2.23.0", "request": "2.x.x", "vows": "0.7.x" diff --git a/test/util/config-utils-spec.js b/test/util/config-utils-spec.js new file mode 100644 index 00000000..3f144e89 --- /dev/null +++ b/test/util/config-utils-spec.js @@ -0,0 +1,30 @@ +var fs = require('fs'); +var configUtils = require('../../lib/util/config-utils'); +var expect = require('chai').expect; + +describe('config-utils', () => { + describe('tryCreateDir', () => { + it('happy path', () => { + expect(() => { + configUtils.tryCreateDir('happypath'); + }).to.not.throw(); + + expect(fs.existsSync('happypath')).to.equal(true); + fs.rmdirSync('happypath'); + }); + + it('throws an error on invalid directory', () => { + expect(() => { + configUtils.tryCreateDir(''); + }).to.throw(/Failed to create directory :ENOENT: no such file or directory, mkdir/); + }); + + it('does not fail when creating directory that already exists', () => { + expect(() => { + configUtils.tryCreateDir('dummy'); + configUtils.tryCreateDir('dummy'); + }).to.not.throw(); + fs.rmdirSync('dummy'); + }); + }); +});