Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/update code format, replace declaration with expressions #846

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions lib/install-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const isBrowserDriver = (fileName) => {
return extensions.some((ext) => fileName.endsWith(ext)) || !fileName.includes('.');
};

const basePath = (fullPath) => {
return path.dirname(fullPath);
};

const logInstallSummary = (logger, paths, urls) => {
installers.forEach((name) => {
if (!paths[name]) {
Expand All @@ -34,22 +30,22 @@ const logInstallSummary = (logger, paths, urls) => {
});
};

function asyncLogEnd(logger) {
const asyncLogEnd = (logger) => {
logger('');
logger('-----');
logger('selenium-standalone installation finished');
logger('-----');
}
};

async function createDirs(paths) {
const createDirs = async (paths) => {
const installDirectories = Object.keys(paths).map((name) => {
return paths[name].installPath;
});

for (const d of installDirectories.map(basePath)) {
for (const d of installDirectories.map((fullPath) => path.dirname(fullPath))) {
await mkdirp(d);
}
}
};

const chmod = (where) =>
new Promise((resolve, reject) => {
Expand All @@ -62,7 +58,7 @@ const chmod = (where) =>
});
});

async function setDriverFilePermissions(where) {
const setDriverFilePermissions = async (where) => {
debug('setDriverFilePermissions', where);

const requireChmod = await new Promise((resolve) =>
Expand All @@ -77,9 +73,9 @@ async function setDriverFilePermissions(where) {
if (requireChmod) {
await chmod(where);
}
}
};

async function isUpToDate(url, requestOpts, etag) {
const isUpToDate = async (url, requestOpts, etag) => {
if (!etag) {
return false;
}
Expand All @@ -104,13 +100,13 @@ async function isUpToDate(url, requestOpts, etag) {
);
return true;
}
}
};

function getTempFileName(suffix) {
const getTempFileName = (suffix) => {
return os.tmpdir() + path.sep + os.uptime() + suffix;
}
};

async function uncompressDownloadedFile(zipFilePath) {
const uncompressDownloadedFile = async (zipFilePath) => {
debug('unzip ' + zipFilePath);

return new Promise((resolve, reject) =>
Expand Down Expand Up @@ -141,9 +137,9 @@ async function uncompressDownloadedFile(zipFilePath) {
zipFile.on('close', resolve);
})
);
}
};

async function uncompressGzippedFile(from, gzipFilePath) {
const uncompressGzippedFile = async (from, gzipFilePath) => {
return new Promise((resolve, reject) => {
const gunzip = zlib.createGunzip();
const extractPath = path.join(path.dirname(gzipFilePath), path.basename(gzipFilePath, '.gz'));
Expand Down Expand Up @@ -183,9 +179,9 @@ async function uncompressGzippedFile(from, gzipFilePath) {
gunzippedContent.pipe(writeStream).on('finish', resolve);
}
});
}
};

async function runInstaller(installerFile, from, to) {
const runInstaller = async (installerFile, from, to) => {
const logFile = getTempFileName('installer.log');
const options = [
'/passive', // no user interaction, only show progress bar
Expand Down Expand Up @@ -228,7 +224,7 @@ async function runInstaller(installerFile, from, to) {

runner.on('error', (errRunner) => reject(logError('runInstaller:runner', errRunner)));
});
}
};

module.exports = {
isBrowserDriver,
Expand Down
4 changes: 2 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable no-shadow */
module.exports = install;

const { createWriteStream } = require('fs');
const { readFile, writeFile } = require('fs').promises;
const path = require('path');
Expand Down Expand Up @@ -321,3 +319,5 @@ async function install(_opts) {
function isDownloadActive() {
return !Array.from(downloadStreams.values()).includes(true);
}

module.exports = install;
111 changes: 56 additions & 55 deletions lib/platformDetection.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
const os = require('os');

function detectBrowserPlatformInternal(wantedArchitecture) {
const platform = os.platform();
switch (platform) {
case 'darwin': {
if (wantedArchitecture) {
return wantedArchitecture === 'arm64' ? 'mac-arm64' : 'mac-x64';
}
return os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
}
case 'linux': {
return 'linux';
}
case 'win32': {
if (wantedArchitecture) {
return wantedArchitecture === 'x64' || (wantedArchitecture === 'arm64' && isWindows11(os.release()))
? 'win32'
: 'win64';
}
return os.arch() === 'x64' || (os.arch() === 'arm64' && isWindows11(os.release())) ? 'win32' : 'win64';
}
default:
return undefined;
}
}

function isWindows11(version) {
const isWindows11 = (version) => {
const parts = version.split('.');
if (parts.length > 2) {
const major = parseInt(parts[0], 10);
Expand All @@ -34,9 +9,9 @@ function isWindows11(version) {
return major > 10 || (major === 10 && minor > 0) || (major === 10 && minor === 0 && patch >= 22000);
}
return false;
}
};

function getArhType(platform) {
const getArhType = (platform) => {
switch (platform) {
case 'linux':
return 'linux64';
Expand All @@ -53,13 +28,39 @@ function getArhType(platform) {
case 'win64':
return 'win64';
}
}

function detectBrowserPlatformCustom(arh) {
return arh ? detectBrowserPlatformInternal(arh) : detectBrowserPlatformInternal();
}
throw new Error('Platform ' + platform + ' is not supported');
};

function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
const detectBrowserPlatformInternal = (wantedArchitecture) => {
const platform = os.platform();
switch (platform) {
case 'darwin': {
if (wantedArchitecture) {
return wantedArchitecture === 'arm64' ? 'mac-arm64' : 'mac-x64';
}
return os.arch() === 'arm64' ? 'mac-arm64' : 'mac-x64';
}
case 'linux': {
return 'linux';
}
case 'win32': {
if (wantedArchitecture) {
return wantedArchitecture === 'x64' || (wantedArchitecture === 'arm64' && isWindows11(os.release()))
? 'win32'
: 'win64';
}
return os.arch() === 'x64' || (os.arch() === 'arm64' && isWindows11(os.release())) ? 'win32' : 'win64';
}
default:
return undefined;
}
};

const detectBrowserPlatformCustom = (arh) =>
arh ? detectBrowserPlatformInternal(arh) : detectBrowserPlatformInternal();

const getChromiumEdgeDriverArchitectureOld = (wantedArchitecture, version) => {
let platform;

if (process.platform === 'linux') {
Expand All @@ -78,9 +79,9 @@ function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
}

return platform;
}
};

function getChromeDriverArchitectureOld(wantedArchitecture, version) {
const getChromeDriverArchitectureOld = (wantedArchitecture, version) => {
let platform;

if (process.platform === 'linux') {
Expand All @@ -99,9 +100,9 @@ function getChromeDriverArchitectureOld(wantedArchitecture, version) {
}

return platform;
}
};

function getIeDriverArchitectureOld(wanted) {
const getIeDriverArchitectureOld = (wanted) => {
let platform;

if (wanted === 'ia32') {
Expand All @@ -111,9 +112,24 @@ function getIeDriverArchitectureOld(wanted) {
}

return platform;
}
};

const getLinuxFirefoxDriverArchitectureOld = (extension, wantedArchitecture = 'x64') => {
const arch = wantedArchitecture === 'x64' ? '64' : '32';
return 'linux' + arch + extension;
};

function getFirefoxDriverArchitectureOld(wantedArchitecture) {
const getMacFirefoxDriverArchitectureOld = (extension) => {
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + extension;
};

const getWindowsFirefoxDriverArchitectureOld = (wantedArchitecture = '64') => {
const arch = wantedArchitecture.substr(-2) === '64' ? '64' : '32';

return `win${arch}.zip`;
};

const getFirefoxDriverArchitectureOld = (wantedArchitecture) => {
const extension = '.tar.gz';

switch (process.platform) {
Expand All @@ -126,22 +142,7 @@ function getFirefoxDriverArchitectureOld(wantedArchitecture) {
default:
throw new Error('No Firefox driver is available for platform "' + process.platform + '"');
}
}

function getLinuxFirefoxDriverArchitectureOld(extension, wantedArchitecture = 'x64') {
const arch = wantedArchitecture === 'x64' ? '64' : '32';
return 'linux' + arch + extension;
}

function getMacFirefoxDriverArchitectureOld(extension) {
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + extension;
}

function getWindowsFirefoxDriverArchitectureOld(wantedArchitecture = '64') {
const arch = wantedArchitecture.substr(-2) === '64' ? '64' : '32';

return `win${arch}.zip`;
}
};

module.exports = {
detectBrowserPlatformCustom,
Expand Down
52 changes: 26 additions & 26 deletions lib/processKiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const fkill = require('fkill');
const findProcess = require('find-process');
const { command } = require('execa');

function getConfigProcessesName(drivers) {
const getConfigProcessesName = (drivers) => {
const driversName = Object.keys(drivers);
const processesName = [];

if (driversName && driversName.length) {
if (driversName.length) {
for (const driverName of driversName) {
if (driverName === 'chrome') {
processesName.push('chromedriver');
Expand All @@ -22,28 +22,9 @@ function getConfigProcessesName(drivers) {
}
}
return processesName;
}

async function processKiller(drivers, portValue) {
if (portValue) {
if (!Number.isNaN(Number(`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : `${portValue}`))) {
const portCast = `${portValue}`.startsWith(':') ? portValue : `:${portValue}`;

await killProcessByFkill([portCast]);
await killProcessByCmd([`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : portValue], 'port');
}
}
if (drivers && typeof drivers === 'object' && Object.keys(drivers).length) {
await killProcess(getConfigProcessesName(drivers), 'name');
}
}

async function killProcess(processesNameArr, type) {
await killProcessByFkill(processesNameArr);
await killProcessByCmd(processesNameArr, type);
}
};

async function killProcessByCmd(processes, type) {
const killProcessByCmd = async (processes, type) => {
if (processes && processes.length) {
for (const processSingle of processes) {
const results = await findProcess(type, processSingle, true);
Expand All @@ -67,9 +48,9 @@ async function killProcessByCmd(processes, type) {
}
}
}
}
};

async function killProcessByFkill(processes) {
const killProcessByFkill = async (processes) => {
for (const process of processes) {
try {
await fkill([process], { force: true, tree: true, ignoreCase: true });
Expand All @@ -79,7 +60,26 @@ async function killProcessByFkill(processes) {
// eslint-disable-next-line no-empty
}
}
}
};

const killProcess = async (processesNameArr, type) => {
await killProcessByFkill(processesNameArr);
await killProcessByCmd(processesNameArr, type);
};

const processKiller = async (drivers, portValue) => {
if (portValue) {
if (!Number.isNaN(Number(`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : `${portValue}`))) {
const portCast = `${portValue}`.startsWith(':') ? portValue : `:${portValue}`;

await killProcessByFkill([portCast]);
await killProcessByCmd([`${portValue}`.startsWith(':') ? `${portValue}`.substring(1) : portValue], 'port');
}
}
if (drivers && typeof drivers === 'object' && Object.keys(drivers).length) {
await killProcess(getConfigProcessesName(drivers), 'name');
}
};

module.exports = {
processKiller,
Expand Down
Loading