Skip to content

Commit

Permalink
chore(config): use config to get os type, arch and process.env vars (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina authored and sjelin committed Dec 20, 2016
1 parent 0a4bc91 commit b6f2399
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 85 deletions.
1 change: 0 additions & 1 deletion lib/binaries/android_sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {arch, type} from 'os';
import * as path from 'path';
import * as rimraf from 'rimraf';

Expand Down
1 change: 0 additions & 1 deletion lib/binaries/appium.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {arch, type} from 'os';
import * as path from 'path';
import * as rimraf from 'rimraf';

Expand Down
1 change: 0 additions & 1 deletion lib/binaries/chrome_driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {arch, type} from 'os';
import * as semver from 'semver';

import {Config} from '../config';
Expand Down
6 changes: 2 additions & 4 deletions lib/binaries/ie_driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as os from 'os';

import {Config} from '../config';

import {Binary, OS} from './binary';
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions lib/cmds/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions lib/cmds/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 = <string[]>require(path.resolve(sdkPath, 'available_avds.json'));
} else if (avds[0] == 'none') {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -351,7 +351,7 @@ function signalWhenReady(
return q.Promise<void>((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) => {
Expand Down
23 changes: 11 additions & 12 deletions lib/cmds/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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');
}
});
Expand All @@ -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) {
Expand All @@ -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'))
Expand All @@ -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();
Expand Down Expand Up @@ -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');
}
Expand All @@ -205,7 +204,7 @@ function updateBinary(

function unzip<T extends Binary>(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);
Expand Down
29 changes: 27 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';

import {Logger} from './cli';
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
15 changes: 7 additions & 8 deletions lib/files/downloader.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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';
import * as url from 'url';

import {Binary} from '../binaries/binary';
import {Logger} from '../cli';
import {Config} from '../config';

let logger = new Logger('downloader');

Expand All @@ -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);
Expand All @@ -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(',');
Expand All @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions lib/files/file_manager.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -85,7 +85,7 @@ export class FileManager {
* @returns A binary map that is available for the operating system.
*/
static setupBinaries(alternateCDN?: string): BinaryMap<Binary> {
return FileManager.compileBinaries_(os.type(), alternateCDN);
return FileManager.compileBinaries_(Config.osType(), alternateCDN);
}

/**
Expand Down Expand Up @@ -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<DownloadedBinary> {
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<DownloadedBinary> = {};
Expand All @@ -168,8 +168,8 @@ export class FileManager {
*/
static toDownload<T extends Binary>(
binary: T, outputDir: string, proxy: string, ignoreSSL: boolean): q.Promise<boolean> {
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<boolean>();
Expand Down
5 changes: 3 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -15,7 +16,7 @@ function spawnFactory(sync: boolean):
child_process.ChildProcess | child_process.SpawnSyncReturns<any> {
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 {
Expand Down
Loading

0 comments on commit b6f2399

Please sign in to comment.