diff --git a/lib/install-utils.js b/lib/install-utils.js index 36e34699..c0634c02 100644 --- a/lib/install-utils.js +++ b/lib/install-utils.js @@ -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]) { @@ -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) => { @@ -62,7 +58,7 @@ const chmod = (where) => }); }); -async function setDriverFilePermissions(where) { +const setDriverFilePermissions = async (where) => { debug('setDriverFilePermissions', where); const requireChmod = await new Promise((resolve) => @@ -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; } @@ -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) => @@ -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')); @@ -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 @@ -228,7 +224,7 @@ async function runInstaller(installerFile, from, to) { runner.on('error', (errRunner) => reject(logError('runInstaller:runner', errRunner))); }); -} +}; module.exports = { isBrowserDriver, diff --git a/lib/install.js b/lib/install.js index e3bf8809..1fc5294b 100644 --- a/lib/install.js +++ b/lib/install.js @@ -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'); @@ -321,3 +319,5 @@ async function install(_opts) { function isDownloadActive() { return !Array.from(downloadStreams.values()).includes(true); } + +module.exports = install; diff --git a/lib/platformDetection.js b/lib/platformDetection.js index bd11cf56..1145b5ab 100644 --- a/lib/platformDetection.js +++ b/lib/platformDetection.js @@ -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); @@ -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'; @@ -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') { @@ -78,9 +79,9 @@ function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) { } return platform; -} +}; -function getChromeDriverArchitectureOld(wantedArchitecture, version) { +const getChromeDriverArchitectureOld = (wantedArchitecture, version) => { let platform; if (process.platform === 'linux') { @@ -99,9 +100,9 @@ function getChromeDriverArchitectureOld(wantedArchitecture, version) { } return platform; -} +}; -function getIeDriverArchitectureOld(wanted) { +const getIeDriverArchitectureOld = (wanted) => { let platform; if (wanted === 'ia32') { @@ -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) { @@ -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, diff --git a/lib/processKiller.js b/lib/processKiller.js index 9a0eabad..3941a9c4 100644 --- a/lib/processKiller.js +++ b/lib/processKiller.js @@ -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'); @@ -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); @@ -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 }); @@ -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, diff --git a/lib/start.js b/lib/start.js index dec5d2cc..127b41aa 100644 --- a/lib/start.js +++ b/lib/start.js @@ -1,5 +1,3 @@ -module.exports = start; - const debug = require('debug')('selenium-standalone:start'); const mapValues = require('lodash.mapvalues'); const merge = require('lodash.merge'); @@ -15,9 +13,16 @@ const defaultConfig = require('./default-config')(); const { checkArgs } = require('./check-args'); const { isSelenium4 } = require('./isSelenium4'); const noop = require('./noop'); -const { processKiller } = require('./processKiller.js'); +const { processKiller } = require('./processKiller'); -async function start(_opts) { +/** + * @param {string[]} list params list + * @param {*} param expected param + * @returns {boolean} + */ +const hasParam = (list, param) => list.includes(param); + +const start = async (_opts) => { const opts = checkArgs('Start API', _opts); if (!opts.javaArgs) { @@ -145,7 +150,7 @@ async function start(_opts) { args.push(...opts.javaArgs, '-jar', fsPaths.selenium.installPath, ...opts.seleniumArgs); - await checkPathsExistence(Object.keys(fsPaths).map((name) => fsPaths[name].installPath)); + checkPathsExistence(Object.keys(fsPaths).map((name) => fsPaths[name].installPath)); const seleniumStatusUrl = statusUrl.getSeleniumStatusUrl(args, opts); @@ -178,8 +183,6 @@ async function start(_opts) { } return selenium; -} +}; -function hasParam(list, param) { - return list.find((p) => p === param); -} +module.exports = start; diff --git a/lib/validation.js b/lib/validation.js index 143553f2..77abee25 100644 --- a/lib/validation.js +++ b/lib/validation.js @@ -1,15 +1,14 @@ -module.exports = { validateMajorVersionPrefix, getVersionWithZeroedPatchPart }; - -function validateMajorVersionPrefix(possibleMajorPrefix) { +const validateMajorVersionPrefix = (possibleMajorPrefix) => { let prefix; if (possibleMajorPrefix) { prefix = possibleMajorPrefix.match(/^[+-]?([0-9]+)/); } + return prefix && prefix.length > 0 ? prefix[0] : ''; -} +}; -function getVersionWithZeroedPatchPart(fullVersion) { +const getVersionWithZeroedPatchPart = (fullVersion) => { if (!/^\d+\.\d+\.\d+$/i.test(fullVersion)) { // If version longer than just 3 numbers, like '4.0.0-beta-1', do nothing return fullVersion; @@ -17,4 +16,6 @@ function getVersionWithZeroedPatchPart(fullVersion) { // else make version patch part zero: '4.1.1' => '4.1.0' const [major, minor] = fullVersion.split('.'); return `${major}.${minor}.0`; -} +}; + +module.exports = { validateMajorVersionPrefix, getVersionWithZeroedPatchPart };