UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

201 lines • 9.81 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IOSDeviceOperations = void 0; const ios_device_lib_1 = require("ios-device-lib"); const decorators_1 = require("../../../decorators"); const constants_1 = require("../../../constants"); const _ = require("lodash"); const assert = require("assert"); const events_1 = require("events"); const yok_1 = require("../../../yok"); class IOSDeviceOperations extends events_1.EventEmitter { constructor($logger) { super(); this.$logger = $logger; this.isInitialized = false; this.shouldDispose = true; } async install(ipaPath, deviceIdentifiers, errorHandler) { this.assertIsInitialized(); this.$logger.trace(`Installing ${ipaPath} on devices with identifiers: ${deviceIdentifiers}.`); return await this.getMultipleResults(() => this.deviceLib.install(ipaPath, deviceIdentifiers), errorHandler); } async uninstall(appIdentifier, deviceIdentifiers, errorHandler) { this.assertIsInitialized(); this.$logger.trace(`Uninstalling ${appIdentifier} from devices with identifiers: ${deviceIdentifiers}.`); return await this.getMultipleResults(() => this.deviceLib.uninstall(appIdentifier, deviceIdentifiers), errorHandler); } async startLookingForDevices(deviceFoundCallback, deviceUpdatedCallback, deviceLostCallback, options) { this.$logger.trace("Starting to look for iOS devices."); this.isInitialized = true; if (!this.deviceLib) { let foundDevice = false; const wrappedDeviceFoundCallback = (deviceInfo) => { foundDevice = true; return deviceFoundCallback(deviceInfo); }; this.deviceLib = new ios_device_lib_1.IOSDeviceLib(wrappedDeviceFoundCallback, deviceUpdatedCallback, deviceLostCallback); if (options && options.shouldReturnImmediateResult) { return; } // We need this because we need to make sure that we have devices. await new Promise((resolve, reject) => { let iterationsCount = 0; const maxIterationsCount = 3; const intervalHandle = setInterval(() => { if (foundDevice && !options.fullDiscovery) { resolve(); return clearInterval(intervalHandle); } iterationsCount++; if (iterationsCount >= maxIterationsCount) { clearInterval(intervalHandle); return resolve(); } }, 2000); }); } } startDeviceLog(deviceIdentifier) { this.assertIsInitialized(); this.setShouldDispose(false); this.$logger.trace(`Printing device log for device with identifier: ${deviceIdentifier}.`); this.attacheDeviceLogDataHandler(); this.deviceLib.startDeviceLog([deviceIdentifier]); } async apps(deviceIdentifiers, errorHandler) { this.assertIsInitialized(); this.$logger.trace(`Getting applications information for devices with identifiers: ${deviceIdentifiers}`); return this.getMultipleResults(() => this.deviceLib.apps(deviceIdentifiers), errorHandler); } async listDirectory(listArray, errorHandler) { 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(() => this.deviceLib.list(listArray), errorHandler); } async readFiles(deviceFilePaths, errorHandler) { 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(() => this.deviceLib.read(deviceFilePaths), errorHandler); } async downloadFiles(deviceFilePaths, errorHandler) { 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(() => this.deviceLib.download(deviceFilePaths), errorHandler); } uploadFiles(files, errorHandler) { 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(() => this.deviceLib.upload(files), errorHandler); } async deleteFiles(deleteArray, errorHandler) { 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(() => this.deviceLib.delete(deleteArray), errorHandler); } async start(startArray, errorHandler) { this.assertIsInitialized(); _.each(startArray, (s) => { this.$logger.trace(`Starting application ${s.appId} on device with identifier: ${s.deviceId}.`); }); return this.getMultipleResults(() => this.deviceLib.start(startArray), errorHandler); } async stop(stopArray, errorHandler) { this.assertIsInitialized(); _.each(stopArray, (s) => { this.$logger.trace(`Stopping application ${s.appId} on device with identifier: ${s.deviceId}.`); }); return this.getMultipleResults(() => this.deviceLib.stop(stopArray), errorHandler); } dispose(signal) { // 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."); } } async postNotification(postNotificationArray, errorHandler) { this.assertIsInitialized(); _.each(postNotificationArray, (n) => { this.$logger.trace(`Sending notification ${n.notificationName} to device with identifier: ${n.deviceId}`); }); return this.getMultipleResults(() => this.deviceLib.postNotification(postNotificationArray), errorHandler); } async awaitNotificationResponse(awaitNotificationResponseArray, errorHandler) { this.assertIsInitialized(); _.each(awaitNotificationResponseArray, (n) => { this.$logger.trace(`Awaiting notification response from socket: ${n.socket} with timeout: ${n.timeout}`); }); return this.getMultipleResults(() => this.deviceLib.awaitNotificationResponse(awaitNotificationResponseArray), errorHandler); } async connectToPort(connectToPortArray, errorHandler) { this.assertIsInitialized(); _.each(connectToPortArray, (c) => { this.$logger.trace(`Connecting to port ${c.port} on device with identifier: ${c.deviceId}`); }); return this.getMultipleResults(() => this.deviceLib.connectToPort(connectToPortArray), errorHandler); } setShouldDispose(shouldDispose) { this.shouldDispose = shouldDispose; } async getMultipleResults(getPromisesMethod, errorHandler) { const result = []; 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) => r.deviceId); this.$logger.trace("Received multiple results:"); this.$logger.trace(groupedResults); return groupedResults; } assertIsInitialized() { assert.ok(this.isInitialized, "iOS device operations not initialized."); } attacheDeviceLogDataHandler() { this.deviceLib.on(constants_1.DEVICE_LOG_EVENT_NAME, (response) => { this.emit(constants_1.DEVICE_LOG_EVENT_NAME, response); }); } } exports.IOSDeviceOperations = IOSDeviceOperations; __decorate([ (0, decorators_1.cache)() ], IOSDeviceOperations.prototype, "startLookingForDevices", null); __decorate([ (0, decorators_1.cache)() ], IOSDeviceOperations.prototype, "attacheDeviceLogDataHandler", null); yok_1.injector.register("iosDeviceOperations", IOSDeviceOperations); //# sourceMappingURL=ios-device-operations.js.map