UNPKG

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