-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (89 loc) · 2.3 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
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
var fsPath = require('path'),
fs = require('fs'),
mozrunner = require('mozilla-runner'),
mozdown = require('mozilla-download'),
spawn = require('./index').spawn,
debug = require('debug')('marionette-b2g-host');
var DOWNLOAD_DIR = fsPath.join(process.cwd(), 'b2g');
/**
* Host interface for marionette-js-runner.
*
* TODO: I think this API is much more sane then the original
* |spawn| interface but we also need to do some refactoring
* in the mozilla-profile-builder project to improve the apis.
*
* @param {Object} [options] for host see spawn for now.
*/
function Host(options) {
// TODO: host api should have some concept of a "asset" directory
// where we can stuff b2g-desktop without saving it in node_modules or
// cwd.
this.options = options || {};
this.options.runtime = this.options.runtime || DOWNLOAD_DIR;
}
/**
* Immutable metadata describing this host.
*
* @type {Object}
*/
Host.metadata = Object.freeze({
host: 'b2g-desktop'
});
Host.prototype = {
/**
* Reference to b2g-desktop process.
*
* @type {ChildProcess}
* @private
*/
_process: null,
/**
* Starts the b2g-desktop process.
*
* @param {String} profile path.
* @param {Object} [options] settings provided by caller.
* @param {Function} callback [Error err].
*/
start: function(profile, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
debug('start');
var self = this;
var target = self.options.runtime;
function download() {
var version = self.options.version;
mozdown.download(
'b2g',
target,
{ version: version },
run
);
}
function run(err) {
if (err) return callback(err);
mozrunner.run('b2g', target, { profile: profile }, saveState);
}
function saveState(err, process) {
if (err) return callback(err);
self._process = process;
callback();
}
download();
},
/**
* Stop the currently running host.
*
* @param {Function} callback [Error err].
*/
stop: function(callback) {
debug('stop');
if (this._process) {
this._process.on('exit', callback);
this._process.kill();
}
}
};
module.exports = Host;