Skip to content

Commit

Permalink
refactor: use built-in truncation of Listr2
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Aug 11, 2023
1 parent 44a4f6c commit 977c15d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 39 deletions.
3 changes: 3 additions & 0 deletions lib/getRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const getMainRendererOptions = ({ debug, quiet }, logger, env) => {

return {
renderer: 'update',
rendererOptions: {
formatOutput: 'truncate',
},
}
}

Expand Down
25 changes: 2 additions & 23 deletions lib/makeCmdTasks.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import cliTruncate from 'cli-truncate'
import debug from 'debug'

import { configurationError } from './messages.js'
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.
*
Expand All @@ -31,11 +13,10 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => {
* @param {string} options.cwd
* @param {Array<string>} 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 = []
Expand All @@ -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 })
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/runAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export const runAll = async (
cwd: groupCwd,
files: task.fileList,
gitDir,
renderer: listrOptions.renderer,
shell,
verbose,
}).then((subTasks) => {
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions test/unit/getRenderer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,29 @@ describe('getRenderer', () => {
expect(getRenderer({}, console, {})).toEqual({
renderer: 'update',
fallbackRenderer: 'verbose',
rendererOptions: {
formatOutput: 'truncate',
},
})
})

it('should return update main renderer and verbose fallback renderer when colors are not forced', () => {
expect(getRenderer({}, console, { FORCE_COLOR: '0' })).toEqual({
renderer: 'update',
fallbackRenderer: 'verbose',
rendererOptions: {
formatOutput: 'truncate',
},
})
})

it('should return update renderers when colors are forced', () => {
expect(getRenderer({}, console, { FORCE_COLOR: '1' })).toEqual({
renderer: 'update',
fallbackRenderer: 'update',
rendererOptions: {
formatOutput: 'truncate',
},
})
})
})
13 changes: 0 additions & 13 deletions test/unit/makeCmdTasks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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…"`
)
})
})

0 comments on commit 977c15d

Please sign in to comment.