Skip to content

Commit

Permalink
Moved log results function into colourLog 📲
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jul 17, 2024
1 parent c7e9d11 commit 6a68040
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
19 changes: 2 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node
import chalk from 'chalk'
import { Command } from 'commander'
import { spaceLog } from 'space-log'

import { Events, Linter } from '@Types'
import colourLog from '@Utils/colourLog'
Expand Down Expand Up @@ -55,21 +53,8 @@ const runLintPilot = ({ title, watch }: RunLintPilot) => {
linter: Linter.Stylelint,
}),
]).then((reports) => {
reports.forEach(({ results, summary }) => {
if (Object.keys(results).length === 0) {
return
}

colourLog.info(`\nLogging ${summary.linter.toLowerCase()} results:`)

Object.entries(results).forEach(([file, formattedResults]) => {
console.log()
console.log(chalk.underline(`${process.cwd()}/${file}`))
spaceLog({
columnKeys: ['severity', 'position', 'message', 'rule'],
spaceSize: 2,
}, formattedResults)
})
reports.forEach(report => {
colourLog.results(report)
})

reports.forEach(({ summary }) => {
Expand Down
94 changes: 74 additions & 20 deletions src/utils/__tests__/colourLog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk'
import spaceLog from 'space-log'

import { Linter, type ReportSummary } from '@Types'

Expand All @@ -19,15 +20,28 @@ jest.mock('chalk', () => ({
dim: jest.fn().mockImplementation(text => text),
magenta: jest.fn().mockImplementation(text => text),
red: jest.fn().mockImplementation(text => text),
underline: jest.fn().mockImplementation(text => `_${text}_`),
yellow: jest.fn().mockImplementation(text => text),
}))

jest.mock('space-log')

jest.useFakeTimers().setSystemTime(1718971200)

describe('colourLog', () => {

const mockedConsoleLog = jest.spyOn(console, 'log').mockImplementation(() => {})

const commonSummary: ReportSummary = {
deprecatedRules: [],
errorCount: 0,
fileCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: Linter.ESLint,
warningCount: 0,
}

describe('config', () => {

it('logs the key in magenta and a single config item in dim', () => {
Expand Down Expand Up @@ -117,17 +131,67 @@ describe('colourLog', () => {

})

describe('summary', () => {
describe('results', () => {

const commonSummary: ReportSummary = {
deprecatedRules: [],
errorCount: 0,
fileCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: Linter.ESLint,
warningCount: 0,
}
it('returns if there are no results', () => {
colourLog.results({
results: {},
summary: commonSummary,
})

expect(mockedConsoleLog).not.toHaveBeenCalled()
})

it('logs the results', () => {
const commonResult = {
message: 'Foo',
messageTheme: () => {},
position: '1:1',
positionTheme: () => {},
rule: 'bar',
ruleTheme: () => {},
severity: 'X',
}

colourLog.results({
results: {
'CONTRIBUTING.md': [commonResult],
'README.md': [commonResult, commonResult],
},
summary: commonSummary,
})

// Info
expect(chalk.blue).toHaveBeenCalledOnceWith('\nLogging eslint results:')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nLogging eslint results:')

// File 1
expect(mockedConsoleLog).toHaveBeenNthCalledWith(2)
expect(chalk.underline).toHaveBeenNthCalledWith(1, `${process.cwd()}/CONTRIBUTING.md`)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(3, `_${process.cwd()}/CONTRIBUTING.md_`)
expect(spaceLog).toHaveBeenNthCalledWith(1, {
columnKeys: ['severity', 'position', 'message', 'rule'],
spaceSize: 2,
}, [commonResult])

// File 2
expect(mockedConsoleLog).toHaveBeenNthCalledWith(4)
expect(chalk.underline).toHaveBeenNthCalledWith(2, `${process.cwd()}/README.md`)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(5, `_${process.cwd()}/README.md_`)
expect(spaceLog).toHaveBeenNthCalledWith(2, {
columnKeys: ['severity', 'position', 'message', 'rule'],
spaceSize: 2,
}, [commonResult, commonResult])

// Log Count
expect(mockedConsoleLog).toHaveBeenCalledTimes(5)
expect(chalk.underline).toHaveBeenCalledTimes(2)
expect(spaceLog).toHaveBeenCalledTimes(2)
})

})

describe('summary', () => {

const startTime = new Date().getTime()
jest.advanceTimersByTime(1000)
Expand Down Expand Up @@ -276,16 +340,6 @@ describe('colourLog', () => {

describe('summaryBlock', () => {

const commonSummary: ReportSummary = {
deprecatedRules: [],
errorCount: 0,
fileCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: Linter.ESLint,
warningCount: 0,
}

it('logs the error count in a red background', () => {
colourLog.summaryBlock({
...commonSummary,
Expand Down
20 changes: 19 additions & 1 deletion src/utils/colourLog.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import chalk from 'chalk'
import { spaceLog } from 'space-log'

import { pluralise } from '@Utils/transform'

import type { ReportSummary } from '@Types'
import type { LintReport, ReportSummary } from '@Types'

const colourLog = {
config: (key: string, configArray: Array<string>) => {
Expand All @@ -29,6 +30,23 @@ const colourLog = {

info: (text: string) => console.log(chalk.blue(text)),

results: ({ results, summary }: LintReport) => {
if (Object.keys(results).length === 0) {
return
}

console.log(chalk.blue(`\nLogging ${summary.linter.toLowerCase()} results:`))

Object.entries(results).forEach(([file, formattedResults]) => {
console.log()
console.log(chalk.underline(`${process.cwd()}/${file}`))
spaceLog({
columnKeys: ['severity', 'position', 'message', 'rule'],
spaceSize: 2,
}, formattedResults)
})
},

summary: (summary: ReportSummary, startTime: number) => {
const { deprecatedRules, errorCount, fileCount, fixableErrorCount, fixableWarningCount, linter, warningCount } = summary

Expand Down

0 comments on commit 6a68040

Please sign in to comment.