Skip to content

Commit

Permalink
Concatonate new lines in console logs to avoid indentation 👉
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jul 15, 2024
1 parent e8db0eb commit 1fae2eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/utils/__tests__/colourLog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ describe('colourLog', () => {
colourLog.configDebug('Debug message', 'config')

expect(chalk.blue).toHaveBeenCalledOnceWith('Debug message')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\n', 'Debug message', '\n', 'config')
expect(mockedConsoleLog).toHaveBeenCalledTimes(2)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nDebug message')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(2, 'config')
})

})
Expand All @@ -78,7 +80,7 @@ describe('colourLog', () => {
colourLog.error('An error occurred')

expect(chalk.red).toHaveBeenCalledOnceWith('An error occurred')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\n', 'An error occurred')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\nAn error occurred')
})

it('logs the text in red but does not log the error if global.debug is false', () => {
Expand All @@ -88,7 +90,7 @@ describe('colourLog', () => {

expect(chalk.red).toHaveBeenCalledOnceWith('An error occurred')
expect(mockedConsoleLog).toHaveBeenCalledTimes(1)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\n', 'An error occurred')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred')
})

it('logs the text in red and logs the error if global.debug is true', () => {
Expand All @@ -98,7 +100,7 @@ describe('colourLog', () => {

expect(chalk.red).toHaveBeenCalledOnceWith('An error occurred')
expect(mockedConsoleLog).toHaveBeenCalledTimes(2)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\n', 'An error occurred')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(2, error)
})

Expand Down Expand Up @@ -134,15 +136,15 @@ describe('colourLog', () => {
expect(chalk.cyan).toHaveBeenCalledWith('Finished eslint')
expect(chalk.yellow).toHaveBeenCalledWith('[1 file, 1000ms]')
expect(mockedConsoleLog).toHaveBeenCalledTimes(2)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\n', 'Finished eslint', '[1 file, 1000ms]')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nFinished eslint', '[1 file, 1000ms]')
}

it('logs the finished lint message along with the file count and duration (single file)', () => {
colourLog.result(commonResult, startTime)

expect(chalk.cyan).toHaveBeenCalledOnceWith('Finished eslint')
expect(chalk.yellow).toHaveBeenCalledOnceWith('[1 file, 1000ms]')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\n', 'Finished eslint', '[1 file, 1000ms]')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\nFinished eslint', '[1 file, 1000ms]')
})

it('logs the finished lint message along with the file count and duration (multiple files)', () => {
Expand All @@ -153,7 +155,7 @@ describe('colourLog', () => {

expect(chalk.cyan).toHaveBeenCalledOnceWith('Finished eslint')
expect(chalk.yellow).toHaveBeenCalledOnceWith('[7 files, 1000ms]')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\n', 'Finished eslint', '[7 files, 1000ms]')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\nFinished eslint', '[7 files, 1000ms]')
})

it('logs the error count in red (single error)', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/colourLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ const colourLog = {

configDebug: (message: string, config: any) => {
if (global.debug) {
console.log('\n', chalk.blue(message), '\n', config)
console.log(`\n${chalk.blue(message)}`)
console.log(config)
}
},

error: (text: string, error?: Error | unknown) => {
console.log('\n', chalk.red(text))
console.log(`\n${chalk.red(text)}`)
if (error && global.debug === true) {
console.log(error)
}
Expand Down Expand Up @@ -60,10 +61,11 @@ const colourLog = {
}

// Output
const finishedLinter = chalk.cyan(`Finished ${linter.toLowerCase()}`)
const files = `${fileCount} ${pluralise('file', fileCount)}`
const endTime = `${new Date().getTime() - startTime}ms`

console.log('\n', chalk.cyan(`Finished ${linter.toLowerCase()}`), chalk.yellow(`[${files}, ${endTime}]`))
console.log(`\n${finishedLinter}`, chalk.yellow(`[${files}, ${endTime}]`))
if (log.length) {
console.log(log.join('\n'))
}
Expand Down

0 comments on commit 1fae2eb

Please sign in to comment.