UNPKG

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