From a282afbbe3f0d5bbcf45c934945d91dc960973a1 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Thu, 14 Dec 2017 22:53:43 +0000 Subject: [PATCH] fix: support nodemon index to expand to index.js With support for custom extensions (picking only the first). Fixes #1165 --- lib/config/exec.js | 31 ++++++++++++++++-- lib/config/load.js | 4 +-- package.json | 2 +- test/cli/exec.test.js | 73 +++++++++++++++++++++++++++++++++++++++--- test/cli/parse.test.js | 2 +- 5 files changed, 102 insertions(+), 10 deletions(-) diff --git a/lib/config/exec.js b/lib/config/exec.js index de0cd306..09c15c08 100644 --- a/lib/config/exec.js +++ b/lib/config/exec.js @@ -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 @@ -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 @@ -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; @@ -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]') { diff --git a/lib/config/load.js b/lib/config/load.js index 73ae2753..06ec8019 100644 --- a/lib/config/load.js +++ b/lib/config/load.js @@ -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 = []; diff --git a/package.json b/package.json index cfa37368..660ed7f2 100644 --- a/package.json +++ b/package.json @@ -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')\"" diff --git a/test/cli/exec.test.js b/test/cli/exec.test.js index 871b96b0..d66ea348 100644 --- a/test/cli/exec.test.js +++ b/test/cli/exec.test.js @@ -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({ @@ -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); @@ -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); + }); }); diff --git a/test/cli/parse.test.js b/test/cli/parse.test.js index b0e1fd2e..b01b8622 100644 --- a/test/cli/parse.test.js +++ b/test/cli/parse.test.js @@ -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 () {