Skip to content

Commit

Permalink
feat: support using ‘nodemonConfig’ in package.json
Browse files Browse the repository at this point in the history
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 remy#873
  • Loading branch information
rhodgkins committed May 13, 2017
1 parent f9fc962 commit dc191c6
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
22 changes: 22 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 @@ -189,4 +196,19 @@ function loadFile(options, config, dir, ready) {
});


}

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 dc191c6

Please sign in to comment.