UNPKG

appium-adb

Version:

Android Debug Bridge interface

130 lines 4.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileExists = fileExists; exports.ls = ls; exports.fileSize = fileSize; exports.rimraf = rimraf; exports.push = push; exports.pull = pull; exports.mkdir = mkdir; const lodash_1 = __importDefault(require("lodash")); const node_path_1 = __importDefault(require("node:path")); function shellEscapeSingleQuotes(str) { return str.replace(/'/g, "'\\''"); } /** * Verify whether a remote path exists on the device under test. * * @param remotePath - The remote path to verify. * @return True if the given path exists on the device. */ async function fileExists(remotePath) { const passFlag = '__PASS__'; const checkCmd = `[ -e '${shellEscapeSingleQuotes(remotePath)}' ] && echo ${passFlag}`; try { return lodash_1.default.includes(await this.shell([checkCmd]), passFlag); } catch { return false; } } /** * Get the output of _ls_ command on the device under test. * * @param remotePath - The remote path (the first argument to the _ls_ command). * @param opts - Additional _ls_ options. * @return The _ls_ output as an array of split lines. * An empty array is returned of the given _remotePath_ * does not exist. */ async function ls(remotePath, opts = []) { try { const args = ['ls', ...opts, remotePath]; const stdout = await this.shell(args); const lines = stdout.split('\n'); return lines .map((l) => l.trim()) .filter(Boolean) .filter((l) => l.indexOf('No such file') === -1); } catch (err) { if (err.message.indexOf('No such file or directory') === -1) { throw err; } return []; } } /** * Get the size of the particular file located on the device under test. * * @param remotePath - The remote path to the file. * @return File size in bytes. * @throws {Error} If there was an error while getting the size of the given file. */ async function fileSize(remotePath) { try { const files = await this.ls(remotePath, ['-la']); if (files.length !== 1) { throw new Error(`Remote path is not a file`); } // https://regex101.com/r/fOs4P4/8 const match = /[rwxsStT\-+]{10}[\s\d]*\s[^\s]+\s+[^\s]+\s+(\d+)/.exec(files[0]); if (!match || lodash_1.default.isNaN(parseInt(match[1], 10))) { throw new Error(`Unable to parse size from list output: '${files[0]}'`); } return parseInt(match[1], 10); } catch (err) { throw new Error(`Unable to get file size for '${remotePath}': ${err.message}`); } } /** * Forcefully recursively remove a path on the device under test. * Be careful while calling this method. * * @param path - The path to be removed recursively. */ async function rimraf(path) { await this.shell(['rm', '-rf', path]); } /** * Send a file to the device under test. * * @param localPath - The path to the file on the local file system. * @param remotePath - The destination path on the remote device. * @param opts - Additional options mapping. See * https://github.com/appium/node-teen_process, * _exec_ method options, for more information about available * options. */ async function push(localPath, remotePath, opts) { await this.mkdir(node_path_1.default.posix.dirname(remotePath)); await this.adbExec(['push', localPath, remotePath], opts); } /** * Receive a file from the device under test. * * @param remotePath - The source path on the remote device. * @param localPath - The destination path to the file on the local file system. * @param opts - Additional options mapping. See * https://github.com/appium/node-teen_process, * _exec_ method options, for more information about available * options. */ async function pull(remotePath, localPath, opts = {}) { // pull folder can take more time, increasing time out to 60 secs await this.adbExec(['pull', remotePath, localPath], { ...opts, timeout: opts.timeout ?? 60000 }); } /** * Recursively create a new folder on the device under test. * * @param remotePath - The new path to be created. * @return mkdir command output. */ async function mkdir(remotePath) { return await this.shell([`mkdir -p -- '${shellEscapeSingleQuotes(remotePath)}'`]); } //# sourceMappingURL=fs-commands.js.map