Skip to content

Commit

Permalink
fix(npm): use global, local, and project without env
Browse files Browse the repository at this point in the history
closes #20
  • Loading branch information
cnishina committed Apr 6, 2016
1 parent 744424a commit a073fd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 49 deletions.
54 changes: 23 additions & 31 deletions bin/webdriver-tool
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,31 @@

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;

var globalPath = path.resolve(globalNpm, '../lib/node_modules/webdriver-tool/built/lib/');
var localPath = path.resolve(__dirname, '../../../node_modules/webdriver-tool/built/lib/');

// check to see if this is webdriver-tool (the project), if it is, run this
var projectPath = path.resolve('.');
var parentPath = path.resolve(projectPath, '..');
var folder = projectPath.replace(parentPath + '/', '');
var cwd = process.cwd();
var dir = __dirname;
var localInstall = path.resolve(cwd, 'node_modules/webdriver-tool/');
var parentPath = path.resolve(cwd, '..');
var folder = cwd.replace(parentPath, '').substring(1);

// Project version.
if (folder === 'webdriver-tool') {
Logger.info('webdriver-tool: using ' + chalk.magenta('project version ' + Config.projectVersion()));
// compare project version to global
if (Config.globalVersion() && Config.projectVersion() !== Config.globalVersion()) {
Logger.info(chalk.yellow('warning: project version [' + Config.projectVersion() +
'] mismatch with global version [' + Config.globalVersion() + ']\n'));
}
require(path.resolve(projectPath, 'built/lib/webdriver.js'));
var chalk = require('chalk');
console.log('Webdriver Tool: using ' + chalk.magenta('project version ' +
require(path.resolve('package.json')).version));
require(path.resolve('built/lib/webdriver.js'));
}
// Local version.
else if (fs.statSync(path.resolve(localInstall)).isDirectory()) {
var chalk = require(path.resolve(localInstall, 'node_modules/chalk/index.js'));
console.log('Webdriver Tool: using ' + chalk.cyan('local installed version ' +
require(path.resolve(localInstall, 'package.json')).version));
require(path.resolve(localInstall, 'built/lib/webdriver.js'));
}
else if (localPath === globalPath) {
// we are using the global version
Logger.info('webdriver-tool: using ' + chalk.cyan('global version ' + Config.globalVersion()));
require(path.resolve(globalPath, 'webdriver.js'));
} else {
Logger.info('webdriver-tool: using ' + chalk.green('local version ' + Config.localVersion()));
if (Config.globalVersion() && Config.localVersion() !== Config.globalVersion()) {
Logger.warn('warning: local version [' + Config.localVersion() +
'] mismatch with global version [' + Config.globalVersion() + ']\n');
}
require(path.resolve(localPath, 'webdriver.js'));
// Global version.
else {
var chalk = require(path.resolve(dir, '../node_modules/chalk/index.js'));
console.log('Webdriver Tool: using ' + chalk.green('global installed version ',
require(path.resolve(dir, '../package.json')).version));
require(path.resolve(dir, '../built/lib/webdriver.js'));
}
30 changes: 12 additions & 18 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,13 @@ export class Config {
* If installed as a node module, return the local version.
*/
static localVersion(): string {
let packageJson = require(Config.packagePath);
return packageJson.version;
}

/**
* If running from the project directory, get the project's version.
*/
static projectVersion(): string {
let projectPath = path.resolve('.');
let projectPackagePath = path.resolve(projectPath, 'package.json');
var cwd = process.cwd();
var localInstall = path.resolve(cwd, 'node_modules/webdriver-tool/');
try {
if (fs.statSync(projectPackagePath).isFile()) {
let projectJson = require(projectPackagePath);
return projectJson.version;
if (fs.statSync(localInstall).isDirectory()) {
return require(path.resolve(localInstall, 'package.json')).version;
} else {
return null;
}
} catch(err) {
return null;
Expand All @@ -73,12 +66,13 @@ export class Config {
* If installed, returns the globally installed webdriver-tool.
*/
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');
var dir = __dirname;
let globalPackageJson = path.resolve(dir, '../package.json');
try {
if (fs.statSync(globalPackagePath).isFile()) {
let globalPackageJson = require(globalPackagePath);
return globalPackageJson.version;
if (fs.statSync(globalPackageJson).isFile()) {
return require(globalPackageJson).version;
} else {
return null;
}
} catch(err) {
return null;
Expand Down

0 comments on commit a073fd0

Please sign in to comment.