-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·139 lines (112 loc) · 3.52 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
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
#! /usr/bin/env node
// Usage: In gaia folder
//
// node PATH_TO_GAIA_DEVELOP/index.js b2g tv browser
// node PATH_TO_GAIA_DEVELOP/index.js simulator tv tv-deck
// node PATH_TO_GAIA_DEVELOP/index.js b2g tv
// node PATH_TO_GAIA_DEVELOP/index.js tv
// node PATH_TO_GAIA_DEVELOP/index.js tv browser
//
// node PATH_TO_GAIA_DEVELOP/index.js nightly phone settings
// node PATH_TO_GAIA_DEVELOP/index.js sms
// node PATH_TO_GAIA_DEVELOP/index.js
//
var fs = require('fs');
var path = require('path');
var chokidar = require('chokidar');
var config = require('./lib/config');
var Make = require('./lib/make');
var Runtime = require('./lib/runtime');
var runtimeTypes = ['firefox', 'developer', 'b2g', 'mulet', 'nightly', 'simulator'];
var deviceTypes = ['phone', 'tv'];
var appsPath = path.join(process.cwd(), 'apps');
var apps = fs.existsSync(appsPath) ?
fs.readdirSync(appsPath).filter(function(file) {
return fs.statSync(path.join(appsPath, file)).isDirectory();
}) : [];
var tvAppsPath = path.join(process.cwd(), 'tv_apps');
var tvApps = fs.existsSync(tvAppsPath) ?
fs.readdirSync(tvAppsPath).filter(function(file) {
return fs.statSync(path.join(tvAppsPath, file)).isDirectory();
}) : [];
// Default runtime and device type
var runtimeType = config.defaultRuntimeType;
var deviceType = config.defaultDeviceType;
var app = '';
process.argv.forEach(function(val, index, array) {
if (runtimeTypes.indexOf(val) > -1) {
runtimeType = val;
} else if (deviceTypes.indexOf(val) > -1) {
deviceType = val;
} else if ((apps && apps.indexOf(val) > -1) ||
(tvApps && tvApps.indexOf(val) > -1)) {
app = val;
}
});
var makeGaia = new Make(deviceType);
var makeApp = new Make(deviceType, app);
var runtime = new Runtime(runtimeType, deviceType);
var watcher;
function onfilechange (path) {
console.log('File', path, 'has been changed');
watcher.close();
if (deviceType === 'tv' || !app || needB2gReopenList.indexOf(app) > -1) {
runtime.stop();
}
if (!app) {
makeGaia.run(function () {
reload();
watch();
});
} else {
makeApp.run(function () {
reload();
watch();
});
}
}
function watch () {
console.log('watch file changes...\n');
watcher = chokidar.watch(['apps', 'tv_apps', 'shared'], {
ignored: /build_stage/,
persistent: true,
cwd: process.cwd()
});
watcher.on('change', onfilechange);
}
// Apps that doesn't work with 'install and relaunch'
var needB2gReopenList = config.needB2gReopenList;
function reload() {
if (deviceType === 'tv' || !app || needB2gReopenList.indexOf(app) > -1) {
if (app && app !== 'system' && app !== 'smart-system') {
runtime.start(app, onRuntimeClose);
} else {
runtime.start(null, onRuntimeClose);
}
} else {
runtime.reload(app);
}
}
function onRuntimeClose () {
process.exit();
}
makeGaia.run(function () {
if (app && app !== 'system' && app !== 'smart-system') {
runtime.start(app, onRuntimeClose);
} else {
runtime.start(null, onRuntimeClose);
}
watch();
});
function exitHandler(options, err) {
runtime.isRunning && runtime.stop();
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));