source: node_modules/nodemon/lib/nodemon.js@ 62b2964

finki-main main
Last change on this file since 62b2964 was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 8.8 KB
Line 
1var debug = require('debug')('nodemon');
2var path = require('path');
3var monitor = require('./monitor');
4var cli = require('./cli');
5var version = require('./version');
6var util = require('util');
7var utils = require('./utils');
8var bus = utils.bus;
9var help = require('./help');
10/** @type {import('..').NodemonEventConfig} */
11var config = require('./config');
12var spawn = require('./spawn');
13const defaults = require('./config/defaults')
14var eventHandlers = {};
15
16// this is fairly dirty, but theoretically sound since it's part of the
17// stable module API
18config.required = utils.isRequired;
19
20/**
21 * @param {import('..').NodemonSettings | string} settings
22 * @returns {import('..').Nodemon}
23 */
24function nodemon(settings) {
25 bus.emit('boot');
26 nodemon.reset();
27
28 /** @type {import('..').NodemonSettings} */
29 let options
30
31 // allow the cli string as the argument to nodemon, and allow for
32 // `node nodemon -V app.js` or just `-V app.js`
33 if (typeof settings === 'string') {
34 settings = settings.trim();
35 if (settings.indexOf('node') !== 0) {
36 if (settings.indexOf('nodemon') !== 0) {
37 settings = 'nodemon ' + settings;
38 }
39 settings = 'node ' + settings;
40 }
41 options = cli.parse(settings);
42 } else options = settings;
43
44 // set the debug flag as early as possible to get all the detailed logging
45 if (options.verbose) {
46 utils.debug = true;
47 }
48
49 if (options.help) {
50 if (process.stdout.isTTY) {
51 process.stdout._handle.setBlocking(true); // nodejs/node#6456
52 }
53 console.log(help(options.help));
54 if (!config.required) {
55 process.exit(0);
56 }
57 }
58
59 if (options.version) {
60 version().then(function (v) {
61 console.log(v);
62 if (!config.required) {
63 process.exit(0);
64 }
65 });
66 return;
67 }
68
69 // nodemon tools like grunt-nodemon. This affects where
70 // the script is being run from, and will affect where
71 // nodemon looks for the nodemon.json files
72 if (options.cwd) {
73 // this is protection to make sure we haven't dont the chdir already...
74 // say like in cli/parse.js (which is where we do this once already!)
75 if (process.cwd() !== path.resolve(config.system.cwd, options.cwd)) {
76 process.chdir(options.cwd);
77 }
78 }
79
80 config.load(options, function (config) {
81 if (!config.options.dump && !config.options.execOptions.script &&
82 config.options.execOptions.exec === 'node') {
83 if (!config.required) {
84 console.log(help('usage'));
85 process.exit();
86 }
87 return;
88 }
89
90 // before we print anything, update the colour setting on logging
91 utils.colours = config.options.colours;
92
93 // always echo out the current version
94 utils.log.info(version.pinned);
95
96 const cwd = process.cwd();
97
98 if (config.options.cwd) {
99 utils.log.detail('process root: ' + cwd);
100 }
101
102 config.loaded.map(file => file.replace(cwd, '.')).forEach(file => {
103 utils.log.detail('reading config ' + file);
104 });
105
106 if (config.options.stdin && config.options.restartable) {
107 // allow nodemon to restart when the use types 'rs\n'
108 process.stdin.resume();
109 process.stdin.setEncoding('utf8');
110 process.stdin.on('data', data => {
111 const str = data.toString().trim().toLowerCase();
112
113 // if the keys entered match the restartable value, then restart!
114 if (str === config.options.restartable) {
115 bus.emit('restart');
116 } else if (data.charCodeAt(0) === 12) { // ctrl+l
117 console.clear();
118 }
119 });
120 } else if (config.options.stdin) {
121 // so let's make sure we don't eat the key presses
122 // but also, since we're wrapping, watch out for
123 // special keys, like ctrl+c x 2 or '.exit' or ctrl+d or ctrl+l
124 var ctrlC = false;
125 var buffer = '';
126
127 process.stdin.on('data', function (data) {
128 data = data.toString();
129 buffer += data;
130 const chr = data.charCodeAt(0);
131
132 // if restartable, echo back
133 if (chr === 3) {
134 if (ctrlC) {
135 process.exit(0);
136 }
137
138 ctrlC = true;
139 return;
140 } else if (buffer === '.exit' || chr === 4) { // ctrl+d
141 process.exit();
142 } else if (chr === 13 || chr === 10) { // enter / carriage return
143 buffer = '';
144 } else if (chr === 12) { // ctrl+l
145 console.clear();
146 buffer = '';
147 }
148 ctrlC = false;
149 });
150 if (process.stdin.setRawMode) {
151 process.stdin.setRawMode(true);
152 }
153 }
154
155 if (config.options.restartable) {
156 utils.log.info('to restart at any time, enter `' +
157 config.options.restartable + '`');
158 }
159
160 if (!config.required) {
161 const restartSignal = config.options.signal === 'SIGUSR2' ? 'SIGHUP' : 'SIGUSR2';
162 process.on(restartSignal, nodemon.restart);
163 utils.bus.on('error', () => {
164 utils.log.fail((new Error().stack));
165 });
166 utils.log.detail((config.options.restartable ? 'or ' : '') + 'send ' +
167 restartSignal + ' to ' + process.pid + ' to restart');
168 }
169
170 const ignoring = config.options.monitor.map(function (rule) {
171 if (rule.slice(0, 1) !== '!') {
172 return false;
173 }
174
175 rule = rule.slice(1);
176
177 // don't notify of default ignores
178 if (defaults.ignoreRoot.indexOf(rule) !== -1) {
179 return false;
180 // return rule.slice(3).slice(0, -3);
181 }
182
183 if (rule.startsWith(cwd)) {
184 return rule.replace(cwd, '.');
185 }
186
187 return rule;
188 }).filter(Boolean).join(' ');
189 if (ignoring) utils.log.detail('ignoring: ' + ignoring);
190
191 utils.log.info('watching path(s): ' + config.options.monitor.map(function (rule) {
192 if (rule.slice(0, 1) !== '!') {
193 try {
194 rule = path.relative(process.cwd(), rule);
195 } catch (e) {}
196
197 return rule;
198 }
199
200 return false;
201 }).filter(Boolean).join(' '));
202
203 utils.log.info('watching extensions: ' + (config.options.execOptions.ext || '(all)'));
204
205 if (config.options.dump) {
206 utils.log._log('log', '--------------');
207 utils.log._log('log', 'node: ' + process.version);
208 utils.log._log('log', 'nodemon: ' + version.pinned);
209 utils.log._log('log', 'command: ' + process.argv.join(' '));
210 utils.log._log('log', 'cwd: ' + cwd);
211 utils.log._log('log', ['OS:', process.platform, process.arch].join(' '));
212 utils.log._log('log', '--------------');
213 utils.log._log('log', util.inspect(config, { depth: null }));
214 utils.log._log('log', '--------------');
215 if (!config.required) {
216 process.exit();
217 }
218
219 return;
220 }
221
222 config.run = true;
223
224 if (config.options.stdout === false) {
225 nodemon.on('start', function () {
226 nodemon.stdout = bus.stdout;
227 nodemon.stderr = bus.stderr;
228
229 bus.emit('readable');
230 });
231 }
232
233 if (config.options.events && Object.keys(config.options.events).length) {
234 Object.keys(config.options.events).forEach(function (key) {
235 utils.log.detail('bind ' + key + ' -> `' +
236 config.options.events[key] + '`');
237 nodemon.on(key, function () {
238 if (config.options && config.options.events) {
239 spawn(config.options.events[key], config,
240 [].slice.apply(arguments));
241 }
242 });
243 });
244 }
245
246 monitor.run(config.options);
247
248 });
249
250 return nodemon;
251}
252
253nodemon.restart = function () {
254 utils.log.status('restarting child process');
255 bus.emit('restart');
256 return nodemon;
257};
258
259nodemon.addListener = nodemon.on = function (event, handler) {
260 if (!eventHandlers[event]) { eventHandlers[event] = []; }
261 eventHandlers[event].push(handler);
262 bus.on(event, handler);
263 return nodemon;
264};
265
266nodemon.once = function (event, handler) {
267 if (!eventHandlers[event]) { eventHandlers[event] = []; }
268 eventHandlers[event].push(handler);
269 bus.once(event, function () {
270 debug('bus.once(%s)', event);
271 eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 1);
272 handler.apply(this, arguments);
273 });
274 return nodemon;
275};
276
277nodemon.emit = function () {
278 bus.emit.apply(bus, [].slice.call(arguments));
279 return nodemon;
280};
281
282nodemon.removeAllListeners = function (event) {
283 // unbind only the `nodemon.on` event handlers
284 Object.keys(eventHandlers).filter(function (e) {
285 return event ? e === event : true;
286 }).forEach(function (event) {
287 eventHandlers[event].forEach(function (handler) {
288 bus.removeListener(event, handler);
289 eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 1);
290 });
291 });
292
293 return nodemon;
294};
295
296nodemon.reset = function (done) {
297 bus.emit('reset', done);
298};
299
300bus.on('reset', function (done) {
301 debug('reset');
302 nodemon.removeAllListeners();
303 monitor.run.kill(true, function () {
304 utils.reset();
305 config.reset();
306 config.run = false;
307 if (done) {
308 done();
309 }
310 });
311});
312
313// expose the full config
314nodemon.config = config;
315
316module.exports = nodemon;
317
Note: See TracBrowser for help on using the repository browser.