Skip to content

Commit f3e0c29

Browse files
lennymremy
authored andcommitted
fix: read config file before defaulting script parameter (#1110)
If I have a nodemon.json config file with an `exec` property, then this is automatically appended with ` index.js` when running `nodemon` standalone. I would expect this to be equivalent to running `nodemon --exec "my cmd"` - that is providing an `exec` prevents defaulting of `script` to index.js. To resolve, move the code which defaults the `script` property when `exec` is undefined to after the config files have been read, so that the behaviour is the same irrespective of whether properties are set in CLI flags or in nodemon.json.
1 parent d0c515a commit f3e0c29

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

lib/cli/parse.js

-21
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,6 @@ function parse(argv) {
8585
}
8686
}
8787

88-
if (script === null && !nodemonOptions.exec) {
89-
var found = findAppScript();
90-
if (found !== null) {
91-
if (found.exec) {
92-
nodemonOptions.exec = found.exec;
93-
}
94-
script = found.script;
95-
nodemonOptions.scriptPosition = args.length;
96-
}
97-
}
98-
9988
nodemonOptions.script = script;
10089
nodemonOptions.args = args;
10190

@@ -212,16 +201,6 @@ function nodemonOption(options, arg, eatNext) {
212201
}
213202
}
214203

215-
function findAppScript() {
216-
// nodemon has been run alone, so try to read the package file
217-
// or try to read the index.js file
218-
if (existsSync('./index.js')) {
219-
return { exec: null, script: 'index.js' };
220-
}
221-
222-
return null;
223-
}
224-
225204
/**
226205
* Given an argument (ie. from nodemonOption()), will parse and return the
227206
* equivalent millisecond value or 0 if the argument cannot be parsed

lib/config/load.js

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ var defaults = require('./defaults');
1010

1111
module.exports = load;
1212

13+
var existsSync = fs.existsSync || path.existsSync;
14+
15+
function findAppScript() {
16+
// nodemon has been run alone, so try to read the package file
17+
// or try to read the index.js file
18+
if (existsSync('./index.js')) {
19+
return 'index.js';
20+
}
21+
}
22+
1323
/**
1424
* Load the nodemon config, first reading the global root/nodemon.json, then
1525
* the local nodemon.json to the exec and then overwritting using any user
@@ -55,6 +65,13 @@ function load(settings, options, config, callback) {
5565
// add in any missing defaults
5666
options = utils.merge(options, defaults);
5767

68+
if (!options.script && !options.exec) {
69+
var found = findAppScript();
70+
if (found) {
71+
options.script = found;
72+
}
73+
}
74+
5875
// work out the execOptions based on the final config we have
5976
options.execOptions = exec({
6077
script: options.script,

0 commit comments

Comments
 (0)