Skip to content

Commit

Permalink
fix: support nodemon index to expand to index.js
Browse files Browse the repository at this point in the history
With support for custom extensions (picking only the first).

Fixes #1165
  • Loading branch information
remy committed Dec 15, 2017
1 parent fd961d6 commit a282afb
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 10 deletions.
31 changes: 29 additions & 2 deletions lib/config/exec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var path = require('path');
var utils = require('../utils');
const path = require('path');
const fs = require('fs');
const existsSync = fs.existsSync;
const utils = require('../utils');

module.exports = exec;
module.exports.expandScript = expandScript;

/**
* Reads the cwd/package.json file and looks to see if it can load a script
Expand Down Expand Up @@ -36,6 +39,24 @@ function replace(map, str) {
});
}

function expandScript(script, ext) {
if (!ext) {
ext = '.js';
}
if (script.indexOf(ext) !== -1) {
return script;
}

if (existsSync(path.resolve(script))) {
return script;
}

if (existsSync(path.resolve(script + ext))) {
return script + ext;
}

return script;
}

/**
* Discovers all the options required to run the script
Expand Down Expand Up @@ -74,6 +95,7 @@ function exec(nodemonOptions, execMap) {

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

var scriptExt = path.extname(script).slice(1);
var extension = options.ext || (scriptExt ? scriptExt + ',json' : 'js,json');
var execDefined = !!options.exec;
Expand Down Expand Up @@ -163,6 +185,11 @@ function exec(nodemonOptions, execMap) {

options.ext = extension;

if (options.script) {
options.script = expandScript(options.script, '.' +
extension.split(',')[0]);
}

options.env = {};
// make sure it's an object (and since we don't have )
if (({}).toString.apply(nodemonOptions.env) === '[object Object]') {
Expand Down
4 changes: 2 additions & 2 deletions lib/config/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ function findAppScript() {

/**
* Load the nodemon config, first reading the global root/nodemon.json, then
* the local nodemon.json to the exec and then overwritting using any user
* the local nodemon.json to the exec and then overwriting using any user
* specified settings (i.e. from the cli)
*
* @param {Object} settings user defined settings
* @param {Function} ready callback that recieves complete config
* @param {Function} ready callback that receives complete config
*/
function load(settings, options, config, callback) {
config.loaded = [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test": "npm run lint && npm run spec",
"spec": "for FILE in test/**/*.test.js; do echo $FILE; TEST=1 mocha --exit --timeout 30000 $FILE; if [ $? -ne 0 ]; then exit 1; fi; sleep 1; done",
"postspec": "npm run clean",
"clean": "rm -rf test/fixtures/test*.js",
"clean": "rm -rf test/fixtures/test*.js test/fixtures/test*.md",
"web": "node web",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"postinstall": "node -e \"console.log('\\u001b[32mLove nodemon? You can now support the project via the open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[96m\\u001b[1mhttps://opencollective.com/nodemon/donate\\u001b[0m\\n')\""
Expand Down
73 changes: 69 additions & 4 deletions test/cli/exec.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
/*global describe:true, it: true */
var exec = require('../../lib/config/exec'),
command = require('../../lib/config/command'),
assert = require('assert'),
utils = require('../../lib/utils');
const path = require('path');
const exec = require('../../lib/config/exec');
const expandScript = exec.expandScript;
const command = require('../../lib/config/command');
const assert = require('assert');
const utils = require('../../lib/utils');

function toCmd(options) {
var cmd = command({
Expand All @@ -17,7 +19,46 @@ function toCmd(options) {
};
}

describe('expandScript', () => {
var pwd = process.cwd();

afterEach(function () {
process.chdir(pwd);
});

beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
});

it('should expand app.js', () => {
const script = expandScript('app');
assert.equal(script, 'app.js', script);
})

it('should expand hello.py', () => {
const script = expandScript('hello', '.py');
assert.equal(script, 'hello.py', script);
})

it('should ignore foo.js', () => {
const script = expandScript('foo', '.js');
assert.equal(script, 'foo', script);
})
});

describe('nodemon exec', function () {
var pwd = process.cwd();

afterEach(function () {
process.chdir(pwd);
});

beforeEach(function () {
// move to the fixtures directory to allow for config loading
process.chdir(path.resolve(pwd, 'test/fixtures'));
});

it('should default to node', function () {
var options = exec({ script: 'index.js' });
var cmd = toCmd(options);
Expand Down Expand Up @@ -136,4 +177,28 @@ describe('nodemon exec', function () {
assert(options.ext.indexOf('js') !== -1);
assert(options.ext.indexOf('jade') !== -1);
});

it('should expand app to app.js', function () {
var options = exec({ script: 'app' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
});

it('should expand based on custom extensions to hello.py', function () {
var options = exec({ script: 'hello', ext: '.py', exec: 'python' });
var cmd = toCmd(options);
assert(cmd.string === 'python hello.py', cmd.string);
});

it('should expand based on custom extensions to app.js (js,jsx,mjs)', function () {
var options = exec({ script: 'app', ext: 'js,jsx,mjs' });
var cmd = toCmd(options);
assert(cmd.string === 'node app.js', cmd.string);
});

it('should not expand index to non-existant index.js', function () {
var options = exec({ script: 'index' });
var cmd = toCmd(options);
assert(cmd.string === 'node index', cmd.string);
});
});
2 changes: 1 addition & 1 deletion test/cli/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('nodemon CLI parser', function () {

it('should support stand alone `nodemon` command', function () {
var settings = parse(asCLI(''));
assert(settings.execOptions.script === pkg.main);
assert(settings.execOptions.script === pkg.main + '.js', `${settings.execOptions.script} === ${pkg.main}`);
});

it('should put --debug in the right place with coffescript', function () {
Expand Down

0 comments on commit a282afb

Please sign in to comment.