multi-automator
Version:
Multi terminal automation
123 lines (122 loc) • 4.37 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.devices = exports.launch = exports.DEVICE_TYPE_MAP = void 0;
const Device_1 = __importDefault(require("./Device"));
const web = __importStar(require("./web/index"));
const iOS = __importStar(require("./iOS/index"));
const android = __importStar(require("./android/index"));
exports.DEVICE_TYPE_MAP = ['web', 'ios', 'android'];
/**
* 检查设备连接,若未输入设备ID,则返回当前连接的第一个设备ID
*
* @param deviceType 设备类型
* @param deviceId 设备ID
* @returns Promise<string>
*/
async function checkDeviceConnect(deviceType, deviceId) {
if ('web' === deviceType) {
return 'web-device-id';
}
const deviceList = await devices(deviceType);
const deviceIds = Object.keys(deviceList);
if (deviceIds.length === 0) {
throw new Error('未发现连接设备');
}
if (undefined === deviceId) {
deviceId = deviceIds[0];
}
else {
if (deviceIds.indexOf(deviceId) === -1) {
throw new Error(`设备未连接: ${deviceId}`);
}
}
return deviceId;
}
/**
* 初始化设备
*
* @param options
* @returns <Promise<device>>
*/
async function launch(options) {
options = options || {};
let { deviceId, deviceType = 'web', webOptions, iOSOptions, androidOptions } = options;
deviceType = deviceType === null || deviceType === void 0 ? void 0 : deviceType.toLocaleLowerCase();
if (-1 === exports.DEVICE_TYPE_MAP.indexOf(deviceType)) {
throw new Error(`暂不支持该类型设备:${deviceType}`);
}
deviceId = await checkDeviceConnect(deviceType, deviceId);
let device = new Device_1.default({ deviceId, deviceType });
if ('web' === deviceType) {
webOptions = webOptions || {};
let webHandler = await web.init(webOptions);
await device.init(webHandler);
}
else if ('ios' === deviceType) {
if (undefined === iOSOptions) {
throw new Error('参数对象 iOSOptions 不能为空');
}
if (undefined === iOSOptions.wdaProjPath) {
throw new Error('参数 iOSOptions.wdaProjPath 不能为空');
}
let iOSHandler = await iOS.init(deviceId, iOSOptions);
await device.init(iOSHandler);
}
else if ('android' === deviceType) {
if (undefined === androidOptions) {
throw new Error('参数对象 androidOptions 不能为空');
}
let androidHandler = await android.init(deviceId, androidOptions);
await device.init(androidHandler);
}
else {
throw new Error(`Not support this os: ${deviceType}`);
}
return device;
}
exports.launch = launch;
/**
* 获取设备列表
*
* @param deviceType 设备类型
* @returns Promise<DevicesMap>
*/
async function devices(deviceType) {
deviceType = deviceType.toLocaleLowerCase();
if ('ios' === deviceType) {
return await iOS.devices();
}
else if ('android' === deviceType) {
return await android.devices();
}
else {
throw new Error(`Not support this os: ${deviceType}`);
}
}
exports.devices = devices;