From 2e3d45063d4a1f84304cd0da8145d001a0ea36d8 Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 13 Aug 2024 20:53:18 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20adding=20new=20additions=20?= =?UTF-8?q?to=20linter=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pre-files/commit-msg-linter.js | 43 ++++++++++++++++++++++++++++------ pre-files/prettier-commit.js | 6 ++--- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/pre-files/commit-msg-linter.js b/pre-files/commit-msg-linter.js index c5fe644..c33c277 100644 --- a/pre-files/commit-msg-linter.js +++ b/pre-files/commit-msg-linter.js @@ -40,15 +40,44 @@ async function run() { // Check for duplicate commit messages in the last 100 commits const duplicateCommitMsg = execSync(`git log -n 100 --pretty=format:%s`) .toString() - .split('\n') - .some((msg) => msg.trim() === commitMsg); + .split('\n'); - if (duplicateCommitMsg) { - spinner.fail('❌ Duplicate commit message detected.'); + // Extract emojis from commitTypes + const emojis = Object.values(commitTypes); + + // Function to remove an emoji or the default emoji from the start of the string + const removeEmoji = (message) => { + for (const emoji of emojis) { + // Check if the message starts with the emoji + if (message.startsWith(emoji)) { + return message.slice(emoji.length).trim(); + } + } + // Check if the message starts with the default emoji + if (message.startsWith(defaultEmoji)) { + return message.slice(defaultEmoji.length).trim(); + } + // Return the message as is if it doesn't start with an emoji + return message; + }; + + // Apply the function to each message + const cleanMessages = duplicateCommitMsg.map(removeEmoji); + + if (cleanMessages.includes(commitMsg)) { + spinner.fail(chalk.bold.red('Duplicate Commit Detected')); + + console.log(); console.error( - chalk.red( - '❌ Duplicate commit message detected. Please use a unique commit message.' - ) + chalk.white.bgRed.bold(' ERROR: ') + + chalk.redBright(' A duplicate commit message has been detected.') + ); + console.log(); + console.log( + chalk.yellowBright('💡 TIP: ') + + chalk.white( + ' Please use a unique commit message to keep the history clean.' + ) ); console.log(); process.exit(1); diff --git a/pre-files/prettier-commit.js b/pre-files/prettier-commit.js index 9074b96..dfe6178 100644 --- a/pre-files/prettier-commit.js +++ b/pre-files/prettier-commit.js @@ -16,9 +16,9 @@ const { execSync } = require('child_process'); if (changes) { spinner.succeed('Changes detected.'); // Read the latest commit message to ensure uniqueness - const latestCommitMessage = execSync('git log -1 --pretty=%B', { - encoding: 'utf-8', - }).trim(); + const latestCommitMessage = execSync(`git log -n 100 --pretty=format:%s`) + .toString() + .split('\n'); // Generate a unique commit message let commitMessage = 'style: format with prettier';