From 96cbee06bc34a328fc20a62200f7b3555e76d6de Mon Sep 17 00:00:00 2001 From: Patrick Taylor Date: Tue, 11 Jun 2024 14:17:45 +0100 Subject: [PATCH] =?UTF-8?q?Send=20a=20notification=20with=20the=20linting?= =?UTF-8?q?=20result=20=F0=9F=93=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/colour-log.mjs | 6 ++---- src/eslint.mjs | 1 + src/index.js | 29 ++++++++++------------------- src/markdownlint.mjs | 1 + src/notifier.mjs | 36 ++++++++++++++++++++++++++++++++++++ src/stylelint.mjs | 1 + src/utils.mjs | 5 +++++ 7 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 src/notifier.mjs create mode 100644 src/utils.mjs diff --git a/src/colour-log.mjs b/src/colour-log.mjs index 95d82fa..07dbc56 100644 --- a/src/colour-log.mjs +++ b/src/colour-log.mjs @@ -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) => { @@ -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`) diff --git a/src/eslint.mjs b/src/eslint.mjs index 3670449..7f08671 100644 --- a/src/eslint.mjs +++ b/src/eslint.mjs @@ -23,6 +23,7 @@ const lintFiles = async filePaths => { files: results.length, fixableErrorCount: 0, fixableWarningCount: 0, + linter: 'ESLint', warningCount: 0, } diff --git a/src/index.js b/src/index.js index dc76beb..d2059ef 100755 --- a/src/index.js +++ b/src/index.js @@ -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() @@ -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) diff --git a/src/markdownlint.mjs b/src/markdownlint.mjs index 59195d0..7309e25 100644 --- a/src/markdownlint.mjs +++ b/src/markdownlint.mjs @@ -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]) => { diff --git a/src/notifier.mjs b/src/notifier.mjs new file mode 100644 index 0000000..344da3b --- /dev/null +++ b/src/notifier.mjs @@ -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, +} diff --git a/src/stylelint.mjs b/src/stylelint.mjs index 8ae990e..2629f96 100644 --- a/src/stylelint.mjs +++ b/src/stylelint.mjs @@ -18,6 +18,7 @@ const lintFiles = async filePaths => { errorCount: 0, files: results.length, fixableErrorCount: 0, + linter: 'Stylelint', } results.forEach(({ deprecations, warnings }) => { diff --git a/src/utils.mjs b/src/utils.mjs new file mode 100644 index 0000000..3423181 --- /dev/null +++ b/src/utils.mjs @@ -0,0 +1,5 @@ +const pluralise = (message, count) => count === 1 ? message : `${message}s` + +export { + pluralise, +}