1 | var path = require('path')
|
2 | var childProcess = require('child_process')
|
3 | var fork = childProcess.fork
|
4 | var resolve = require('resolve').sync
|
5 | var hook = require('./hook')
|
6 | var ipc = require('./ipc')
|
7 | var resolveMain = require('./resolveMain')
|
8 |
|
9 |
|
10 |
|
11 | process.argv.splice(1, 1)
|
12 |
|
13 |
|
14 | var main = resolveMain(process.argv[1])
|
15 |
|
16 | var cfg = require('./cfg')(main, {})
|
17 |
|
18 |
|
19 | if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development'
|
20 |
|
21 | if (process.env.NODE_DEV_PRELOAD) {
|
22 | require(process.env.NODE_DEV_PRELOAD)
|
23 | }
|
24 |
|
25 |
|
26 | process.on('SIGTERM', function () {
|
27 | if (process.listeners('SIGTERM').length === 1) process.exit(0)
|
28 | })
|
29 |
|
30 | if (cfg.fork) {
|
31 |
|
32 |
|
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 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var caught = false
|
49 | process.on('uncaughtException', function (err) {
|
50 |
|
51 |
|
52 | if (caught) return
|
53 | caught = true
|
54 |
|
55 |
|
56 | var hasCustomHandler = process.listeners('uncaughtException').length > 1
|
57 | var isTsError = err && err.message && /TypeScript/.test(err.message)
|
58 | if (!hasCustomHandler && !isTsError) {
|
59 |
|
60 | }
|
61 | ipc.send({
|
62 | error: isTsError ? '' : (err && err.name) || 'Error',
|
63 |
|
64 | message: err ? err.message : '',
|
65 | code: err && err.code,
|
66 | willTerminate: hasCustomHandler,
|
67 | })
|
68 | })
|
69 |
|
70 |
|
71 | hook(cfg, module, function (file) {
|
72 | ipc.send({ required: file })
|
73 | })
|
74 |
|
75 |
|
76 | var ext = path.extname(main).slice(1)
|
77 | var mod = cfg.extensions[ext]
|
78 |
|
79 |
|
80 | if (typeof mod == 'object' && mod.name) {
|
81 | var fn = require(resolve(mod.name, { basedir: path.dirname(main) }))
|
82 | if (typeof fn == 'function' && mod.options) {
|
83 |
|
84 | fn(mod.options)
|
85 | }
|
86 | } else if (typeof mod == 'string') {
|
87 | require(resolve(mod, { basedir: path.dirname(main) }))
|
88 | }
|
89 |
|
90 |
|
91 | require(main)
|