Skip to content

Commit

Permalink
fix (#508): Fixed config file loading bugs in the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna-Klatzer committed Jun 24, 2024
1 parent 2e67210 commit 3c86d92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
14 changes: 7 additions & 7 deletions kipper/cli/src/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @since 0.11.0
*/
import { EvaluatedKipperConfigFile, KipperConfigFile, KipperConfigInterpreter } from "@kipper/config";
import * as fs from "fs";

export let defaultConfigInterpreter = new KipperConfigInterpreter();

Expand Down Expand Up @@ -54,13 +55,12 @@ export async function loadConfig(
*/
export async function loadAutoConfig(): Promise<EvaluatedKipperConfigFile | undefined> {
const workdir = process.cwd();
try {
return await loadConfig({ path: `${workdir}/kip-config.json`, encoding: "utf8" });
} catch (e) {
try {
return await loadConfig({ path: `${workdir}/kipper-config.json`, encoding: "utf8" });
} catch (e) {
return undefined;

const potentialPaths = [`${workdir}/kip-config.json`, `${workdir}/kipper-config.json`];
for (const path of potentialPaths) {
if (fs.existsSync(path)) {
return loadConfig({ path, encoding: "utf8" });

Check warning on line 62 in kipper/cli/src/config-loader.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/config-loader.ts#L62

Added line #L62 was not covered by tests
}
}
return undefined;
}
7 changes: 7 additions & 0 deletions kipper/cli/src/copy-resources.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { EvaluatedKipperConfigFile } from "@kipper/config";
import * as fs from "node:fs/promises";
import * as fsSync from "node:fs";
import { KipperLogger } from "@kipper/core";

import path from "node:path";

/**
* Copy resources from the source to the destination.
* @param resources The resources to copy.
Expand All @@ -13,6 +16,10 @@ export async function copyConfigResources(
logger?: KipperLogger,
): Promise<void> {
for (const resource of resources) {
const dir = path.dirname(resource.out);

Check warning on line 19 in kipper/cli/src/copy-resources.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/copy-resources.ts#L19

Added line #L19 was not covered by tests
if (!fsSync.existsSync(dir) || !(await fs.stat(dir)).isDirectory()) {
await fs.mkdir(dir, { recursive: true });

Check warning on line 21 in kipper/cli/src/copy-resources.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/copy-resources.ts#L21

Added line #L21 was not covered by tests
}
await fs.copyFile(resource.src, resource.out);

Check warning on line 23 in kipper/cli/src/copy-resources.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/copy-resources.ts#L23

Added line #L23 was not covered by tests
if (logger) {
logger.debug(`Copied resource from ${resource.src} to ${resource.out}`);
Expand Down
18 changes: 16 additions & 2 deletions kipper/cli/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Command } from "@oclif/command";
import { KipperInternalError } from "@kipper/core";
import { KipperCLIError } from "./errors";
import { CLIError as OclifCLIError, PrettyPrintableError } from "@oclif/errors";
import { ConfigError } from "@kipper/config";

/**
* Wraps the given function with an async error handler that will pretty print errors using the {@link Command.error}
Expand All @@ -22,10 +23,11 @@ export function prettifiedErrors<TProto extends Command>() {
await originalFunc.call(this, ...argArray);
} catch (error) {
const cliError = error instanceof KipperCLIError || error instanceof OclifCLIError;
const configError = error instanceof ConfigError;
const internalError = error instanceof KipperInternalError;

// Error configuration
const name: string = cliError ? "Error" : internalError ? "Unexpected Internal Error" : "CLI Error";
const name: string = getErrorName(cliError, configError, internalError);

Check warning on line 30 in kipper/cli/src/decorators.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/decorators.ts#L30

Added line #L30 was not covered by tests
const msg: string =
error && typeof error === "object" && "message" in error && typeof error.message === "string"
? error.message
Expand All @@ -34,7 +36,7 @@ export function prettifiedErrors<TProto extends Command>() {
const errConfig: { exit: number } & PrettyPrintableError = {
exit: 1,
suggestions:
internalError || !cliError
internalError || (!cliError && !configError)
? [
"Ensure no invalid types or data were passed to module functions or classes. Otherwise report the " +
"issue on https://github.com/Kipper-Lang/Kipper. Help us improve Kipper!️",
Expand All @@ -59,3 +61,15 @@ export function prettifiedErrors<TProto extends Command>() {
return func as TypedPropertyDescriptor<(...argArray: Array<any>) => Promise<void>>;
};
}

function getErrorName(cliError: boolean, configError: boolean, internalError: boolean): string {
if (cliError) {
return "Error";

Check warning on line 67 in kipper/cli/src/decorators.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/decorators.ts#L67

Added line #L67 was not covered by tests
} else if (configError) {
return "Config Error";

Check warning on line 69 in kipper/cli/src/decorators.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/decorators.ts#L69

Added line #L69 was not covered by tests
} else if (internalError) {
return "Internal Error";

Check warning on line 71 in kipper/cli/src/decorators.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/decorators.ts#L71

Added line #L71 was not covered by tests
} else {
return "CLI Error";

Check warning on line 73 in kipper/cli/src/decorators.ts

View check run for this annotation

Codecov / codecov/patch

kipper/cli/src/decorators.ts#L73

Added line #L73 was not covered by tests
}
}

0 comments on commit 3c86d92

Please sign in to comment.