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