From 977c15d750d1ea0da678ef1a49efdc0d20a01314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Fri, 11 Aug 2023 21:10:13 +0300 Subject: [PATCH] refactor: use built-in truncation of Listr2 --- lib/getRenderer.js | 3 +++ lib/makeCmdTasks.js | 25 ++----------------------- lib/runAll.js | 1 - package-lock.json | 1 - package.json | 1 - test/unit/getRenderer.spec.js | 9 +++++++++ test/unit/makeCmdTasks.spec.js | 13 ------------- 7 files changed, 14 insertions(+), 39 deletions(-) diff --git a/lib/getRenderer.js b/lib/getRenderer.js index 97ab93ccd..349109a16 100644 --- a/lib/getRenderer.js +++ b/lib/getRenderer.js @@ -40,6 +40,9 @@ const getMainRendererOptions = ({ debug, quiet }, logger, env) => { return { renderer: 'update', + rendererOptions: { + formatOutput: 'truncate', + }, } } diff --git a/lib/makeCmdTasks.js b/lib/makeCmdTasks.js index 1de2b9251..e5d8ae3e4 100644 --- a/lib/makeCmdTasks.js +++ b/lib/makeCmdTasks.js @@ -1,4 +1,3 @@ -import cliTruncate from 'cli-truncate' import debug from 'debug' import { configurationError } from './messages.js' @@ -6,23 +5,6 @@ import { resolveTaskFn } from './resolveTaskFn.js' const debugLog = debug('lint-staged:makeCmdTasks') -const STDOUT_COLUMNS_DEFAULT = 80 - -const listrPrefixLength = { - update: ` X `.length, // indented task title where X is a checkmark or a cross (failure) - verbose: `[STARTED] `.length, // verbose renderer uses 7-letter STARTED/SUCCESS prefixes -} - -/** - * Get length of title based on the number of available columns prefix length - * @param {string} renderer The name of the Listr renderer - * @returns {number} - */ -const getTitleLength = (renderer, columns = process.stdout.columns) => { - const prefixLength = listrPrefixLength[renderer] || 0 - return (columns || STDOUT_COLUMNS_DEFAULT) - prefixLength -} - /** * Creates and returns an array of listr tasks which map to the given commands. * @@ -31,11 +13,10 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => { * @param {string} options.cwd * @param {Array} options.files * @param {string} options.gitDir - * @param {string} options.renderer * @param {Boolean} shell * @param {Boolean} verbose */ -export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => { +export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbose }) => { debugLog('Creating listr tasks for commands %o', commands) const commandArray = Array.isArray(commands) ? commands : [commands] const cmdTasks = [] @@ -60,10 +41,8 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, she ) } - // Truncate title to single line based on renderer - const title = cliTruncate(command, getTitleLength(renderer)) const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose }) - cmdTasks.push({ title, command, task }) + cmdTasks.push({ title: command, command, task }) } } diff --git a/lib/runAll.js b/lib/runAll.js index 819e5daf4..7377b23eb 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -182,7 +182,6 @@ export const runAll = async ( cwd: groupCwd, files: task.fileList, gitDir, - renderer: listrOptions.renderer, shell, verbose, }).then((subTasks) => { diff --git a/package-lock.json b/package-lock.json index c6cbfb29a..c7fe8d38e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "MIT", "dependencies": { "chalk": "5.3.0", - "cli-truncate": "3.1.0", "commander": "11.0.0", "debug": "4.3.4", "execa": "7.2.0", diff --git a/package.json b/package.json index d12ce0958..216c498a5 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ }, "dependencies": { "chalk": "5.3.0", - "cli-truncate": "3.1.0", "commander": "11.0.0", "debug": "4.3.4", "execa": "7.2.0", diff --git a/test/unit/getRenderer.spec.js b/test/unit/getRenderer.spec.js index eb1dea7f7..1e8fe466c 100644 --- a/test/unit/getRenderer.spec.js +++ b/test/unit/getRenderer.spec.js @@ -36,6 +36,9 @@ describe('getRenderer', () => { expect(getRenderer({}, console, {})).toEqual({ renderer: 'update', fallbackRenderer: 'verbose', + rendererOptions: { + formatOutput: 'truncate', + }, }) }) @@ -43,6 +46,9 @@ describe('getRenderer', () => { expect(getRenderer({}, console, { FORCE_COLOR: '0' })).toEqual({ renderer: 'update', fallbackRenderer: 'verbose', + rendererOptions: { + formatOutput: 'truncate', + }, }) }) @@ -50,6 +56,9 @@ describe('getRenderer', () => { expect(getRenderer({}, console, { FORCE_COLOR: '1' })).toEqual({ renderer: 'update', fallbackRenderer: 'update', + rendererOptions: { + formatOutput: 'truncate', + }, }) }) }) diff --git a/test/unit/makeCmdTasks.spec.js b/test/unit/makeCmdTasks.spec.js index 75ca7eae8..f1c344f73 100644 --- a/test/unit/makeCmdTasks.spec.js +++ b/test/unit/makeCmdTasks.spec.js @@ -124,17 +124,4 @@ describe('makeCmdTasks', () => { Function task should return a string or an array of strings" `) }) - - it('should truncate task title', async () => { - const longString = new Array(1000) - .fill() - .map((_, index) => index) - .join('') - - const res = await makeCmdTasks({ commands: () => longString, gitDir, files: ['test.js'] }) - expect(res.length).toBe(1) - expect(res[0].title).toMatchInlineSnapshot( - `"0123456789101112131415161718192021222324252627282930313233343536373839404142434…"` - ) - }) })