inspector.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env node
  2. var DebugServer = require('../lib/debug-server').DebugServer,
  3. fs = require('fs'),
  4. path = require('path'),
  5. config = require('../lib/config'),
  6. packageJson = require('../package.json'),
  7. notifyParentProcess = getNotifyParentProcessFn();
  8. if (config.help || config.h) {
  9. console.log('Usage:');
  10. console.log(' node-inspector [options]');
  11. console.log('\nOptions:');
  12. console.log(config._describeOptions());
  13. console.log();
  14. process.exit();
  15. }
  16. if (config.version || config.v) {
  17. console.log('v' + require('../package.json').version);
  18. process.exit();
  19. }
  20. console.log('Node Inspector v%s', packageJson.version);
  21. var debugServer = new DebugServer();
  22. debugServer.on('error', onError);
  23. debugServer.on('listening', onListening);
  24. debugServer.on('close', function () {
  25. process.exit();
  26. });
  27. debugServer.start(config);
  28. function onError(err) {
  29. console.error(
  30. 'Cannot start the server at %s:%s. Error: %s.',
  31. config.webHost || '0.0.0.0',
  32. config.webPort,
  33. err.message || err
  34. );
  35. if (err.code === 'EADDRINUSE') {
  36. console.error(
  37. 'There is another process already listening at this address.\n' +
  38. 'Run `node-inspector --web-port={port}` to use a different port.'
  39. );
  40. }
  41. notifyParentProcess({
  42. event: 'SERVER.ERROR',
  43. error: err
  44. });
  45. }
  46. function onListening() {
  47. var address = this.address();
  48. console.log('Visit %s to start debugging.', address.url);
  49. notifyParentProcess({
  50. event: 'SERVER.LISTENING',
  51. address: address
  52. });
  53. }
  54. function getNotifyParentProcessFn() {
  55. if (!process.send) {
  56. return function(msg) {};
  57. }
  58. return function(msg) {
  59. process.send(msg);
  60. };
  61. }