Skip to content

Commit

Permalink
fix: support implicit filename extension
Browse files Browse the repository at this point in the history
Fixes #1193
  • Loading branch information
remy committed Dec 29, 2017
1 parent 48048aa commit 6e839d4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Simply specify the config in the same format as you would for a config file but
"...": "... other standard package.json values",
"nodemonConfig": {
"ignore": ["test/*", "docs/*"],
"delay": "2500"
"delay": "2500"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion lib/config/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ function command(settings) {
executable: executable,
args: args,
};
}
}
28 changes: 20 additions & 8 deletions lib/config/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,39 @@ function exec(nodemonOptions, execMap) {
execMap = {};
}

var options = utils.clone(nodemonOptions || {});

if (!options.script && options.args.length) { // try with the first argument
const script = expandScript(options.args[0],
options.ext && ('.' + (options.ext || 'js').split(',')[0]));

if (script !== options.args[0]) { // if the script was found, shift it off our args
options.script = script;
options.args.shift();
}
}

// if there's no exec found yet, then try to read it from the local
// package.json this logic used to sit in the cli/parse, but actually the cli
// should be parsed first, then the user options (via nodemon.json) then
// finally default down to pot shots at the directory via package.json
if (!nodemonOptions.exec && !nodemonOptions.script) {
if (!options.exec && !options.script) {
var found = execFromPackage();
if (found !== null) {
if (found.exec) {
nodemonOptions.exec = found.exec;
options.exec = found.exec;
}
if (!nodemonOptions.script) {
nodemonOptions.script = found.script;
if (!options.script) {
options.script = found.script;
}
if (Array.isArray(nodemonOptions.args) &&
nodemonOptions.scriptPosition === null) {
nodemonOptions.scriptPosition = nodemonOptions.args.length;
if (Array.isArray(options.args) &&
options.scriptPosition === null) {
options.scriptPosition = options.args.length;
}
}
}

var options = utils.clone(nodemonOptions || {});
// var options = utils.clone(nodemonOptions || {});
var script = path.basename(options.script || '');

var scriptExt = path.extname(script).slice(1);
Expand Down
41 changes: 24 additions & 17 deletions lib/config/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var exec = require('./exec');
var defaults = require('./defaults');

module.exports = load;
module.exports.mutateExecOptions = mutateExecOptions;

var existsSync = fs.existsSync || path.existsSync;

Expand Down Expand Up @@ -72,23 +73,7 @@ function load(settings, options, config, callback) {
}
}

// work out the execOptions based on the final config we have
options.execOptions = exec({
script: options.script,
exec: options.exec,
args: options.args,
scriptPosition: options.scriptPosition,
nodeArgs: options.nodeArgs,
execArgs: options.execArgs,
ext: options.ext,
env: options.env,
}, options.execMap);

// clean up values that we don't need at the top level
delete options.scriptPosition;
delete options.script;
delete options.args;
delete options.ext;
mutateExecOptions(options);

if (options.quiet) {
utils.quiet();
Expand Down Expand Up @@ -230,3 +215,25 @@ function loadPackageJSON(config, ready) {
ready(settings.nodemonConfig || {});
});
}

function mutateExecOptions(options) {
// work out the execOptions based on the final config we have
options.execOptions = exec({
script: options.script,
exec: options.exec,
args: options.args,
scriptPosition: options.scriptPosition,
nodeArgs: options.nodeArgs,
execArgs: options.execArgs,
ext: options.ext,
env: options.env,
}, options.execMap);

// clean up values that we don't need at the top level
delete options.scriptPosition;
delete options.script;
delete options.args;
delete options.ext;

return options;
}
36 changes: 24 additions & 12 deletions test/cli/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var cli = require('../../lib/cli/'),
command = require('../../lib/config/command'),
utils = require('../../lib/utils');

const mutateExecOptions = require('../../lib/config/load').mutateExecOptions;

function asCLI(cmd) {
return ('node nodemon ' + (cmd || '')).trim();
}
Expand All @@ -26,6 +28,7 @@ function parse(cmd) {
});

return parsed;
return mutateExecOptions(cli.parse(cmd));
}

function commandToString(command) {
Expand Down Expand Up @@ -97,18 +100,6 @@ describe('nodemon CLI parser', function () {
assert(cmd === 'node --harmony app.js', 'command is ' + cmd);
});

// it('should put the script at the end if found in package.scripts.start', function () {
// var pwd = process.cwd();
// process.chdir('test/fixtures/packages/start'); // allows us to load text/fixtures/package.json
// var settings = parse(asCLI('--harmony')),
// cmd = commandToString(command(settings));

// process.chdir(pwd);
// console.log(settings, cmd);

// assert(cmd === 'node --harmony app.js', 'command is ' + cmd);
// });

it('should support default express4 format', function () {
var pwd = process.cwd();
process.chdir('test/fixtures/packages/express4'); // allows us to load text/fixtures/package.json
Expand Down Expand Up @@ -274,6 +265,27 @@ describe('nodemon respects custom "ext" and "execMap"', function () {
});
});

describe('nodemon should support implicit extensions', () => {
it('should expand script to script.js', () => {
const cwd = process.cwd();
process.chdir('test/fixtures/');
const settings = parse(asCLI('env'));
process.chdir(cwd);
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node env.js', 'implicit extension added');
});

it('should support non-js', () => {
const cwd = process.cwd();
process.chdir('test/fixtures/');
const settings = parse(asCLI('hello --ext py'));
process.chdir(cwd);
var cmd = commandToString(command(settings));
assert.equal(cmd, 'node hello.py', 'implicit extension added');
});

});

describe('nodemon should slurp properly', () => {
it('should read quotes as a single entity', () => {
const settings = parse(asCLI('notindex.js -- -b "hello - world"'));
Expand Down

0 comments on commit 6e839d4

Please sign in to comment.