diff --git a/lib/binaries/android_sdk.ts b/lib/binaries/android_sdk.ts index 75a1532d..4032aed7 100644 --- a/lib/binaries/android_sdk.ts +++ b/lib/binaries/android_sdk.ts @@ -1,4 +1,3 @@ -import {arch, type} from 'os'; import * as path from 'path'; import * as rimraf from 'rimraf'; diff --git a/lib/binaries/appium.ts b/lib/binaries/appium.ts index 3b1df2ec..7952b064 100644 --- a/lib/binaries/appium.ts +++ b/lib/binaries/appium.ts @@ -1,4 +1,3 @@ -import {arch, type} from 'os'; import * as path from 'path'; import * as rimraf from 'rimraf'; diff --git a/lib/binaries/chrome_driver.ts b/lib/binaries/chrome_driver.ts index 645ed880..dccc2e4d 100644 --- a/lib/binaries/chrome_driver.ts +++ b/lib/binaries/chrome_driver.ts @@ -1,4 +1,3 @@ -import {arch, type} from 'os'; import * as semver from 'semver'; import {Config} from '../config'; diff --git a/lib/binaries/ie_driver.ts b/lib/binaries/ie_driver.ts index 587c8527..e34e1169 100644 --- a/lib/binaries/ie_driver.ts +++ b/lib/binaries/ie_driver.ts @@ -1,5 +1,3 @@ -import * as os from 'os'; - import {Config} from '../config'; import {Binary, OS} from './binary'; @@ -22,7 +20,7 @@ export class IEDriver extends Binary { this.versionCustom = IEDriver.versionDefault; this.prefixDefault = 'IEDriverServer'; this.suffixDefault = '.zip'; - this.arch = os.arch(); + this.arch = Config.osArch(); } id(): string { @@ -34,7 +32,7 @@ export class IEDriver extends Binary { } version(): string { - if (os.type() == 'Windows_NT') { + if (Config.osType() == 'Windows_NT') { if (this.arch == 'x64') { return '_x64_' + this.versionCustom; } else { diff --git a/lib/cmds/initialize.ts b/lib/cmds/initialize.ts index 751eb857..66006060 100644 --- a/lib/cmds/initialize.ts +++ b/lib/cmds/initialize.ts @@ -2,11 +2,11 @@ import {ChildProcess, spawnSync} from 'child_process'; import * as fs from 'fs'; import * as glob from 'glob'; import * as ini from 'ini'; -import * as os from 'os'; import * as path from 'path'; import * as q from 'q'; import {Logger} from '../cli'; +import {Config} from '../config'; import {spawn} from '../utils'; @@ -75,11 +75,11 @@ function downloadAndroidUpdates( function setupHardwareAcceleration(sdkPath: string) { // TODO(sjelin): linux setup let toolDir = path.resolve(sdkPath, 'extras', 'intel', 'Hardware_Accelerated_Execution_Manager'); - if (os.type() == 'Darwin') { + if (Config.osType() == 'Darwin') { console.log('Enabling hardware acceleration (requires root access)'); // We don't need the wrapped spawnSync because we know we're on OSX spawnSync('sudo', ['silent_install.sh'], {stdio: 'inherit', cwd: toolDir}); - } else if (os.type() == 'Windows_NT') { + } else if (Config.osType() == 'Windows_NT') { console.log('Enabling hardware acceleration (requires admin access)'); // We don't need the wrapped spawnSync because we know we're on windows spawnSync('silent_install.bat', [], {stdio: 'inherit', cwd: toolDir}); @@ -231,7 +231,7 @@ export function android( verbose: boolean): void { let avdDescriptors: AVDDescriptor[]; let tools = ['platform-tool', 'tool']; - if ((os.type() == 'Darwin') || (os.type() == 'Windows_NT')) { + if ((Config.osType() == 'Darwin') || (Config.osType() == 'Windows_NT')) { tools.push('extra-intel-Hardware_Accelerated_Execution_Manager'); } @@ -279,7 +279,7 @@ export function android( }; export function iOS(logger: Logger) { - if (os.type() != 'Darwin') { + if (Config.osType() != 'Darwin') { throw new Error('Must be on a Mac to simulate iOS devices.'); } try { diff --git a/lib/cmds/start.ts b/lib/cmds/start.ts index b0b87501..a9d15fa1 100644 --- a/lib/cmds/start.ts +++ b/lib/cmds/start.ts @@ -2,7 +2,6 @@ import {ChildProcess} from 'child_process'; import * as fs from 'fs'; import * as http from 'http'; import * as minimist from 'minimist'; -import * as os from 'os'; import * as path from 'path'; import * as q from 'q'; @@ -40,11 +39,11 @@ let prog = new Program() .addOption(Opts[Opt.QUIET]) .addOption(Opts[Opt.DETACH]); -if (os.type() === 'Darwin') { +if (Config.osType() === 'Darwin') { prog.addOption(Opts[Opt.IOS]); } -if (os.type() === 'Windows_NT') { +if (Config.osType() === 'Windows_NT') { prog.addOption(Opts[Opt.VERSIONS_IE]) .addOption(Opts[Opt.IE32]) .addOption(Opts[Opt.IE]) @@ -73,7 +72,7 @@ function start(options: Options) { return detachedRun(options); } - let osType = os.type(); + let osType = Config.osType(); let stdio = options[Opt.QUIET].getBoolean() ? 'pipe' : 'inherit'; let binaries = FileManager.setupBinaries(); let seleniumPort = options[Opt.SELENIUM_PORT].getString(); @@ -230,7 +229,7 @@ function start(options: Options) { function startAndroid( outputDir: string, sdk: Binary, avds: string[], useSnapshots: boolean, port: number, stdio: string): void { - let sdkPath = path.resolve(outputDir, sdk.executableFilename(os.type())); + let sdkPath = path.resolve(outputDir, sdk.executableFilename(Config.osType())); if (avds[0] == 'all') { avds = require(path.resolve(sdkPath, 'available_avds.json')); } else if (avds[0] == 'none') { @@ -279,7 +278,8 @@ function startAppium( outputDir: string, binary: Binary, androidSDK: Binary, port: string, stdio: string) { logger.info('Starting appium server'); if (androidSDK) { - process.env.ANDROID_HOME = path.resolve(outputDir, androidSDK.executableFilename(os.type())); + process.env.ANDROID_HOME = + path.resolve(outputDir, androidSDK.executableFilename(Config.osType())); } appiumProcess = spawn( 'npm', ['run', 'appium'].concat(port ? ['--', '--port', port] : []), stdio, @@ -351,7 +351,7 @@ function signalWhenReady( return q.Promise((resolve, reject) => { let child = spawn( path.resolve( - outputDir, androidSDK.executableFilename(os.type()), 'platform-tools', 'adb'), + outputDir, androidSDK.executableFilename(Config.osType()), 'platform-tools', 'adb'), ['-s', 'emulator-' + port, 'wait-for-device'], 'ignore'); let done = false; child.on('error', (err: Error) => { diff --git a/lib/cmds/update.ts b/lib/cmds/update.ts index b86e8a01..33122dcc 100644 --- a/lib/cmds/update.ts +++ b/lib/cmds/update.ts @@ -2,7 +2,6 @@ import * as AdmZip from 'adm-zip'; import * as child_process from 'child_process'; import * as fs from 'fs'; import * as minimist from 'minimist'; -import * as os from 'os'; import * as path from 'path'; import * as q from 'q'; import * as rimraf from 'rimraf'; @@ -35,11 +34,11 @@ let prog = new Program() .addOption(Opts[Opt.ANDROID_PLATFORMS]) .addOption(Opts[Opt.ANDROID_ACCEPT_LICENSES]); -if (os.type() === 'Darwin') { +if (Config.osType() === 'Darwin') { prog.addOption(Opts[Opt.IOS]); } -if (os.type() === 'Windows_NT') { +if (Config.osType() === 'Windows_NT') { prog.addOption(Opts[Opt.IE]).addOption(Opts[Opt.IE32]); } @@ -49,7 +48,7 @@ prog.addOption(Opts[Opt.VERSIONS_STANDALONE]) .addOption(Opts[Opt.VERSIONS_ANDROID]) .addOption(Opts[Opt.VERSIONS_GECKO]); -if (os.type() === 'Windows_NT') { +if (Config.osType() === 'Windows_NT') { prog.addOption(Opts[Opt.VERSIONS_IE]); } export let program = prog; @@ -125,7 +124,7 @@ function update(options: Options): void { } else { logger.info( binary.name + ': file exists ' + - path.resolve(outputDir, binary.filename(os.type(), os.arch()))); + path.resolve(outputDir, binary.filename(Config.osType(), Config.osArch()))); logger.info(binary.name + ': v' + binary.versionCustom + ' up to date'); } }); @@ -140,7 +139,7 @@ function update(options: Options): void { } if (ie) { let binary = binaries[IEDriver.id]; - binary.arch = os.arch(); // Win32 or x64 + binary.arch = Config.osArch(); // Win32 or x64 updateBinary(binary, outputDir, proxy, ignoreSSL).done(); } if (ie32) { @@ -150,7 +149,7 @@ function update(options: Options): void { } if (android) { let binary = binaries[AndroidSDK.id]; - let sdk_path = path.resolve(outputDir, binary.executableFilename(os.type())); + let sdk_path = path.resolve(outputDir, binary.executableFilename(Config.osType())); let oldAVDList: string; q.nfcall(fs.readFile, path.resolve(sdk_path, 'available_avds.json')) @@ -166,8 +165,8 @@ function update(options: Options): void { }) .then(() => { initializeAndroid( - path.resolve(outputDir, binary.executableFilename(os.type())), android_api_levels, - android_architectures, android_platforms, android_accept_licenses, + path.resolve(outputDir, binary.executableFilename(Config.osType())), + android_api_levels, android_architectures, android_platforms, android_accept_licenses, binaries[AndroidSDK.id].versionCustom, JSON.parse(oldAVDList), logger, verbose); }) .done(); @@ -195,8 +194,8 @@ function updateBinary( } else { logger.info( binary.name + ': file exists ' + - path.resolve(outputDir, binary.filename(os.type(), os.arch()))); - let fileName = binary.filename(os.type(), os.arch()); + path.resolve(outputDir, binary.filename(Config.osType(), Config.osArch()))); + let fileName = binary.filename(Config.osType(), Config.osArch()); unzip(binary, outputDir, fileName); logger.info(binary.name + ': v' + binary.versionCustom + ' up to date'); } @@ -205,7 +204,7 @@ function updateBinary( function unzip(binary: T, outputDir: string, fileName: string): void { // remove the previously saved file and unzip it - let osType = os.type(); + let osType = Config.osType(); let mv = path.resolve(outputDir, binary.executableFilename(osType)); try { fs.unlinkSync(mv); diff --git a/lib/config.ts b/lib/config.ts index dcba0a9f..30372ef8 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as os from 'os'; import * as path from 'path'; import {Logger} from './cli'; @@ -29,8 +30,6 @@ export class Config { static nodeModuleName = 'webdriver-manager'; static cwd = process.cwd(); - - static localInstall: string; static parentPath = path.resolve(Config.cwd, '..'); static dir = __dirname; @@ -39,6 +38,32 @@ export class Config { static isProjectVersion = Config.folder === Config.nodeModuleName; static isLocalVersion = false; + static osArch_ = os.arch(); + static osType_ = os.type(); + static noProxy_ = process.env.NO_PROXY || process.env.no_proxy; + static httpsProxy_ = process.env.HTTPS_PROXY || process.env.https_proxy; + static httpProxy_ = process.env.HTTP_PROXY || process.env.http_proxy; + + static osArch(): string { + return Config.osArch_; + } + + static osType(): string { + return Config.osType_; + } + + static noProxy(): string { + return Config.noProxy_; + } + + static httpProxy(): string { + return Config.httpProxy_; + } + + static httpsProxy(): string { + return Config.httpsProxy_; + } + static getConfigFile_(): string { return path.resolve(Config.dir, '..', Config.configFile); } diff --git a/lib/files/downloader.ts b/lib/files/downloader.ts index f80444fd..1637e652 100644 --- a/lib/files/downloader.ts +++ b/lib/files/downloader.ts @@ -1,6 +1,5 @@ import * as fs from 'fs'; import * as http from 'http'; -import * as os from 'os'; import * as path from 'path'; import * as q from 'q'; import * as request from 'request'; @@ -8,6 +7,7 @@ import * as url from 'url'; import {Binary} from '../binaries/binary'; import {Logger} from '../cli'; +import {Config} from '../config'; let logger = new Logger('downloader'); @@ -27,13 +27,13 @@ export class Downloader { binary: Binary, outputDir: string, opt_proxy?: string, opt_ignoreSSL?: boolean, opt_callback?: Function): void { logger.info(binary.name + ': downloading version ' + binary.version()); - var url = binary.url(os.type(), os.arch()); + var url = binary.url(Config.osType(), Config.osArch()); if (!url) { logger.error(binary.name + ' v' + binary.version() + ' is not available for your system.'); return; } Downloader.httpGetFile_( - url, binary.filename(os.type(), os.arch()), outputDir, opt_proxy, opt_ignoreSSL, + url, binary.filename(Config.osType(), Config.osArch()), outputDir, opt_proxy, opt_ignoreSSL, (filePath: string) => { if (opt_callback) { opt_callback(binary, outputDir, filePath); @@ -57,7 +57,7 @@ export class Downloader { // If the NO_PROXY environment variable exists and matches the host name, // to ignore the resolve proxy. // the checks to see if it exists and equal to empty string is to help with testing - let noProxy: string = process.env.NO_PROXY || process.env.no_proxy; + let noProxy: string = Config.noProxy(); if (noProxy) { // array of hostnames/domain names listed in the NO_PROXY environment variable let noProxyTokens = noProxy.split(','); @@ -72,13 +72,12 @@ export class Downloader { // If the HTTPS_PROXY and HTTP_PROXY environment variable is set, use that as the proxy if (protocol === 'https:') { - return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || - process.env.http_proxy; + return Config.httpsProxy() || Config.httpProxy(); } else if (protocol === 'http:') { - return process.env.HTTP_PROXY || process.env.http_proxy; + return Config.httpProxy(); } } - return null; + return undefined; } static httpHeadContentLength(fileUrl: string, opt_proxy?: string, opt_ignoreSSL?: boolean): diff --git a/lib/files/file_manager.ts b/lib/files/file_manager.ts index 17455ee8..a6e123e5 100644 --- a/lib/files/file_manager.ts +++ b/lib/files/file_manager.ts @@ -1,11 +1,11 @@ import * as fs from 'fs'; -import * as os from 'os'; import * as path from 'path'; import * as q from 'q'; import * as rimraf from 'rimraf'; import {Binary, BinaryMap, ChromeDriver, IEDriver, AndroidSDK, Appium, StandAlone, OS} from '../binaries'; +import {Config} from '../config'; import {DownloadedBinary} from './downloaded_binary'; import {Downloader} from './downloader'; import {Logger} from '../cli'; @@ -85,7 +85,7 @@ export class FileManager { * @returns A binary map that is available for the operating system. */ static setupBinaries(alternateCDN?: string): BinaryMap { - return FileManager.compileBinaries_(os.type(), alternateCDN); + return FileManager.compileBinaries_(Config.osType(), alternateCDN); } /** @@ -146,8 +146,8 @@ export class FileManager { * @returns An dictionary map of all the downloaded binaries found in the output folder. */ static downloadedBinaries(outputDir: string): BinaryMap { - let ostype = os.type(); - let arch = os.arch(); + let ostype = Config.osType(); + let arch = Config.osArch(); let binaries = FileManager.setupBinaries(); let existingFiles = FileManager.getExistingFiles(outputDir); let downloaded: BinaryMap = {}; @@ -168,8 +168,8 @@ export class FileManager { */ static toDownload( binary: T, outputDir: string, proxy: string, ignoreSSL: boolean): q.Promise { - let osType = os.type(); - let osArch = os.arch(); + let osType = Config.osType(); + let osArch = Config.osArch(); let filePath: string; let readData: Buffer; let deferred = q.defer(); diff --git a/lib/utils.ts b/lib/utils.ts index 6dc0da8e..9675d129 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,6 +1,7 @@ import * as child_process from 'child_process'; import * as fs from 'fs'; -import * as os from 'os'; + +import {Config} from './config'; function spawnFactory(sync: false): @@ -15,7 +16,7 @@ function spawnFactory(sync: boolean): child_process.ChildProcess | child_process.SpawnSyncReturns { return (cmd: string, args: string[], stdio?: string, opts?: child_process.SpawnOptions | child_process.SpawnSyncOptions) => { - if ((os.type() === 'Windows_NT') && (cmd.slice(-4) !== '.exe')) { + if ((Config.osType() === 'Windows_NT') && (cmd.slice(-4) !== '.exe')) { if (fs.existsSync(cmd + '.exe')) { cmd += '.exe'; } else { diff --git a/spec/files/downloader_spec.ts b/spec/files/downloader_spec.ts index 53d67587..9523c36c 100644 --- a/spec/files/downloader_spec.ts +++ b/spec/files/downloader_spec.ts @@ -1,3 +1,4 @@ +import {Config} from '../../lib/config'; import {Downloader} from '../../lib/files'; describe('downloader', () => { @@ -21,9 +22,9 @@ describe('downloader', () => { }); it('should always return the proxy arg with env var set', () => { - process.env.HTTP_PROXY = envHttpProxy; - process.env.HTTPS_PROXY = envHttpsProxy; - process.env.NO_PROXY = envNoProxy; + Config.httpProxy_ = envHttpProxy; + Config.httpsProxy_ = envHttpsProxy; + Config.noProxy_ = envNoProxy; let proxy = Downloader.resolveProxy_(fileUrlHttp, opt_proxy); expect(proxy).toBe(opt_proxy); }); @@ -31,64 +32,42 @@ describe('downloader', () => { describe('environment variables', () => { beforeEach(() => { - delete process.env.HTTP_PROXY; - delete process.env.http_proxy; - delete process.env.HTTPS_PROXY; - delete process.env.https_proxy; - delete process.env.NO_PROXY; - delete process.env.no_proxy; + Config.httpProxy_ = undefined; + Config.httpsProxy_ = undefined; + Config.noProxy_ = undefined; }); it('should return the HTTP env variable', () => { - process.env.HTTP_PROXY = envHttpProxy; - let proxy = Downloader.resolveProxy_(fileUrlHttp); - expect(proxy).toBe(envHttpProxy); - }); - - it('should return the http env variable', () => { - process.env.http_proxy = envHttpProxy; + Config.httpProxy_ = envHttpProxy; let proxy = Downloader.resolveProxy_(fileUrlHttp); expect(proxy).toBe(envHttpProxy); }); it('should return the HTTPS env variable for https protocol', () => { - process.env.HTTPS_PROXY = envHttpsProxy; - let proxy = Downloader.resolveProxy_(fileUrlHttps); - expect(proxy).toBe(envHttpsProxy); - }); - - it('should return the https env variable for https protocol', () => { - process.env.https_proxy = envHttpsProxy; + Config.httpProxy_ = envHttpsProxy; let proxy = Downloader.resolveProxy_(fileUrlHttps); expect(proxy).toBe(envHttpsProxy); }); it('should return the HTTP env variable for https protocol', () => { - process.env.HTTP_PROXY = envHttpProxy; - let proxy = Downloader.resolveProxy_(fileUrlHttps); - expect(proxy).toBe(envHttpProxy); - }); - - it('should return the https env variable for https protocol', () => { - process.env.http_proxy = envHttpProxy; + Config.httpProxy_ = envHttpProxy; let proxy = Downloader.resolveProxy_(fileUrlHttps); expect(proxy).toBe(envHttpProxy); }); describe('NO_PROXY environment variable', () => { beforeEach(() => { - delete process.env.NO_PROXY; - delete process.env.no_proxy; + Config.noProxy_ = undefined; }); - it('should return null when the NO_PROXY matches the fileUrl', () => { - process.env.NO_PROXY = envNoProxy; + it('should return undefined when the NO_PROXY matches the fileUrl', () => { + Config.noProxy_ = envNoProxy; let proxy = Downloader.resolveProxy_(fileUrlHttp); expect(proxy).toBeUndefined(); }); - it('should return null when the no_proxy matches the fileUrl', () => { - process.env.no_proxy = envNoProxy; + it('should return undefined when the no_proxy matches the fileUrl', () => { + Config.noProxy_ = envNoProxy; let proxy = Downloader.resolveProxy_(fileUrlHttp); expect(proxy).toBeUndefined(); });