Skip to content

Commit

Permalink
feat: use default config when config is empty (#1070)
Browse files Browse the repository at this point in the history
* feat: use default config when config is empty

* pass description
  • Loading branch information
shamsartem authored Nov 18, 2024
1 parent 89cbed0 commit f8e2e29
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions packages/cli/package/src/lib/configs/initConfigNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
CLI_NAME_FULL,
SCHEMAS_DIR_NAME,
} from "../const.js";
import { dbg } from "../dbg.js";
import { numToStr } from "../helpers/typesafeStringify.js";
import { removeProperties } from "../helpers/utils.js";

Expand Down Expand Up @@ -156,6 +157,7 @@ export function getConfigInitFunction<
expectedConfigPath,
getDefaultConfig,
getSchemaDirPath,
description,
});

if (getLatestConfigRes === null) {
Expand Down Expand Up @@ -274,13 +276,15 @@ async function getLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>({
expectedConfigPath,
getDefaultConfig,
getSchemaDirPath,
description,
}: {
options: OptionsTuple<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>;
expectedConfigPath: string;
getDefaultConfig:
| GetDefaultConfig<GetLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>>
| undefined;
getSchemaDirPath: GetPath | undefined;
description: string;
}): Promise<{
latestConfig: GetLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>;
latestConfigString: string;
Expand All @@ -301,6 +305,18 @@ async function getLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>({
let configString: string;
let actualConfigPath = expectedConfigPath;

async function getDefaultConfigString() {
if (getDefaultConfig === undefined) {
return null;
}

return yamlDiffPatch(
`# ${description}\n\n`,
{},
{ version: options.length - 1, ...(await getDefaultConfig()) },
);
}

try {
// try reading config file
// if it fails, try replacing .yaml with .yml or vice versa and read again
Expand All @@ -320,23 +336,23 @@ async function getLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>({
actualConfigPath = newConfigPath;
}
} catch {
if (getDefaultConfig === undefined) {
const defaultConfigString = await getDefaultConfigString();

if (defaultConfigString === null) {
return null;
}

const latestConfigOptions = options[options.length - 1];
const { description } = latestConfigOptions.schema;
configString = defaultConfigString;
}

assert(
description !== undefined,
`Unreachable. addVersionTitleAndDescriptionToConfigOptions function must ensure that description is defined`,
);
// if config file is empty for some reason - try using default
if (configString.trim() === "") {
dbg(`Config at ${actualConfigPath} is empty. Using default config`);
const defaultConfigString = await getDefaultConfigString();

configString = yamlDiffPatch(
`# ${description}\n\n`,
{},
{ version: options.length - 1, ...(await getDefaultConfig()) },
);
if (defaultConfigString !== null) {
configString = defaultConfigString;
}
}

const currentConfigUnknown: unknown = parse(configString);
Expand All @@ -346,7 +362,7 @@ async function getLatestConfig<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>({
currentConfigUnknown === null
) {
return commandObj.error(
`Invalid config at ${color.yellow(actualConfigPath)}. Expected to be an object`,
`Invalid config at ${color.yellow(actualConfigPath)}. Expected to be an object. Got: ${configString}`,
);
}

Expand Down

0 comments on commit f8e2e29

Please sign in to comment.