UNPKG

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