Skip to content

Commit

Permalink
Fix configure bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sethwebster committed Aug 1, 2023
1 parent c48997a commit 4ae7027
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sethwebster/ava-commit",
"version": "0.0.8",
"version": "0.0.9",
"description": "",
"main": "./src/index.ts",
"type": "module",
Expand Down
15 changes: 7 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { combineSummaries, summarizeDiffs, summarizeSummaries } from './lib/summ
import cache from './lib/cache.js';
import checkStagedCommits from './lib/checkStagedCommits.js';
import packageJson from './lib/packageJson.js';
import displayOptions from './lib/displayOptions.js';

const program = new Command();

Expand All @@ -20,6 +21,7 @@ program.version(packageJson.packageVersion())
.option<number>('-l,--length [number]', 'Length of commit message', (val, prev) => {
return parseInt(val);
}, 80)
.option("--release-notes", "Generate release notes", false)
.option('--configure', 'Configure the tool')
.addHelpText('after', `\n`)
.addHelpText('after', `Examples:`)
Expand All @@ -29,35 +31,32 @@ program.version(packageJson.packageVersion())
.addHelpText('after', ` $ ava-commit --length 150 # create a commit message for staged files, targeting max summary of 150 characters`)
.parse(process.argv);

export const options = program.opts();

function displayOptions(options: string[]) {
const message = options.map((m, i) => `${chalk.bold(chalk.yellow(i + 1))}. ${m}`).join("\n");
console.log(`Commit message options:\n${message}`);
}
export const options = program.opts();

async function main(noCache?: boolean) {
const existingConfig = loadConfig();
const envOpenAiKey = process.env.OPENAI_API_KEY ?? undefined;
let openAiKey = envOpenAiKey ?? existingConfig.openAIApiKey;
const hasApiKey = existingConfig.openAIApiKey !== undefined || envOpenAiKey !== undefined;
if (!hasApiKey || options.configure) {
configure();
main();
configure().then(()=>{
main();
});
return;
}

if (!hasApiKey || !openAiKey || openAiKey.length === 0) {
console.error("You must set the OPENAI_API_KEY environment variable, or run `ava-commit --configure`");
return;
}

try {
let summaries: string[];
let commitMessages: string[];
await checkStagedCommits();
const diffs = await git.diff();
const previousSummaryRun = cache.getPreviousRun(diffs);

if (previousSummaryRun && !noCache) {
console.log(chalk.green("Using cached summaries and commit messages from previous run."));
summaries = previousSummaryRun.summaries;
Expand Down
37 changes: 27 additions & 10 deletions src/lib/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import path from 'path';
import chalk from 'chalk';
import Readline from 'readline';
import { makeConfigPath } from './environment.js';
import MessagesForCurrentLanguage from './messages.js';

const DEFAULT_CONFIG: Options = {
openAIApiKey: undefined,
model: "gpt-4",
cliLanguage: "en-us",
commitMessageLanguage: "en-us",
}

const welcomeMessages = [
Expand All @@ -19,6 +22,10 @@ const welcomeMessages = [

const welcomeMessage = welcomeMessages.join("\n");

function getLanguage() {
let locale = Intl.DateTimeFormat().resolvedOptions().locale;
return locale.toLowerCase();
}

function createConfigPath() {
const configPath = makeConfigPath();
Expand All @@ -32,7 +39,7 @@ function saveConfig(options: Options) {
try {
const path = makeConfigPath();
createConfigPath();
const config = JSON.stringify({...DEFAULT_CONFIG, ...options});
const config = JSON.stringify({ ...DEFAULT_CONFIG, ...options });
fs.writeFileSync(path, config);
} catch (e) {
console.error(e);
Expand All @@ -54,19 +61,29 @@ export function loadConfig(): Options {
}

export async function configure() {
console.log(welcomeMessage);
const existingConfig = loadConfig();
const rl = Readline.createInterface({
input: process.stdin,
output: process.stdout,
})
return new Promise<void>((resolve, reject) => {
console.log(welcomeMessage);
const existingConfig = loadConfig();
const rl = Readline.createInterface({
input: process.stdin,
output: process.stdout,
})

rl.question("Enter your OpenAI API key > ", (answer) => {
saveConfig({...existingConfig, openAIApiKey: answer});
rl.question(MessagesForCurrentLanguage.prompts['enter-openai-key'], (answer) => {
saveConfig({ ...existingConfig, openAIApiKey: answer });
resolve();
});
});
}

const AllLanguageLocales = ["en-us", "en-gb", "en-au", "en-ca", "en-in", "en-za", "en-nz", "en-ie", "en-jm", "en-bz", "en-tt", "en-zw", "en-ph", "en-my", "en-sg", "en-pk", "en-ng", "en-gh", "en-hk", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz", "en-ke", "en-ug", "en-tz"] as const;
export type LanguageLocales = typeof AllLanguageLocales[number];
export const AvailableLanguages: LanguageLocales[] = ["en-us"] as LanguageLocales[]
export type AvailableLanguages = typeof AvailableLanguages[number];

interface Options {
openAIApiKey: string | undefined;
model: "gpt-4" | "gpt-3.5-turbo-16k"
model: "gpt-4" | "gpt-3.5-turbo-16k";
cliLanguage: LanguageLocales
commitMessageLanguage: LanguageLocales;
}
6 changes: 6 additions & 0 deletions src/lib/displayOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import chalk from "chalk";

export default function displayOptions(options: string[]) {
const message = options.map((m, i) => `${chalk.bold(chalk.yellow(i + 1))}. ${m}`).join("\n");
console.log(`Commit message options:\n${message}`);
}
50 changes: 50 additions & 0 deletions src/lib/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { AvailableLanguages, LanguageLocales } from "./configure.js"

type CliMessages = {
prompts: {
"enter-openai-key": string;
},
errors: {
"no-diff": string;
}
}

type Messages = {
[key in AvailableLanguages]: CliMessages | undefined;
};

const Messages: Messages = {
"en-us": {
prompts: {
"enter-openai-key": "Enter your OpenAI API key > ",
},
errors: {
"no-diff": "No changes to commit",
}
},
"en-au": undefined,
"en-ca": undefined,
"en-gb": undefined,
"en-in": undefined,
"en-ie": undefined,
"en-jm": undefined,
"en-nz": undefined,
"en-ph": undefined,
"en-sg": undefined,
"en-za": undefined,
"en-tt": undefined,
"en-zw": undefined,
"en-bz": undefined,
"en-hk": undefined,
"en-my": undefined,
"en-pk": undefined,
"en-ng": undefined,
"en-gh": undefined,
"en-ke": undefined,
"en-ug": undefined,
"en-tz": undefined,
}

const MessagesForCurrentLanguage = Messages[process.env.LANG as LanguageLocales] ?? Messages["en-us"] as CliMessages;

export default MessagesForCurrentLanguage;

0 comments on commit 4ae7027

Please sign in to comment.