diff --git a/README.md b/README.md index 6e3dbbc2..38e79e80 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,25 @@ The above `nodemon.json` file might be my global config so that I have support f A further example of options can be seen in [sample-nodemon.md](https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md) +### package.json + +If you want to keep all your package configurations in one place, nodemon supports using `package.json` for configuration. +Simply specify the config in the same format as you would for a config file but under `nodemonConfig` in the `package.json` file, for example, take the following `package.json`: + + { + "name": "nodemon", + "homepage": "http://nodemon.io", + ... + ... other standard package.json values + ... + "nodemonConfig": { + "ignore": ["test/*", "docs/*"], + "delay": "2500" + } + } + +Note that if you specify a `--config` file or provide a local `nodemon.json` any `package.json` config is ignored. + *This section needs better documentation, but for now you can also see `nodemon --help config` ([also here](https://github.com/remy/nodemon/blob/master/doc/cli/config.txt))*. ## Using nodemon as a module diff --git a/doc/cli/config.txt b/doc/cli/config.txt index 3d1b3084..479f8f6a 100644 --- a/doc/cli/config.txt +++ b/doc/cli/config.txt @@ -6,6 +6,7 @@ * $HOME/nodemon.json * $PWD/nodemon.json OR --config + * nodemonConfig in package.json All config options in the .json file map 1-to-1 with the CLI options, so a config could read as: diff --git a/lib/config/load.js b/lib/config/load.js index 5422f67b..dec02769 100644 --- a/lib/config/load.js +++ b/lib/config/load.js @@ -171,6 +171,13 @@ function loadFile(options, config, dir, ready) { var filename = options.configFile || path.join(dir, 'nodemon.json'); fs.readFile(filename, 'utf8', function (err, data) { if (err) { + if (err.code === 'ENOENT') { + if (!options.configFile && dir !== utils.home) { + // if no specified local config file and local nodemon.json + // doesn't exist, try the package.json + return loadPackageJSON(config, callback); + } + } return callback({}); } @@ -188,6 +195,19 @@ function loadFile(options, config, dir, ready) { // options values will overwrite settings callback(settings); }); +} + +function loadPackageJSON(config, ready) { + if (!ready) { + ready = function () {}; + } + utils.log.detail('Looking in package.json for nodemonConfig'); + var dir = process.cwd(); + var filename = path.join(dir, 'package.json'); + var packageLoadOptions = { configFile: filename }; + return loadFile(packageLoadOptions, config, dir, function (settings) { + ready(settings.nodemonConfig || {}); + }); } diff --git a/test/config/load.test.js b/test/config/load.test.js index 1d119fe6..9e179acb 100644 --- a/test/config/load.test.js +++ b/test/config/load.test.js @@ -107,6 +107,19 @@ describe('config load', function () { }); }); + it('should read package.json config', function (done) { + var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings'); + process.chdir(dir); + + var config = {}, + settings = { quiet: true }, + options = {}; + load(settings, options, config, function (config) { + assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec); + done(); + }); + }); + it('should give local files preference', function (done) { var config = {}, settings = { quiet: true }, @@ -120,6 +133,36 @@ describe('config load', function () { }); }); + it('should give local files preference over package.json config', function (done) { + var dir = path.resolve(pwd, 'test/fixtures/packages/nodemon-settings-and-package-json-settings'); + process.chdir(dir); + + var config = {}, + settings = { quiet: true }, + options = {}; + load(settings, options, config, function (config) { + assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec); + done(); + }); + }); + + it('should give package.json config preference', function (done) { + var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings'); + process.chdir(dir); + + var config = {}, + settings = { quiet: true }, + options = {}; + load(settings, options, config, function (config) { + removeRegExp(config); + assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec); + assert.ok(config.ignore.indexOf('one') !== -1, 'ignore contains "one": ' + config.ignore); + assert.ok(config.ignore.indexOf('three') !== -1, 'ignore contains "three": ' + config.ignore); + assert.deepEqual(config.watch, ['four'], 'watch is "four": ' + config.watch); + done(); + }); + }); + it('should give user specified settings preference', function (done) { var config = {}, settings = { ignore: ['one'], watch: ['one'], quiet: true }, @@ -132,6 +175,19 @@ describe('config load', function () { }); }); + it('should give user specified settings preference over package.json config', function (done) { + var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings'); + process.chdir(dir); + + var config = {}, + settings = { exec: 'foo-user', quiet: true }, + options = {}; + load(settings, options, config, function (config) { + assert.deepEqual(config.exec, 'foo-user', 'exec is "foo-user": ' + config.exec); + done(); + }); + }); + it('should give user specified exec preference over package.scripts.start', function (done) { var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-settings'); process.chdir(dir); @@ -146,6 +202,20 @@ describe('config load', function () { }); }); + it('should give package.json specified exec config over package.scripts.start', function (done) { + var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-package-json-settings'); + process.chdir(dir); + + var config = {}, + settings = {}, + options = {}; + + load(settings, options, config, function (config) { + assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec); + done(); + }); + }); + // it('should put the script at the end if found in package.scripts.start', function (done) { // process.chdir(path.resolve(pwd, 'test/fixtures/packages/start')); // allows us to load text/fixtures/package.json // var settings = cli.parse(asCLI('--harmony')); diff --git a/test/fixtures/packages/nodemon-settings-and-package-json-settings/nodemon.json b/test/fixtures/packages/nodemon-settings-and-package-json-settings/nodemon.json new file mode 100644 index 00000000..e81bdb30 --- /dev/null +++ b/test/fixtures/packages/nodemon-settings-and-package-json-settings/nodemon.json @@ -0,0 +1,3 @@ +{ + "exec": "foo" +} diff --git a/test/fixtures/packages/nodemon-settings-and-package-json-settings/package.json b/test/fixtures/packages/nodemon-settings-and-package-json-settings/package.json new file mode 100644 index 00000000..c508e455 --- /dev/null +++ b/test/fixtures/packages/nodemon-settings-and-package-json-settings/package.json @@ -0,0 +1,5 @@ +{ + "nodemonConfig": { + "exec": "foo-ignored" + } +} diff --git a/test/fixtures/packages/package-json-settings/package.json b/test/fixtures/packages/package-json-settings/package.json new file mode 100644 index 00000000..d2d53825 --- /dev/null +++ b/test/fixtures/packages/package-json-settings/package.json @@ -0,0 +1,7 @@ +{ + "nodemonConfig": { + "exec": "foo", + "ignore": ["one", "three"], + "watch": ["four"] + } +} diff --git a/test/fixtures/packages/start-and-package-json-settings/package.json b/test/fixtures/packages/start-and-package-json-settings/package.json new file mode 100644 index 00000000..285e9237 --- /dev/null +++ b/test/fixtures/packages/start-and-package-json-settings/package.json @@ -0,0 +1,8 @@ +{ + "scripts": { + "start": "node app.js" + }, + "nodemonConfig": { + "exec": "foo" + } +}