UNPKG

appium-lg-webos-driver

Version:
165 lines 6.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uninstallApp = exports.installApp = exports.closeApp = exports.launchApp = exports.getDeviceInfo = exports.extendDevMode = void 0; const lodash_1 = __importDefault(require("lodash")); const logger_1 = __importDefault(require("../logger")); const path_1 = __importDefault(require("path")); const teen_process_1 = require("teen_process"); const support_1 = require("appium/support"); const env_1 = require("@humanwhocodes/env"); const ARES = 'ares'; const ARES_DEVICE_INFO = 'ares-device-info'; const ARES_EXTEND_DEV = 'ares-extend-dev'; const ARES_INSTALL = 'ares-install'; const ARES_LAUNCH = 'ares-launch'; //const ARES_PACKAGE = 'ares-package'; const env = new env_1.Env(); const WEBOS_CLI_PATH_PREFIX = ['CLI', 'bin']; /** @type {string} */ let LG_WEBOS_TV_SDK_HOME; try { LG_WEBOS_TV_SDK_HOME = env.require('LG_WEBOS_TV_SDK_HOME'); } catch { throw new Error(`Ensure the "LG_WEBOS_TV_SDK_HOME" environment variable is set; see https://webostv.developer.lge.com/sdk/command-line-interface/installation/ for more information`); } /** * Verify the existence of the LG WebOS CLI tools. * * @throws Error if the tools can't be verified */ const assertLgHome = lodash_1.default.once(async function assertLgHome() { const aresCliPath = path_1.default.resolve(LG_WEBOS_TV_SDK_HOME, ...WEBOS_CLI_PATH_PREFIX, ARES); try { // ensure the cli is executable await support_1.fs.access(aresCliPath, support_1.fs.constants.R_OK | support_1.fs.constants.X_OK); } catch { throw new Error(`Could not verify the appropriate LG WebOS TV ` + `SDK binaries exist. Ensure the ` + `LG_WEBOS_TV_SDK_HOME environment variable is set ` + `and the CLI dir exists inside of it with accessible ` + `permissions`); } }); /** * Run an Ares related command * @param {string} bin - name of ares binary to run * @param {string[]} [args] - list of args to apply * * @returns {Promise<import('teen_process').ExecResult<string>>} */ async function runCmd(bin, args = []) { await assertLgHome(); const _bin = path_1.default.resolve(LG_WEBOS_TV_SDK_HOME, ...WEBOS_CLI_PATH_PREFIX, bin); logger_1.default.info(`Running command: ${_bin} ${args.join(' ')}`); try { return await (0, teen_process_1.exec)(_bin, args, { shell: support_1.system.isWindows() }); } catch (err) { const e = /** @type {import('teen_process').ExecError} */ (err); const stdout = e.stdout?.replace(/[\r\n]+/, ' '); const stderr = e.stderr?.replace(/[\r\n]+/, ' '); e.message = `${e.message}. Stdout was: '${stdout}'. Stderr was: '${stderr}'`; throw e; } } /** * Run an Ares related command that takes an optional --device flag * * @param {string} bin - name of ares binary to run * @param {string} [deviceName] - device name/ID as shown in ares-setup-device * @param {string[]} [args] - array of args to apply to command * * @returns {Promise<import('teen_process').ExecResult<string>>} */ async function runDeviceCmd(bin, deviceName, args = []) { if (deviceName) { args.push('--device', deviceName); } return await runCmd(bin, args); } /** * Attempt to extend developer mode on the connected device. This will as * a side effect cause the developer app to be launched * * @param {string} [deviceName] - device to extend, otherwise whichever is connected * @returns {Promise<void>} */ async function extendDevMode(deviceName) { logger_1.default.info(`Extending dev mode on device`); await runDeviceCmd(ARES_EXTEND_DEV, deviceName); } exports.extendDevMode = extendDevMode; /** * Retrieve info about a device using ares-device-info * * @param {string} [deviceName] - device to explicitly get info for, otherwise default * @returns {Promise<Record<string, string>>} */ async function getDeviceInfo(deviceName) { logger_1.default.info(`Getting device info`); let { stdout } = await runDeviceCmd(ARES_DEVICE_INFO, deviceName); stdout = stdout.trim(); const dataParseRe = /^(.+) : (.+)$/gm; const matches = stdout.matchAll(dataParseRe); return [...matches].reduce((acc, m) => { acc[m[1]] = m[2]; return acc; }, /** @type {Record<string,string>} */ ({})); } exports.getDeviceInfo = getDeviceInfo; /** * Launch an installed app by its app id, including params if desired * * @param {string} appId - the app ID * @param {string} [deviceName] - device name to launch an app on * @param {import('type-fest').JsonObject} [launchParams] - dictionary of app launch parameters, will be JSON * stringified and passed to ares-launch */ async function launchApp(appId, deviceName, launchParams) { logger_1.default.info(`Launching app '${appId}'`); const args = [appId]; if (launchParams) { args.push('--params', JSON.stringify(launchParams)); } await runDeviceCmd(ARES_LAUNCH, deviceName, args); } exports.launchApp = launchApp; /** * Close the current app * @param {string} appId * @param {string} [deviceName] - device name to close current app on */ async function closeApp(appId, deviceName) { logger_1.default.info(`Closing app '${appId}'`); await runDeviceCmd(ARES_LAUNCH, deviceName, ['-c', appId]); } exports.closeApp = closeApp; /** * Install an IPK file to the device * @param {string} ipkPath - path to .ipk file * @param {string} appId - the package ID of the app * @param {string} [deviceName] - device name to install app on */ async function installApp(ipkPath, appId, deviceName) { logger_1.default.info(`Installing app '${appId}' from ${ipkPath}`); await runDeviceCmd(ARES_INSTALL, deviceName, [ipkPath]); } exports.installApp = installApp; /** * Uninstall an app from the device * @param {string} appId - the package ID of the app * @param {string} [deviceName] - device name to uninstall app on */ async function uninstallApp(appId, deviceName) { logger_1.default.info(`Uninstalling app '${appId}'`); await runDeviceCmd(ARES_INSTALL, deviceName, ['-r', appId]); } exports.uninstallApp = uninstallApp; //# sourceMappingURL=ares.js.map