load.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. var debug = require('debug')('nodemon');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var exists = fs.exists || path.exists;
  5. var utils = require('../utils');
  6. var rules = require('../rules');
  7. var parse = require('../rules/parse');
  8. var exec = require('./exec');
  9. var defaults = require('./defaults');
  10. module.exports = load;
  11. module.exports.mutateExecOptions = mutateExecOptions;
  12. var existsSync = fs.existsSync || path.existsSync;
  13. function findAppScript() {
  14. // nodemon has been run alone, so try to read the package file
  15. // or try to read the index.js file
  16. if (existsSync('./index.js')) {
  17. return 'index.js';
  18. }
  19. }
  20. /**
  21. * Load the nodemon config, first reading the global root/nodemon.json, then
  22. * the local nodemon.json to the exec and then overwriting using any user
  23. * specified settings (i.e. from the cli)
  24. *
  25. * @param {Object} settings user defined settings
  26. * @param {Function} ready callback that receives complete config
  27. */
  28. function load(settings, options, config, callback) {
  29. config.loaded = [];
  30. // first load the root nodemon.json
  31. loadFile(options, config, utils.home, function (options) {
  32. // then load the user's local configuration file
  33. if (settings.configFile) {
  34. options.configFile = path.resolve(settings.configFile);
  35. }
  36. loadFile(options, config, process.cwd(), function (options) {
  37. // Then merge over with the user settings (parsed from the cli).
  38. // Note that merge protects and favours existing values over new values,
  39. // and thus command line arguments get priority
  40. options = utils.merge(settings, options);
  41. // legacy support
  42. if (!Array.isArray(options.ignore)) {
  43. options.ignore = [options.ignore];
  44. }
  45. if (!options.ignoreRoot) {
  46. options.ignoreRoot = defaults.ignoreRoot;
  47. }
  48. // blend the user ignore and the default ignore together
  49. if (options.ignoreRoot && options.ignore) {
  50. if (!Array.isArray(options.ignoreRoot)) {
  51. options.ignoreRoot = [options.ignoreRoot];
  52. }
  53. options.ignore = options.ignoreRoot.concat(options.ignore);
  54. } else {
  55. options.ignore = defaults.ignore.concat(options.ignore);
  56. }
  57. // add in any missing defaults
  58. options = utils.merge(options, defaults);
  59. if (!options.script && !options.exec) {
  60. var found = findAppScript();
  61. if (found) {
  62. if (!options.args) {
  63. options.args = [];
  64. }
  65. // if the script is found as a result of not being on the command
  66. // line, then we move any of the pre double-dash args in execArgs
  67. const n = options.scriptPosition === null ?
  68. options.args.length : options.scriptPosition;
  69. options.execArgs = (options.execArgs || [])
  70. .concat(options.args.splice(0, n));
  71. options.scriptPosition = null;
  72. options.script = found;
  73. }
  74. }
  75. mutateExecOptions(options);
  76. if (options.quiet) {
  77. utils.quiet();
  78. }
  79. if (options.verbose) {
  80. utils.debug = true;
  81. }
  82. // simplify the ready callback to be called after the rules are normalised
  83. // from strings to regexp through the rules lib. Note that this gets
  84. // created *after* options is overwritten twice in the lines above.
  85. var ready = function (options) {
  86. normaliseRules(options, callback);
  87. };
  88. // if we didn't pick up a nodemon.json file & there's no cli ignores
  89. // then try loading an old style .nodemonignore file
  90. if (config.loaded.length === 0) {
  91. var legacy = loadLegacyIgnore.bind(null, options, config, ready);
  92. // first try .nodemonignore, if that doesn't exist, try nodemon-ignore
  93. return legacy('.nodemonignore', function () {
  94. legacy('nodemon-ignore', function (options) {
  95. ready(options);
  96. });
  97. });
  98. }
  99. ready(options);
  100. });
  101. });
  102. }
  103. /**
  104. * Loads the old style nodemonignore files which is a list of patterns
  105. * in a file to ignore
  106. *
  107. * @param {Object} options nodemon user options
  108. * @param {Function} success
  109. * @param {String} filename ignore file (.nodemonignore or nodemon-ignore)
  110. * @param {Function} fail (optional) failure callback
  111. */
  112. function loadLegacyIgnore(options, config, success, filename, fail) {
  113. var ignoreFile = path.join(process.cwd(), filename);
  114. exists(ignoreFile, function (exists) {
  115. if (exists) {
  116. config.loaded.push(ignoreFile);
  117. return parse(ignoreFile, function (error, rules) {
  118. options.ignore = rules.raw;
  119. success(options);
  120. });
  121. }
  122. if (fail) {
  123. fail(options);
  124. } else {
  125. success(options);
  126. }
  127. });
  128. }
  129. function normaliseRules(options, ready) {
  130. // convert ignore and watch options to rules/regexp
  131. rules.watch.add(options.watch);
  132. rules.ignore.add(options.ignore);
  133. // normalise the watch and ignore arrays
  134. options.watch = options.watch === false ? false : rules.rules.watch;
  135. options.ignore = rules.rules.ignore;
  136. ready(options);
  137. }
  138. /**
  139. * Looks for a config in the current working directory, and a config in the
  140. * user's home directory, merging the two together, giving priority to local
  141. * config. This can then be overwritten later by command line arguments
  142. *
  143. * @param {Function} ready callback to pass loaded settings to
  144. */
  145. function loadFile(options, config, dir, ready) {
  146. if (!ready) {
  147. ready = function () { };
  148. }
  149. var callback = function (settings) {
  150. // prefer the local nodemon.json and fill in missing items using
  151. // the global options
  152. ready(utils.merge(settings, options));
  153. };
  154. if (!dir) {
  155. return callback({});
  156. }
  157. var filename = options.configFile || path.join(dir, 'nodemon.json');
  158. if (config.loaded.indexOf(filename) !== -1) {
  159. // don't bother re-parsing the same config file
  160. return callback({});
  161. }
  162. fs.readFile(filename, 'utf8', function (err, data) {
  163. if (err) {
  164. if (err.code === 'ENOENT') {
  165. if (!options.configFile && dir !== utils.home) {
  166. // if no specified local config file and local nodemon.json
  167. // doesn't exist, try the package.json
  168. return loadPackageJSON(config, callback);
  169. }
  170. }
  171. return callback({});
  172. }
  173. var settings = {};
  174. try {
  175. settings = JSON.parse(data.toString('utf8').replace(/^\uFEFF/, ''));
  176. if (!filename.endsWith('package.json') || settings.nodemonConfig) {
  177. config.loaded.push(filename);
  178. }
  179. } catch (e) {
  180. utils.log.fail('Failed to parse config ' + filename);
  181. console.error(e);
  182. process.exit(1);
  183. }
  184. // options values will overwrite settings
  185. callback(settings);
  186. });
  187. }
  188. function loadPackageJSON(config, ready) {
  189. if (!ready) {
  190. ready = () => { };
  191. }
  192. const dir = process.cwd();
  193. const filename = path.join(dir, 'package.json');
  194. const packageLoadOptions = { configFile: filename };
  195. return loadFile(packageLoadOptions, config, dir, settings => {
  196. ready(settings.nodemonConfig || {});
  197. });
  198. }
  199. function mutateExecOptions(options) {
  200. // work out the execOptions based on the final config we have
  201. options.execOptions = exec({
  202. script: options.script,
  203. exec: options.exec,
  204. args: options.args,
  205. scriptPosition: options.scriptPosition,
  206. nodeArgs: options.nodeArgs,
  207. execArgs: options.execArgs,
  208. ext: options.ext,
  209. env: options.env,
  210. }, options.execMap);
  211. // clean up values that we don't need at the top level
  212. delete options.scriptPosition;
  213. delete options.script;
  214. delete options.args;
  215. delete options.ext;
  216. return options;
  217. }