-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhive.js
73 lines (68 loc) · 1.64 KB
/
hive.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
var native = process.iotjs ? require('../out/yoda-hive.node') : require('../build/Release/hive.node')
var moduleNotFoundRE = /^Module not found:/
var resolve = process.iotjs
? function resolve (id, options) {
var paths = [ process.cwd() ]
if (options && options.paths) {
paths = options.paths
}
var path = require('path')
if (path.isAbsolute(id)) {
return id
}
for (var idx in paths) {
var it = paths[idx]
var pp = path.resolve(it, id)
try {
require(pp)
return pp
} catch (e) {
if (!moduleNotFoundRE.test(e.message)) {
throw e
}
}
}
throw new Error('Module not found')
}
: require.resolve
var onExitCallback
native.onChildExit((pid, code, signal) => {
onExitCallback(pid, code, signal)
})
module.exports.fork = fork
module.exports.kill = native.kill
module.exports.onExit = onExit
module.exports.stop = native.stop
/**
*
* @param {string} cwd
* @param {string[]} argv
* @param {{[name: string]: string}} environs
*/
function fork (cwd, argv, environs) {
var pid = native.fork(process)
if (pid === 0) {
forkMain(cwd, argv, environs)
}
return pid
}
/**
*
* @param {(pid: number, code: number, signal: number) => void} cb
*/
function onExit (cb) {
onExitCallback = cb
}
function forkMain (cwd, argv, environs) {
process.chdir(cwd)
process.argv = process.argv.slice(0, 1)
if (Array.isArray(argv)) {
process.argv = process.argv.concat(argv)
}
process.env = Object.assign(process.env, environs)
if (argv[0]) {
var target = resolve(argv[0], { paths: [ cwd ] })
process.argv[1] = target
require(target)
}
}