Skip to content

Commit a282afb

Browse files
committed
fix: support nodemon index to expand to index.js
With support for custom extensions (picking only the first). Fixes #1165
1 parent fd961d6 commit a282afb

File tree

5 files changed

+102
-10
lines changed

5 files changed

+102
-10
lines changed

lib/config/exec.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
var path = require('path');
2-
var utils = require('../utils');
1+
const path = require('path');
2+
const fs = require('fs');
3+
const existsSync = fs.existsSync;
4+
const utils = require('../utils');
35

46
module.exports = exec;
7+
module.exports.expandScript = expandScript;
58

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

42+
function expandScript(script, ext) {
43+
if (!ext) {
44+
ext = '.js';
45+
}
46+
if (script.indexOf(ext) !== -1) {
47+
return script;
48+
}
49+
50+
if (existsSync(path.resolve(script))) {
51+
return script;
52+
}
53+
54+
if (existsSync(path.resolve(script + ext))) {
55+
return script + ext;
56+
}
57+
58+
return script;
59+
}
3960

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

7596
var options = utils.clone(nodemonOptions || {});
7697
var script = path.basename(options.script || '');
98+
7799
var scriptExt = path.extname(script).slice(1);
78100
var extension = options.ext || (scriptExt ? scriptExt + ',json' : 'js,json');
79101
var execDefined = !!options.exec;
@@ -163,6 +185,11 @@ function exec(nodemonOptions, execMap) {
163185

164186
options.ext = extension;
165187

188+
if (options.script) {
189+
options.script = expandScript(options.script, '.' +
190+
extension.split(',')[0]);
191+
}
192+
166193
options.env = {};
167194
// make sure it's an object (and since we don't have )
168195
if (({}).toString.apply(nodemonOptions.env) === '[object Object]') {

lib/config/load.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ function findAppScript() {
2222

2323
/**
2424
* Load the nodemon config, first reading the global root/nodemon.json, then
25-
* the local nodemon.json to the exec and then overwritting using any user
25+
* the local nodemon.json to the exec and then overwriting using any user
2626
* specified settings (i.e. from the cli)
2727
*
2828
* @param {Object} settings user defined settings
29-
* @param {Function} ready callback that recieves complete config
29+
* @param {Function} ready callback that receives complete config
3030
*/
3131
function load(settings, options, config, callback) {
3232
config.loaded = [];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"test": "npm run lint && npm run spec",
3535
"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",
3636
"postspec": "npm run clean",
37-
"clean": "rm -rf test/fixtures/test*.js",
37+
"clean": "rm -rf test/fixtures/test*.js test/fixtures/test*.md",
3838
"web": "node web",
3939
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
4040
"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')\""

test/cli/exec.test.js

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22
/*global describe:true, it: true */
3-
var exec = require('../../lib/config/exec'),
4-
command = require('../../lib/config/command'),
5-
assert = require('assert'),
6-
utils = require('../../lib/utils');
3+
const path = require('path');
4+
const exec = require('../../lib/config/exec');
5+
const expandScript = exec.expandScript;
6+
const command = require('../../lib/config/command');
7+
const assert = require('assert');
8+
const utils = require('../../lib/utils');
79

810
function toCmd(options) {
911
var cmd = command({
@@ -17,7 +19,46 @@ function toCmd(options) {
1719
};
1820
}
1921

22+
describe('expandScript', () => {
23+
var pwd = process.cwd();
24+
25+
afterEach(function () {
26+
process.chdir(pwd);
27+
});
28+
29+
beforeEach(function () {
30+
// move to the fixtures directory to allow for config loading
31+
process.chdir(path.resolve(pwd, 'test/fixtures'));
32+
});
33+
34+
it('should expand app.js', () => {
35+
const script = expandScript('app');
36+
assert.equal(script, 'app.js', script);
37+
})
38+
39+
it('should expand hello.py', () => {
40+
const script = expandScript('hello', '.py');
41+
assert.equal(script, 'hello.py', script);
42+
})
43+
44+
it('should ignore foo.js', () => {
45+
const script = expandScript('foo', '.js');
46+
assert.equal(script, 'foo', script);
47+
})
48+
});
49+
2050
describe('nodemon exec', function () {
51+
var pwd = process.cwd();
52+
53+
afterEach(function () {
54+
process.chdir(pwd);
55+
});
56+
57+
beforeEach(function () {
58+
// move to the fixtures directory to allow for config loading
59+
process.chdir(path.resolve(pwd, 'test/fixtures'));
60+
});
61+
2162
it('should default to node', function () {
2263
var options = exec({ script: 'index.js' });
2364
var cmd = toCmd(options);
@@ -136,4 +177,28 @@ describe('nodemon exec', function () {
136177
assert(options.ext.indexOf('js') !== -1);
137178
assert(options.ext.indexOf('jade') !== -1);
138179
});
180+
181+
it('should expand app to app.js', function () {
182+
var options = exec({ script: 'app' });
183+
var cmd = toCmd(options);
184+
assert(cmd.string === 'node app.js', cmd.string);
185+
});
186+
187+
it('should expand based on custom extensions to hello.py', function () {
188+
var options = exec({ script: 'hello', ext: '.py', exec: 'python' });
189+
var cmd = toCmd(options);
190+
assert(cmd.string === 'python hello.py', cmd.string);
191+
});
192+
193+
it('should expand based on custom extensions to app.js (js,jsx,mjs)', function () {
194+
var options = exec({ script: 'app', ext: 'js,jsx,mjs' });
195+
var cmd = toCmd(options);
196+
assert(cmd.string === 'node app.js', cmd.string);
197+
});
198+
199+
it('should not expand index to non-existant index.js', function () {
200+
var options = exec({ script: 'index' });
201+
var cmd = toCmd(options);
202+
assert(cmd.string === 'node index', cmd.string);
203+
});
139204
});

test/cli/parse.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('nodemon CLI parser', function () {
162162

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

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

0 commit comments

Comments
 (0)