-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tup.js
169 lines (149 loc) · 3.72 KB
/
tup.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
module.exports = tup
tup.close = close
var path = require('path')
var crypto = require('crypto')
var spawn = require('child_process').spawn
var fs = require('fs')
function sha (fn) {
return crypto.createHash('sha256').update(fn.toString()).digest('hex')
}
var called = false
function tup (fn) {
if (called) {
throw new Error('You may only use t-up once per process')
}
called = true
process.nextTick(function () {
var hash = sha(fn)
if (process.env.T_UP_HASH === hash) {
// already in child process, yay!
fn(function (er) {
if (er) {
throw er
} else {
console.log('\nT-UP DONE %s\n', hash)
}
})
} else {
child(hash)
}
})
}
function child (hash) {
var cwd = process.cwd()
var pidfile = path.resolve(cwd, '.t-up', hash)
var t = require('tap')
kill(pidfile)
t.pass('killed pidfile ' + pidfile)
var mkdirp = require('mkdirp')
mkdirp.sync(cwd + '/.t-up/')
var fd = fs.openSync(pidfile, 'wx')
var args = process.execArgv.concat(process.argv.slice(1))
var env = Object.keys(process.env).reduce(function (env, k) {
if (!env[k]) {
env[k] = process.env[k]
}
return env
}, {
T_UP_HASH: hash
})
var child = spawn(process.execPath, args, {
env: env,
cwd: cwd,
stdio: [ 'ignore', 'pipe', 'pipe' ],
detached: true
})
t.pass('spawned child, awaiting stdout data')
fs.writeSync(fd, child.pid + '\n')
child.stderr.pipe(process.stderr)
var unrefed = false
var out = ''
child.stdout.on('data', function (c) {
out += c
if (out.split(/[\r\n]+/).indexOf('T-UP DONE ' + hash) !== -1) {
unrefed = true
child.unref()
child.stdout.unref()
child.stderr.unref()
}
})
child.on('exit', function (exitCode, signal) {
// almost certainly won't get an 'exit' event if we've already
// unrefed the child, but avoid a false failure anyway.
/* istanbul ignore else */
if (!unrefed) {
t.fail('child process exited early', {
exitCode: exitCode,
signal: signal
})
}
})
}
function kill (pidfile) {
var t = require('tap')
t.pass('kill(' + pidfile + ')')
var rimraf = require('rimraf')
try {
var pid = fs.readFileSync(pidfile, 'utf8').trim()
} catch (er) {
t.pass('could not read pidfile, probably that is fine')
return
}
try {
process.kill(pid, 'SIGTERM')
} catch (e) {
// very unlikely to get some error other than ESRCH, but if so,
// go ahead and blow up.
/* istanbul ignore else */
if (e.code === 'ESRCH') {
t.pass('process was not running')
} else {
t.error(e)
}
}
rimraf.sync(pidfile)
t.pass('removed pidfile')
try {
var piddir = path.dirname(pidfile)
var files = fs.readdirSync(piddir)
if (!files.length) {
rimraf.sync(piddir)
}
} catch (e) {}
// Give it 200ms, then make sure SIGTERM was enough
// on windows, SIGTERM is the same as SIGKILL
/* istanbul ignore else */
if (process.platform !== 'win32') {
setTimeout(function () {
var er
try {
process.kill(pid, 'SIGKILL')
} catch (e) {
er = e
}
if (!er) {
t.fail('exit delayed, SIGKILL was required')
} else {
// very unlikely to get some error other than ESRCH, but if so,
// go ahead and blow up.
/* istanbul ignore else */
if (er.code === 'ESRCH') {
t.pass('exited successfully with SIGTERM')
} else {
throw er
}
}
}, 200)
}
}
function close () {
var dir = path.resolve(process.cwd(), '.t-up')
try {
var pidfiles = fs.readdirSync(dir)
} catch (er) {
return
}
pidfiles.forEach(function (pf) {
kill(path.resolve(dir, pf))
})
}