Browse files

feat: support --config command line arg

Implement `--config <file>` option to allow config loading from an arbitrary file instead of being
bound to the static `$PWD/nodemon.json`.

Closes #755 
Closes #855
1 parent 90a8e6a commit ee110add5399829021ebe53c17cb4dd006d3e5e4 @knksmith57 knksmith57 committed with Jul 25, 2016
Showing with 22 additions and 5 deletions.
  1. +1 −1 README.md
  2. +1 −1 doc/cli/config.txt
  3. +2 −0 doc/cli/help.txt
  4. +4 −0 lib/cli/parse.js
  5. +5 −2 lib/config/load.js
  6. +9 −1 test/cli/parse.test.js
View
2 README.md
@@ -69,7 +69,7 @@ Whilst nodemon is running, if you need to manually restart your application, ins
## Config files
-nodemon supports local and global configuration files. These are named `nodemon.json` and can be located in the current working directory or in your home directory.
+nodemon supports local and global configuration files. These are usually named `nodemon.json` and can be located in the current working directory or in your home directory. An alternative local configuration file can be specified with the `--config <file>` option.
The specificity is as follows, so that a command line argument will always override the config file settings:
View
2 doc/cli/config.txt
@@ -5,7 +5,7 @@
nodemon can also be configured via a local and global config file:
* $HOME/nodemon.json
- * $PWD/nodemon.json
+ * $PWD/nodemon.json OR --config <file>
All config options in the .json file map 1-to-1 with the CLI options, so a
config could read as:
View
2 doc/cli/help.txt
@@ -2,6 +2,7 @@
Options:
+ --config file ............ alternate nodemon.json config file to use
-e, --ext ................ extensions to look for, ie. js,jade,hbs.
-x, --exec app ........... execute script with "app", ie. -x "python -v".
-w, --watch dir........... watch directory "dir" or files. use once for
@@ -30,6 +31,7 @@
Examples:
$ nodemon server.js
+ $ nodemon --config my/custom/nodemon.json server.js
$ nodemon -w ../foo server.js apparg1 apparg2
$ PORT=8000 nodemon --debug-brk server.js
$ nodemon --exec python app.py
View
4 lib/cli/parse.js
@@ -152,6 +152,10 @@ function nodemonOption(options, arg, eatNext) {
options.hidden = true;
} else
+ if (arg === '--config') {
+ options.configFile = eatNext();
+ } else
+
if (arg === '--watch' || arg === '-w') {
if (!options.watch) { options.watch = []; }
options.watch.push(eatNext());
View
7 lib/config/load.js
@@ -22,7 +22,10 @@ function load(settings, options, config, callback) {
config.loaded = [];
// first load the root nodemon.json
loadFile(options, config, utils.home, function (options) {
- // then load the user's local nodemon.json
+ // then load the user's local configuration file
+ if (settings.configFile) {
+ options.configFile = path.resolve(settings.configFile);
+ }
loadFile(options, config, process.cwd(), function (options) {
// Then merge over with the user settings (parsed from the cli).
// Note that merge protects and favours existing values over new values,
@@ -165,7 +168,7 @@ function loadFile(options, config, dir, ready) {
return callback({});
}
- var filename = path.join(dir, 'nodemon.json');
+ var filename = options.configFile || path.join(dir, 'nodemon.json');
fs.readFile(filename, 'utf8', function (err, data) {
if (err) {
return callback({});
View
10 test/cli/parse.test.js
@@ -186,6 +186,13 @@ describe('nodemon CLI parser', function () {
assert(settings.script === 'lib/index.js');
});
+ it('should parse `nodemon --config my/.nodemon.json server.js`', function () {
+ var settings = parse(asCLI('--config my/.nodemon.json test/fixtures/app.js'));
+
+ assert(settings.configFile === 'my/.nodemon.json');
+ assert(settings.script === 'test/fixtures/app.js');
+ });
+
it('should parse `nodemon test/fixtures/app.coffee`', function () {
var settings = parse(asCLI('test/fixtures/app.coffee'));
@@ -240,7 +247,7 @@ describe('nodemon argument parser', function () {
it('should support long versions of flags', function () {
- var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --on-change-only --ext jade');
+ var settings = cli.parse('node nodemon --version --exec java --verbose --quiet --watch fixtures --ignore fixtures --no-stdin --delay 5 --legacy-watch --exitcrash --on-change-only --ext jade --config my/.nodemon.json');
assert(settings.version, 'version');
assert(settings.verbose, 'verbose');
assert(settings.exec === 'java', 'exec');
@@ -252,6 +259,7 @@ describe('nodemon argument parser', function () {
assert(settings.delay === 5000, 'delay 5 seconds');
assert(settings.runOnChangeOnly, 'run on change only');
assert(settings.ext === 'jade', 'extension is jade');
+ assert(settings.configFile === 'my/.nodemon.json', 'custom config file name is my/.nodemon.json')
});
});

0 comments on commit ee110ad

Please sign in to comment.