feat: config file at ~/.aicommits (#51)
Co-authored-by: Yu Le <is.yuler@gmail.com>
This commit is contained in:
10
src/cli.ts
10
src/cli.ts
@@ -3,11 +3,15 @@
|
||||
import { execSync } from 'child_process';
|
||||
import chalk from 'chalk';
|
||||
import inquirer from 'inquirer';
|
||||
import { generateCommitMessage } from './utils';
|
||||
|
||||
const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY;
|
||||
import {
|
||||
getConfig,
|
||||
generateCommitMessage,
|
||||
} from './utils';
|
||||
|
||||
(async () => {
|
||||
const config = await getConfig();
|
||||
const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY ?? config.OPENAI_KEY;
|
||||
|
||||
console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!'));
|
||||
|
||||
if (!OPENAI_KEY) {
|
||||
|
||||
21
src/utils.ts
21
src/utils.ts
@@ -1,5 +1,26 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
import ini from 'ini';
|
||||
import { Configuration, OpenAIApi } from 'openai';
|
||||
|
||||
const fileExists = (filePath: string) => fs.access(filePath).then(() => true, () => false);
|
||||
|
||||
type ConfigType = {
|
||||
OPENAI_KEY?: string;
|
||||
};
|
||||
|
||||
export const getConfig = async (): Promise<ConfigType> => {
|
||||
const configPath = path.join(os.homedir(), '.aicommits');
|
||||
const configExists = await fileExists(configPath);
|
||||
if (!configExists) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const configString = await fs.readFile(configPath, 'utf8');
|
||||
return ini.parse(configString);
|
||||
};
|
||||
|
||||
export const generateCommitMessage = async (
|
||||
apiKey: string,
|
||||
prompt: string,
|
||||
|
||||
Reference in New Issue
Block a user