-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (57 loc) · 1.68 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
'use strict'
const Provider = require('browser-provider')
const Browser = require('abstract-browser')
const bin = require('electron')
const version = require('electron/package.json').version
const spawn = require('child_process').spawn
const path = require('path')
const kProcess = Symbol('kProcess')
class ElectronProvider extends Provider.promises {
async _manifests () {
return [{
name: 'electron',
title: `Electron ${version}`,
version,
options: { headless: true },
supports: { headless: true }
}]
}
_browser (manifest, target) {
return new ElectronBrowser(manifest, target)
}
}
class ElectronBrowser extends Browser {
constructor (manifest, target) {
super(manifest, target)
this[kProcess] = null
}
_open (cb) {
const opts = { manifest: this.manifest, target: this.target }
const entry = path.join(__dirname, 'electron.js')
const args = [entry, JSON.stringify(opts)]
this[kProcess] = spawn(bin, args, {
stdio: 'ignore'
})
this[kProcess].on('close', (code, signal) => {
this[kProcess] = null
if (code) this.emit('error', new Error(`Electron exited with code ${code}`))
else if (signal) this.emit('error', new Error(`Electron exited by signal ${signal}`))
else this.emit('error', new Error('Premature exit'))
})
cb()
}
_close (cb) {
if (this[kProcess]) {
this[kProcess].removeAllListeners('close')
this[kProcess].on('close', function (code) {
if (code) cb(new Error(`Electron exited with code ${code}`))
else cb()
})
this[kProcess].kill()
this[kProcess] = null
} else {
cb()
}
}
}
module.exports = ElectronProvider