From ee110add5399829021ebe53c17cb4dd006d3e5e4 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Mon, 25 Jul 2016 16:01:45 -0500 Subject: [PATCH] feat: support --config command line arg Implement `--config ` option to allow config loading from an arbitrary file instead of being bound to the static `$PWD/nodemon.json`. Closes #755 Closes #855 --- README.md | 2 +- doc/cli/config.txt | 2 +- doc/cli/help.txt | 2 ++ lib/cli/parse.js | 4 ++++ lib/config/load.js | 7 +++++-- test/cli/parse.test.js | 10 +++++++++- 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a71e6696..ced94ece 100644 --- a/README.md +++ b/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 ` option. The specificity is as follows, so that a command line argument will always override the config file settings: diff --git a/doc/cli/config.txt b/doc/cli/config.txt index ffbaba30..3d1b3084 100644 --- a/doc/cli/config.txt +++ b/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 All config options in the .json file map 1-to-1 with the CLI options, so a config could read as: diff --git a/doc/cli/help.txt b/doc/cli/help.txt index a78270f5..86f480a1 100644 --- a/doc/cli/help.txt +++ b/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 diff --git a/lib/cli/parse.js b/lib/cli/parse.js index 4ca704c5..2fb40e2f 100644 --- a/lib/cli/parse.js +++ b/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()); diff --git a/lib/config/load.js b/lib/config/load.js index 25a1b7f7..94b560ed 100644 --- a/lib/config/load.js +++ b/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({}); diff --git a/test/cli/parse.test.js b/test/cli/parse.test.js index f67a9214..26d328de 100644 --- a/test/cli/parse.test.js +++ b/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') }); });