index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Manages the internal config of nodemon, checking for the state of support
  3. * with fs.watch, how nodemon can watch files (using find or fs methods).
  4. *
  5. * This is *not* the user's config.
  6. */
  7. var debug = require('debug')('nodemon');
  8. var load = require('./load');
  9. var rules = require('../rules');
  10. var utils = require('../utils');
  11. var pinVersion = require('../version').pin;
  12. var command = require('./command');
  13. var rulesToMonitor = require('../monitor/match').rulesToMonitor;
  14. var bus = utils.bus;
  15. function reset() {
  16. rules.reset();
  17. config.dirs = [];
  18. config.options = { ignore: [], watch: [], monitor: [] };
  19. config.lastStarted = 0;
  20. config.loaded = [];
  21. }
  22. var config = {
  23. run: false,
  24. system: {
  25. cwd: process.cwd(),
  26. },
  27. required: false,
  28. dirs: [],
  29. timeout: 1000,
  30. options: {},
  31. };
  32. /**
  33. * Take user defined settings, then detect the local machine capability, then
  34. * look for local and global nodemon.json files and merge together the final
  35. * settings with the config for nodemon.
  36. *
  37. * @param {Object} settings user defined settings for nodemon (typically on
  38. * the cli)
  39. * @param {Function} ready callback fired once the config is loaded
  40. */
  41. config.load = function (settings, ready) {
  42. reset();
  43. var config = this;
  44. load(settings, config.options, config, function (options) {
  45. config.options = options;
  46. if (options.watch.length === 0) {
  47. // this is to catch when the watch is left blank
  48. options.watch.push('*.*');
  49. }
  50. if (options['watch_interval']) { // jshint ignore:line
  51. options.watchInterval = options['watch_interval']; // jshint ignore:line
  52. }
  53. config.watchInterval = options.watchInterval || null;
  54. if (options.signal) {
  55. config.signal = options.signal;
  56. }
  57. var cmd = command(config.options);
  58. config.command = {
  59. raw: cmd,
  60. string: utils.stringify(cmd.executable, cmd.args),
  61. };
  62. // now run automatic checks on system adding to the config object
  63. options.monitor = rulesToMonitor(options.watch, options.ignore, config);
  64. var cwd = process.cwd();
  65. debug('config: dirs', config.dirs);
  66. if (config.dirs.length === 0) {
  67. config.dirs.unshift(cwd);
  68. }
  69. bus.emit('config:update', config);
  70. pinVersion().then(function () {
  71. ready(config);
  72. }).catch(e => {
  73. // this doesn't help testing, but does give exposure on syntax errors
  74. console.error(e.stack);
  75. setTimeout(() => { throw e; }, 0);
  76. });
  77. });
  78. };
  79. config.reset = reset;
  80. module.exports = config;