-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
51 lines (49 loc) · 1.33 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
if (process.versions['electron']) {
module.exports = function (args) {
var filename = args[1] || 'test.js'
var old = process.stdout.write
process.stdout.write = function (msg) {
if (msg.toString().slice(0, 4) === '# ok' || msg.toString().slice(0, 6) === '# fail') {
process.nextTick(function () {
require('remote').require('app').quit()
})
}
old.apply(process.stdout, arguments)
}
process.nextTick(function () {
require(filename)
})
}
} else {
var fs = require('fs')
var path = require('path')
var through = require('through2')
var tempfile = require('tempfile')
var electronSpawn = require('electron-spawn')
module.exports = function (filename) {
if (filename) {
return electronSpawn(__filename, filename)
} else {
return stream()
}
}
function stream () {
var tmpname = tempfile('.js')
var s = {
stdin: fs.createWriteStream(tmpname),
stdout: through(),
stderr: through()
}
s.stdin.on('close', function () {
var electron = electronSpawn(__filename, tmpname)
electron.stdout.pipe(s.stdout)
electron.stderr.pipe(s.stderr)
electron.on('exit', function () {
fs.unlink(tmpname, function (err) {
if (err) console.error(err)
})
})
})
return s
}
}