11#!/usr/bin/env node
22import OpenAI from "openai" ;
3- import { promisify } from ' util' ;
3+ import { promisify } from " util" ;
44import path from "path" ;
55import process from "process" ;
6- import { exec as originalExec , execSync } from ' child_process' ;
6+ import { exec as originalExec , execSync } from " child_process" ;
77import prompts from "prompts" ;
88import { program } from "commander" ;
99
@@ -13,20 +13,20 @@ let model = "gpt-4-turbo-preview"; // Default model
1313export async function getGitSummary ( ) {
1414 try {
1515 const dotenv = await import ( "dotenv" ) ;
16- const envPath = path . join ( process . cwd ( ) , ' .env' ) ;
16+ const envPath = path . join ( process . cwd ( ) , " .env" ) ;
1717 dotenv . config ( { path : envPath } ) ;
18- openai = new OpenAI ( { apiKey : process . env . OPENAI_API_KEY } ) ;
18+ openai = new OpenAI ( { apiKey : process . env . OPENAI_API_KEY } ) ;
1919
20-
2120 const exec = promisify ( originalExec ) ;
22- const { stdout } = await exec ( "git diff --cached -- . ':(exclude)*lock.json' ':(exclude)*lock.yaml'" ) ;
21+ const { stdout } = await exec (
22+ "git diff --cached -- . ':(exclude)*lock.json' ':(exclude)*lock.yaml'"
23+ ) ;
2324 const summary = stdout . trim ( ) ;
2425 if ( summary . length === 0 ) {
2526 return null ;
2627 }
27-
28+
2829 return summary ;
29-
3030 } catch ( error ) {
3131 console . error ( "Error while summarizing Git changes:" , error ) ;
3232 process . exit ( 1 ) ;
@@ -36,34 +36,39 @@ export async function getGitSummary() {
3636const gptCommit = async ( ) => {
3737 const gitSummary = await getGitSummary ( ) ;
3838 if ( ! gitSummary ) {
39- console . log ( ' No changes to commit. Commit canceled.' ) ;
39+ console . log ( " No changes to commit. Commit canceled." ) ;
4040 process . exit ( 0 ) ;
4141 }
42-
42+
4343 const messages = [
44- { role : "system" , content : "You are a helpful assistant." } ,
45- { role : "user" , content : `Generate a Git commit message based on the following summary: ${ gitSummary } \n\nCommit message: ` }
46- ] ;
47-
44+ { role : "system" , content : "You are a helpful assistant." } ,
45+ {
46+ role : "user" ,
47+ content : `Generate a Git commit message based on the following summary: ${ gitSummary } \n\nCommit message: ` ,
48+ } ,
49+ ] ;
50+
4851 const parameters = {
4952 model,
5053 messages,
51- n :1 ,
54+ n : 1 ,
5255 temperature : 0 ,
5356 max_tokens : 50 ,
5457 } ;
55-
58+
5659 const response = await openai . chat . completions . create ( parameters ) ;
57-
58- const message = response . choices [ 0 ] . message . content . replace ( / [ ^ \w \s ] / gi, '' ) . trim ( ) ;
59-
60+
61+ const message = response . choices [ 0 ] . message . content
62+ . replace ( / [ ^ \w \s . - ] / gi, "" )
63+ . trim ( ) ;
64+
6065 const confirm = await prompts ( {
6166 type : "confirm" ,
6267 name : "value" ,
6368 message : `${ message } .` ,
6469 initial : true ,
6570 } ) ;
66-
71+
6772 if ( confirm . value ) {
6873 execSync ( `git commit -m "${ message } "` ) ; // escape double quart
6974 console . log ( "Committed with the suggested message." ) ;
@@ -78,7 +83,9 @@ const gitExtension = (args) => {
7883
7984 program
8085 . command ( "commit" )
81- . description ( "Generate a Git commit message based on the summary of changes" )
86+ . description (
87+ "Generate a Git commit message based on the summary of changes"
88+ )
8289 . action ( async ( ) => {
8390 await gptCommit ( ) ;
8491 } ) ;
@@ -94,9 +101,9 @@ const gitExtension = (args) => {
94101 choices : [
95102 { title : "gpt-3.5-turbo-instruct" , value : "gpt-3.5-turbo-instruct" } ,
96103 { title : "gpt-4-1106-preview" , value : "gpt-4-1106-preview" } ,
97- { title : "gpt-4-0125-preview" , value : "gpt-4-0125-preview" } // New model added
104+ { title : "gpt-4-0125-preview" , value : "gpt-4-0125-preview" } , // New model added
98105 ] ,
99- initial : 0
106+ initial : 0 ,
100107 } ) ;
101108
102109 model = response . value ;
0 commit comments