wrap.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. var childProcess = require('child_process');
  23. var child_process_1 = require("child_process");
  24. var resolve = require('resolve').sync;
  25. var hook_1 = require("./hook");
  26. var ipc = __importStar(require("./ipc"));
  27. var resolveMain_1 = require("./resolveMain");
  28. var cfg_1 = require("./cfg");
  29. // const Module = require('module')
  30. // Remove wrap.js from the argv array
  31. process.argv.splice(1, 1);
  32. // Resolve the location of the main script relative to cwd
  33. var main = resolveMain_1.resolveMain(process.argv[1]);
  34. var cfg = cfg_1.makeCfg(main, {});
  35. if (process.env.TS_NODE_DEV === undefined) {
  36. process.env.TS_NODE_DEV = 'true';
  37. }
  38. if (process.env.NODE_DEV_PRELOAD) {
  39. require(process.env.NODE_DEV_PRELOAD);
  40. }
  41. // Listen SIGTERM and exit unless there is another listener
  42. process.on('SIGTERM', function () {
  43. if (process.listeners('SIGTERM').length === 1)
  44. process.exit(0);
  45. });
  46. if (cfg.fork) {
  47. var oldFork_1 = child_process_1.fork;
  48. // Overwrite child_process.fork() so that we can hook into forked processes
  49. // too. We also need to relay messages about required files to the parent.
  50. var newFork = function (modulePath, args, options) {
  51. var child = oldFork_1(__filename, [modulePath].concat(args), options);
  52. ipc.relay(child);
  53. return child;
  54. };
  55. childProcess.fork = newFork;
  56. }
  57. // const lastRequired = null
  58. // const origRequire = Module.prototype.require
  59. // Module.prototype.require = function (requirePath) {
  60. // lastRequired = { path: requirePath, filename: this.filename }
  61. // return origRequire.apply(this, arguments)
  62. // }
  63. // Error handler that displays a notification and logs the stack to stderr:
  64. var caught = false;
  65. process.on('uncaughtException', function (err) {
  66. // NB: err can be null
  67. // Handle exception only once
  68. if (caught)
  69. return;
  70. caught = true;
  71. // If there's a custom uncaughtException handler expect it to terminate
  72. // the process.
  73. var hasCustomHandler = process.listeners('uncaughtException').length > 1;
  74. var isTsError = err && err.message && /TypeScript/.test(err.message);
  75. if (!hasCustomHandler && !isTsError) {
  76. console.error((err && err.stack) || err);
  77. }
  78. ipc.send({
  79. error: isTsError ? '' : (err && err.name) || 'Error',
  80. // lastRequired: lastRequired,
  81. message: err ? err.message : '',
  82. code: err && err.code,
  83. willTerminate: hasCustomHandler,
  84. });
  85. });
  86. // Hook into require() and notify the parent process about required files
  87. hook_1.makeHook(cfg, module, function (file) {
  88. ipc.send({ required: file });
  89. });
  90. // Check if a module is registered for this extension
  91. // const ext = path.extname(main).slice(1)
  92. // const mod = cfg.extensions[ext]
  93. // // Support extensions where 'require' returns a function that accepts options
  94. // if (typeof mod == 'object' && mod.name) {
  95. // const fn = require(resolve(mod.name, { basedir: path.dirname(main) }))
  96. // if (typeof fn == 'function' && mod.options) {
  97. // // require returned a function, call it with options
  98. // fn(mod.options)
  99. // }
  100. // } else if (typeof mod == 'string') {
  101. // require(resolve(mod, { basedir: path.dirname(main) }))
  102. // }
  103. // Execute the wrapped script
  104. require(main);