Skip to content
Myr

Replace use of var with const and let (#1161)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by GitHub · parent 30368b8 · Browse files · 8 files+71 added−70 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+71 added−70 removed

  • tests:+3 added−3 removed
  • other:+68 added−67 removed

8 files+71 added−70 removed

modified:examples/deploy+1 added-1 removed
12 unmodified lines
13 .command('setup [env]')14 .description('run setup commands for all envs')15 .option('-s, --setup_mode [mode]', 'Which setup mode to use')16 .action(function(env, options) {17 var mode = options.setup_mode || 'normal';17 const mode = options.setup_mode || 'normal';18 env = env || 'all';19 console.log('setup for %s env(s) with %s mode', env, mode);20 });21
16 unmodified lines
modified:examples/pizza+1 added-1 removed
16 unmodified lines
17if (program.peppers) console.log(' - peppers');18if (program.pineapple) console.log(' - pineapple');19if (program.bbq) console.log(' - bbq');2021var cheese = !program.cheese ? 'no' : program.cheese;21const cheese = !program.cheese ? 'no' : program.cheese;2223console.log(' - %s cheese', cheese);24console.log(program.args);
modified:examples/pm-install+1 added-1 removed
5 unmodified lines
6program7 .option('-f, --force', 'force installation')8 .parse(process.argv);910var pkgs = program.args;10const pkgs = program.args;1112if (!pkgs.length) {13 console.error('packages required');14 process.exit(1);
8 unmodified lines
modified:tests/fixtures/inspect.js+1 added-1 removed
modified:tests/fixtures/pm-cache.js+1 added-1 removed
modified:tests/command.executableSubcommand.signals.test.js+1 added-1 removed
modified:.eslintrc.json+1 added
modified:index.js+64 added-64 removed
1/**2 * Module dependencies.3 */45var EventEmitter = require('events').EventEmitter;6var spawn = require('child_process').spawn;7var path = require('path');8var fs = require('fs');5const EventEmitter = require('events').EventEmitter;6const spawn = require('child_process').spawn;7const path = require('path');8const fs = require('fs');910class Option {11 /**12 * Initialize a new `Option` with the given `flags` and `description`.
136 unmodified lines
149 * @api public150 */151152 command(nameAndArgs, actionOptsOrExecDesc, execOpts) {153 var desc = actionOptsOrExecDesc;154 var opts = execOpts;153 let desc = actionOptsOrExecDesc;154 let opts = execOpts;155 if (typeof desc === 'object' && desc !== null) {156 opts = desc;157 desc = null;158 }159 opts = opts || {};160 var args = nameAndArgs.split(/ +/);161 var cmd = new Command(args.shift());160 const args = nameAndArgs.split(/ +/);161 const cmd = new Command(args.shift());162163 if (desc) {164 cmd.description(desc);165 cmd._executableHandler = true;
107 unmodified lines
273 */274275 _parseExpectedArgs(args) {276 if (!args.length) return;277 var self = this;277 const self = this;278 args.forEach(function(arg) {279 var argDetails = {279 const argDetails = {280 required: false,281 name: '',282 variadic: false283 };
77 unmodified lines
361 * @api public362 */363364 action(fn) {365 var self = this;366 var listener = function(args) {365 const self = this;366 const listener = function(args) {367 // The .action callback takes an extra parameter which is the command or options.368 var expectedArgsCount = self._args.length;369 var actionArgs = args.slice(0, expectedArgsCount);368 const expectedArgsCount = self._args.length;369 const actionArgs = args.slice(0, expectedArgsCount);370 if (self._passCommandToAction) {371 actionArgs[expectedArgsCount] = self;372 } else {373 actionArgs[expectedArgsCount] = self.opts();
27 unmodified lines
401 * @api private402 */403404 _optionEx(config, flags, description, fn, defaultValue) {405 var self = this,406 option = new Option(flags, description),407 oname = option.name(),408 name = option.attributeName();405 const self = this;406 const option = new Option(flags, description);407 const oname = option.name();408 const name = option.attributeName();409 option.mandatory = !!config.mandatory;410411 // default as 3rd arg412 if (typeof fn !== 'function') {413 if (fn instanceof RegExp) {414 // This is a bit simplistic (especially no error messages), and probably better handled by caller using custom option processing.415 // No longer documented in README, but still present for backwards compatibility.416 var regex = fn;416 const regex = fn;417 fn = function(val, def) {418 var m = regex.exec(val);418 const m = regex.exec(val);419 return m ? m[0] : def;420 };421 } else {422 defaultValue = fn;
443 unmodified lines
866 */867868 _checkForMissingMandatoryOptions() {869 // Walk up hierarchy so can call in subcommand after checking for displaying help.870 for (var cmd = this; cmd; cmd = cmd.parent) {870 for (let cmd = this; cmd; cmd = cmd.parent) {871 cmd.options.forEach((anOption) => {872 if (anOption.mandatory && (cmd._getOptionValue(anOption.attributeName()) === undefined)) {873 cmd.missingMandatoryOptionValue(anOption);874 }
107 unmodified lines
982 */983 opts() {984 if (this._storeOptionsAsProperties) {985 // Preserve original behaviour so backwards compatible when still using properties986 var result = {},987 len = this.options.length;986 const result = {};987 const len = this.options.length;988989 for (var i = 0; i < len; i++) {990 var key = this.options[i].attributeName();989 for (let i = 0; i < len; i++) {990 const key = this.options[i].attributeName();991 result[key] = key === this._versionOptionName ? this._version : this[key];992 }993 return result;994 }
105 unmodified lines
1100 if (arguments.length === 0) return this._version;1101 this._version = str;1102 flags = flags || '-V, --version';1103 description = description || 'output the version number';1104 var versionOption = new Option(flags, description);1104 const versionOption = new Option(flags, description);1105 this._versionOptionName = versionOption.long.substr(2) || 'version';1106 this.options.push(versionOption);1107 var self = this;1107 const self = this;1108 this.on('option:' + this._versionOptionName, function() {1109 process.stdout.write(str + '\n');1110 self._exit(0, 'commander.version', str);1111 });
24 unmodified lines
1136 * @api public1137 */11381139 alias(alias) {1140 var command = this;1140 let command = this;1141 if (this.commands.length !== 0) {1142 command = this.commands[this.commands.length - 1];1143 }1144
13 unmodified lines
1158 * @api public1159 */11601161 usage(str) {1162 var args = this._args.map(function(arg) {1162 const args = this._args.map(function(arg) {1163 return humanReadableArgName(arg);1164 });11651166 var usage = '[options]' +1166 const usage = '[options]' +1167 (this.commands.length ? ' [command]' : '') +1168 (this._args.length ? ' ' + args.join(' ') : '');11691170 if (arguments.length === 0) return this._usage || usage;
26 unmodified lines
1197 prepareCommands() {1198 const commandDetails = this.commands.filter(function(cmd) {1199 return !cmd._noHelp;1200 }).map(function(cmd) {1201 var args = cmd._args.map(function(arg) {1201 const args = cmd._args.map(function(arg) {1202 return humanReadableArgName(arg);1203 }).join(' ');12041205 return [
18 unmodified lines
1224 * @api private1225 */12261227 largestCommandLength() {1228 var commands = this.prepareCommands();1228 const commands = this.prepareCommands();1229 return commands.reduce(function(max, command) {1230 return Math.max(max, command[0].length);1231 }, 0);1232 };
5 unmodified lines
1238 * @api private1239 */12401241 largestOptionLength() {1242 var options = [].slice.call(this.options);1242 const options = [].slice.call(this.options);1243 options.push({1244 flags: this._helpFlags1245 });1246
22 unmodified lines
1269 * @api private1270 */12711272 padWidth() {1273 var width = this.largestOptionLength();1273 let width = this.largestOptionLength();1274 if (this._argsDescription && this._args.length) {1275 if (this.largestArgLength() > width) {1276 width = this.largestArgLength();1277 }
15 unmodified lines
1293 * @api private1294 */12951296 optionHelp() {1297 var width = this.padWidth();1297 const width = this.padWidth();12981299 var columns = process.stdout.columns || 80;1300 var descriptionWidth = columns - width - 4;1299 const columns = process.stdout.columns || 80;1300 const descriptionWidth = columns - width - 4;13011302 // Append the help information1303 return this.options.map(function(option) {1304 const fullDesc = option.description +
12 unmodified lines
13171318 commandHelp() {1319 if (!this.commands.length && !this._lazyHasImplicitHelpCommand()) return '';13201321 var commands = this.prepareCommands();1322 var width = this.padWidth();1321 const commands = this.prepareCommands();1322 const width = this.padWidth();13231324 var columns = process.stdout.columns || 80;1325 var descriptionWidth = columns - width - 4;1324 const columns = process.stdout.columns || 80;1325 const descriptionWidth = columns - width - 4;13261327 return [1328 'Commands:',1329 commands.map(function(cmd) {1330 var desc = cmd[1] ? ' ' + cmd[1] : '';1330 const desc = cmd[1] ? ' ' + cmd[1] : '';1331 return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);1332 }).join('\n').replace(/^/gm, ' '),1333 ''1334 ].join('\n');
6 unmodified lines
1341 * @api private1342 */13431344 helpInformation() {1345 var desc = [];1345 let desc = [];1346 if (this._description) {1347 desc = [1348 this._description,1349 ''1350 ];13511352 var argsDescription = this._argsDescription;1352 const argsDescription = this._argsDescription;1353 if (argsDescription && this._args.length) {1354 var width = this.padWidth();1355 var columns = process.stdout.columns || 80;1356 var descriptionWidth = columns - width - 5;1354 const width = this.padWidth();1355 const columns = process.stdout.columns || 80;1356 const descriptionWidth = columns - width - 5;1357 desc.push('Arguments:');1358 desc.push('');1359 this._args.forEach(function(arg) {1360 desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name], descriptionWidth, width + 4));
1 unmodified line
1362 desc.push('');1363 }1364 }13651366 var cmdName = this._name;1366 let cmdName = this._name;1367 if (this._alias) {1368 cmdName = cmdName + '|' + this._alias;1369 }1370 var parentCmdNames = '';1371 for (var parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {1370 let parentCmdNames = '';1371 for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {1372 parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;1373 }1374 var usage = [1374 const usage = [1375 'Usage: ' + parentCmdNames + cmdName + ' ' + this.usage(),1376 ''1377 ];13781379 var cmds = [];1380 var commandHelp = this.commandHelp();1379 let cmds = [];1380 const commandHelp = this.commandHelp();1381 if (commandHelp) cmds = [commandHelp];13821383 var options = [1383 const options = [1384 'Options:',1385 '' + this.optionHelp().replace(/^/gm, ' '),1386 ''1387 ];
41 unmodified lines
1429 helpOption(flags, description) {1430 this._helpFlags = flags || this._helpFlags;1431 this._helpDescription = description || this._helpDescription;14321433 var splitFlags = this._helpFlags.split(/[ ,|]+/);1433 const splitFlags = this._helpFlags.split(/[ ,|]+/);14341435 if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();14361437 this._helpLongFlag = splitFlags.shift();
65 unmodified lines
1503 * @api private1504 */15051506function pad(str, width) {1507 var len = Math.max(0, width - str.length);1507 const len = Math.max(0, width - str.length);1508 return str + Array(len + 1).join(' ');1509}15101511/**
6 unmodified lines
1518 * @return {String}1519 * @api private1520 */1521function wrap(str, width, indent) {1522 var regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');1523 var lines = str.match(regex) || [];1522 const regex = new RegExp('.{1,' + (width - 1) + '}([\\s\u200B]|$)|[^\\s\u200B]+?([\\s\u200B]|$)', 'g');1523 const lines = str.match(regex) || [];1524 return lines.map(function(line, i) {1525 if (line.slice(-1) === '\n') {1526 line = line.slice(0, line.length - 1);1527 }
48 unmodified lines
1576 * @api private1577 */15781579function humanReadableArgName(arg) {1580 var nameOutput = arg.name + (arg.variadic === true ? '...' : '');1580 const nameOutput = arg.name + (arg.variadic === true ? '...' : '');15811582 return arg.required1583 ? '<' + nameOutput + '>'1584 : '[' + nameOutput + ']';
12 unmodified lines
1597 // --inspect[=[host:]port]1598 // --inspect-brk[=[host:]port]1599 // --inspect-port=[host:]port1600 return args.map((arg) => {1601 var result = arg;1601 let result = arg;1602 if (arg.indexOf('--inspect') === 0) {1603 var debugOption;1604 var debugHost = '127.0.0.1';1605 var debugPort = '9229';1606 var match;1603 let debugOption;1604 let debugHost = '127.0.0.1';1605 let debugPort = '9229';1606 let match;1607 if ((match = arg.match(/^(--inspect(-brk)?)$/)) !== null) {1608 // e.g. --inspect1609 debugOption = match[1];1610 } else if ((match = arg.match(/^(--inspect(-brk|-port)?)=([^:]+)$/)) !== null) {
22 unmodified lines