load.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 || options.args.length;
  68. options.execArgs = (options.execArgs || [])
  69. .concat(options.args.splice(0, n));
  70. options.scriptPosition = null;
  71. options.script = found;
  72. }
  73. }
  74. mutateExecOptions(options);
  75. if (options.quiet) {
  76. utils.quiet();
  77. }
  78. if (options.verbose) {
  79. utils.debug = true;
  80. }
  81. // simplify the ready callback to be called after the rules are normalised
  82. // from strings to regexp through the rules lib. Note that this gets
  83. // created *after* options is overwritten twice in the lines above.
  84. var ready = function (options) {
  85. normaliseRules(options, callback);
  86. };
  87. // if we didn't pick up a nodemon.json file & there's no cli ignores
  88. // then try loading an old style .nodemonignore file
  89. if (config.loaded.length === 0) {
  90. var legacy = loadLegacyIgnore.bind(null, options, config, ready);
  91. // first try .nodemonignore, if that doesn't exist, try nodemon-ignore
  92. return legacy('.nodemonignore', function () {
  93. legacy('nodemon-ignore', function (options) {
  94. ready(options);
  95. });
  96. });
  97. }
  98. ready(options);
  99. });
  100. });
  101. }
  102. /**
  103. * Loads the old style nodemonignore files which is a list of patterns
  104. * in a file to ignore
  105. *
  106. * @param {Object} options nodemon user options
  107. * @param {Function} success
  108. * @param {String} filename ignore file (.nodemonignore or nodemon-ignore)
  109. * @param {Function} fail (optional) failure callback
  110. */
  111. function loadLegacyIgnore(options, config, success, filename, fail) {
  112. var ignoreFile = path.join(process.cwd(), filename);
  113. exists(ignoreFile, function (exists) {
  114. if (exists) {
  115. config.loaded.push(ignoreFile);
  116. return parse(ignoreFile, function (error, rules) {
  117. options.ignore = rules.raw;
  118. success(options);
  119. });
  120. }
  121. if (fail) {
  122. fail(options);
  123. } else {
  124. success(options);
  125. }
  126. });
  127. }
  128. function normaliseRules(options, ready) {
  129. // convert ignore and watch options to rules/regexp
  130. rules.watch.add(options.watch);
  131. rules.ignore.add(options.ignore);
  132. // normalise the watch and ignore arrays
  133. options.watch = options.watch === false ? false : rules.rules.watch;
  134. options.ignore = rules.rules.ignore;
  135. ready(options);
  136. }
  137. /**
  138. * Looks for a config in the current working directory, and a config in the
  139. * user's home directory, merging the two together, giving priority to local
  140. * config. This can then be overwritten later by command line arguments
  141. *
  142. * @param {Function} ready callback to pass loaded settings to
  143. */
  144. function loadFile(options, config, dir, ready) {
  145. if (!ready) {
  146. ready = function () { };
  147. }
  148. var callback = function (settings) {
  149. // prefer the local nodemon.json and fill in missing items using
  150. // the global options
  151. ready(utils.merge(settings, options));
  152. };
  153. if (!dir) {
  154. return callback({});
  155. }
  156. var filename = options.configFile || path.join(dir, 'nodemon.json');
  157. if (config.loaded.indexOf(filename) !== -1) {
  158. // don't bother re-parsing the same config file
  159. return callback({});
  160. }
  161. fs.readFile(filename, 'utf8', function (err, data) {
  162. if (err) {
  163. if (err.code === 'ENOENT') {
  164. if (!options.configFile && dir !== utils.home) {
  165. // if no specified local config file and local nodemon.json
  166. // doesn't exist, try the package.json
  167. return loadPackageJSON(config, callback);
  168. }
  169. }
  170. return callback({});
  171. }
  172. var settings = {};
  173. try {
  174. settings = JSON.parse(data.toString('utf8').replace(/^\uFEFF/, ''));
  175. if (!filename.endsWith('package.json') || settings.nodemonConfig) {
  176. config.loaded.push(filename);
  177. }
  178. } catch (e) {
  179. utils.log.fail('Failed to parse config ' + filename);
  180. console.error(e);
  181. process.exit(1);
  182. }
  183. // options values will overwrite settings
  184. callback(settings);
  185. });
  186. }
  187. function loadPackageJSON(config, ready) {
  188. if (!ready) {
  189. ready = () => { };
  190. }
  191. const dir = process.cwd();
  192. const filename = path.join(dir, 'package.json');
  193. const packageLoadOptions = { configFile: filename };
  194. return loadFile(packageLoadOptions, config, dir, settings => {
  195. ready(settings.nodemonConfig || {});
  196. });
  197. }
  198. function mutateExecOptions(options) {
  199. // work out the execOptions based on the final config we have
  200. options.execOptions = exec({
  201. script: options.script,
  202. exec: options.exec,
  203. args: options.args,
  204. scriptPosition: options.scriptPosition,
  205. nodeArgs: options.nodeArgs,
  206. execArgs: options.execArgs,
  207. ext: options.ext,
  208. env: options.env,
  209. }, options.execMap);
  210. // clean up values that we don't need at the top level
  211. delete options.scriptPosition;
  212. delete options.script;
  213. delete options.args;
  214. delete options.ext;
  215. return options;
  216. }