Skip to content

Commit

Permalink
feat(local): use the local version of webdriver-tool if it is installed
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
cnishina committed Mar 25, 2016
1 parent 27d3fca commit 8c47291
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
28 changes: 27 additions & 1 deletion bin/webdriver-tool
Original file line number Diff line number Diff line change
@@ -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'));
}
27 changes: 16 additions & 11 deletions lib/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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'));
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions lib/cli/util.ts
Original file line number Diff line number Diff line change
@@ -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 {}
20 changes: 15 additions & 5 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as path from 'path';

/**
Expand Down Expand Up @@ -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;
}
}
}
1 change: 0 additions & 1 deletion lib/webdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {Config} from './config';

let commandline = new Cli()
.usage('webdriver-tool <command> [options]')
.setVersion(Config.version())
.program(clean.program)
.program(start.program)
.program(status.program)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 8c47291

Please sign in to comment.