diff --git a/src/CommandsEnum.ts b/src/CommandsEnum.ts new file mode 100644 index 0000000..d3667e5 --- /dev/null +++ b/src/CommandsEnum.ts @@ -0,0 +1,4 @@ +export enum COMMANDS { + config = 'config', + hook = 'hook' +} diff --git a/src/api.ts b/src/api.ts index e3da590..b82e518 100644 --- a/src/api.ts +++ b/src/api.ts @@ -5,7 +5,7 @@ import { OpenAIApi } from 'openai'; -import { getConfig } from './commands/config'; +import { CONFIG_MODES, getConfig } from './commands/config'; const config = getConfig(); @@ -13,7 +13,7 @@ let apiKey = config?.OPENAI_API_KEY; const [command, mode] = process.argv.slice(2); -if (!apiKey && command !== 'config' && mode !== 'set') { +if (!apiKey && command !== 'config' && mode !== CONFIG_MODES.set) { intro('opencommit'); outro( diff --git a/src/commands/config.ts b/src/commands/config.ts index d6b65d8..55c59ae 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -5,6 +5,7 @@ import { existsSync, writeFileSync, readFileSync } from 'fs'; import { homedir } from 'os'; import { intro, outro } from '@clack/prompts'; import chalk from 'chalk'; +import { COMMANDS } from '../CommandsEnum'; export enum CONFIG_KEYS { OPENAI_API_KEY = 'OPENAI_API_KEY', @@ -12,6 +13,11 @@ export enum CONFIG_KEYS { emoji = 'emoji' } +export enum CONFIG_MODES { + get = 'get', + set = 'set' +} + const validateConfig = ( key: string, condition: any, @@ -110,7 +116,7 @@ export const setConfig = (keyValues: [key: string, value: string][]) => { export const configCommand = command( { - name: 'config', + name: COMMANDS.config, parameters: ['', ''] }, async (argv) => { @@ -118,12 +124,12 @@ export const configCommand = command( try { const { mode, keyValues } = argv._; - if (mode === 'get') { + if (mode === CONFIG_MODES.get) { const config = getConfig() || {}; for (const key of keyValues) { outro(`${key}=${config[key as keyof typeof config]}`); } - } else if (mode === 'set') { + } else if (mode === CONFIG_MODES.set) { await setConfig( keyValues.map((keyValue) => keyValue.split('=') as [string, string]) ); diff --git a/src/commands/githook.ts b/src/commands/githook.ts index 0e85d61..02ac98e 100644 --- a/src/commands/githook.ts +++ b/src/commands/githook.ts @@ -5,6 +5,7 @@ import { assertGitRepo } from '../utils/git.js'; import { existsSync } from 'fs'; import chalk from 'chalk'; import { intro, outro } from '@clack/prompts'; +import { COMMANDS } from '../CommandsEnum.js'; const HOOK_NAME = 'prepare-commit-msg'; const SYMLINK_URL = `.git/hooks/${HOOK_NAME}`; @@ -15,7 +16,7 @@ const isHookExists = existsSync(SYMLINK_URL); export const hookCommand = command( { - name: 'hook', + name: COMMANDS.hook, parameters: [''] }, async (argv) => {