forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios-device-operations.ts
378 lines (323 loc) · 10.1 KB
/
ios-device-operations.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { IOSDeviceLib as IOSDeviceLibModule } from "ios-device-lib";
import { cache } from "../../../decorators";
import { DEVICE_LOG_EVENT_NAME } from "../../../constants";
import * as _ from "lodash";
import assert = require("assert");
import { EventEmitter } from "events";
import {
IDisposable,
IShouldDispose,
IDictionary,
} from "../../../declarations";
import { injector } from "../../../yok";
export class IOSDeviceOperations
extends EventEmitter
implements IIOSDeviceOperations, IDisposable, IShouldDispose {
public isInitialized: boolean;
public shouldDispose: boolean;
private deviceLib: IOSDeviceLib.IOSDeviceLib;
constructor(private $logger: ILogger) {
super();
this.isInitialized = false;
this.shouldDispose = true;
}
public async install(
ipaPath: string,
deviceIdentifiers: string[],
errorHandler: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
this.$logger.trace(
`Installing ${ipaPath} on devices with identifiers: ${deviceIdentifiers}.`
);
return await this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.install(ipaPath, deviceIdentifiers),
errorHandler
);
}
public async uninstall(
appIdentifier: string,
deviceIdentifiers: string[],
errorHandler: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
this.$logger.trace(
`Uninstalling ${appIdentifier} from devices with identifiers: ${deviceIdentifiers}.`
);
return await this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.uninstall(appIdentifier, deviceIdentifiers),
errorHandler
);
}
@cache()
public async startLookingForDevices(
deviceFoundCallback: DeviceInfoCallback,
deviceUpdatedCallback: DeviceInfoCallback,
deviceLostCallback: DeviceInfoCallback,
options?: Mobile.IDeviceLookingOptions
): Promise<void> {
this.$logger.trace("Starting to look for iOS devices.");
this.isInitialized = true;
if (!this.deviceLib) {
let foundDevice = false;
const wrappedDeviceFoundCallback = (
deviceInfo: IOSDeviceLib.IDeviceActionInfo
) => {
foundDevice = true;
return deviceFoundCallback(deviceInfo);
};
this.deviceLib = new IOSDeviceLibModule(
wrappedDeviceFoundCallback,
deviceUpdatedCallback,
deviceLostCallback
);
if (options && options.shouldReturnImmediateResult) {
return;
}
// We need this because we need to make sure that we have devices.
await new Promise<void>((resolve, reject) => {
let iterationsCount = 0;
const maxIterationsCount = 3;
const intervalHandle: NodeJS.Timer = setInterval(() => {
if (foundDevice && !options.fullDiscovery) {
resolve();
return clearInterval(intervalHandle);
}
iterationsCount++;
if (iterationsCount >= maxIterationsCount) {
clearInterval(intervalHandle);
return resolve();
}
}, 2000);
});
}
}
public startDeviceLog(deviceIdentifier: string): void {
this.assertIsInitialized();
this.setShouldDispose(false);
this.$logger.trace(
`Printing device log for device with identifier: ${deviceIdentifier}.`
);
this.attacheDeviceLogDataHandler();
this.deviceLib.startDeviceLog([deviceIdentifier]);
}
public async apps(
deviceIdentifiers: string[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceAppInfo> {
this.assertIsInitialized();
this.$logger.trace(
`Getting applications information for devices with identifiers: ${deviceIdentifiers}`
);
return this.getMultipleResults(
() => this.deviceLib.apps(deviceIdentifiers),
errorHandler
);
}
public async listDirectory(
listArray: IOSDeviceLib.IReadOperationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceMultipleResponse> {
this.assertIsInitialized();
_.each(listArray, (l) => {
this.$logger.trace(
`Listing directory: ${l.path} for application ${l.appId} on device with identifier: ${l.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceMultipleResponse>(
() => this.deviceLib.list(listArray),
errorHandler
);
}
public async readFiles(
deviceFilePaths: IOSDeviceLib.IReadOperationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(deviceFilePaths, (p) => {
this.$logger.trace(
`Reading file: ${p.path} from application ${p.appId} on device with identifier: ${p.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.read(deviceFilePaths),
errorHandler
);
}
public async downloadFiles(
deviceFilePaths: IOSDeviceLib.IFileOperationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(deviceFilePaths, (d) => {
this.$logger.trace(
`Downloading file: ${d.source} from application ${d.appId} on device with identifier: ${d.deviceId} to ${d.destination}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.download(deviceFilePaths),
errorHandler
);
}
public uploadFiles(
files: IOSDeviceLib.IUploadFilesData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(files, (f) => {
this.$logger.trace("Uploading files:");
this.$logger.trace(f.files);
this.$logger.trace(
`For application ${f.appId} on device with identifier: ${f.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.upload(files),
errorHandler
);
}
public async deleteFiles(
deleteArray: IOSDeviceLib.IDeleteFileData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(deleteArray, (d) => {
this.$logger.trace(
`Deleting file: ${d.destination} from application ${d.appId} on device with identifier: ${d.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.delete(deleteArray),
errorHandler
);
}
public async start(
startArray: IOSDeviceLib.IDdiApplicationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(startArray, (s) => {
this.$logger.trace(
`Starting application ${s.appId} on device with identifier: ${s.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.start(startArray),
errorHandler
);
}
public async stop(
stopArray: IOSDeviceLib.IDdiApplicationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(stopArray, (s) => {
this.$logger.trace(
`Stopping application ${s.appId} on device with identifier: ${s.deviceId}.`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.stop(stopArray),
errorHandler
);
}
public dispose(signal?: string): void {
// We need to check if we should dispose the device lib.
// For example we do not want to dispose it when we start printing the device logs.
if (this.shouldDispose && this.deviceLib) {
this.deviceLib.removeAllListeners();
this.deviceLib.dispose(signal);
this.deviceLib = null;
this.$logger.trace("IOSDeviceOperations disposed.");
}
}
public async postNotification(
postNotificationArray: IOSDeviceLib.IPostNotificationData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(postNotificationArray, (n) => {
this.$logger.trace(
`Sending notification ${n.notificationName} to device with identifier: ${n.deviceId}`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() => this.deviceLib.postNotification(postNotificationArray),
errorHandler
);
}
public async awaitNotificationResponse(
awaitNotificationResponseArray: IOSDeviceLib.IAwaitNotificatioNResponseData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IOSDeviceResponse> {
this.assertIsInitialized();
_.each(awaitNotificationResponseArray, (n) => {
this.$logger.trace(
`Awaiting notification response from socket: ${n.socket} with timeout: ${n.timeout}`
);
});
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(
() =>
this.deviceLib.awaitNotificationResponse(
awaitNotificationResponseArray
),
errorHandler
);
}
public async connectToPort(
connectToPortArray: IOSDeviceLib.IConnectToPortData[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IDictionary<IOSDeviceLib.IConnectToPortResponse[]>> {
this.assertIsInitialized();
_.each(connectToPortArray, (c) => {
this.$logger.trace(
`Connecting to port ${c.port} on device with identifier: ${c.deviceId}`
);
});
return this.getMultipleResults<IOSDeviceLib.IConnectToPortResponse>(
() => this.deviceLib.connectToPort(connectToPortArray),
errorHandler
);
}
public setShouldDispose(shouldDispose: boolean): void {
this.shouldDispose = shouldDispose;
}
private async getMultipleResults<T>(
getPromisesMethod: () => Promise<T>[],
errorHandler?: DeviceOperationErrorHandler
): Promise<IDictionary<T[]>> {
const result: T[] = [];
const promises = getPromisesMethod();
for (const promise of promises) {
if (errorHandler) {
try {
result.push(await promise);
} catch (err) {
this.$logger.trace(
`Error while executing ios device operation: ${err.message} with code: ${err.code}`
);
errorHandler(err);
}
} else {
result.push(await promise);
}
}
const groupedResults = _.groupBy(result, (r) => <string>(<any>r).deviceId);
this.$logger.trace("Received multiple results:");
this.$logger.trace(groupedResults);
return groupedResults;
}
private assertIsInitialized(): void {
assert.ok(this.isInitialized, "iOS device operations not initialized.");
}
@cache()
private attacheDeviceLogDataHandler(): void {
this.deviceLib.on(
DEVICE_LOG_EVENT_NAME,
(response: IOSDeviceLib.IDeviceLogData) => {
this.emit(DEVICE_LOG_EVENT_NAME, response);
}
);
}
}
injector.register("iosDeviceOperations", IOSDeviceOperations);