|
| 1 | +import {fs, logger, zip, net, node} from 'appium/support.js'; |
| 2 | +import _ from 'lodash'; |
| 3 | +import os from 'os'; |
| 4 | +import path from 'path'; |
| 5 | +import {parseArgValue} from './utils.js'; |
| 6 | + |
| 7 | +const log = logger.getLogger('download-wda-sim'); |
| 8 | +const wdaUrl = (version, zipFileName) => |
| 9 | + `https://github.com/appium/WebDriverAgent/releases/download/v${version}/${zipFileName}`; |
| 10 | +const destZip = (platform) => { |
| 11 | + const scheme = `WebDriverAgentRunner${_.toLower(platform) === 'tvos' ? '_tvOS' : ''}`; |
| 12 | + return `${scheme}-Build-Sim-${os.arch() === 'arm64' ? 'arm64' : 'x86_64'}.zip`; |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Return installed appium-webdriveragent package version |
| 17 | + * @returns {number} |
| 18 | + */ |
| 19 | +async function webdriveragentPkgVersion() { |
| 20 | + const pkgPath = path.join( |
| 21 | + node.getModuleRootSync('appium-xcuitest-driver', import.meta.url), |
| 22 | + 'node_modules', |
| 23 | + 'appium-webdriveragent', |
| 24 | + 'package.json' |
| 25 | + ); |
| 26 | + return JSON.parse(await fs.readFile(pkgPath, 'utf8')).version; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Prepare the working root directory. |
| 31 | + * @returns {string} Root directory to download and unzip. |
| 32 | + */ |
| 33 | +async function prepareRootDir() { |
| 34 | + const destDirRoot = parseArgValue('outdir'); |
| 35 | + if (!destDirRoot) { |
| 36 | + throw new Error(`--outdir is required`); |
| 37 | + } |
| 38 | + const destDir = path.resolve(process.cwd(), destDirRoot); |
| 39 | + if (await fs.exists(destDir)) { |
| 40 | + throw new Error(`${destDir} already exists`); |
| 41 | + } |
| 42 | + await fs.mkdir(destDir, {recursive: true}); |
| 43 | + return destDir; |
| 44 | +} |
| 45 | + |
| 46 | +async function getWDAPrebuiltPackage() { |
| 47 | + const destDir = await prepareRootDir(); |
| 48 | + const platform = parseArgValue('platform'); |
| 49 | + const zipFileName = destZip(platform); |
| 50 | + const wdaVersion = await webdriveragentPkgVersion(); |
| 51 | + const urlToDownload = wdaUrl(wdaVersion, zipFileName); |
| 52 | + const downloadedZipFile = path.join(destDir, zipFileName); |
| 53 | + try { |
| 54 | + log.info(`Downloading ${urlToDownload}`); |
| 55 | + await net.downloadFile(urlToDownload, downloadedZipFile); |
| 56 | + |
| 57 | + log.info(`Unpacking ${downloadedZipFile} into ${destDir}`); |
| 58 | + await zip.extractAllTo(downloadedZipFile, destDir); |
| 59 | + |
| 60 | + log.info(`Deleting ${downloadedZipFile}`); |
| 61 | + } finally { |
| 62 | + if (await fs.exists(downloadedZipFile)) { |
| 63 | + await fs.unlink(downloadedZipFile); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +(async () => await getWDAPrebuiltPackage())(); |
0 commit comments