Skip to content

Commit

Permalink
Merge pull request #13 from ize-302/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ize-302 authored Sep 28, 2024
2 parents 8626359 + 46481ad commit 0ddda12
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 174 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ press ENTER
bun run build

# Run command
./dist/cli.js gitmo cm
node ./dist/cli.js gitmo cm
```

---
Expand Down
69 changes: 35 additions & 34 deletions biome.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
File renamed without changes.
57 changes: 21 additions & 36 deletions src/gitmo.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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";
import commitMessagePrompt from "@/utils/commitMessagePrompt.js";

shell.config.silent = false;

const program = new Command();

export const init = () => {
const gitmo = () => {
try {
program
.name("gitmo")
Expand All @@ -25,22 +23,15 @@ 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}'`);
const stagedChagesExists = await hasStagedChanges();
if (stagedChagesExists) {
if (message) {
const transformedMessage = await transformMessage(message);
shell.exec(`git commit -m '${transformedMessage}'`);
} else {
const response = await promptContinue();
if (response.continue === "yes") {
shell.exec(`git commit -m '${message}'`);
} else {
onCancel();
}
const response = await commitMessagePrompt();
const transformedMessage = await transformMessage(response.commitMessage);
shell.exec(`git commit -m '${transformedMessage}'`);
}
}
});
Expand All @@ -49,29 +40,21 @@ export const init = () => {
.command("ac [message]")
.description("Ament last commit")
.action(async (message) => {
if (!message) {
const modifiedMessage = await showCommitMessagePrompt();
shell.exec(`git commit -m '${modifiedMessage}'`);
if (message) {
const transformedMessage = await transformMessage(message);
shell.exec(`git commit --amend -m '${transformedMessage}'`);
} 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();
}
}
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");
shell.exec("npm i -g gitmo");
});

// Parse the command-line arguments
Expand All @@ -81,3 +64,5 @@ export const init = () => {
process.exit(1);
}
};

export default gitmo;
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import { init } from "./gitmo.js";
import gitmo from "./gitmo.js";

init();
gitmo();
17 changes: 17 additions & 0 deletions src/utils/commitMessagePrompt.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 5 additions & 11 deletions src/utils/hasStagedChanges.ts
Original file line number Diff line number Diff line change
@@ -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;
34 changes: 0 additions & 34 deletions src/utils/isConventionalCommit.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/utils/promptContinue.ts → src/utils/optionsPrompt.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import prompts from "prompts";
import onCancel from "./onCancel.js";
import onCancel from "@/utils/onCancel.js";

const promptContinue = async () => {
const optionsPrompt = async () => {
const response = await prompts(
{
type: "select",
name: "continue",
message: "continue anyway?",
name: "choice",
message: "Not a conventional commit. continue anyway?",
choices: [
{ title: "Yes", value: "yes" },
{ title: "Edit commit message", value: "correction" },
{ title: "Cancel", value: "cancel" },
],
instructions: false,
Expand All @@ -18,4 +19,4 @@ const promptContinue = async () => {
return response;
};

export default promptContinue;
export default optionsPrompt;
34 changes: 0 additions & 34 deletions src/utils/showCommitMessagePrompt.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/utils/transformMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import emojisData from "@/emojis-data.json";
import optionsPrompt from "@/utils/optionsPrompt.js";
import commitMessagePrompt from "@/utils/commitMessagePrompt.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}`;
}
// ("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;
34 changes: 17 additions & 17 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 0ddda12

Please sign in to comment.