Skip to content

Commit

Permalink
feat: nodemonConfig support in package.json
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rhodgkins authored and remy committed Sep 4, 2017
1 parent 63e8606 commit fb5da38
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/cli/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions lib/config/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
}

Expand All @@ -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 || {});
});
}
70 changes: 70 additions & 0 deletions test/config/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand All @@ -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);
Expand All @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exec": "foo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"nodemonConfig": {
"exec": "foo-ignored"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/packages/package-json-settings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"nodemonConfig": {
"exec": "foo",
"ignore": ["one", "three"],
"watch": ["four"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"start": "node app.js"
},
"nodemonConfig": {
"exec": "foo"
}
}

0 comments on commit fb5da38

Please sign in to comment.