From 3c86d9293815aa285b46bde50fe5fe30a0bd26c5 Mon Sep 17 00:00:00 2001 From: Luna-Klatzer Date: Mon, 24 Jun 2024 10:45:04 +0200 Subject: [PATCH] fix (#508): Fixed config file loading bugs in the CLI --- kipper/cli/src/config-loader.ts | 14 +++++++------- kipper/cli/src/copy-resources.ts | 7 +++++++ kipper/cli/src/decorators.ts | 18 ++++++++++++++++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/kipper/cli/src/config-loader.ts b/kipper/cli/src/config-loader.ts index 93e9af72c..d5250c527 100644 --- a/kipper/cli/src/config-loader.ts +++ b/kipper/cli/src/config-loader.ts @@ -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(); @@ -54,13 +55,12 @@ export async function loadConfig( */ export async function loadAutoConfig(): Promise { 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" }); } } + return undefined; } diff --git a/kipper/cli/src/copy-resources.ts b/kipper/cli/src/copy-resources.ts index d2e02fdc9..4abd88c42 100644 --- a/kipper/cli/src/copy-resources.ts +++ b/kipper/cli/src/copy-resources.ts @@ -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. @@ -13,6 +16,10 @@ export async function copyConfigResources( logger?: KipperLogger, ): Promise { for (const resource of resources) { + const dir = path.dirname(resource.out); + if (!fsSync.existsSync(dir) || !(await fs.stat(dir)).isDirectory()) { + await fs.mkdir(dir, { recursive: true }); + } await fs.copyFile(resource.src, resource.out); if (logger) { logger.debug(`Copied resource from ${resource.src} to ${resource.out}`); diff --git a/kipper/cli/src/decorators.ts b/kipper/cli/src/decorators.ts index 0cc8757a9..277aeaeac 100644 --- a/kipper/cli/src/decorators.ts +++ b/kipper/cli/src/decorators.ts @@ -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} @@ -22,10 +23,11 @@ export function prettifiedErrors() { 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); const msg: string = error && typeof error === "object" && "message" in error && typeof error.message === "string" ? error.message @@ -34,7 +36,7 @@ export function prettifiedErrors() { 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!️", @@ -59,3 +61,15 @@ export function prettifiedErrors() { return func as TypedPropertyDescriptor<(...argArray: Array) => Promise>; }; } + +function getErrorName(cliError: boolean, configError: boolean, internalError: boolean): string { + if (cliError) { + return "Error"; + } else if (configError) { + return "Config Error"; + } else if (internalError) { + return "Internal Error"; + } else { + return "CLI Error"; + } +}