feat: nodemonConfig support in package.json

* feat: support using ‘nodemonConfig’ in package.json

Implements loading configuration options from the `nodemonConfig` value in the package.json, nodemon.json is still preferred before looking at the new option.
Also includes tests.

Closes #873

* docs: update for using ‘nodemonConfig’ in package.json

Add to both the README and `nodemon --help config`.

#1028
This commit is contained in:
Rich Hodgkins
2017-09-04 20:26:17 +01:00
committed by Remy Sharp
parent 63e8606748
commit fb5da380c8
8 changed files with 135 additions and 2 deletions

View File

@@ -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

View File

@@ -6,6 +6,7 @@
* $HOME/nodemon.json
* $PWD/nodemon.json OR --config <file>
* 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:

View File

@@ -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 || {});
});
}

View File

@@ -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'));

View File

@@ -0,0 +1,3 @@
{
"exec": "foo"
}

View File

@@ -0,0 +1,5 @@
{
"nodemonConfig": {
"exec": "foo-ignored"
}
}

View File

@@ -0,0 +1,7 @@
{
"nodemonConfig": {
"exec": "foo",
"ignore": ["one", "three"],
"watch": ["four"]
}
}

View File

@@ -0,0 +1,8 @@
{
"scripts": {
"start": "node app.js"
},
"nodemonConfig": {
"exec": "foo"
}
}