-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
196 lines (182 loc) · 6.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const webpack = require('webpack')
const childProcess = require('child_process')
const { supportsColor, yellow, red } = require('chalk')
const defaultConfig = {
// Called whenever the process has been started
onStart: () => {},
// Delay calling `onStart` until output has been written to `stdout`
waitForOutput: false,
// Called whenever the node process closes
onClose: (reason) => {
switch (reason) {
case 'kill':
console.log(yellow('Process restarted..'))
break
case 'clean':
console.log(yellow('Process exited cleanly, restarts on new compilation'))
break
case 'crash':
console.log(red('Process crashed, restarts on new compilation'))
break
}
},
// Called whenever input is written to the terminal with the trimmed input-string
onInput: (input, startProcess) => {
switch (input) {
case 'rs':
case 'restart':
startProcess()
break
case 'debug':
startProcess({ debug: true })
break
case 'normal':
startProcess({ debug: false })
break
}
},
// Called whenever webpack compiles
onCompile: (error, stats, startProcess) => {
if (error) {
console.log(red('Webpack compilation error'), error)
} else if (stats.hasErrors()) {
console.log(stats.toString({ chunks: false, colors: true }))
} else {
console.log(stats.toString({ chunks: false, colors: true }))
startProcess()
}
},
// Default options passed to process start
defaultStartupOptions: { debug: false },
// Enhance the environment variables of the node process
getEnvironment: () => ({}),
// Get additional CLI arguments for the node process
getArguments: () => [],
// Entrypoint, if null it will be webpackConfig.output.filename
entrypoint: null,
// Current working directory, if null it will be webpackConfig.output.path
cwd: null,
// Wait between SIGINT and SIGKILL
terminationDelay: 2000,
}
module.exports = (webpackConfig, nodeDevConfig) => {
const {
onStart,
waitForOutput,
onClose,
onInput,
onCompile,
defaultStartupOptions,
getEnvironment,
getArguments,
entrypoint,
cwd,
terminationDelay,
} = Object.assign({}, defaultConfig, {
entrypoint: webpackConfig.output.filename,
cwd: webpackConfig.output.path,
}, webpackConfig.nodeDev, nodeDevConfig)
let nodeProcess = null
let killPromise = null
let startupOptions = defaultStartupOptions
webpack(webpackConfig)
.watch({}, (error, stats) => {
onCompile(error, stats, startProcess, startupOptions)
})
var stdin = process.openStdin()
stdin.addListener('data', (chunk) => {
const input = chunk.toString().trim()
onInput(input, startProcess, startupOptions)
})
const initProcess = (options) => {
startupOptions = Object.assign({}, startupOptions, options)
nodeProcess = childProcess.spawn(
'node',
startupOptions.debug
? ['--inspect', entrypoint, ...getArguments(startupOptions)]
: [entrypoint, ...getArguments(startupOptions)],
{
cwd,
// Clone environment
env: Object.assign({}, process.env, {
// Force chalk to register the child process with the same color properties
// Any value passed enables force-color
FORCE_COLOR: supportsColor ? 'true' : undefined,
}, getEnvironment(startupOptions)),
// Do not inherit stdout, manually piped piped below
stdio: [process.stdin, 'pipe', process.stderr],
}
)
let hasOutput = false
nodeProcess.stdout.addListener('data', (chunk) => {
if (waitForOutput && !hasOutput) {
onStart(startProcess, startupOptions)
hasOutput = true
}
process.stdout.write(chunk)
})
if (!waitForOutput) {
onStart(startProcess, startupOptions)
}
nodeProcess.on('close', (code) => {
if (killPromise) {
onClose('kill', startProcess, startupOptions)
} else if (code === 0) {
onClose('clean', startProcess, startupOptions)
} else {
onClose('crash', startProcess, startupOptions)
}
nodeProcess = null
killPromise = null
})
}
const startProcess = (options) => {
if (!nodeProcess && !killPromise) {
process.nextTick(() => initProcess(options))
} else if (!killPromise) {
killProcess().then(() => initProcess(options))
}
}
const killProcess = () => {
if (!nodeProcess) {
throw new Error('Node process not running, cannot kill')
}
if (killPromise) {
return killPromise
}
killPromise = new Promise((resolve) => {
let killTimeout = null
if (terminationDelay) {
// Send SIGKILL after timeout
killTimeout = setTimeout(() => {
killTimeout = null
nodeProcess.kill('SIGKILL')
}, 2000)
}
nodeProcess.on('close', () => {
if (killTimeout) {
clearTimeout(killTimeout)
}
resolve()
})
nodeProcess.kill(terminationDelay === 0 ? 'SIGKILL' : 'SIGINT')
})
return killPromise
}
// Ensure process is killed before exit
process.on('exit', () => {
if (nodeProcess) {
nodeProcess.kill('SIGKILL')
}
})
// catch ctrl-c
process.on('SIGINT', () => {
// Use same exit code as nodejs
process.exit(128 + 2)
})
// catch kill
process.on('SIGTERM', () => {
// Use same exit code as nodejs
process.exit(128 + 15)
})
}