From c719a59a969ab045ad7ab9876e62e9de315e1d52 Mon Sep 17 00:00:00 2001 From: Ildar Sagdejev Date: Wed, 11 Oct 2017 17:01:58 -0400 Subject: [PATCH] Use safe-commander wrapper to circumvent name collision bug in commander.js The --domain command line option was colliding with a property on the commander object. See: https://github.com/tj/commander.js/issues/404 This fixes the app crashing when --domain is not the last option passed on the command line. --- index.js | 28 ++++++++++++++-------------- package.json | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index bd7e43a..bc9ee18 100755 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var manifest = require('./package.json'), Bitly = require('bitly'), - app = require('commander'), + app = require('safe-commander'), rc = require('rc')(manifest.name), homedir = require('os').homedir(), url = require('url'), @@ -99,7 +99,7 @@ var bitly, action = history, arg0 = app.args[0] -if( app.archive ) { +if( app.optsObj.archive ) { if( arg0 ) { action = archive } else @@ -163,7 +163,7 @@ function applyAltCount( args ) { let arg = args[i], rest = arg.slice( 1 ) if( arg[0] === "-" && /^\d+$/.test( rest ) ) { - app.count = parseInt( rest ) + app.optsObj.count = parseInt( rest ) break } } @@ -192,7 +192,7 @@ function preValidateToken( token ) { function getBitlyToken() { return new Promise( function( resolve, reject ) { - if( app.ask || (!app.key && !preValidateToken( rc.key )) ) { + if( app.optsObj.ask || (!app.optsObj.key && !preValidateToken( rc.key )) ) { print( "Please enter your Bitly access token." ) print( "You can obtain your token from: " + "https://bitly.com/a/oauth_apps".yellow ) print() @@ -201,7 +201,7 @@ function getBitlyToken() { print() if( key !== undefined ) { // Save the key if there isn't one saved already, or if --save option is passed - if( app.save || !rc.key ) + if( app.optsObj.save || !rc.key ) saveConfig( key ) resolve( key ) @@ -209,14 +209,14 @@ function getBitlyToken() { reject() } } ) - } else if( app.key ) { - if( !preValidateToken( app.key ) ) { + } else if( app.optsObj.key ) { + if( !preValidateToken( app.optsObj.key ) ) { reject( 'The authentication token you have provided does not appear to be valid' ) } else { - if( app.save ) - saveConfig( app.key ) + if( app.optsObj.save ) + saveConfig( app.optsObj.key ) - resolve( app.key ) + resolve( app.optsObj.key ) } } else { resolve( rc.key ) @@ -234,7 +234,7 @@ function expandOrShorten( arg ) { if( domains.default.indexOf( u.hostname ) !== -1 || domains.extended.indexOf( u.hostname ) !== -1 ) { expand( arg ) } else { - shorten( uri, app.domain ) + shorten( uri, app.optsObj.domain ) } } else expand( arg ) @@ -286,10 +286,10 @@ function archive( shortUrl ) { function history( offset ) { offset = offset || 0 - let count = Math.min( app.count, BITLY_MAX_HISTORY_CHUNK ) + let count = Math.min( app.optsObj.count, BITLY_MAX_HISTORY_CHUNK ) if( count !== 0 ) { - app.count -= count + app.optsObj.count -= count bitly._doRequest( bitly._generateNiceUrl( { offset, limit: count }, 'user/link_history' ) ).then( res => { printHistory( res.data.link_history ) @@ -310,7 +310,7 @@ function printHistory( link_history ) { ' - ' + item.long_url.red ) // Print additional details (if "--verbose") - if( app.verbose ) { + if( app.optsObj.verbose ) { const INDENT = ' ' if( item.title !== "" ) diff --git a/package.json b/package.json index fba9fac..9457449 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitly-client", - "version": "1.5.4", + "version": "1.5.5", "description": "Use Bitly from the command line", "author": "Ildar Sagdejev (http://specious.github.io)", "license": "ISC", @@ -21,7 +21,7 @@ "dependencies": { "bitly": "^3.0.3", "colors": "^1.1.2", - "commander": "^2.11.0", + "safe-commander": "^2.11.0", "rc": "^1.2.1", "read": "^1.0.7" }