-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode-dev
More file actions
executable file
·66 lines (58 loc) · 1.47 KB
/
Copy pathnode-dev
File metadata and controls
executable file
·66 lines (58 loc) · 1.47 KB
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
#! /usr/bin/env node
var spawn = require('child_process').spawn
, fs = require('fs')
, env = process.env
, node = process.argv[0]
, child
// Pass the location of the node-dev script to the child process
env.NODE_DEV = process.argv[1]
try {
var cluster = require('cluster')
// Re-expose the id so that child process knows about being a worker
if (cluster.worker) env.NODE_UNIQUE_ID = cluster.worker.id
}
catch (err) {
// The cluster module does not exist in 0.4.x
}
var args = process.argv.slice(2)
if (!args.length) {
try {
var json = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
if (!json.main) {
console.log('Error: package.json does not specify a "main" script.')
throw new Error()
}
args.push(json.main)
}
catch (err) {
console.log('Usage: node-dev [options] script [arguments]\n')
process.exit(1)
}
}
// Find the first arg that is not an option
for (var i=0; i < args.length; i++) {
if (!/^-/.test(args[i])) {
args.splice(i, 0, __dirname + '/wrapper.js')
break
}
}
// Relay IPC messages
process.on('message', function(m) {
if (child && child.send) child.send(m)
})
function start() {
child = spawn(node, args, {
cwd: process.cwd(),
env: env,
customFds: [0, 1, 2],
stdio: [0, 1, 2, 'ipc']
})
.on('message', function(m) {
if (process.send) process.send(m)
})
.on('exit', function(code) {
if (code === 101) process.nextTick(start)
else process.exit(code)
})
}
start()