refactor: reuse config validators on cli config
This commit is contained in:
@@ -19,24 +19,33 @@ const parseAssert = (
|
||||
};
|
||||
|
||||
const configParsers = {
|
||||
OPENAI_KEY(key: string) {
|
||||
parseAssert('OPENAI_KEY', key, 'Cannot be empty');
|
||||
OPENAI_KEY(key?: string) {
|
||||
if (!key) {
|
||||
throw new KnownError('Please set your OpenAI API key via `aicommits config set OPENAI_KEY=<your token>`');
|
||||
}
|
||||
parseAssert('OPENAI_KEY', key.startsWith('sk-'), 'Must start with "sk-"');
|
||||
parseAssert('OPENAI_KEY', key.length === 51, 'Must be 51 characters long');
|
||||
|
||||
return key;
|
||||
},
|
||||
locale(key: string) {
|
||||
parseAssert('locale', key, 'Cannot be empty');
|
||||
parseAssert('locale', /^[a-z-]+$/i.test(key), 'Must be a valid locale (letters and dashes/underscores). You can consult the list of codes in: https://wikipedia.org/wiki/List_of_ISO_639-1_codes');
|
||||
locale(locale?: string) {
|
||||
if (!locale) {
|
||||
return 'en';
|
||||
}
|
||||
|
||||
return key;
|
||||
parseAssert('locale', locale, 'Cannot be empty');
|
||||
parseAssert('locale', /^[a-z-]+$/i.test(locale), 'Must be a valid locale (letters and dashes/underscores). You can consult the list of codes in: https://wikipedia.org/wiki/List_of_ISO_639-1_codes');
|
||||
|
||||
return locale;
|
||||
},
|
||||
generate(key: string) {
|
||||
parseAssert('generate', key, 'Cannot be empty');
|
||||
parseAssert('generate', /^\d+$/.test(key), 'Must be an integer');
|
||||
generate(count?: string) {
|
||||
if (!count) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const parsed = Number(key);
|
||||
parseAssert('generate', /^\d+$/.test(count), 'Must be an integer');
|
||||
|
||||
const parsed = Number(count);
|
||||
parseAssert('generate', parsed > 0, 'Must be greater than 0');
|
||||
parseAssert('generate', parsed <= 5, 'Must be less or equal to 5');
|
||||
|
||||
@@ -44,45 +53,53 @@ const configParsers = {
|
||||
},
|
||||
} as const;
|
||||
|
||||
type ValidKeys = keyof typeof configParsers;
|
||||
type ConfigType = {
|
||||
[key in ValidKeys]?: ReturnType<typeof configParsers[key]>;
|
||||
type ConfigKeys = keyof typeof configParsers;
|
||||
|
||||
type RawConfig = {
|
||||
[key in ConfigKeys]?: string;
|
||||
};
|
||||
|
||||
type ValidConfig = {
|
||||
[Key in ConfigKeys]: ReturnType<typeof configParsers[Key]>;
|
||||
};
|
||||
|
||||
const configPath = path.join(os.homedir(), '.aicommits');
|
||||
|
||||
export const getConfig = async (): Promise<ConfigType> => {
|
||||
const readConfigFile = async (): Promise<RawConfig> => {
|
||||
const configExists = await fileExists(configPath);
|
||||
if (!configExists) {
|
||||
return {};
|
||||
return Object.create(null);
|
||||
}
|
||||
|
||||
const configString = await fs.readFile(configPath, 'utf8');
|
||||
const config = ini.parse(configString);
|
||||
for (const key of Object.keys(config)) {
|
||||
if (hasOwn(configParsers, key)) {
|
||||
const parsed = configParsers[key as ValidKeys](config[key]);
|
||||
config[key as ValidKeys] = parsed;
|
||||
} else {
|
||||
console.warn(`\n⚠️ Unknown config property "${key}" found in ${configPath}`);
|
||||
}
|
||||
return ini.parse(configString);
|
||||
};
|
||||
|
||||
export const getConfig = async (cliConfig?: RawConfig): Promise<ValidConfig> => {
|
||||
const config = await readConfigFile();
|
||||
const parsedConfig: Record<string, unknown> = {};
|
||||
|
||||
for (const key of Object.keys(configParsers) as ConfigKeys[]) {
|
||||
const parser = configParsers[key];
|
||||
const value = cliConfig?.[key] ?? config[key];
|
||||
parsedConfig[key] = parser(value);
|
||||
}
|
||||
|
||||
return config;
|
||||
return parsedConfig as ValidConfig;
|
||||
};
|
||||
|
||||
export const setConfigs = async (
|
||||
keyValues: [key: string, value: string][],
|
||||
) => {
|
||||
const config = await getConfig();
|
||||
const config = await readConfigFile();
|
||||
|
||||
for (const [key, value] of keyValues) {
|
||||
if (!hasOwn(configParsers, key)) {
|
||||
throw new KnownError(`Invalid config property: ${key}`);
|
||||
}
|
||||
|
||||
const parsed = configParsers[key as ValidKeys](value);
|
||||
config[key as ValidKeys] = parsed as any;
|
||||
const parsed = configParsers[key as ConfigKeys](value);
|
||||
config[key as ConfigKeys] = parsed as any;
|
||||
}
|
||||
|
||||
await fs.writeFile(configPath, ini.stringify(config), 'utf8');
|
||||
|
||||
Reference in New Issue
Block a user