Skip to content

Commit

Permalink
feat: Add geckodriver download script
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Sep 18, 2024
1 parent d99863e commit 2f0f950
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from 'appium/support';

const log = logger.getLogger('GeckoDriver');
export const log = logger.getLogger('GeckoDriver');

export default log;
100 changes: 98 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import _ from 'lodash';
import { fs, net, zip, tempDir } from 'appium/support';
import tar from 'tar-stream';
import zlib from 'node:zlib';
import B from 'bluebird';
import path from 'node:path';

const GECKO_CAP_PREFIXES = ['moz:'];
// https://www.w3.org/TR/webdriver/#capabilities
Expand All @@ -13,7 +18,7 @@ const STANDARD_CAPS = [
'unhandledPromptBehavior',
];

function formatCapsForServer (caps) {
export function formatCapsForServer (caps) {
const result = {};
if (caps.browserName) {
result.browserName = 'firefox';
Expand All @@ -30,4 +35,95 @@ function formatCapsForServer (caps) {
return result;
}

export { formatCapsForServer };
/**
*
* @param {string} srcUrl
* @param {string} dstPath
* @returns {Promise<void>}
*/
export async function downloadToFile(srcUrl, dstPath) {
await net.downloadFile(srcUrl, dstPath);
}

/**
*
* @param {string} p
* @returns {Promise<void>}
*/
export async function mkdirp(p) {
await fs.mkdirp(p);
}

/**
*
* @param {string} srcAcrhive
* @param {string} fileToExtract
* @param {string} dstPath
* @returns {Promise<void>}
*/
export async function extractFileFromTarGz(srcAcrhive, fileToExtract, dstPath) {
const chunks = [];
const extract = tar.extract();
const extractPromise = new B((resolve, reject) => {
extract.on('entry', (header, stream, next) => {
if (header.name === fileToExtract) {
stream.on('data', (chunk) => {
chunks.push(chunk);
});
}
stream.on('end', function() {
next();
});
stream.resume();
});
extract.once('error', reject);
extract.once('finish', async () => {
if (chunks.length) {
try {
await fs.writeFile(dstPath, Buffer.concat(chunks));
} catch (e) {
return reject(e);
}
} else {
return reject(
new Error(`The file '${fileToExtract}' could not be found in the '${srcAcrhive}' archive`)
);
}
resolve();
});
});

fs.createReadStream(srcAcrhive)
.pipe(zlib.createGunzip())
.pipe(extract);

await extractPromise;
}

/**
*
* @param {string} srcAcrhive
* @param {string} fileToExtract
* @param {string} dstPath
* @returns {Promise<void>}
*/
export async function extractFileFromZip(srcAcrhive, fileToExtract, dstPath) {
let didFindEntry = false;
await zip.readEntries(srcAcrhive, async ({entry, extractEntryTo}) => {
if (didFindEntry || entry.fileName !== fileToExtract) {
return;
}
didFindEntry = true;

const tmpRoot = await tempDir.openDir();
try {
await extractEntryTo(tmpRoot);
await fs.mv(path.resolve(tmpRoot, entry.fileName), dstPath);
} finally {
await fs.rimraf(tmpRoot);
}
});
if (!didFindEntry) {
throw new Error(`The file '${fileToExtract}' could not be found in the '${srcAcrhive}' archive`);
}
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,38 @@
"Android",
"Windows"
],
"scripts": {
"install-geckodriver": "./scripts/install-geckodriver.mjs"
},
"mainClass": "GeckoDriver"
},
"main": "./build/index.js",
"bin": {},
"directories": {
"lib": "lib"
},
"files": [
"index.js",
"lib",
"build",
"CHANGELOG.md",
"LICENSE",
"npm-shrinkwrap.json",
"scripts/*.mjs"
],
"peerDependencies": {
"appium": "^2.4.1"
},
"dependencies": {
"appium-adb": "^12.0.3",
"asyncbox": "^3.0.0",
"axios": "^1.7.7",
"bluebird": "^3.5.1",
"lodash": "^4.17.4",
"portscanner": "2.2.0",
"semver": "^7.6.3",
"source-map-support": "^0.x",
"tar-stream": "^3.1.7",
"teen_process": "^2.0.0"
},
"scripts": {
Expand Down
Loading

0 comments on commit 2f0f950

Please sign in to comment.