fix(config): allow get on config properties without key

This commit is contained in:
Hiroki Osame
2023-04-25 23:24:31 +09:00
parent e634c1482a
commit 4d7712f1e8
3 changed files with 29 additions and 17 deletions

View File

@@ -12,7 +12,7 @@ export default command({
const { mode, keyValue: keyValues } = argv._;
if (mode === 'get') {
const config = await getConfig();
const config = await getConfig({}, true);
for (const key of keyValues) {
if (hasOwn(config, key)) {
console.log(`${key}=${config[key as keyof typeof config]}`);

View File

@@ -104,14 +104,24 @@ const readConfigFile = async (): Promise<RawConfig> => {
return ini.parse(configString);
};
export const getConfig = async (cliConfig?: RawConfig): Promise<ValidConfig> => {
export const getConfig = async (
cliConfig?: RawConfig,
suppressErrors?: boolean,
): 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);
if (suppressErrors) {
try {
parsedConfig[key] = parser(value);
} catch {}
} else {
parsedConfig[key] = parser(value);
}
}
return parsedConfig as ValidConfig;