chore: add eslint (#37)
This commit is contained in:
60
package.json
60
package.json
@@ -1,29 +1,35 @@
|
|||||||
{
|
{
|
||||||
"name": "aicommits",
|
"name": "aicommits",
|
||||||
"version": "1.0.5",
|
"version": "1.0.5",
|
||||||
"description": "Writes your git commit messages for you with AI",
|
"description": "Writes your git commit messages for you with AI",
|
||||||
"files": [
|
"keywords": [
|
||||||
"dist"
|
"ai",
|
||||||
],
|
"git",
|
||||||
"bin": "dist/index.js",
|
"commit"
|
||||||
"repository": "https://github.com/Nutlope/aicommits",
|
],
|
||||||
"author": "Hassan El Mghari (@nutlope)",
|
"license": "MIT",
|
||||||
"license": "MIT",
|
"repository": "https://github.com/Nutlope/aicommits",
|
||||||
"devDependencies": {
|
"author": "Hassan El Mghari (@nutlope)",
|
||||||
"@types/node": "^18.13.0",
|
"files": [
|
||||||
"typescript": "^4.9.5"
|
"dist"
|
||||||
},
|
],
|
||||||
"scripts": {
|
"bin": "dist/index.js",
|
||||||
"build": "tsc"
|
"scripts": {
|
||||||
},
|
"build": "tsc",
|
||||||
"dependencies": {
|
"lint": "eslint --cache ."
|
||||||
"chalk": "^4.1.2",
|
},
|
||||||
"inquirer": "^8.0.0",
|
"dependencies": {
|
||||||
"node-fetch": "^2.6.9"
|
"chalk": "^4.1.2",
|
||||||
},
|
"inquirer": "^8.0.0",
|
||||||
"keywords": [
|
"node-fetch": "^2.6.9"
|
||||||
"ai",
|
},
|
||||||
"git",
|
"devDependencies": {
|
||||||
"commit"
|
"@pvtnbr/eslint-config": "^0.33.0",
|
||||||
]
|
"@types/node": "^18.13.0",
|
||||||
|
"eslint": "^8.34.0",
|
||||||
|
"typescript": "^4.9.5"
|
||||||
|
},
|
||||||
|
"eslintConfig": {
|
||||||
|
"extends": "@pvtnbr"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2367
pnpm-lock.yaml
generated
2367
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
198
src/aicommits.ts
198
src/aicommits.ts
@@ -1,121 +1,121 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import chalk from "chalk";
|
import { execSync } from 'child_process';
|
||||||
import { execSync } from "child_process";
|
import chalk from 'chalk';
|
||||||
import inquirer from "inquirer";
|
import inquirer from 'inquirer';
|
||||||
import fetch from "node-fetch";
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
let OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY;
|
const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY;
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommits!"));
|
console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!'));
|
||||||
|
|
||||||
if (!OPENAI_KEY) {
|
if (!OPENAI_KEY) {
|
||||||
console.error(
|
console.error(
|
||||||
chalk.white("▲ ") +
|
`${chalk.white('▲ ')
|
||||||
"Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'"
|
}Please save your OpenAI API key as an env variable by doing 'export OPENAI_KEY=YOUR_API_KEY'`,
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
execSync("git rev-parse --is-inside-work-tree", {
|
execSync('git rev-parse --is-inside-work-tree', {
|
||||||
encoding: "utf8",
|
encoding: 'utf8',
|
||||||
stdio: "ignore",
|
stdio: 'ignore',
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch {
|
||||||
console.error(chalk.white("▲ ") + "This is not a git repository");
|
console.error(`${chalk.white('▲ ')}This is not a git repository`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const diff = execSync(
|
const diff = execSync(
|
||||||
`git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"`,
|
'git diff --cached . ":(exclude)package-lock.json" ":(exclude)yarn.lock" ":(exclude)pnpm-lock.yaml"',
|
||||||
{
|
{
|
||||||
encoding: "utf8",
|
encoding: 'utf8',
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!diff) {
|
if (!diff) {
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") +
|
`${chalk.white('▲ ')
|
||||||
"No staged changes found. Make sure there are changes and run `git add .`"
|
}No staged changes found. Make sure there are changes and run \`git add .\``,
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
// Accounting for GPT-3's input req of 4k tokens (approx 8k chars)
|
||||||
if (diff.length > 8000) {
|
if (diff.length > 8000) {
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + "The diff is too large to write a commit message."
|
`${chalk.white('▲ ')}The diff is too large to write a commit message.`,
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
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}`;
|
const 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}`;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + chalk.gray("Generating your AI commit message...\n")
|
chalk.white('▲ ') + chalk.gray('Generating your AI commit message...\n'),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const aiCommitMessage = await generateCommitMessage(prompt);
|
const aiCommitMessage = await generateCommitMessage(prompt);
|
||||||
console.log(
|
console.log(
|
||||||
chalk.white("▲ ") + chalk.bold("Commit message: ") + aiCommitMessage +
|
`${chalk.white('▲ ') + chalk.bold('Commit message: ') + aiCommitMessage
|
||||||
"\n",
|
}\n`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const confirmationMessage = await inquirer.prompt([
|
const confirmationMessage = await inquirer.prompt([
|
||||||
{
|
{
|
||||||
name: "useCommitMessage",
|
name: 'useCommitMessage',
|
||||||
message: "Would you like to use this commit message? (Y / n)",
|
message: 'Would you like to use this commit message? (Y / n)',
|
||||||
choices: ["Y", "y", "n"],
|
choices: ['Y', 'y', 'n'],
|
||||||
default: "y",
|
default: 'y',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (confirmationMessage.useCommitMessage === "n") {
|
if (confirmationMessage.useCommitMessage === 'n') {
|
||||||
console.log(chalk.white("▲ ") + "Commit message has not been commited.");
|
console.log(`${chalk.white('▲ ')}Commit message has not been commited.`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
execSync(`git commit -m "${aiCommitMessage}"`, {
|
execSync(`git commit -m "${aiCommitMessage}"`, {
|
||||||
stdio: "inherit",
|
stdio: 'inherit',
|
||||||
encoding: "utf8",
|
encoding: 'utf8',
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
console.error(chalk.white("▲ ") + chalk.red(e.message));
|
console.error(chalk.white('▲ ') + chalk.red(error.message));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateCommitMessage(prompt: string) {
|
async function generateCommitMessage(prompt: string) {
|
||||||
const payload = {
|
const payload = {
|
||||||
model: "text-davinci-003",
|
model: 'text-davinci-003',
|
||||||
prompt,
|
prompt,
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
top_p: 1,
|
top_p: 1,
|
||||||
frequency_penalty: 0,
|
frequency_penalty: 0,
|
||||||
presence_penalty: 0,
|
presence_penalty: 0,
|
||||||
max_tokens: 200,
|
max_tokens: 200,
|
||||||
stream: false,
|
stream: false,
|
||||||
n: 1,
|
n: 1,
|
||||||
};
|
};
|
||||||
const response = await fetch("https://api.openai.com/v1/completions", {
|
const response = await fetch('https://api.openai.com/v1/completions', {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${OPENAI_KEY ?? ""}`,
|
Authorization: `Bearer ${OPENAI_KEY ?? ''}`,
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: 'POST',
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
});
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
const errorJson: any = await response.json();
|
const errorJson: any = await response.json();
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`OpenAI API failed while processing the request '${errorJson?.error?.message}'`,
|
`OpenAI API failed while processing the request '${errorJson?.error?.message}'`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const json: any = await response.json();
|
const json: any = await response.json();
|
||||||
const aiCommit = json.choices[0].text;
|
const aiCommit = json.choices[0].text;
|
||||||
|
|
||||||
return aiCommit.replace(/(\r\n|\n|\r)/gm, "");
|
return aiCommit.replace(/(\r\n|\n|\r)/g, '');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#! /usr/bin/env node
|
#! /usr/bin/env node
|
||||||
import { main } from "./aicommits";
|
import { main } from './aicommits';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
await main();
|
await main();
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es2016",
|
"target": "es2016",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user