-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
209 lines (184 loc) · 6 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
207
208
209
var remote = require('./remote_debugger'),
ADB = require('adb').DebugBridge;
var FFOS_Cli = function FFOS_Cli() {
var config;
var adb = new ADB();
var configure = function configure(json) {
config = json;
};
// 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, callback) {
adb.traceDevice(function onDevices(devices) {
if (!devices || devices.length == 0) {
callback('No devices');
}
var device = devices[0];
try {
device.takeSnapshot(function onSnapshot(frame) {
frame.writeImageFile(fileName);
if (callback) {
callback(null);
}
});
} catch (e) {
callback(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, callback) {
var localPort = 'tcp:6000';
var remotePort = 'localfilesystem:/data/local/debugger-socket';
if (config && config.localPort && config.remotePort) {
localPort = config.localPort;
remotePort = config.remotePort;
}
adb.forward(localPort, remotePort, function onForward() {
//Build the remote url with the appId
var remoteFile = '/data/local/tmp/b2g/' + appId + '/application.zip';
console.log('Doing push for file ' + remoteFile);
pushFile(localZip, remoteFile, function onPushed(err, success) {
// Know bug in adb library it returns error 15 despite of uploading the file
if (err && err != 15) {
callback(err);
return;
}
installRemote(localPort, appId, appType, callback);
});
});
};
/*
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, callback) {
var localPort = 'tcp:6000';
var remotePort = 'localfilesystem:/data/local/debugger-socket';
if (config && config.localPort && config.remotePort) {
localPort = config.localPort;
remotePort = config.remotePort;
}
adb.forward(localPort, remotePort, function onForward() {
closeRemote(localPort, appId, callback);
});
};
/*
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, callback) {
var localPort = 'tcp:6000';
var remotePort = 'localfilesystem:/data/local/debugger-socket';
if (config && config.localPort && config.remotePort) {
localPort = config.localPort;
remotePort = config.remotePort;
}
adb.forward(localPort, remotePort, function onForward() {
launchRemote(localPort, appId, callback);
});
};
/*
Shortcut of the previous function to install packaged apps
*/
var installHostedApp = function installHostedApp(appId,
manifestFile, callback) {
installApp(appId, manifestFile, '1', callback);
};
var installPackagedApp = function installPackagedApp(appId,
localZip, callback) {
installApp(appId, localZip, '2', callback);
};
// Uses the remote protocol to tell the system to install an app
// previously uploaded
var installRemote = function installRemote(remotePort, appId, appType, cb) {
remote.init(remotePort.split(':')[1]);
remote.installApp(appId, appType, function onInstall(err, data) {
if (err) {
cb(err);
return;
}
cb(null, data);
});
};
// Uses the remote protocol to tell the system to stop an app
var closeRemote = function closeRemote(remotePort, appId, cb) {
remote.init(remotePort.split(':')[1]);
remote.closeApp(appId, function onClose(err, data) {
if (err) {
cb(err);
return;
}
cb(null, data);
});
};
// Uses the remote protocol to tell the system to launch an app
var launchRemote = function launchRemote(remotePort, appId, cb) {
remote.init(remotePort.split(':')[1]);
remote.launchApp(appId, function onLaunch(err, data) {
if (err) {
cb(err);
return;
}
cb(null, data);
});
};
// Push a local file to a remote location on the phone
var pushFile = function pushFile(local, remote, callback) {
adb.traceDevice(function onDevices(devices) {
// Work with the first device we found, if any
if (!devices || devices.length == 0) {
callback('No devices found');
return;
}
var device = devices[0];
device.getSyncService(function onSyncService(sync) {
sync.pushFile(local, remote, callback);
});
});
};
// Resets the B2G process as the name says
var resetB2G = function resetB2G(callback) {
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) {
if (callback) {
callback();
}
});
});
}
});
};
return {
'config': config,
'logcat': logcat,
'screenshot': screenshot,
'installHostedApp': installHostedApp,
'installPackagedApp': installPackagedApp,
'closeApp': closeApp,
'launchApp': launchApp,
'resetB2G': resetB2G
};
}();
module.exports = FFOS_Cli;