Skip to content

Commit fb5da38

Browse files
rhodgkinsremy
authored andcommitted
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
1 parent 63e8606 commit fb5da38

File tree

8 files changed

+133
-0
lines changed

8 files changed

+133
-0
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ The above `nodemon.json` file might be my global config so that I have support f
9292

9393
A further example of options can be seen in [sample-nodemon.md](https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
9494

95+
### package.json
96+
97+
If you want to keep all your package configurations in one place, nodemon supports using `package.json` for configuration.
98+
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`:
99+
100+
{
101+
"name": "nodemon",
102+
"homepage": "http://nodemon.io",
103+
...
104+
... other standard package.json values
105+
...
106+
"nodemonConfig": {
107+
"ignore": ["test/*", "docs/*"],
108+
"delay": "2500"
109+
}
110+
}
111+
112+
Note that if you specify a `--config` file or provide a local `nodemon.json` any `package.json` config is ignored.
113+
95114
*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))*.
96115

97116
## Using nodemon as a module

doc/cli/config.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* $HOME/nodemon.json
88
* $PWD/nodemon.json OR --config <file>
9+
* nodemonConfig in package.json
910

1011
All config options in the .json file map 1-to-1 with the CLI options, so a
1112
config could read as:

lib/config/load.js

+20
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ function loadFile(options, config, dir, ready) {
171171
var filename = options.configFile || path.join(dir, 'nodemon.json');
172172
fs.readFile(filename, 'utf8', function (err, data) {
173173
if (err) {
174+
if (err.code === 'ENOENT') {
175+
if (!options.configFile && dir !== utils.home) {
176+
// if no specified local config file and local nodemon.json
177+
// doesn't exist, try the package.json
178+
return loadPackageJSON(config, callback);
179+
}
180+
}
174181
return callback({});
175182
}
176183

@@ -188,6 +195,19 @@ function loadFile(options, config, dir, ready) {
188195
// options values will overwrite settings
189196
callback(settings);
190197
});
198+
}
199+
200+
function loadPackageJSON(config, ready) {
201+
if (!ready) {
202+
ready = function () {};
203+
}
191204

205+
utils.log.detail('Looking in package.json for nodemonConfig');
192206

207+
var dir = process.cwd();
208+
var filename = path.join(dir, 'package.json');
209+
var packageLoadOptions = { configFile: filename };
210+
return loadFile(packageLoadOptions, config, dir, function (settings) {
211+
ready(settings.nodemonConfig || {});
212+
});
193213
}

test/config/load.test.js

+70
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ describe('config load', function () {
107107
});
108108
});
109109

110+
it('should read package.json config', function (done) {
111+
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
112+
process.chdir(dir);
113+
114+
var config = {},
115+
settings = { quiet: true },
116+
options = {};
117+
load(settings, options, config, function (config) {
118+
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
119+
done();
120+
});
121+
});
122+
110123
it('should give local files preference', function (done) {
111124
var config = {},
112125
settings = { quiet: true },
@@ -120,6 +133,36 @@ describe('config load', function () {
120133
});
121134
});
122135

136+
it('should give local files preference over package.json config', function (done) {
137+
var dir = path.resolve(pwd, 'test/fixtures/packages/nodemon-settings-and-package-json-settings');
138+
process.chdir(dir);
139+
140+
var config = {},
141+
settings = { quiet: true },
142+
options = {};
143+
load(settings, options, config, function (config) {
144+
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
145+
done();
146+
});
147+
});
148+
149+
it('should give package.json config preference', function (done) {
150+
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
151+
process.chdir(dir);
152+
153+
var config = {},
154+
settings = { quiet: true },
155+
options = {};
156+
load(settings, options, config, function (config) {
157+
removeRegExp(config);
158+
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
159+
assert.ok(config.ignore.indexOf('one') !== -1, 'ignore contains "one": ' + config.ignore);
160+
assert.ok(config.ignore.indexOf('three') !== -1, 'ignore contains "three": ' + config.ignore);
161+
assert.deepEqual(config.watch, ['four'], 'watch is "four": ' + config.watch);
162+
done();
163+
});
164+
});
165+
123166
it('should give user specified settings preference', function (done) {
124167
var config = {},
125168
settings = { ignore: ['one'], watch: ['one'], quiet: true },
@@ -132,6 +175,19 @@ describe('config load', function () {
132175
});
133176
});
134177

178+
it('should give user specified settings preference over package.json config', function (done) {
179+
var dir = path.resolve(pwd, 'test/fixtures/packages/package-json-settings');
180+
process.chdir(dir);
181+
182+
var config = {},
183+
settings = { exec: 'foo-user', quiet: true },
184+
options = {};
185+
load(settings, options, config, function (config) {
186+
assert.deepEqual(config.exec, 'foo-user', 'exec is "foo-user": ' + config.exec);
187+
done();
188+
});
189+
});
190+
135191
it('should give user specified exec preference over package.scripts.start', function (done) {
136192
var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-settings');
137193
process.chdir(dir);
@@ -146,6 +202,20 @@ describe('config load', function () {
146202
});
147203
});
148204

205+
it('should give package.json specified exec config over package.scripts.start', function (done) {
206+
var dir = path.resolve(pwd, 'test/fixtures/packages/start-and-package-json-settings');
207+
process.chdir(dir);
208+
209+
var config = {},
210+
settings = {},
211+
options = {};
212+
213+
load(settings, options, config, function (config) {
214+
assert.deepEqual(config.exec, 'foo', 'exec is "foo": ' + config.exec);
215+
done();
216+
});
217+
});
218+
149219
// it('should put the script at the end if found in package.scripts.start', function (done) {
150220
// process.chdir(path.resolve(pwd, 'test/fixtures/packages/start')); // allows us to load text/fixtures/package.json
151221
// var settings = cli.parse(asCLI('--harmony'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exec": "foo"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"nodemonConfig": {
3+
"exec": "foo-ignored"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"nodemonConfig": {
3+
"exec": "foo",
4+
"ignore": ["one", "three"],
5+
"watch": ["four"]
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"scripts": {
3+
"start": "node app.js"
4+
},
5+
"nodemonConfig": {
6+
"exec": "foo"
7+
}
8+
}

0 commit comments

Comments
 (0)