forked from facebookarchive/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathfetch-prebuilt-wda.mjs
More file actions
77 lines (67 loc) · 2.39 KB
/
Copy pathfetch-prebuilt-wda.mjs
File metadata and controls
77 lines (67 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import axios from 'axios';
import { logger, fs, mkdirp, net } from '@appium/support';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const isMainModule = process.argv[1] && path.resolve(process.argv[1]) === __filename;
const log = logger.getLogger('WDA');
/**
* Download all prebuilt WebDriverAgent archives for the current package version.
*/
async function fetchPrebuiltWebDriverAgentAssets () {
const packageJson = JSON.parse(readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf8'));
const tag = packageJson.version;
log.info(`Getting links to webdriveragent release ${tag}`);
const downloadUrl = `https://api.github.com/repos/appium/webdriveragent/releases/tags/v${tag}`;
log.info(`Getting WDA release ${downloadUrl}`);
let releases;
try {
releases = (await axios({
url: downloadUrl,
headers: {
'user-agent': 'appium',
'accept': 'application/json, */*',
},
})).data;
} catch (e) {
throw new Error(`Could not fetch endpoint ${downloadUrl}. Reason: ${e.message}`);
}
const webdriveragentsDir = path.resolve(__dirname, '..', 'prebuilt-agents');
log.info(`Creating webdriveragents directory at: ${webdriveragentsDir}`);
await fs.rimraf(webdriveragentsDir);
await mkdirp(webdriveragentsDir);
// Define a method that does a streaming download of an asset
async function downloadAgent (url, targetPath) {
try {
await net.downloadFile(url, targetPath);
} catch (err) {
throw new Error(`Problem downloading webdriveragent from url ${url}: ${err.message}`);
}
}
log.info(`Downloading assets to: ${webdriveragentsDir}`);
const agentsDownloading = [];
for (const asset of releases.assets) {
const url = asset.browser_download_url;
log.info(`Downloading: ${url}`);
try {
const nameOfAgent = url.split('/').at(-1);
if (!nameOfAgent) {
continue;
}
agentsDownloading.push(downloadAgent(url, path.join(webdriveragentsDir, nameOfAgent)));
} catch { }
}
// Wait for them all to finish
return await Promise.all(agentsDownloading);
}
if (isMainModule) {
try {
await fetchPrebuiltWebDriverAgentAssets();
} catch (e) {
log.error(e);
process.exit(1);
}
}
export default fetchPrebuiltWebDriverAgentAssets;