aicommits 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.main = void 0;
8
+ const chalk_1 = __importDefault(require("chalk"));
9
+ const child_process_1 = require("child_process");
10
+ const inquirer_1 = __importDefault(require("inquirer"));
11
+ const node_fetch_1 = __importDefault(require("node-fetch"));
12
+ let OPENAI_API_KEY = process.env.OPENAI_API_KEY;
13
+ async function main() {
14
+ console.log(chalk_1.default.white("▲ ") + chalk_1.default.green("Welcome to AICommit!"));
15
+ if (!OPENAI_API_KEY) {
16
+ console.error(chalk_1.default.white("▲ ") +
17
+ "Please specify an OpenAI key using export OPEN_AI_KEY='YOUR_API_KEY'");
18
+ process.exit(1);
19
+ }
20
+ try {
21
+ (0, child_process_1.execSync)("git rev-parse --is-inside-work-tree", {
22
+ encoding: "utf8",
23
+ stdio: "ignore",
24
+ });
25
+ }
26
+ catch (e) {
27
+ console.error(chalk_1.default.white("▲ ") + "This is not a git repository");
28
+ process.exit(1);
29
+ }
30
+ const diff = (0, child_process_1.execSync)("git diff --cached", { encoding: "utf8" });
31
+ if (!diff) {
32
+ console.log(chalk_1.default.white("▲ ") +
33
+ "No staged changes found. Make sure there are changes and run `git add .`");
34
+ process.exit(1);
35
+ }
36
+ // Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
37
+ if (diff.length > 8000) {
38
+ console.log(chalk_1.default.white("▲ ") + "The diff is too large to write a commit message.");
39
+ process.exit(1);
40
+ }
41
+ let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, return a complete sentence, and do not repeat yourself: ${diff}`;
42
+ console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
43
+ const aiCommitMessage = await generateCommitMessage(prompt);
44
+ console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage + "\n");
45
+ const confirmationMessage = await inquirer_1.default.prompt([
46
+ {
47
+ name: "useCommitMessage",
48
+ message: "Would you like to use this commit message? (Y / n)",
49
+ choices: ["Y", "y", "n"],
50
+ default: "y",
51
+ },
52
+ ]);
53
+ if (confirmationMessage.useCommitMessage === "n") {
54
+ console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
55
+ process.exit(1);
56
+ }
57
+ (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
58
+ stdio: "inherit",
59
+ encoding: "utf8",
60
+ });
61
+ }
62
+ exports.main = main;
63
+ async function generateCommitMessage(prompt) {
64
+ const payload = {
65
+ model: "text-davinci-003",
66
+ prompt,
67
+ temperature: 0.7,
68
+ top_p: 1,
69
+ frequency_penalty: 0,
70
+ presence_penalty: 0,
71
+ max_tokens: 200,
72
+ stream: false,
73
+ n: 1,
74
+ };
75
+ const response = await (0, node_fetch_1.default)("https://api.openai.com/v1/completions", {
76
+ headers: {
77
+ "Content-Type": "application/json",
78
+ Authorization: `Bearer ${OPENAI_API_KEY !== null && OPENAI_API_KEY !== void 0 ? OPENAI_API_KEY : ""}`,
79
+ },
80
+ method: "POST",
81
+ body: JSON.stringify(payload),
82
+ });
83
+ const json = await response.json();
84
+ const aiCommit = json.choices[0].text;
85
+ return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
86
+ }
package/bin/index.js ADDED
@@ -0,0 +1,7 @@
1
+ #! /usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const aicommits_1 = require("./aicommits");
5
+ (async () => {
6
+ await (0, aicommits_1.main)();
7
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aicommits",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Writes your git commit messages for you with AI",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -10,6 +10,7 @@
10
10
  "author": "Hassan El Mghari (@nutlope)",
11
11
  "license": "MIT",
12
12
  "devDependencies": {
13
+ "@types/node": "^18.13.0",
13
14
  "typescript": "^4.9.5"
14
15
  },
15
16
  "scripts": {
package/tsconfig.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
4
- "module": "commonjs" /* Specify what module code is generated. */,
5
- "outDir": "./bin" /* Specify an output folder for all emitted files. */,
6
- "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
7
- "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
3
+ "target": "es2017",
4
+ "module": "commonjs",
5
+ "outDir": "./bin",
6
+ "esModuleInterop": true,
7
+ "forceConsistentCasingInFileNames": true,
8
8
  "skipLibCheck": true /* Skip type checking all .d.ts files. */
9
9
  },
10
10
  "exclude": ["node_modules"]
package/index.ts DELETED
@@ -1,106 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import chalk from "chalk";
4
- import { execSync } from "child_process";
5
- import inquirer from "inquirer";
6
- import fetch from "node-fetch";
7
-
8
- let OPENAI_API_KEY = process.env.OPENAI_API_KEY;
9
-
10
- async function main() {
11
- console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommit!"));
12
-
13
- if (!OPENAI_API_KEY) {
14
- console.error(
15
- chalk.white("▲ ") +
16
- "Please specify an OpenAI key using export OPEN_AI_KEY='YOUR_API_KEY'"
17
- );
18
- process.exit(1);
19
- }
20
- try {
21
- execSync("git rev-parse --is-inside-work-tree", {
22
- encoding: "utf8",
23
- stdio: "ignore",
24
- });
25
- } catch (e) {
26
- console.error(chalk.white("▲ ") + "This is not a git repository");
27
- process.exit(1);
28
- }
29
-
30
- const diff = execSync("git diff --cached", { encoding: "utf8" });
31
-
32
- if (!diff) {
33
- console.log(
34
- chalk.white("▲ ") +
35
- "No staged changes found. Make sure there are changes and run `git add .`"
36
- );
37
- process.exit(1);
38
- }
39
-
40
- // Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
41
- if (diff.length > 8000) {
42
- console.log(
43
- chalk.white("▲ ") + "The diff is too large to write a commit message."
44
- );
45
- process.exit(1);
46
- }
47
-
48
- let prompt = `I want you to act like a git commit message writer. I will input a git diff and your job is to convert it into a useful commit message. Do not preface the commit with anything, return a complete sentence, and do not repeat yourself: ${diff}`;
49
-
50
- console.log(
51
- chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n")
52
- );
53
- const aiCommitMessage = await generateCommitMessage(prompt);
54
-
55
- console.log(
56
- chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage + "\n"
57
- );
58
-
59
- const confirmationMessage = await inquirer.prompt([
60
- {
61
- name: "useCommitMessage",
62
- message: "Would you like to use this commit message? (Y / n)",
63
- choices: ["Y", "y", "n"],
64
- default: "y",
65
- },
66
- ]);
67
-
68
- if (confirmationMessage.useCommitMessage === "n") {
69
- console.log(chalk.white("▲ ") + "Commit message has not been commited.");
70
- process.exit(1);
71
- }
72
-
73
- execSync(`git commit -m "${aiCommitMessage}"`, {
74
- stdio: "inherit",
75
- encoding: "utf8",
76
- });
77
- }
78
-
79
- async function generateCommitMessage(prompt: string) {
80
- const payload = {
81
- model: "text-davinci-003",
82
- prompt,
83
- temperature: 0.7,
84
- top_p: 1,
85
- frequency_penalty: 0,
86
- presence_penalty: 0,
87
- max_tokens: 200,
88
- stream: false,
89
- n: 1,
90
- };
91
- const response = await fetch("https://api.openai.com/v1/completions", {
92
- headers: {
93
- "Content-Type": "application/json",
94
- Authorization: `Bearer ${OPENAI_API_KEY ?? ""}`,
95
- },
96
- method: "POST",
97
- body: JSON.stringify(payload),
98
- });
99
-
100
- const json: any = await response.json();
101
- const aiCommit = json.choices[0].text;
102
-
103
- return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
104
- }
105
-
106
- await main();
package/screenshot.png DELETED
Binary file