From 8c472918ac73390890bbc39fcc4c7a2e86d3b262 Mon Sep 17 00:00:00 2001 From: Craig Nishina Date: Fri, 25 Mar 2016 16:22:34 -0700 Subject: [PATCH] feat(local): use the local version of webdriver-tool if it is installed closes #5 --- bin/webdriver-tool | 28 +++++++++++++++++++++++++++- lib/cli/cli.ts | 27 ++++++++++++++++----------- lib/cli/util.ts | 19 +++++++++++++++++++ lib/config.ts | 20 +++++++++++++++----- lib/webdriver.ts | 1 - package.json | 3 ++- typings.json | 1 + 7 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 lib/cli/util.ts diff --git a/bin/webdriver-tool b/bin/webdriver-tool index 1ea912e1..58c9252b 100644 --- a/bin/webdriver-tool +++ b/bin/webdriver-tool @@ -1,3 +1,29 @@ #!/usr/bin/env node -require('../built/lib/webdriver.js'); +var path = require('path'); +var fs = require('fs'); +var chalk = require('chalk'); + +var Logger = require('../built/lib/cli/util').Logger; +var Config = require('../built/lib/config').Config; + +var globalNpm = process.env.NPM_BIN || process.env.NVM_BIN; +globalPath = path.resolve(globalNpm, '../lib/node_modules/webdriver-tool/built/lib/'); +localPath = path.resolve(__dirname, '../node_modules/webdriver-tool/built/lib/'); + +try { + if (fs.statSync(localPath).isDirectory()) { + Logger.info('webdriver-tool: using ' + chalk.green('local version ' + Config.localVersion()));; + } + try { + if (fs.statSync(globalPath).isDirectory()) { + Logger.info('webdriver-tool: using ' + chalk.cyan('global version ' + Config.globalVersion())); + } + } catch (err) { } + require(path.resolve(localPath, 'webdriver.js')); +} +// we are using the global version +catch (err) { + Logger.info('webdriver-tool: using ' + chalk.cyan('global version ' + Config.globalVersion())); + require(path.resolve(globalPath, 'webdriver.js')); +} diff --git a/lib/cli/cli.ts b/lib/cli/cli.ts index df1e5f96..a4f13948 100644 --- a/lib/cli/cli.ts +++ b/lib/cli/cli.ts @@ -1,6 +1,12 @@ +import * as chalk from 'chalk'; +import * as path from 'path'; + +import {Config} from '../config'; +import {Logger} from './util'; import {Programs, Program} from './programs'; import {MinimistArgs, Options} from './options'; + /** * The Cli contains the usage and the collection of programs. * @@ -31,15 +37,6 @@ export class Cli { return this; } - - /** - * Register the version for the cli - */ - setVersion(version: string): Cli { - this.version = version; - return this; - } - /** * Prints help for the programs registered to the cli. */ @@ -66,8 +63,16 @@ export class Cli { * Print the version */ printVersion(): void { - if (this.version) { - console.log('Version ' + this.version); + let localVersion = Config.localVersion(); + Logger.info(chalk.green('local version: ' + localVersion)); + + let globalVersion = Config.globalVersion(); + if (globalVersion) { + Logger.info(chalk.cyan('global version: ' + globalVersion)); + + if (globalVersion !== localVersion) { + Logger.info(chalk.yellow('warning version mismatch')); + } } } diff --git a/lib/cli/util.ts b/lib/cli/util.ts new file mode 100644 index 00000000..5c9583fb --- /dev/null +++ b/lib/cli/util.ts @@ -0,0 +1,19 @@ +import * as chalk from 'chalk'; + +export enum LOG_LEVEL { DEBUG, WARN, INFO } + +export class Logger { + static info(message: string, opt_noTimestamp?: boolean): void { + if (!opt_noTimestamp) { + message = Logger.timestamp() + ' ' + message; + } + console.log(message); + } + + static timestamp(): string { + let d = new Date(); + return '[' + chalk.gray(d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds()) + ']'; + } +} + +export class Req {} diff --git a/lib/config.ts b/lib/config.ts index 318bedab..924b1936 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,3 +1,4 @@ +import * as fs from 'fs'; import * as path from 'path'; /** @@ -44,12 +45,21 @@ export class Config { return configCdnUrls; } - /** - * Get the current version of webdriver-manager from the package.json - * @returns The webdriver-manager version. - */ - static version(): string { + static localVersion(): string { let packageJson = require(Config.packagePath); return packageJson.version; } + + static globalVersion(): string { + let globalNpm = process.env.NPM_BIN || process.env.NVM_BIN; + let globalPackagePath = path.resolve(globalNpm, '../lib/node_modules/webdriver-tool/built/package.json'); + try { + if (fs.statSync(globalPackagePath).isFile()) { + let globalPackageJson = require(globalPackagePath); + return globalPackageJson.version; + } + } catch(err) { + return null; + } + } } diff --git a/lib/webdriver.ts b/lib/webdriver.ts index d0e5e6c2..27168efa 100644 --- a/lib/webdriver.ts +++ b/lib/webdriver.ts @@ -11,7 +11,6 @@ import {Config} from './config'; let commandline = new Cli() .usage('webdriver-tool [options]') - .setVersion(Config.version()) .program(clean.program) .program(start.program) .program(status.program) diff --git a/package.json b/package.json index 58f0f754..9891596e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webdriver-tool", - "version": "0.0.3", + "version": "0.0.4", "description": "Webdriver tool to manage selenium standalone server", "scripts": { "prepublish": "gulp prepublish", @@ -31,6 +31,7 @@ "request": "^2.69.0" }, "devDependencies": { + "chalk": "^1.1.1", "clang-format": "^1.0.35", "gulp": "^3.9.1", "gulp-clang-format": "^1.0.23", diff --git a/typings.json b/typings.json index 53b8281c..37d6f8bc 100644 --- a/typings.json +++ b/typings.json @@ -1,6 +1,7 @@ { "ambientDependencies": { "adm-zip": "registry:dt/adm-zip#0.0.0+20160211023517", + "chalk": "registry:dt/chalk#0.4.0+20160317120654", "form-data": "registry:dt/form-data#0.0.0+20150826060748", "jasmine": "registry:dt/jasmine#2.2.0+20160308082659", "minimist": "registry:dt/minimist#1.1.3+20151229171613",