exec.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const path = require('path');
  2. const fs = require('fs');
  3. const existsSync = fs.existsSync;
  4. const utils = require('../utils');
  5. module.exports = exec;
  6. module.exports.expandScript = expandScript;
  7. /**
  8. * Reads the cwd/package.json file and looks to see if it can load a script
  9. * and possibly an exec first from package.main, then package.start.
  10. *
  11. * @return {Object} exec & script if found
  12. */
  13. function execFromPackage() {
  14. // doing a try/catch because we can't use the path.exist callback pattern
  15. // or we could, but the code would get messy, so this will do exactly
  16. // what we're after - if the file doesn't exist, it'll throw.
  17. try {
  18. // note: this isn't nodemon's package, it's the user's cwd package
  19. var pkg = require(path.join(process.cwd(), 'package.json'));
  20. if (pkg.main !== undefined) {
  21. // no app found to run - so give them a tip and get the feck out
  22. return { exec: null, script: pkg.main };
  23. }
  24. if (pkg.scripts && pkg.scripts.start) {
  25. return { exec: pkg.scripts.start };
  26. }
  27. } catch (e) { }
  28. return null;
  29. }
  30. function replace(map, str) {
  31. var re = new RegExp('{{(' + Object.keys(map).join('|') + ')}}', 'g');
  32. return str.replace(re, function (all, m) {
  33. return map[m] || all || '';
  34. });
  35. }
  36. function expandScript(script, ext) {
  37. if (!ext) {
  38. ext = '.js';
  39. }
  40. if (script.indexOf(ext) !== -1) {
  41. return script;
  42. }
  43. if (existsSync(path.resolve(script))) {
  44. return script;
  45. }
  46. if (existsSync(path.resolve(script + ext))) {
  47. return script + ext;
  48. }
  49. return script;
  50. }
  51. /**
  52. * Discovers all the options required to run the script
  53. * and if a custom exec has been passed in, then it will
  54. * also try to work out what extensions to monitor and
  55. * whether there's a special way of running that script.
  56. *
  57. * @param {Object} nodemonOptions
  58. * @param {Object} execMap
  59. * @return {Object} new and updated version of nodemonOptions
  60. */
  61. function exec(nodemonOptions, execMap) {
  62. if (!execMap) {
  63. execMap = {};
  64. }
  65. var options = utils.clone(nodemonOptions || {});
  66. var script;
  67. // if there's no script passed, try to get it from the first argument
  68. if (!options.script && (options.args || []).length) {
  69. script = expandScript(options.args[0],
  70. options.ext && ('.' + (options.ext || 'js').split(',')[0]));
  71. // if the script was found, shift it off our args
  72. if (script !== options.args[0]) {
  73. options.script = script;
  74. options.args.shift();
  75. }
  76. }
  77. // if there's no exec found yet, then try to read it from the local
  78. // package.json this logic used to sit in the cli/parse, but actually the cli
  79. // should be parsed first, then the user options (via nodemon.json) then
  80. // finally default down to pot shots at the directory via package.json
  81. if (!options.exec && !options.script) {
  82. var found = execFromPackage();
  83. if (found !== null) {
  84. if (found.exec) {
  85. options.exec = found.exec;
  86. }
  87. if (!options.script) {
  88. options.script = found.script;
  89. }
  90. if (Array.isArray(options.args) &&
  91. options.scriptPosition === null) {
  92. options.scriptPosition = options.args.length;
  93. }
  94. }
  95. }
  96. // var options = utils.clone(nodemonOptions || {});
  97. script = path.basename(options.script || '');
  98. var scriptExt = path.extname(script).slice(1);
  99. var extension = options.ext;
  100. if (extension === undefined) {
  101. var isJS = scriptExt === 'js' || scriptExt === 'mjs';
  102. extension = (isJS || !scriptExt) ? 'js,mjs' : scriptExt;
  103. extension += ',json'; // Always watch JSON files
  104. }
  105. var execDefined = !!options.exec;
  106. // allows the user to simplify cli usage:
  107. // https://github.com/remy/nodemon/issues/195
  108. // but always give preference to the user defined argument
  109. if (!options.exec && execMap[scriptExt] !== undefined) {
  110. options.exec = execMap[scriptExt];
  111. execDefined = true;
  112. }
  113. options.execArgs = nodemonOptions.execArgs || [];
  114. if (Array.isArray(options.exec)) {
  115. options.execArgs = options.exec;
  116. options.exec = options.execArgs.shift();
  117. }
  118. if (options.exec === undefined) {
  119. options.exec = 'node';
  120. } else {
  121. // allow variable substitution for {{filename}} and {{pwd}}
  122. var substitution = replace.bind(null, {
  123. filename: options.script,
  124. pwd: process.cwd(),
  125. });
  126. var newExec = substitution(options.exec);
  127. if (newExec !== options.exec &&
  128. options.exec.indexOf('{{filename}}') !== -1) {
  129. options.script = null;
  130. }
  131. options.exec = newExec;
  132. var newExecArgs = options.execArgs.map(substitution);
  133. if (newExecArgs.join('') !== options.execArgs.join('')) {
  134. options.execArgs = newExecArgs;
  135. delete options.script;
  136. }
  137. }
  138. if (options.exec === 'node' && options.nodeArgs && options.nodeArgs.length) {
  139. options.execArgs = options.execArgs.concat(options.nodeArgs);
  140. }
  141. // note: indexOf('coffee') handles both .coffee and .litcoffee
  142. if (!execDefined && options.exec === 'node' &&
  143. scriptExt.indexOf('coffee') !== -1) {
  144. options.exec = 'coffee';
  145. // we need to get execArgs set before the script
  146. // for example, in `nodemon --debug my-script.coffee --my-flag`, debug is an
  147. // execArg, while my-flag is a script arg
  148. var leadingArgs = (options.args || []).splice(0, options.scriptPosition);
  149. options.execArgs = options.execArgs.concat(leadingArgs);
  150. options.scriptPosition = 0;
  151. if (options.execArgs.length > 0) {
  152. // because this is the coffee executable, we need to combine the exec args
  153. // into a single argument after the nodejs flag
  154. options.execArgs = ['--nodejs', options.execArgs.join(' ')];
  155. }
  156. }
  157. if (options.exec === 'coffee') {
  158. // don't override user specified extension tracking
  159. if (options.ext === undefined) {
  160. if (extension) { extension += ','; }
  161. extension += 'coffee,litcoffee';
  162. }
  163. // because windows can't find 'coffee', it needs the real file 'coffee.cmd'
  164. if (utils.isWindows) {
  165. options.exec += '.cmd';
  166. }
  167. }
  168. // allow users to make a mistake on the extension to monitor
  169. // converts .js, pug => js,pug
  170. // BIG NOTE: user can't do this: nodemon -e *.js
  171. // because the terminal will automatically expand the glob against
  172. // the file system :(
  173. extension = (extension.match(/[^,*\s]+/g) || [])
  174. .map(ext => ext.replace(/^\./, ''))
  175. .join(',');
  176. options.ext = extension;
  177. if (options.script) {
  178. options.script = expandScript(options.script,
  179. extension && ('.' + extension.split(',')[0]));
  180. }
  181. options.env = {};
  182. // make sure it's an object (and since we don't have )
  183. if (({}).toString.apply(nodemonOptions.env) === '[object Object]') {
  184. options.env = utils.clone(nodemonOptions.env);
  185. } else if (nodemonOptions.env !== undefined) {
  186. throw new Error('nodemon env values must be an object: { PORT: 8000 }');
  187. }
  188. return options;
  189. }