Skip to content

Commit

Permalink
Send a notification with the linting result 📳
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jun 11, 2024
1 parent 355efa8 commit 96cbee0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/colour-log.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'

const pluralise = (message, count) => count === 1 ? message : `${message}s`
import { pluralise } from './utils.mjs'

const colourLog = {
config: (key, configArray) => {
Expand Down Expand Up @@ -47,9 +47,7 @@ const colourLog = {
log.length && console.log(log.join('\n'))
},

resultBlock: ({ linter, result }) => {
const { errorCount, warningCount } = result.processedResult

resultBlock: ({ errorCount, linter, warningCount }) => {
if (errorCount > 0) {
const message = chalk.bgRed.black(` ${errorCount} ${linter} ${pluralise('Error', errorCount)} `)
console.log(`💔 ${message}\n`)
Expand Down
1 change: 1 addition & 0 deletions src/eslint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const lintFiles = async filePaths => {
files: results.length,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'ESLint',
warningCount: 0,
}

Expand Down
29 changes: 10 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
import { Command } from 'commander'
import notifier from 'node-notifier'

import { colourLog } from './colour-log.mjs'
import eslint from './eslint.mjs'
import markdownlint from './markdownlint.mjs'
import { notifyResults } from './notifier.mjs'
import stylelint from './stylelint.mjs'

const program = new Command()
Expand Down Expand Up @@ -79,27 +79,18 @@ program
runESLint(),
runMarkdownLint(),
runStylelint(),
]).then(([eslintResult, markdownlintResult, stylelintResult]) => {
console.log(eslintResult, stylelintResult, markdownlintResult)
]).then((results) => {
console.log()

colourLog.resultBlock({
linter: 'ESLint',
result: eslintResult,
})
colourLog.resultBlock({
linter: 'Markdownlint',
result: markdownlintResult,
})
colourLog.resultBlock({
linter: 'Stylelint',
result: stylelintResult,
results.forEach(({ processedResult: { errorCount, linter, warningCount} }) => {
colourLog.resultBlock({
errorCount,
linter,
warningCount,
})
})

notifier.notify({
message: 'All lint checks have passed. Your code is clean!',
sound: 'Purr',
title: '✅ Lint Success',
})
notifyResults(results)

console.log()
process.exit(0)
Expand Down
1 change: 1 addition & 0 deletions src/markdownlint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const lintFiles = filePaths => new Promise((resolve, reject) => {
errorCount: 0,
files: Object.keys(results).length,
fixableErrorCount: 0,
linter: 'Markdownlint',
}

Object.entries(results).forEach(([_file, errors]) => {
Expand Down
36 changes: 36 additions & 0 deletions src/notifier.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import notifier from 'node-notifier'

import { pluralise } from './utils.mjs'

const notifyResults = results => {
let totalErrorCount = results.reduce((total, { processedResult: { errorCount = 0 } }) => total + errorCount, 0)
let totalWarningCount = results.reduce((total, { processedResult: { warningCount = 0 } }) => total + warningCount, 0)

if (totalErrorCount > 0) {
notifier.notify({
message: `${totalErrorCount} ${pluralise('error', totalErrorCount)} found. Please fix ${totalErrorCount > 1 ? 'them ' : 'it '}before continuing.`,
sound: 'Frog',
title: '🚨 Lint Pilot 🚨',
})
return
}

if (totalWarningCount > 0) {
notifier.notify({
message: `${totalWarningCount} ${pluralise('warning', totalWarningCount)} found. Please review ${totalWarningCount > 1 ? 'them ' : ''}before continuing.`,
sound: 'Frog',
title: '🚧 Lint Pilot 🚧',
})
return
}

notifier.notify({
message: 'All lint checks have passed. Your code is clean!',
sound: 'Purr',
title: '✅ Lint Pilot ✅',
})
}

export {
notifyResults,
}
1 change: 1 addition & 0 deletions src/stylelint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const lintFiles = async filePaths => {
errorCount: 0,
files: results.length,
fixableErrorCount: 0,
linter: 'Stylelint',
}

results.forEach(({ deprecations, warnings }) => {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const pluralise = (message, count) => count === 1 ? message : `${message}s`

export {
pluralise,
}

0 comments on commit 96cbee0

Please sign in to comment.