Skip to content

Commit

Permalink
Merge pull request #12 from ize-302/develop
Browse files Browse the repository at this point in the history
feat: add amend commit command
  • Loading branch information
ize-302 authored Sep 27, 2024
2 parents e5fa98f + 3397500 commit 8626359
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 182 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ gitmo --help
```

```
A cli tool that adds appropriate emoji
to your commit message based on conventional commits specification
Usage
$ gitmo [option] [command]
Options
--commit, -c Add commit using the gitmo
--version, -v Print current installed version
--update, -u Update gitmo cli
Examples
$ gitmo -c
Usage: gitmo [options] [command]
A cli tool that adds appropriate emoji to your commit message based on conventional commits specification
Options:
-V, --version output the version number
-h, --help display help for command
Commands:
cm [message] Submit commit
ac [message] Ament last commit
update Update gitmo cli
help [command] display help for command
Example:
gitmo cm "feat: first commit"
```

## Commit types
Expand All @@ -60,7 +65,7 @@ gitmo --help

```bash
# Note: This should be done after staging your changes
gitmo -c
gitmo cm
```

You get this prompt:
Expand All @@ -80,7 +85,7 @@ press ENTER
bun run build

# Run command
./dist/cli.js gitmo -c
./dist/cli.js gitmo cm
```

---
Expand Down
2 changes: 1 addition & 1 deletion build.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
await Bun.build({
target: 'node',
entrypoints: ['./src/cli.ts'],
entrypoints: ['./src/index.ts'],
outdir: './dist',
minify: true,
external: ['shelljs'],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "A cli tool that adds appropriate emoji to your commit message based on conventional commits specification",
"bin": {
"gitmo": "dist/cli.js"
"gitmo": "./dist/index.js"
},
"type": "module",
"scripts": {
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"homepage": "https://github.com/ize-302/gitmo",
"dependencies": {
"meow": "^13.2.0",
"commander": "^12.1.0",
"prompts": "^2.4.2",
"shelljs": "^0.8.5"
},
Expand All @@ -49,5 +49,6 @@
"@types/shelljs": "^0.8.15",
"typescript": "^5.6.2"
},
"module": "index.ts"
"module": "index.ts",
"main": "dist/index.js"
}
28 changes: 0 additions & 28 deletions src/cli.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/constants/flags.js

This file was deleted.

83 changes: 83 additions & 0 deletions src/gitmo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Command } from "commander";
import shell from "shelljs";

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 = () => {
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);
}
};
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
import { init } from "./gitmo.js";

init();
129 changes: 0 additions & 129 deletions src/utils/handleCommand.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/utils/hasStagedChanges.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 8626359

Please sign in to comment.