nodemon.js 8.6 KB

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