|
| 1 | +var path = require('path'), |
| 2 | + utils = require('../utils'); |
| 3 | + |
| 4 | +module.exports = exec; |
| 5 | + |
| 6 | +/** |
| 7 | + * Discovers all the options required to run the script |
| 8 | + * and if a custom exec has been passed in, then it will |
| 9 | + * also try to work out what extensions to monitor and |
| 10 | + * whether there's a special way of running that script. |
| 11 | + * |
| 12 | + * @param {Object} nodemonOptions |
| 13 | + * @param {Object} extensionMap |
| 14 | + * @return {Object} new and updated version of nodemonOptions |
| 15 | + */ |
| 16 | +function exec(nodemonOptions, extensionMap) { |
| 17 | + if (!extensionMap) { |
| 18 | + extensionMap = {}; |
| 19 | + } |
| 20 | + |
| 21 | + var options = utils.clone(nodemonOptions || {}), |
| 22 | + script = path.basename(options.script || ''); |
| 23 | + scriptExt = path.extname(script), |
| 24 | + extension = options.ext || scriptExt || 'js'; |
| 25 | + |
| 26 | + if (options.exec === undefined) { |
| 27 | + options.exec = 'node'; |
| 28 | + } else { |
| 29 | + // allow variable substitution for {{filename}} and {{pwd}} |
| 30 | + options.exec = (options.exec || '').replace(/\{\{(filename|pwd)\}\}/, function (all, m) { |
| 31 | + if (m === 'filename') { |
| 32 | + return script || ''; |
| 33 | + } else if (m === 'pwd') { |
| 34 | + return process.cwd() || ''; |
| 35 | + } |
| 36 | + return all; |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + options.execArgs = []; |
| 41 | + |
| 42 | + if (options.exec.indexOf(' ') !== -1) { |
| 43 | + var execOptions = options.exec.split(' '); |
| 44 | + options.exec = execOptions.splice(0, 1)[0]; |
| 45 | + options.execArgs = execOptions; |
| 46 | + } |
| 47 | + |
| 48 | + // TODO decide whether this extensionMap idea is useful or not |
| 49 | + if (extensionMap[extension] !== undefined) { |
| 50 | + options.exec = extensionMap[extension]; |
| 51 | + } |
| 52 | + |
| 53 | + // note: indexOf('coffee') handles both .coffee and .litcoffee |
| 54 | + else if (options.exec === 'node' && scriptExt.indexOf('coffee') !== -1) { |
| 55 | + options.exec = 'coffee'; |
| 56 | + // ensure that we call: `coffee --nodejs ...` |
| 57 | + if (options.execArgs === undefined) options.execArgs = []; |
| 58 | + |
| 59 | + if (options.execArgs.indexOf('--nodejs') === -1) { |
| 60 | + options.execArgs.unshift('--nodejs'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + else if (options.exec === 'node' && options.nodeArgs && options.nodeArgs.length) { |
| 65 | + options.execArgs = options.execArgs.concat(options.nodeArgs); |
| 66 | + } |
| 67 | + |
| 68 | + if (options.exec === 'coffee') { |
| 69 | + // coffeescript requires --nodejs --debug |
| 70 | + // this code is a dance to get the order of the debug flags right when |
| 71 | + // combined with coffeescript |
| 72 | + if (options.nodeArgs) { |
| 73 | + options.execArgs = options.execArgs.concat(options.nodeArgs); |
| 74 | + } |
| 75 | + |
| 76 | + // don't override user specified extension tracking |
| 77 | + if (!options.ext) { |
| 78 | + extension = '.coffee|.litcoffee|.js'; |
| 79 | + } |
| 80 | + |
| 81 | + // because windows can't find 'coffee', it needs the real file 'coffee.cmd' |
| 82 | + if (utils.isWindows) { |
| 83 | + options.exec += '.cmd'; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // allow users to make a mistake on the extension to monitor |
| 88 | + // converts js,jade => .js$|.jade$ |
| 89 | + // and 'js jade' => .js$|.jade$ |
| 90 | + // BIG NOTE: user can't do this: nodemon -e *.js |
| 91 | + // because the terminal will automatically expand the glob against |
| 92 | + // the file system :( |
| 93 | + if (extension.indexOf(' ') !== -1 || |
| 94 | + extension.indexOf(',') !== -1 || |
| 95 | + extension.indexOf('*.') !== -1) { |
| 96 | + |
| 97 | + extension = extension.replace(/\s+/g, '|') // convert spaces to pipes |
| 98 | + .replace(/,/g, '|') // convert commas to pipes |
| 99 | + .split('|') // split on those pipes |
| 100 | + .map(function (item) { |
| 101 | + return '.' + item.replace(/^[\*\.]+/, ''); // remove "*." |
| 102 | + }).join('$|'); // return regexp string like: .js$|.jade$ |
| 103 | + } |
| 104 | + |
| 105 | + // this final part ensures both multiple extension and |
| 106 | + // single extensions work |
| 107 | + extension += '$'; |
| 108 | + |
| 109 | + options.ext = extension; |
| 110 | + |
| 111 | + return options; |
| 112 | +} |
0 commit comments