* feat(CommandsEnum.ts): add COMMANDS enum
* refactor(api.ts): use CONFIG_MODES enum from config.ts * refactor(config.ts): use COMMANDS and CONFIG_MODES enums, extract validateConfig function * feat(config.ts): add CONFIG_MODES enum and refactor configCommand function to use it * refactor(githook.ts): import COMMANDS enum from CommandsEnum.js file
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
export enum COMMANDS {
|
||||||
|
config = 'config',
|
||||||
|
hook = 'hook'
|
||||||
|
}
|
||||||
+2
-2
@@ -5,7 +5,7 @@ import {
|
|||||||
OpenAIApi
|
OpenAIApi
|
||||||
} from 'openai';
|
} from 'openai';
|
||||||
|
|
||||||
import { getConfig } from './commands/config';
|
import { CONFIG_MODES, getConfig } from './commands/config';
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ let apiKey = config?.OPENAI_API_KEY;
|
|||||||
|
|
||||||
const [command, mode] = process.argv.slice(2);
|
const [command, mode] = process.argv.slice(2);
|
||||||
|
|
||||||
if (!apiKey && command !== 'config' && mode !== 'set') {
|
if (!apiKey && command !== 'config' && mode !== CONFIG_MODES.set) {
|
||||||
intro('opencommit');
|
intro('opencommit');
|
||||||
|
|
||||||
outro(
|
outro(
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { existsSync, writeFileSync, readFileSync } from 'fs';
|
|||||||
import { homedir } from 'os';
|
import { homedir } from 'os';
|
||||||
import { intro, outro } from '@clack/prompts';
|
import { intro, outro } from '@clack/prompts';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
|
import { COMMANDS } from '../CommandsEnum';
|
||||||
|
|
||||||
export enum CONFIG_KEYS {
|
export enum CONFIG_KEYS {
|
||||||
OPENAI_API_KEY = 'OPENAI_API_KEY',
|
OPENAI_API_KEY = 'OPENAI_API_KEY',
|
||||||
@@ -12,6 +13,11 @@ export enum CONFIG_KEYS {
|
|||||||
emoji = 'emoji'
|
emoji = 'emoji'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum CONFIG_MODES {
|
||||||
|
get = 'get',
|
||||||
|
set = 'set'
|
||||||
|
}
|
||||||
|
|
||||||
const validateConfig = (
|
const validateConfig = (
|
||||||
key: string,
|
key: string,
|
||||||
condition: any,
|
condition: any,
|
||||||
@@ -110,7 +116,7 @@ export const setConfig = (keyValues: [key: string, value: string][]) => {
|
|||||||
|
|
||||||
export const configCommand = command(
|
export const configCommand = command(
|
||||||
{
|
{
|
||||||
name: 'config',
|
name: COMMANDS.config,
|
||||||
parameters: ['<mode>', '<key=values...>']
|
parameters: ['<mode>', '<key=values...>']
|
||||||
},
|
},
|
||||||
async (argv) => {
|
async (argv) => {
|
||||||
@@ -118,12 +124,12 @@ export const configCommand = command(
|
|||||||
try {
|
try {
|
||||||
const { mode, keyValues } = argv._;
|
const { mode, keyValues } = argv._;
|
||||||
|
|
||||||
if (mode === 'get') {
|
if (mode === CONFIG_MODES.get) {
|
||||||
const config = getConfig() || {};
|
const config = getConfig() || {};
|
||||||
for (const key of keyValues) {
|
for (const key of keyValues) {
|
||||||
outro(`${key}=${config[key as keyof typeof config]}`);
|
outro(`${key}=${config[key as keyof typeof config]}`);
|
||||||
}
|
}
|
||||||
} else if (mode === 'set') {
|
} else if (mode === CONFIG_MODES.set) {
|
||||||
await setConfig(
|
await setConfig(
|
||||||
keyValues.map((keyValue) => keyValue.split('=') as [string, string])
|
keyValues.map((keyValue) => keyValue.split('=') as [string, string])
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { assertGitRepo } from '../utils/git.js';
|
|||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import { intro, outro } from '@clack/prompts';
|
import { intro, outro } from '@clack/prompts';
|
||||||
|
import { COMMANDS } from '../CommandsEnum.js';
|
||||||
|
|
||||||
const HOOK_NAME = 'prepare-commit-msg';
|
const HOOK_NAME = 'prepare-commit-msg';
|
||||||
const SYMLINK_URL = `.git/hooks/${HOOK_NAME}`;
|
const SYMLINK_URL = `.git/hooks/${HOOK_NAME}`;
|
||||||
@@ -15,7 +16,7 @@ const isHookExists = existsSync(SYMLINK_URL);
|
|||||||
|
|
||||||
export const hookCommand = command(
|
export const hookCommand = command(
|
||||||
{
|
{
|
||||||
name: 'hook',
|
name: COMMANDS.hook,
|
||||||
parameters: ['<set/unset>']
|
parameters: ['<set/unset>']
|
||||||
},
|
},
|
||||||
async (argv) => {
|
async (argv) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user