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 |
|
11 | module.exports = load;
|
12 | module.exports.mutateExecOptions = mutateExecOptions;
|
13 |
|
14 | var existsSync = fs.existsSync || path.existsSync;
|
15 |
|
16 | function findAppScript() {
|
17 |
|
18 |
|
19 | if (existsSync('./index.js')) {
|
20 | return 'index.js';
|
21 | }
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function load(settings, options, config, callback) {
|
33 | config.loaded = [];
|
34 |
|
35 | loadFile(options, config, utils.home, function (options) {
|
36 |
|
37 | if (settings.configFile) {
|
38 | options.configFile = path.resolve(settings.configFile);
|
39 | }
|
40 | loadFile(options, config, process.cwd(), function (options) {
|
41 |
|
42 |
|
43 |
|
44 | options = utils.merge(settings, options);
|
45 |
|
46 |
|
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 |
|
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 |
|
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 |
|
76 |
|
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 |
|
97 |
|
98 |
|
99 | var ready = function (options) {
|
100 | normaliseRules(options, callback);
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 | if (config.loaded.length === 0) {
|
106 | var legacy = loadLegacyIgnore.bind(null, options, config, ready);
|
107 |
|
108 |
|
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 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function 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 |
|
150 | function normaliseRules(options, ready) {
|
151 |
|
152 | rules.watch.add(options.watch);
|
153 | rules.ignore.add(options.ignore);
|
154 |
|
155 |
|
156 | options.watch = options.watch === false ? false : rules.rules.watch;
|
157 | options.ignore = rules.rules.ignore;
|
158 |
|
159 | ready(options);
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function loadFile(options, config, dir, ready) {
|
170 | if (!ready) {
|
171 | ready = function () { };
|
172 | }
|
173 |
|
174 | var callback = function (settings) {
|
175 |
|
176 |
|
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 |
|
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 |
|
196 |
|
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 |
|
217 | callback(settings);
|
218 | });
|
219 | }
|
220 |
|
221 | function 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 |
|
234 | function mutateExecOptions(options) {
|
235 |
|
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 |
|
248 | delete options.scriptPosition;
|
249 | delete options.script;
|
250 | delete options.args;
|
251 | delete options.ext;
|
252 |
|
253 | return options;
|
254 | }
|