aicommits 1.0.4 → 1.0.5

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Hassan El Mghari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  Install the CLI then grab your [OpenAI key](https://openai.com/api/) and add it as an env variable with the two commands below.
18
18
 
19
19
  1. `npm install -g aicommits`
20
- 2. `export OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx`
20
+ 2. `export OPENAI_KEY=sk-xxxxxxxxxxxxxxxx`
21
21
 
22
22
  It's recommended to add the line in #2 to your `.zshrc` or `.bashrc` so it persists instead of having to define it in each terminal session.
23
23
 
@@ -45,6 +45,8 @@ The next version of the CLI, version 2, will address both of these limitations a
45
45
  - Add support for diffs greater than 200 lines by grabbing the diff per file
46
46
  - Add support for a flag that can auto-accept
47
47
  - Add ability to specify a commit message from inside aicommit
48
+ - Use gpt-3-tokenizer
49
+ - Add automated github releases
48
50
  - Add opt-in emoji flag
49
51
  - Add opt-in languages flag
50
52
  - Build landing page for the 2.0 launch
package/bin/aicommits.js CHANGED
@@ -12,19 +12,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12
12
  var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
+ var _a;
15
16
  Object.defineProperty(exports, "__esModule", { value: true });
16
17
  exports.main = void 0;
17
18
  const chalk_1 = __importDefault(require("chalk"));
18
19
  const child_process_1 = require("child_process");
19
20
  const inquirer_1 = __importDefault(require("inquirer"));
20
21
  const node_fetch_1 = __importDefault(require("node-fetch"));
21
- let OPENAI_API_KEY = process.env.OPENAI_API_KEY;
22
+ let OPENAI_KEY = (_a = process.env.OPENAI_KEY) !== null && _a !== void 0 ? _a : process.env.OPENAI_API_KEY;
22
23
  function main() {
23
24
  return __awaiter(this, void 0, void 0, function* () {
24
25
  console.log(chalk_1.default.white("▲ ") + chalk_1.default.green("Welcome to AICommits!"));
25
- if (!OPENAI_API_KEY) {
26
+ if (!OPENAI_KEY) {
26
27
  console.error(chalk_1.default.white("▲ ") +
27
- "Please save your OpenAI API key as an env variable by doing 'export OPENAI_API_KEY=YOUR_API_KEY'");
28
+ "Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'");
28
29
  process.exit(1);
29
30
  }
30
31
  try {
@@ -52,28 +53,36 @@ function main() {
52
53
  }
53
54
  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, use the present tense, return a complete sentence, and do not repeat yourself: ${diff}`;
54
55
  console.log(chalk_1.default.white("▲ ") + chalk_1.default.gray("Generating your AI commit message...\n"));
55
- const aiCommitMessage = yield generateCommitMessage(prompt);
56
- console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage + "\n");
57
- const confirmationMessage = yield inquirer_1.default.prompt([
58
- {
59
- name: "useCommitMessage",
60
- message: "Would you like to use this commit message? (Y / n)",
61
- choices: ["Y", "y", "n"],
62
- default: "y",
63
- },
64
- ]);
65
- if (confirmationMessage.useCommitMessage === "n") {
66
- console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
56
+ try {
57
+ const aiCommitMessage = yield generateCommitMessage(prompt);
58
+ console.log(chalk_1.default.white("▲ ") + chalk_1.default.bold("Commit message: ") + aiCommitMessage +
59
+ "\n");
60
+ const confirmationMessage = yield inquirer_1.default.prompt([
61
+ {
62
+ name: "useCommitMessage",
63
+ message: "Would you like to use this commit message? (Y / n)",
64
+ choices: ["Y", "y", "n"],
65
+ default: "y",
66
+ },
67
+ ]);
68
+ if (confirmationMessage.useCommitMessage === "n") {
69
+ console.log(chalk_1.default.white("▲ ") + "Commit message has not been commited.");
70
+ process.exit(1);
71
+ }
72
+ (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
73
+ stdio: "inherit",
74
+ encoding: "utf8",
75
+ });
76
+ }
77
+ catch (e) {
78
+ console.error(chalk_1.default.white("▲ ") + chalk_1.default.red(e.message));
67
79
  process.exit(1);
68
80
  }
69
- (0, child_process_1.execSync)(`git commit -m "${aiCommitMessage}"`, {
70
- stdio: "inherit",
71
- encoding: "utf8",
72
- });
73
81
  });
74
82
  }
75
83
  exports.main = main;
76
84
  function generateCommitMessage(prompt) {
85
+ var _a;
77
86
  return __awaiter(this, void 0, void 0, function* () {
78
87
  const payload = {
79
88
  model: "text-davinci-003",
@@ -89,11 +98,15 @@ function generateCommitMessage(prompt) {
89
98
  const response = yield (0, node_fetch_1.default)("https://api.openai.com/v1/completions", {
90
99
  headers: {
91
100
  "Content-Type": "application/json",
92
- Authorization: `Bearer ${OPENAI_API_KEY !== null && OPENAI_API_KEY !== void 0 ? OPENAI_API_KEY : ""}`,
101
+ Authorization: `Bearer ${OPENAI_KEY !== null && OPENAI_KEY !== void 0 ? OPENAI_KEY : ""}`,
93
102
  },
94
103
  method: "POST",
95
104
  body: JSON.stringify(payload),
96
105
  });
106
+ if (response.status !== 200) {
107
+ const errorJson = yield response.json();
108
+ throw new Error(`OpenAI API failed while processing the request '${(_a = errorJson === null || errorJson === void 0 ? void 0 : errorJson.error) === null || _a === void 0 ? void 0 : _a.message}'`);
109
+ }
97
110
  const json = yield response.json();
98
111
  const aiCommit = json.choices[0].text;
99
112
  return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aicommits",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Writes your git commit messages for you with AI",
5
5
  "files": [
6
6
  "bin"