-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
206 lines (183 loc) · 5.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var remote = require('./remote_debugger')
, ADB = require('adb').DebugBridge
, Promise = require('promise');
var FFOS_Cli = function FFOS_Cli() {
var config;
var adb = new ADB();
var remoteInitialized = false;
var portForwarded = false;
var localPort = 'tcp:6000';
var remotePort = 'localfilesystem:/data/local/debugger-socket';
var configure = function configure(json) {
config = json;
if (config) {
localPort = config.localPort || localPort;
remotePort = config.remotePort || remotePort;
}
};
function ensureRemoteInit() {
if (!remoteInitialized) {
remote.init(localPort.split(':')[1]);
remoteInitialized = true;
}
}
function ensurePortForwarded() {
if (!portForwarded) {
portForwarded = true;
return new Promise(function(resolve) {
adb.forward(localPort, remotePort, resolve);
});
} else {
return Promise.resolve();
}
}
// Start displaying the logcat for the first device we find
var logcat = function logcat() {
adb.traceDevice(function onDevices(devices) {
if (!devices || devices.length == 0) {
return;
}
devices[0].logcat();
});
};
// Takes a screenshot from device if any, pass a file name
// and a callback to know when we finished.
// The callback expected 1 parameter, in case an error
// happened
var screenshot = function screenshot(fileName) {
return new Promise(function(resolve, reject) {
adb.traceDevice(function onDevices(devices) {
if (!devices || devices.length == 0) {
reject('No devices');
}
var device = devices[0];
try {
device.takeSnapshot(function onSnapshot(frame) {
frame.writeImageFile(fileName);
resolve();
});
} catch (e) {
reject(e);
}
});
});
};
/*
For installing an app just follow the steps:
1.- Forward the remote debugger port (use config if present)
2.- Upload the selected zip file to the app id
3.- Use the remote client to tell the system to install the app
*/
var installApp = function installApp(appId, localZip, appType) {
return ensurePortForwarded().then(function onForward() {
//Build the remote url with the appId
var remoteFile = '/data/local/tmp/b2g/' + appId + '/application.zip';
return pushFile(localZip, remoteFile).then(function onPushed(err, success) {
// Know bug in adb library it returns error 15 despite of uploading the file
if (err && err != 15) {
return Promise.reject(err);
}
return installRemote(appId, appType);
});
});
};
/*
For closing an app just follow the steps:
1.- Forward the remote debugger port (use config if present)
2.- Use the remote client to tell the system to stop the app
*/
var closeApp = function closeApp(appId) {
return appCommandRemote("close", appId, null);
};
/*
For launching an app just follow the steps:
1.- Forward the remote debugger port (use config if present)
2.- Use the remote client to tell the system to launch the app
*/
var launchApp = function launchApp(appId) {
return appCommandRemote("launch", appId, null);
};
/*
For launching any generic command just follow the steps:
1.- Forward the remote debugger port (use config if present)
2.- Use the remote client to tell the system to execute the command
*/
var appCommand = function appCommand(command, appId, actor) {
return appCommandRemote(command, appId, actor);
};
/*
Shortcut of the previous function to install packaged apps
*/
var installHostedApp = function installHostedApp(appId, manifestFile) {
return installApp(appId, manifestFile, '1');
};
var installPackagedApp = function installPackagedApp(appId, localZip) {
return installApp(appId, localZip, '2');
};
// Uses the remote protocol to tell the system to install an app
// previously uploaded
var installRemote = function installRemote(appId, appType) {
ensureRemoteInit();
return new Promise(function (resolve, reject) {
remote.installApp(appId, appType, function onInstall(err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
var appCommandRemote = function appCommandRemote(command, appId, actor) {
ensureRemoteInit();
return new Promise(function(resolve, reject) {
remote.appCommand(command, appId, actor, function onLaunch(err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
});
};
// Push a local file to a remote location on the phone
var pushFile = function pushFile(local, remote) {
return new Promise(function(resolve, reject) {
adb.traceDevice(function onDevices(devices) {
// Work with the first device we found, if any
if (!devices || devices.length == 0) {
return reject('No devices found');
}
var device = devices[0];
device.getSyncService(function onSyncService(sync) {
sync.pushFile(local, remote, resolve);
});
});
});
};
// Resets the B2G process as the name says
var resetB2G = function resetB2G() {
return new Promise(function(resolve) {
adb.traceDevice(function onDevices(devices) {
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
device.shellCmd('stop', ['b2g'], function onCmd(data) {
device.shellCmd('start', ['b2g'], function onCmd(data) {
resolve();
});
});
}
});
});
};
return {
'config': config,
'logcat': logcat,
'screenshot': screenshot,
'installHostedApp': installHostedApp,
'installPackagedApp': installPackagedApp,
'closeApp': closeApp,
'launchApp': launchApp,
'appCommand': appCommand,
'resetB2G': resetB2G
};
}();
module.exports = FFOS_Cli;