From 4c2ebe8636699fa843b4335bf2a87471118816e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 10:58:18 +0100 Subject: [PATCH 01/12] remove _.isFunction --- packages/core/index.js | 9 +++------ packages/inquirer/lib/objects/choice.js | 5 +---- packages/inquirer/lib/ui/prompt.js | 3 +-- packages/inquirer/lib/utils/utils.js | 5 +---- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/packages/core/index.js b/packages/core/index.js index 63fdd8fa9..875cac2ff 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -1,6 +1,3 @@ -const _ = { - isFunction: require('lodash/isFunction'), -}; const readline = require('readline'); const chalk = require('chalk'); const MuteStream = require('mute-stream'); @@ -49,7 +46,7 @@ class StateManager { this.screen = new ScreenManager(this.rl); let config = configFactory; - if (_.isFunction(configFactory)) { + if (typeof configFactory === 'function') { config = configFactory(this.rl); } @@ -69,7 +66,7 @@ class StateManager { // Load asynchronous properties const showLoader = setTimeout(this.startLoading, 500); - if (_.isFunction(message)) { + if (typeof message === 'function') { message = await runAsync(message)(); } @@ -196,7 +193,7 @@ class StateManager { prefix: this.getPrefix(), ...state, // Only pass message down if it's a string. Otherwise we're still in init state - message: _.isFunction(message) ? 'Loading...' : message, + message: typeof message === 'function' ? 'Loading...' : message, value: transformer(value, { isFinal: status === 'done' }), validate: undefined, filter: undefined, diff --git a/packages/inquirer/lib/objects/choice.js b/packages/inquirer/lib/objects/choice.js index 25042a9b1..46c2364bf 100644 --- a/packages/inquirer/lib/objects/choice.js +++ b/packages/inquirer/lib/objects/choice.js @@ -1,7 +1,4 @@ 'use strict'; -const _ = { - isFunction: require('lodash/isFunction'), -}; /** * Choice object @@ -31,7 +28,7 @@ module.exports = class Choice { }); } - if (_.isFunction(val.disabled)) { + if (typeof val.disabled === 'function') { this.disabled = val.disabled(answers); } else { this.disabled = val.disabled; diff --git a/packages/inquirer/lib/ui/prompt.js b/packages/inquirer/lib/ui/prompt.js index 0d0e3ea26..a10f95973 100644 --- a/packages/inquirer/lib/ui/prompt.js +++ b/packages/inquirer/lib/ui/prompt.js @@ -4,7 +4,6 @@ const _ = { clone: require('lodash/clone'), get: require('lodash/get'), set: require('lodash/set'), - isFunction: require('lodash/isFunction'), }; const { defer, empty, from, of } = require('rxjs'); const { concatMap, filter, publish, reduce } = require('rxjs/operators'); @@ -129,7 +128,7 @@ class PromptUI extends Base { return empty(); } - if (!_.isFunction(question.when)) { + if (typeof question.when !== 'function') { return of(question); } diff --git a/packages/inquirer/lib/utils/utils.js b/packages/inquirer/lib/utils/utils.js index 921175105..40b125010 100644 --- a/packages/inquirer/lib/utils/utils.js +++ b/packages/inquirer/lib/utils/utils.js @@ -1,7 +1,4 @@ 'use strict'; -const _ = { - isFunction: require('lodash/isFunction'), -}; const { from, of } = require('rxjs'); const runAsync = require('run-async'); @@ -15,7 +12,7 @@ const runAsync = require('run-async'); */ exports.fetchAsyncQuestionProperty = function (question, prop, answers) { - if (!_.isFunction(question[prop])) { + if (typeof question[prop] !== 'function') { return of(question); } From 70a3335da218f65ae894851cd21e7629ac0b41b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:00:22 +0100 Subject: [PATCH 02/12] rm _.last --- packages/core/lib/screen-manager.js | 5 +---- packages/inquirer/lib/utils/screen-manager.js | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/core/lib/screen-manager.js b/packages/core/lib/screen-manager.js index 786d1f125..ccfc88d7d 100644 --- a/packages/core/lib/screen-manager.js +++ b/packages/core/lib/screen-manager.js @@ -1,6 +1,3 @@ -const _ = { - last: require('lodash/last'), -}; const cliWidth = require('cli-width'); const stripAnsi = require('strip-ansi'); const stringWidth = require('string-width'); @@ -9,7 +6,7 @@ const util = require('./readline'); const { breakLines } = require('./utils'); const height = (content) => content.split('\n').length; -const lastLine = (content) => _.last(content.split('\n')); +const lastLine = (content) => content.split('\n').pop(); module.exports = class ScreenManager { constructor(rl) { diff --git a/packages/inquirer/lib/utils/screen-manager.js b/packages/inquirer/lib/utils/screen-manager.js index 12412c643..133526dcf 100644 --- a/packages/inquirer/lib/utils/screen-manager.js +++ b/packages/inquirer/lib/utils/screen-manager.js @@ -1,6 +1,5 @@ 'use strict'; const _ = { - last: require('lodash/last'), flatten: require('lodash/flatten'), }; const util = require('./readline'); @@ -13,8 +12,9 @@ function height(content) { return content.split('\n').length; } +/** @param {string} content */ function lastLine(content) { - return _.last(content.split('\n')); + return content.split('\n').pop(); } class ScreenManager { From 085928d8f4f896d69e310570075d265bc0a37b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:01:30 +0100 Subject: [PATCH 03/12] rm _.flatten --- packages/core/lib/utils.js | 14 ++++++-------- packages/inquirer/lib/utils/paginator.js | 8 ++++++-- packages/inquirer/lib/utils/screen-manager.js | 19 ++++++++++--------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/core/lib/utils.js b/packages/core/lib/utils.js index 1fe34a363..dca9d3064 100644 --- a/packages/core/lib/utils.js +++ b/packages/core/lib/utils.js @@ -1,22 +1,20 @@ -const _ = { - flatten: require('lodash/flatten'), -}; - /** * Force line returns at specific width. This function is ANSI code friendly and it'll * ignore invisible codes during width calculation. - * @param {string} lines + * @param {string} content * @param {number} width * @return {string} */ exports.breakLines = (content, width) => { const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g'); - return _.flatten( - content.split('\n').map((line) => { + return content + .split('\n') + .map((line) => { const chunk = line.match(regex); // Remove the last match as it's always empty chunk.pop(); return chunk || ''; }) - ).join('\n'); + .flat() + .join('\n'); }; diff --git a/packages/inquirer/lib/utils/paginator.js b/packages/inquirer/lib/utils/paginator.js index 767bf6729..ff1900e6a 100644 --- a/packages/inquirer/lib/utils/paginator.js +++ b/packages/inquirer/lib/utils/paginator.js @@ -11,6 +11,10 @@ const chalk = require('chalk'); */ class Paginator { + /** + * @param {import("./screen-manager")} [screen] + * @param {{isInfinite?: boolean}} [options] + */ constructor(screen, options = {}) { const { isInfinite = true } = options; this.lastIndex = 0; @@ -25,7 +29,7 @@ class Paginator { if (this.screen) { lines = this.screen.breakLines(lines); active = _.sum(lines.map((lineParts) => lineParts.length).splice(0, active)); - lines = _.flatten(lines); + lines = lines.flat(); } // Make sure there's enough lines to paginate @@ -58,7 +62,7 @@ class Paginator { } // Duplicate the lines so it give an infinite list look - const infinite = _.flatten([lines, lines, lines]); + const infinite = [lines, lines, lines].flat(); const topIndex = Math.max(0, active + lines.length - this.pointer); return infinite.splice(topIndex, pageSize); diff --git a/packages/inquirer/lib/utils/screen-manager.js b/packages/inquirer/lib/utils/screen-manager.js index 133526dcf..9fd1c1b71 100644 --- a/packages/inquirer/lib/utils/screen-manager.js +++ b/packages/inquirer/lib/utils/screen-manager.js @@ -1,7 +1,4 @@ 'use strict'; -const _ = { - flatten: require('lodash/flatten'), -}; const util = require('./readline'); const cliWidth = require('cli-width'); const stripAnsi = require('strip-ansi'); @@ -153,11 +150,13 @@ class ScreenManager { return width; } - breakLines(lines, width) { + /** + * @param {string[]} lines + */ + breakLines(lines, width = this.normalizedCliWidth()) { // Break lines who're longer than the cli width so we can normalize the natural line // returns behavior across terminals. - width = width || this.normalizedCliWidth(); - const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g'); + const regex = new RegExp(`(?:(?:\\033[[0-9;]*m)*.?){1,${width}}`, 'g'); return lines.map((line) => { const chunk = line.match(regex); // Last match is always empty @@ -166,9 +165,11 @@ class ScreenManager { }); } - forceLineReturn(content, width) { - width = width || this.normalizedCliWidth(); - return _.flatten(this.breakLines(content.split('\n'), width)).join('\n'); + /** + * @param {string} content + */ + forceLineReturn(content, width = this.normalizedCliWidth()) { + return this.breakLines(content.split('\n'), width).flat().join('\n'); } } From bbc2fe8199ceb3829ff4c2d159033fa28b45b3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:01:56 +0100 Subject: [PATCH 04/12] rm _.assign --- packages/inquirer/lib/prompts/base.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/inquirer/lib/prompts/base.js b/packages/inquirer/lib/prompts/base.js index ec503eeca..81dfb77e4 100644 --- a/packages/inquirer/lib/prompts/base.js +++ b/packages/inquirer/lib/prompts/base.js @@ -4,7 +4,6 @@ * Should be extended by prompt types. */ const _ = { - assign: require('lodash/assign'), defaults: require('lodash/defaults'), clone: require('lodash/clone'), }; @@ -17,7 +16,7 @@ const ScreenManager = require('../utils/screen-manager'); class Prompt { constructor(question, rl, answers) { // Setup instance defaults property - _.assign(this, { + Object.assign(this, { answers, status: 'pending', }); From a0d90d84571a5f498f713ed53fa608b8e3640ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:02:45 +0100 Subject: [PATCH 05/12] rm _.isBoolean --- packages/inquirer/lib/prompts/confirm.js | 7 ++----- packages/inquirer/lib/ui/bottom-bar.js | 5 +---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/inquirer/lib/prompts/confirm.js b/packages/inquirer/lib/prompts/confirm.js index 287279af0..7d628a15b 100644 --- a/packages/inquirer/lib/prompts/confirm.js +++ b/packages/inquirer/lib/prompts/confirm.js @@ -3,9 +3,6 @@ * `confirm` type prompt */ -const _ = { - isBoolean: require('lodash/isBoolean'), -}; const chalk = require('chalk'); const { take, takeUntil } = require('rxjs/operators'); const Base = require('./base'); @@ -28,8 +25,8 @@ class ConfirmPrompt extends Base { }, }); - if (_.isBoolean(this.opt.default)) { - rawDefault = this.opt.default; + if (this.opt.default != null) { + rawDefault = Boolean(this.opt.default); } this.opt.default = rawDefault ? 'Y/n' : 'y/N'; diff --git a/packages/inquirer/lib/ui/bottom-bar.js b/packages/inquirer/lib/ui/bottom-bar.js index 9235273ec..3da44d7d2 100644 --- a/packages/inquirer/lib/ui/bottom-bar.js +++ b/packages/inquirer/lib/ui/bottom-bar.js @@ -11,9 +11,7 @@ const _ = { }; class BottomBar extends Base { - constructor(opt) { - opt = opt || {}; - + constructor(opt = {}) { super(opt); this.log = through(this.writeLog.bind(this)); @@ -79,7 +77,6 @@ class BottomBar extends Base { /** * Helper for writing message in Prompt - * @param {BottomBar} prompt - The Prompt object that extends tty * @param {String} message - The message to be output */ write(message) { From 47c4ff55fd6ee44e52085e2035762a73beba2417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:03:10 +0100 Subject: [PATCH 06/12] rm _.map --- packages/inquirer/lib/prompts/checkbox.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/inquirer/lib/prompts/checkbox.js b/packages/inquirer/lib/prompts/checkbox.js index 4863a6588..64992cc0b 100644 --- a/packages/inquirer/lib/prompts/checkbox.js +++ b/packages/inquirer/lib/prompts/checkbox.js @@ -3,9 +3,6 @@ * `list` type prompt */ -const _ = { - map: require('lodash/map'), -}; const chalk = require('chalk'); const cliCursor = require('cli-cursor'); const figures = require('figures'); @@ -167,8 +164,8 @@ class CheckboxPrompt extends Base { (choice) => Boolean(choice.checked) && !choice.disabled ); - this.selection = _.map(choices, 'short'); - return _.map(choices, 'value'); + this.selection = choices.map((choice) => choice.short); + return choices.map((choice) => choice.value); } onUpKey() { From 0773276083c607a3819d0d93ec2a3fb633f2ad08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:03:33 +0100 Subject: [PATCH 07/12] rm _.uniq --- packages/inquirer/lib/prompts/expand.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/inquirer/lib/prompts/expand.js b/packages/inquirer/lib/prompts/expand.js index 9be3617ee..3afed1bbe 100644 --- a/packages/inquirer/lib/prompts/expand.js +++ b/packages/inquirer/lib/prompts/expand.js @@ -3,9 +3,6 @@ * `rawlist` type prompt */ -const _ = { - uniq: require('lodash/uniq'), -}; const chalk = require('chalk'); const { map, takeUntil } = require('rxjs/operators'); const Base = require('./base'); @@ -218,7 +215,7 @@ class ExpandPrompt extends Base { if (errors.length) { throw new Error( 'Duplicate key error: `key` param must be unique. Duplicates: ' + - _.uniq(errors).join(', ') + [...new Set(errors)].join(',') ); } } From f44575bdb462d4b047bdd67f04291ca388be2b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:03:51 +0100 Subject: [PATCH 08/12] rm _.clone --- packages/inquirer/lib/ui/prompt.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/inquirer/lib/ui/prompt.js b/packages/inquirer/lib/ui/prompt.js index a10f95973..82e313226 100644 --- a/packages/inquirer/lib/ui/prompt.js +++ b/packages/inquirer/lib/ui/prompt.js @@ -1,7 +1,6 @@ 'use strict'; const _ = { isPlainObject: require('lodash/isPlainObject'), - clone: require('lodash/clone'), get: require('lodash/get'), set: require('lodash/set'), }; @@ -24,7 +23,7 @@ class PromptUI extends Base { run(questions, answers) { // Keep global reference to the answers if (_.isPlainObject(answers)) { - this.answers = _.clone(answers); + this.answers = { ...answers }; } else { this.answers = {}; } @@ -78,7 +77,7 @@ class PromptUI extends Base { } processQuestion(question) { - question = _.clone(question); + question = { ...question }; return defer(() => { const obs = of(question); From 9a36bcb724c6785a6f2994aaf2c99ac0f6a55f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:09:14 +0100 Subject: [PATCH 09/12] rm _.omit --- packages/inquirer/lib/ui/baseUI.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/inquirer/lib/ui/baseUI.js b/packages/inquirer/lib/ui/baseUI.js index 917b44d90..b2fb45cfe 100644 --- a/packages/inquirer/lib/ui/baseUI.js +++ b/packages/inquirer/lib/ui/baseUI.js @@ -1,7 +1,4 @@ 'use strict'; -const _ = { - omit: require('lodash/omit'), -}; const MuteStream = require('mute-stream'); const readline = require('readline'); @@ -61,8 +58,7 @@ class UI { } } -function setupReadlineOptions(opt) { - opt = opt || {}; +function setupReadlineOptions(opt = {}) { // Inquirer 8.x: // opt.skipTTYChecks = opt.skipTTYChecks === undefined ? opt.input !== undefined : opt.skipTTYChecks; opt.skipTTYChecks = opt.skipTTYChecks === undefined ? true : opt.skipTTYChecks; @@ -85,11 +81,13 @@ function setupReadlineOptions(opt) { ms.pipe(opt.output || process.stdout); const output = ms; + const { input: _, output: __, ...rest } = opt; + return { terminal: true, input, output, - ..._.omit(opt, ['input', 'output']), + ...rest, }; } From 852b65a5ae5d912d2202f4969f21e7a554a60782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:18:34 +0100 Subject: [PATCH 10/12] rm _.sum --- packages/inquirer/lib/utils/paginator.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/inquirer/lib/utils/paginator.js b/packages/inquirer/lib/utils/paginator.js index ff1900e6a..f70368351 100644 --- a/packages/inquirer/lib/utils/paginator.js +++ b/packages/inquirer/lib/utils/paginator.js @@ -1,9 +1,5 @@ 'use strict'; -const _ = { - sum: require('lodash/sum'), - flatten: require('lodash/flatten'), -}; const chalk = require('chalk'); /** @@ -28,7 +24,10 @@ class Paginator { if (this.screen) { lines = this.screen.breakLines(lines); - active = _.sum(lines.map((lineParts) => lineParts.length).splice(0, active)); + active = lines + .map((lineParts) => lineParts.length) + .splice(0, active) + .reduce((a, b) => a + b); lines = lines.flat(); } From 6fab6943be2277981500eeb57fb8b11668c9f35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 11:26:03 +0100 Subject: [PATCH 11/12] add initialValue to sum --- packages/inquirer/lib/utils/paginator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/inquirer/lib/utils/paginator.js b/packages/inquirer/lib/utils/paginator.js index f70368351..6c2e8549e 100644 --- a/packages/inquirer/lib/utils/paginator.js +++ b/packages/inquirer/lib/utils/paginator.js @@ -27,7 +27,7 @@ class Paginator { active = lines .map((lineParts) => lineParts.length) .splice(0, active) - .reduce((a, b) => a + b); + .reduce((a, b) => a + b, 0); lines = lines.flat(); } From c557dcdfc4fc26c5b7762b9b8a08b68c855d8e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 18 Nov 2021 17:43:00 +0100 Subject: [PATCH 12/12] fix code review --- packages/core/lib/utils.js | 3 +-- packages/inquirer/lib/ui/baseUI.js | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/core/lib/utils.js b/packages/core/lib/utils.js index dca9d3064..2a744eaaf 100644 --- a/packages/core/lib/utils.js +++ b/packages/core/lib/utils.js @@ -9,12 +9,11 @@ exports.breakLines = (content, width) => { const regex = new RegExp('(?:(?:\\033[[0-9;]*m)*.?){1,' + width + '}', 'g'); return content .split('\n') - .map((line) => { + .flatMap((line) => { const chunk = line.match(regex); // Remove the last match as it's always empty chunk.pop(); return chunk || ''; }) - .flat() .join('\n'); }; diff --git a/packages/inquirer/lib/ui/baseUI.js b/packages/inquirer/lib/ui/baseUI.js index b2fb45cfe..037d6ddf2 100644 --- a/packages/inquirer/lib/ui/baseUI.js +++ b/packages/inquirer/lib/ui/baseUI.js @@ -81,13 +81,11 @@ function setupReadlineOptions(opt = {}) { ms.pipe(opt.output || process.stdout); const output = ms; - const { input: _, output: __, ...rest } = opt; - return { terminal: true, + ...opt, input, output, - ...rest, }; }