From fd0fbdcaf251bf11fca932abc61d0fc6a842e6e5 Mon Sep 17 00:00:00 2001 From: Matthias ETIENNE Date: Sun, 26 Feb 2017 15:03:51 +0100 Subject: [PATCH] feat: Node.js 4.4.5+ compatibility Make Caporal compatible with node.js 4.4.5+ --- lib/autocomplete.js | 10 ++++------ lib/command.js | 2 +- lib/program.js | 4 ++-- lib/validator.js | 2 +- package.json | 2 +- tests/verbosity-quiet.js | 5 ++++- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/autocomplete.js b/lib/autocomplete.js index b30ac0c..6a5b13e 100644 --- a/lib/autocomplete.js +++ b/lib/autocomplete.js @@ -47,9 +47,9 @@ class Autocomplete { while(!cmd && args.length) { commandArr.push(args.shift()); const cmdStr = commandArr.join(' '); - [cmd] = commands.filter(c => { + cmd = commands.filter(c => { return (c.name() === cmdStr || c.getAlias() === cmdStr) - }); + })[0]; } if (!cmd && this._program._getDefaultCommand()) { @@ -123,8 +123,8 @@ class Autocomplete { if ((o.getShortName() && o.getShortName() != lastPartial && o.getShortName().startsWith(lastPartial)) || (o.getLongName() && o.getLongName() != lastPartial && o.getLongName().startsWith(lastPartial)) ) { return o.getLongOrShortName() + ':' + o.description(); - } else if(!lastPartialIsOption && !optionsAlreadyUsed.includes(o.getShortName()) && - !optionsAlreadyUsed.includes(o.getLongName())) { + } else if(!lastPartialIsOption && optionsAlreadyUsed.indexOf(o.getShortName()) === -1 && + optionsAlreadyUsed.indexOf(o.getLongName()) === -1) { return o.getLongOrShortName() + ':' + o.description(); } }).filter(o => typeof o != 'undefined') : []; @@ -196,8 +196,6 @@ class Autocomplete { const completions = [] .concat.apply([], results) .filter(e => typeof e != 'undefined'); - console.log("results", results); - console.log("completions", completions); done(null, completions); return completions; }) diff --git a/lib/command.js b/lib/command.js index 74e6d90..44909bb 100644 --- a/lib/command.js +++ b/lib/command.js @@ -267,7 +267,7 @@ class Command extends GetterSetter { */ _validateOptions(options) { return Object.keys(options).reduce((acc, key) => { - if (Command.NATIVE_OPTIONS.includes(key)) { + if (Command.NATIVE_OPTIONS.indexOf(key) !== -1) { return acc; } const value = acc[key]; diff --git a/lib/program.js b/lib/program.js index 0037d40..df93245 100644 --- a/lib/program.js +++ b/lib/program.js @@ -30,7 +30,7 @@ class Program extends GetterSetter { * @private */ help(cmdStr) { - const [cmd] = this._commands.filter(c => (c.name() === cmdStr || c.getAlias() === cmdStr)); + const cmd = this._commands.filter(c => (c.name() === cmdStr || c.getAlias() === cmdStr))[0]; this._help.display(cmd); } @@ -98,7 +98,7 @@ class Program extends GetterSetter { while(!cmd && argsCopy.length) { commandArr.push(argsCopy.shift()); const cmdStr = commandArr.join(' '); - [cmd] = this._commands.filter(c => (c.name() === cmdStr || c.getAlias() === cmdStr)); + cmd = this._commands.filter(c => (c.name() === cmdStr || c.getAlias() === cmdStr))[0]; } if (options.V || options.version) { diff --git a/lib/validator.js b/lib/validator.js index f895eeb..83d73bc 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -147,7 +147,7 @@ class Validator { * @private */ _validateWithArray(value) { - if (!this._validator.map(String).includes(value)) { + if (this._validator.map(String).indexOf(value) === -1) { throw new ValidationError( "Array validation failed", {validator: this._validator, value}, diff --git a/package.json b/package.json index 1337207..24b3bf6 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "postinstall": "(test -f ./node_modules/husky/bin/install.js && node ./node_modules/husky/bin/install.js) || exit 0" }, "engines": { - "node": ">= 6.0.0" + "node": ">= 4.4.5" }, "keywords": [ "cli", diff --git a/tests/verbosity-quiet.js b/tests/verbosity-quiet.js index b67b276..558f999 100644 --- a/tests/verbosity-quiet.js +++ b/tests/verbosity-quiet.js @@ -19,7 +19,10 @@ describe('Passing --quiet', () => { it(`should only output warnings & errors`, (done) => { let output = 0; - const listener = (out, level, txt) => output++; + const listener = function(out, level, txt) { + output++ + }; + logger.on('logging', listener); program.parse(makeArgv(['foo', '--quiet']));