From 58ab6927e88e2a687b05ab3236a68ba12393f967 Mon Sep 17 00:00:00 2001 From: ize-302 Date: Sat, 28 Sep 2024 15:38:46 +0100 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20missing=20emoji=20in=20commit=20mess?= =?UTF-8?q?age=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biome.json | 69 +++++++-------- {src => oldsrc}/commit-types.json | 0 oldsrc/gitmo.ts | 83 +++++++++++++++++++ oldsrc/index.ts | 4 + oldsrc/utils/hasStagedChanges.ts | 22 +++++ {src => oldsrc}/utils/isConventionalCommit.ts | 0 {src => oldsrc}/utils/onCancel.ts | 0 {src => oldsrc}/utils/promptContinue.ts | 3 +- .../utils/showCommitMessagePrompt.ts | 0 src/emojis-data.json | 57 +++++++++++++ src/gitmo.ts | 61 +++----------- src/index.ts | 4 +- src/utils/hasStagedChanges.ts | 16 ++-- src/utils/optionsPrompt.ts | 26 ++++++ src/utils/transformMessage.ts | 17 ++++ text.txt | 7 ++ tsconfig.json | 34 ++++---- 17 files changed, 288 insertions(+), 115 deletions(-) rename {src => oldsrc}/commit-types.json (100%) create mode 100755 oldsrc/gitmo.ts create mode 100644 oldsrc/index.ts create mode 100644 oldsrc/utils/hasStagedChanges.ts rename {src => oldsrc}/utils/isConventionalCommit.ts (100%) rename {src => oldsrc}/utils/onCancel.ts (100%) rename {src => oldsrc}/utils/promptContinue.ts (76%) rename {src => oldsrc}/utils/showCommitMessagePrompt.ts (100%) create mode 100644 src/emojis-data.json mode change 100755 => 100644 src/gitmo.ts create mode 100644 src/utils/optionsPrompt.ts create mode 100644 src/utils/transformMessage.ts create mode 100644 text.txt diff --git a/biome.json b/biome.json index c7f1514..fbc9488 100644 --- a/biome.json +++ b/biome.json @@ -1,36 +1,37 @@ { - "$schema": "https://biomejs.dev/schemas/1.9.0/schema.json", - "vcs": { - "enabled": false, - "clientKind": "git", - "useIgnoreFile": false - }, - "files": { - "ignoreUnknown": false, - "ignore": [] - }, - "formatter": { - "enabled": true, - "indentStyle": "tab", - "indentWidth": 2 - }, - "organizeImports": { - "enabled": true - }, - "linter": { - "enabled": true, - "rules": { - "recommended": true - } - }, - "json": { - "formatter": { - "trailingCommas": "none" - } - }, - "javascript": { - "formatter": { - "quoteStyle": "double" - } - } + "$schema": "https://biomejs.dev/schemas/1.9.0/schema.json", + "vcs": { + "enabled": false, + "clientKind": "git", + "useIgnoreFile": false + }, + "files": { + "ignoreUnknown": false, + "ignore": [] + }, + "formatter": { + "enabled": true, + "indentStyle": "tab", + "indentWidth": 2, + "lineWidth": 120 + }, + "organizeImports": { + "enabled": true + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true + } + }, + "json": { + "formatter": { + "trailingCommas": "none" + } + }, + "javascript": { + "formatter": { + "quoteStyle": "double" + } + } } diff --git a/src/commit-types.json b/oldsrc/commit-types.json similarity index 100% rename from src/commit-types.json rename to oldsrc/commit-types.json diff --git a/oldsrc/gitmo.ts b/oldsrc/gitmo.ts new file mode 100755 index 0000000..ccd6180 --- /dev/null +++ b/oldsrc/gitmo.ts @@ -0,0 +1,83 @@ +import { Command } from "commander"; +import shell from "shelljs"; + +import hasStagedChanges from "oldsrc/utils/hasStagedChanges.js"; +import isConventionalCommit from "oldsrc/utils/isConventionalCommit.js"; +import onCancel from "oldsrc/utils/onCancel.js"; +import promptContinue from "oldsrc/utils/promptContinue.js"; +import showCommitMessagePrompt from "oldsrc/utils/showCommitMessagePrompt.js"; +import packageJson from "../package.json"; + +shell.config.silent = false; + +const program = new Command(); + +export const init = () => { + try { + program + .name("gitmo") + .description( + "A cli tool that adds appropriate emoji to your commit message based on conventional commits specification", + ) + .version(packageJson.version); + + program + .command("cm [message]") + .description("Submit commit") + .action(async (message) => { + if (!message) { + if (hasStagedChanges()) { + const modifiedMessage = await showCommitMessagePrompt(); + shell.exec(`git commit -m '${modifiedMessage}'`); + } + } else { + const isValidCommit = await isConventionalCommit(message); + if (isValidCommit) { + shell.exec(`git commit -m '${message}'`); + } else { + const response = await promptContinue(); + if (response.continue === "yes") { + shell.exec(`git commit -m '${message}'`); + } else { + onCancel(); + } + } + } + }); + + program + .command("ac [message]") + .description("Ament last commit") + .action(async (message) => { + if (!message) { + const modifiedMessage = await showCommitMessagePrompt(); + shell.exec(`git commit -m '${modifiedMessage}'`); + } else { + const isValidCommit = await isConventionalCommit(message); + if (isValidCommit) { + shell.exec(`git commit --amend -m '${message}'`); + } else { + const response = await promptContinue(); + if (response.continue === "yes") { + shell.exec(`git commit --amend -m '${message}'`); + } else { + onCancel(); + } + } + } + }); + + program + .command("update") + .description("Update gitmo cli") + .action(() => { + shell.exec("npm i -g gitmo"); + }); + + // Parse the command-line arguments + program.parse(process.argv); + } catch (error) { + console.log(error); + process.exit(1); + } +}; diff --git a/oldsrc/index.ts b/oldsrc/index.ts new file mode 100644 index 0000000..b97f38a --- /dev/null +++ b/oldsrc/index.ts @@ -0,0 +1,4 @@ +#!/usr/bin/env node +import { init } from "./gitmo.js"; + +init(); diff --git a/oldsrc/utils/hasStagedChanges.ts b/oldsrc/utils/hasStagedChanges.ts new file mode 100644 index 0000000..99c7018 --- /dev/null +++ b/oldsrc/utils/hasStagedChanges.ts @@ -0,0 +1,22 @@ +import shell from "shelljs"; + +// check if there is exisiting staged commit +const hasStagedChanges = () => { + // check if there are staged changes + const gitStatus = shell.exec("git status --porcelain", { + silent: true, + }).stdout; + // Parse the status output + const stagedChanges = gitStatus + .split("\n") + .filter((line) => line.startsWith("A ") || line.startsWith("M ")); + if (stagedChanges.length === 0) { + console.log( + "You have no changes staged for commit. Please stage you commit before continuing!", + ); + } else { + return true; + } +}; + +export default hasStagedChanges; diff --git a/src/utils/isConventionalCommit.ts b/oldsrc/utils/isConventionalCommit.ts similarity index 100% rename from src/utils/isConventionalCommit.ts rename to oldsrc/utils/isConventionalCommit.ts diff --git a/src/utils/onCancel.ts b/oldsrc/utils/onCancel.ts similarity index 100% rename from src/utils/onCancel.ts rename to oldsrc/utils/onCancel.ts diff --git a/src/utils/promptContinue.ts b/oldsrc/utils/promptContinue.ts similarity index 76% rename from src/utils/promptContinue.ts rename to oldsrc/utils/promptContinue.ts index 5b882f7..1de5728 100644 --- a/src/utils/promptContinue.ts +++ b/oldsrc/utils/promptContinue.ts @@ -6,9 +6,10 @@ const promptContinue = async () => { { type: "select", name: "continue", - message: "continue anyway?", + message: "Not a conventional commit. continue anyway?", choices: [ { title: "Yes", value: "yes" }, + { title: "Edit commit message", value: "correction" }, { title: "Cancel", value: "cancel" }, ], instructions: false, diff --git a/src/utils/showCommitMessagePrompt.ts b/oldsrc/utils/showCommitMessagePrompt.ts similarity index 100% rename from src/utils/showCommitMessagePrompt.ts rename to oldsrc/utils/showCommitMessagePrompt.ts diff --git a/src/emojis-data.json b/src/emojis-data.json new file mode 100644 index 0000000..ed3b424 --- /dev/null +++ b/src/emojis-data.json @@ -0,0 +1,57 @@ +[ + { + "name": "feat", + "emoji": "✨", + "description": "A new feature" + }, + { + "name": "fix", + "emoji": "🐛", + "description": "A bug Fix" + }, + { + "name": "docs", + "emoji": "📚", + "description": "Documentation only changes" + }, + { + "name": "style", + "emoji": "💄", + "description": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" + }, + { + "name": "refactor", + "emoji": "📦", + "description": "A code change that neither fixes a bug nor adds a feature" + }, + { + "name": "perf", + "emoji": "🚀", + "description": "A code change that improves performance" + }, + { + "name": "test", + "emoji": "🚨", + "description": "Adding missing tests or correcting existing tests" + }, + { + "name": "ci", + "emoji": "⚙️", + "description": "Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)" + }, + { + "name": "build", + "emoji": "🛠", + "description": "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)" + }, + { + "name": "chore", + "emoji": "♻️", + "description": "Other changes that don't modify src or test files" + }, + { + "name": "revert", + "emoji": "🗑", + "description": "Reverts a previous commit" + } +] diff --git a/src/gitmo.ts b/src/gitmo.ts old mode 100755 new mode 100644 index 2e1118b..e414ff1 --- a/src/gitmo.ts +++ b/src/gitmo.ts @@ -1,18 +1,15 @@ import { Command } from "commander"; import shell from "shelljs"; +import packageJson from "../package.json"; +import transformMessage from "@/utils/transformMessage.js"; import hasStagedChanges from "@/utils/hasStagedChanges.js"; -import isConventionalCommit from "@/utils/isConventionalCommit.js"; -import onCancel from "@/utils/onCancel.js"; -import promptContinue from "@/utils/promptContinue.js"; -import showCommitMessagePrompt from "@/utils/showCommitMessagePrompt.js"; -import packageJson from "../package.json"; shell.config.silent = false; const program = new Command(); -export const init = () => { +const gitmo = () => { try { program .name("gitmo") @@ -25,55 +22,17 @@ export const init = () => { .command("cm [message]") .description("Submit commit") .action(async (message) => { - if (!message) { - if (hasStagedChanges()) { - const modifiedMessage = await showCommitMessagePrompt(); - shell.exec(`git commit -m '${modifiedMessage}'`); - } - } else { - const isValidCommit = await isConventionalCommit(message); - if (isValidCommit) { - shell.exec(`git commit -m '${message}'`); - } else { - const response = await promptContinue(); - if (response.continue === "yes") { - shell.exec(`git commit -m '${message}'`); - } else { - onCancel(); - } - } - } - }); - - program - .command("ac [message]") - .description("Ament last commit") - .action(async (message) => { - if (!message) { - const modifiedMessage = await showCommitMessagePrompt(); - shell.exec(`git commit -m '${modifiedMessage}'`); - } else { - const isValidCommit = await isConventionalCommit(message); - if (isValidCommit) { - shell.exec(`git commit --amend -m '${message}'`); + const stagedChagesExists = await hasStagedChanges(); + if (stagedChagesExists) { + if (message) { + const transformedMessage = await transformMessage(message); + console.log(transformedMessage); // commit this } else { - const response = await promptContinue(); - if (response.continue === "yes") { - shell.exec(`git commit --amend -m '${message}'`); - } else { - onCancel(); - } + console.log("prompt"); } } }); - program - .command("update") - .description("Update gitmo cli") - .action(() => { - shell.exec("npm i -g gitmo"); - }); - // Parse the command-line arguments program.parse(process.argv); } catch (error) { @@ -81,3 +40,5 @@ export const init = () => { process.exit(1); } }; + +export default gitmo; diff --git a/src/index.ts b/src/index.ts index b97f38a..31eeaf7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ #!/usr/bin/env node -import { init } from "./gitmo.js"; +import gitmo from "./gitmo.js"; -init(); +gitmo(); diff --git a/src/utils/hasStagedChanges.ts b/src/utils/hasStagedChanges.ts index 99c7018..2ed0178 100644 --- a/src/utils/hasStagedChanges.ts +++ b/src/utils/hasStagedChanges.ts @@ -1,22 +1,16 @@ import shell from "shelljs"; -// check if there is exisiting staged commit const hasStagedChanges = () => { - // check if there are staged changes const gitStatus = shell.exec("git status --porcelain", { silent: true, }).stdout; - // Parse the status output - const stagedChanges = gitStatus - .split("\n") - .filter((line) => line.startsWith("A ") || line.startsWith("M ")); + const stagedChanges = gitStatus.split("\n").filter((line) => /^[AM] /.test(line)); + if (stagedChanges.length === 0) { - console.log( - "You have no changes staged for commit. Please stage you commit before continuing!", - ); - } else { - return true; + console.log("You have no changes staged for commit. Please stage you commit before continuing!"); + return false; } + return true; }; export default hasStagedChanges; diff --git a/src/utils/optionsPrompt.ts b/src/utils/optionsPrompt.ts new file mode 100644 index 0000000..6bf9899 --- /dev/null +++ b/src/utils/optionsPrompt.ts @@ -0,0 +1,26 @@ +import prompts from "prompts"; + +const onCancel = () => { + console.log("Exited Gitmo!"); + process.exit(1); +}; + +const optionsPrompt = async () => { + const response = await prompts( + { + type: "select", + name: "continue", + message: "Not a conventional commit. continue anyway?", + choices: [ + { title: "Yes", value: "yes" }, + { title: "Edit commit message", value: "correction" }, + { title: "Cancel", value: "cancel" }, + ], + instructions: false, + }, + { onCancel }, + ); + return response; +}; + +export default optionsPrompt; diff --git a/src/utils/transformMessage.ts b/src/utils/transformMessage.ts new file mode 100644 index 0000000..ee94079 --- /dev/null +++ b/src/utils/transformMessage.ts @@ -0,0 +1,17 @@ +import emojisData from "@/emojis-data.json"; +import optionsPrompt from "./optionsPrompt.js"; + +// this adds appropriate emoji to message +const transformMessage = async (originalMessage: string) => { + const prefixes = ["feat", "fix", "docs", "style", "refactor", "perf", "test", "ci", "build", "chore", "revert"]; + const getMatchedPrefix = prefixes.find((prefix) => originalMessage.startsWith(prefix)); + if (getMatchedPrefix) { + const findMatchedEmoji = emojisData.find((item) => item.name === getMatchedPrefix); + return `${originalMessage} ${findMatchedEmoji?.emoji}`; + } + console.log("Not a conventional commit"); + await optionsPrompt(); + return; +}; + +export default transformMessage; diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..2a50969 --- /dev/null +++ b/text.txt @@ -0,0 +1,7 @@ +$ gitmo cm + + +$ gitmo cm "MESSAGE" + +- get original message +- transform original message \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 27dbfd7..0ac3cfe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,19 @@ { - "compilerOptions": { - "target": "es6", - "module": "node16", - "moduleResolution": "node16", - "baseUrl": "./", - "paths": { - "@/*": ["./src/*"] - }, - "resolveJsonModule": true, - "allowJs": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true, - "noEmit": true, - "outDir": "dist" - } + "compilerOptions": { + "target": "es6", + "module": "node16", + "moduleResolution": "node16", + "baseUrl": "./", + "paths": { + "@/*": ["src/*"] + }, + "resolveJsonModule": true, + "allowJs": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noEmit": true, + "outDir": "dist" + } } From 46481ad95579abde1a17b9e2c826933e5a7d98ae Mon Sep 17 00:00:00 2001 From: ize-302 Date: Sat, 28 Sep 2024 15:39:58 +0100 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20missing=20emoji=20in=20commit=20mess?= =?UTF-8?q?age=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- oldsrc/commit-types.json | 57 ----------------- oldsrc/gitmo.ts | 83 ------------------------- oldsrc/index.ts | 4 -- oldsrc/utils/hasStagedChanges.ts | 22 ------- oldsrc/utils/isConventionalCommit.ts | 34 ---------- oldsrc/utils/promptContinue.ts | 22 ------- oldsrc/utils/showCommitMessagePrompt.ts | 34 ---------- src/gitmo.ts | 28 ++++++++- src/utils/commitMessagePrompt.ts | 17 +++++ {oldsrc => src}/utils/onCancel.ts | 0 src/utils/optionsPrompt.ts | 8 +-- src/utils/transformMessage.ts | 15 +++-- text.txt | 7 --- 14 files changed, 57 insertions(+), 276 deletions(-) delete mode 100644 oldsrc/commit-types.json delete mode 100755 oldsrc/gitmo.ts delete mode 100644 oldsrc/index.ts delete mode 100644 oldsrc/utils/hasStagedChanges.ts delete mode 100644 oldsrc/utils/isConventionalCommit.ts delete mode 100644 oldsrc/utils/promptContinue.ts delete mode 100644 oldsrc/utils/showCommitMessagePrompt.ts create mode 100644 src/utils/commitMessagePrompt.ts rename {oldsrc => src}/utils/onCancel.ts (100%) delete mode 100644 text.txt diff --git a/README.md b/README.md index 7d11e29..2e8d214 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ press ENTER bun run build # Run command -./dist/cli.js gitmo cm +node ./dist/cli.js gitmo cm ``` --- diff --git a/oldsrc/commit-types.json b/oldsrc/commit-types.json deleted file mode 100644 index ed3b424..0000000 --- a/oldsrc/commit-types.json +++ /dev/null @@ -1,57 +0,0 @@ -[ - { - "name": "feat", - "emoji": "✨", - "description": "A new feature" - }, - { - "name": "fix", - "emoji": "🐛", - "description": "A bug Fix" - }, - { - "name": "docs", - "emoji": "📚", - "description": "Documentation only changes" - }, - { - "name": "style", - "emoji": "💄", - "description": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" - }, - { - "name": "refactor", - "emoji": "📦", - "description": "A code change that neither fixes a bug nor adds a feature" - }, - { - "name": "perf", - "emoji": "🚀", - "description": "A code change that improves performance" - }, - { - "name": "test", - "emoji": "🚨", - "description": "Adding missing tests or correcting existing tests" - }, - { - "name": "ci", - "emoji": "⚙️", - "description": "Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)" - }, - { - "name": "build", - "emoji": "🛠", - "description": "Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)" - }, - { - "name": "chore", - "emoji": "♻️", - "description": "Other changes that don't modify src or test files" - }, - { - "name": "revert", - "emoji": "🗑", - "description": "Reverts a previous commit" - } -] diff --git a/oldsrc/gitmo.ts b/oldsrc/gitmo.ts deleted file mode 100755 index ccd6180..0000000 --- a/oldsrc/gitmo.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { Command } from "commander"; -import shell from "shelljs"; - -import hasStagedChanges from "oldsrc/utils/hasStagedChanges.js"; -import isConventionalCommit from "oldsrc/utils/isConventionalCommit.js"; -import onCancel from "oldsrc/utils/onCancel.js"; -import promptContinue from "oldsrc/utils/promptContinue.js"; -import showCommitMessagePrompt from "oldsrc/utils/showCommitMessagePrompt.js"; -import packageJson from "../package.json"; - -shell.config.silent = false; - -const program = new Command(); - -export const init = () => { - try { - program - .name("gitmo") - .description( - "A cli tool that adds appropriate emoji to your commit message based on conventional commits specification", - ) - .version(packageJson.version); - - program - .command("cm [message]") - .description("Submit commit") - .action(async (message) => { - if (!message) { - if (hasStagedChanges()) { - const modifiedMessage = await showCommitMessagePrompt(); - shell.exec(`git commit -m '${modifiedMessage}'`); - } - } else { - const isValidCommit = await isConventionalCommit(message); - if (isValidCommit) { - shell.exec(`git commit -m '${message}'`); - } else { - const response = await promptContinue(); - if (response.continue === "yes") { - shell.exec(`git commit -m '${message}'`); - } else { - onCancel(); - } - } - } - }); - - program - .command("ac [message]") - .description("Ament last commit") - .action(async (message) => { - if (!message) { - const modifiedMessage = await showCommitMessagePrompt(); - shell.exec(`git commit -m '${modifiedMessage}'`); - } else { - const isValidCommit = await isConventionalCommit(message); - if (isValidCommit) { - shell.exec(`git commit --amend -m '${message}'`); - } else { - const response = await promptContinue(); - if (response.continue === "yes") { - shell.exec(`git commit --amend -m '${message}'`); - } else { - onCancel(); - } - } - } - }); - - program - .command("update") - .description("Update gitmo cli") - .action(() => { - shell.exec("npm i -g gitmo"); - }); - - // Parse the command-line arguments - program.parse(process.argv); - } catch (error) { - console.log(error); - process.exit(1); - } -}; diff --git a/oldsrc/index.ts b/oldsrc/index.ts deleted file mode 100644 index b97f38a..0000000 --- a/oldsrc/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env node -import { init } from "./gitmo.js"; - -init(); diff --git a/oldsrc/utils/hasStagedChanges.ts b/oldsrc/utils/hasStagedChanges.ts deleted file mode 100644 index 99c7018..0000000 --- a/oldsrc/utils/hasStagedChanges.ts +++ /dev/null @@ -1,22 +0,0 @@ -import shell from "shelljs"; - -// check if there is exisiting staged commit -const hasStagedChanges = () => { - // check if there are staged changes - const gitStatus = shell.exec("git status --porcelain", { - silent: true, - }).stdout; - // Parse the status output - const stagedChanges = gitStatus - .split("\n") - .filter((line) => line.startsWith("A ") || line.startsWith("M ")); - if (stagedChanges.length === 0) { - console.log( - "You have no changes staged for commit. Please stage you commit before continuing!", - ); - } else { - return true; - } -}; - -export default hasStagedChanges; diff --git a/oldsrc/utils/isConventionalCommit.ts b/oldsrc/utils/isConventionalCommit.ts deleted file mode 100644 index c8e5b40..0000000 --- a/oldsrc/utils/isConventionalCommit.ts +++ /dev/null @@ -1,34 +0,0 @@ -import commitTypes from "@/commit-types.json"; - -const isConventionalCommit = async (commitMessage: string) => { - if ( - commitMessage.startsWith("feat") || - commitMessage.startsWith("fix") || - commitMessage.startsWith("docs") || - commitMessage.startsWith("style") || - commitMessage.startsWith("refactor") || - commitMessage.startsWith("perf") || - commitMessage.startsWith("test") || - commitMessage.startsWith("ci") || - commitMessage.startsWith("build") || - commitMessage.startsWith("chore") || - commitMessage.startsWith("revert") - ) { - /** - * Here is the structure of a typical conventional commit message - * type(scope): commit message - * - * Note: I want to extract the type i.e the string that comes before "(scope)" - * */ - // Find the index of the opening parenthesis and colon - const parenthesisIndex = commitMessage.indexOf("("); - const colonIndex = commitMessage.indexOf(":"); - // Determine the end index for the substring (whichever comes first) - const endIndex = parenthesisIndex !== -1 ? parenthesisIndex : colonIndex; - // Extract the substring from the start to the end index - const beforeColon = commitMessage.substring(0, endIndex).trim(); - return commitTypes.find((item) => item.name === beforeColon); - } -}; - -export default isConventionalCommit; diff --git a/oldsrc/utils/promptContinue.ts b/oldsrc/utils/promptContinue.ts deleted file mode 100644 index 1de5728..0000000 --- a/oldsrc/utils/promptContinue.ts +++ /dev/null @@ -1,22 +0,0 @@ -import prompts from "prompts"; -import onCancel from "./onCancel.js"; - -const promptContinue = async () => { - const response = await prompts( - { - type: "select", - name: "continue", - message: "Not a conventional commit. continue anyway?", - choices: [ - { title: "Yes", value: "yes" }, - { title: "Edit commit message", value: "correction" }, - { title: "Cancel", value: "cancel" }, - ], - instructions: false, - }, - { onCancel }, - ); - return response; -}; - -export default promptContinue; diff --git a/oldsrc/utils/showCommitMessagePrompt.ts b/oldsrc/utils/showCommitMessagePrompt.ts deleted file mode 100644 index 2c65269..0000000 --- a/oldsrc/utils/showCommitMessagePrompt.ts +++ /dev/null @@ -1,34 +0,0 @@ -import prompts from "prompts"; -import isConventionalCommit from "./isConventionalCommit.js"; -import onCancel from "./onCancel.js"; -import promptContinue from "./promptContinue.js"; - -const showCommitMessagePrompt = async () => { - const response = await prompts( - { - type: "text", - name: "commitMessage", - message: "commit message", - }, - { onCancel }, - ); - const { commitMessage } = response; - let updatedCommitMessage = ""; - const isValidCommit = await isConventionalCommit(commitMessage); - if (isValidCommit) { - updatedCommitMessage = `${response.commitMessage} ${isValidCommit?.emoji}`; - } else { - console.log( - "Commit message does not align with the specifications of conventional commit", - ); - const response = await promptContinue(); - if (response.continue === "yes") { - updatedCommitMessage = commitMessage; - } else { - onCancel(); - } - } - return updatedCommitMessage; -}; - -export default showCommitMessagePrompt; diff --git a/src/gitmo.ts b/src/gitmo.ts index e414ff1..b23cc5d 100644 --- a/src/gitmo.ts +++ b/src/gitmo.ts @@ -4,6 +4,7 @@ import packageJson from "../package.json"; import transformMessage from "@/utils/transformMessage.js"; import hasStagedChanges from "@/utils/hasStagedChanges.js"; +import commitMessagePrompt from "@/utils/commitMessagePrompt.js"; shell.config.silent = false; @@ -26,13 +27,36 @@ const gitmo = () => { if (stagedChagesExists) { if (message) { const transformedMessage = await transformMessage(message); - console.log(transformedMessage); // commit this + shell.exec(`git commit -m '${transformedMessage}'`); } else { - console.log("prompt"); + const response = await commitMessagePrompt(); + const transformedMessage = await transformMessage(response.commitMessage); + shell.exec(`git commit -m '${transformedMessage}'`); } } }); + program + .command("ac [message]") + .description("Ament last commit") + .action(async (message) => { + if (message) { + const transformedMessage = await transformMessage(message); + shell.exec(`git commit --amend -m '${transformedMessage}'`); + } else { + const response = await commitMessagePrompt(); + const transformedMessage = await transformMessage(response.commitMessage); + shell.exec(`git commit --amend -m '${transformedMessage}'`); + } + }); + + program + .command("update") + .description("Update gitmo cli") + .action(() => { + shell.exec("npm i -g gitmo"); + }); + // Parse the command-line arguments program.parse(process.argv); } catch (error) { diff --git a/src/utils/commitMessagePrompt.ts b/src/utils/commitMessagePrompt.ts new file mode 100644 index 0000000..2edc276 --- /dev/null +++ b/src/utils/commitMessagePrompt.ts @@ -0,0 +1,17 @@ +import prompts from "prompts"; +import onCancel from "@/utils/onCancel.js"; + +const commitMessagePrompt = async (originalMessage?: string) => { + const commitMessageResponse = await prompts( + { + type: "text", + name: "commitMessage", + message: "commit message", + initial: originalMessage, + }, + { onCancel }, + ); + return commitMessageResponse; +}; + +export default commitMessagePrompt; diff --git a/oldsrc/utils/onCancel.ts b/src/utils/onCancel.ts similarity index 100% rename from oldsrc/utils/onCancel.ts rename to src/utils/onCancel.ts diff --git a/src/utils/optionsPrompt.ts b/src/utils/optionsPrompt.ts index 6bf9899..e9e6150 100644 --- a/src/utils/optionsPrompt.ts +++ b/src/utils/optionsPrompt.ts @@ -1,15 +1,11 @@ import prompts from "prompts"; - -const onCancel = () => { - console.log("Exited Gitmo!"); - process.exit(1); -}; +import onCancel from "@/utils/onCancel.js"; const optionsPrompt = async () => { const response = await prompts( { type: "select", - name: "continue", + name: "choice", message: "Not a conventional commit. continue anyway?", choices: [ { title: "Yes", value: "yes" }, diff --git a/src/utils/transformMessage.ts b/src/utils/transformMessage.ts index ee94079..c7b109b 100644 --- a/src/utils/transformMessage.ts +++ b/src/utils/transformMessage.ts @@ -1,5 +1,6 @@ import emojisData from "@/emojis-data.json"; -import optionsPrompt from "./optionsPrompt.js"; +import optionsPrompt from "@/utils/optionsPrompt.js"; +import commitMessagePrompt from "@/utils/commitMessagePrompt.js"; // this adds appropriate emoji to message const transformMessage = async (originalMessage: string) => { @@ -9,9 +10,15 @@ const transformMessage = async (originalMessage: string) => { const findMatchedEmoji = emojisData.find((item) => item.name === getMatchedPrefix); return `${originalMessage} ${findMatchedEmoji?.emoji}`; } - console.log("Not a conventional commit"); - await optionsPrompt(); - return; + // ("Not a conventional commit. Pick an option to continue"); + const optionsPromptResponse = await optionsPrompt(); + if (optionsPromptResponse.choice === "yes") { + return originalMessage; + } + if (optionsPromptResponse.choice === "correction") { + const response = await commitMessagePrompt(originalMessage); + return await transformMessage(response.commitMessage); + } }; export default transformMessage; diff --git a/text.txt b/text.txt deleted file mode 100644 index 2a50969..0000000 --- a/text.txt +++ /dev/null @@ -1,7 +0,0 @@ -$ gitmo cm - - -$ gitmo cm "MESSAGE" - -- get original message -- transform original message \ No newline at end of file