Skip to content

Commit

Permalink
feat: imporved error logging on config load
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Jun 8, 2024
1 parent c1628f4 commit 622c877
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
27 changes: 18 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import signale from "signale";

import { description, version } from "./package.json";
import { machBuild, machWatch } from "./src/mach";
import { type MachArgs, MachArgsSchema, MachConfigSchema } from "./src/types";
import { type MachArgs, MachArgsSchema, type MachConfig, MachConfigSchema } from "./src/types";

try {
dotenv.config();
Expand All @@ -23,6 +23,12 @@ try {

const cli = new Command();

const logger = new signale.Signale({
types: {
error: { badge: "×", label: "error", color: "red", stream: process.stderr },
},
});

const commandWithOptions = (name: string) =>
cli
.command(name)
Expand Down Expand Up @@ -50,21 +56,24 @@ const commandWithOptions = (name: string) =>
await import(config.replace(/\\/g, "/"))
.then((module) => {
// Check config integrity
const result = MachConfigSchema.safeParse(module.default);
if (result.success) {
actionCommand.setOptionValue("config", result.data);
signale.info("Loaded config file", chalk.cyanBright(config), "\n");
} else {
signale.error("Invalid config file", chalk.redBright(config));
let result: MachConfig;
try {
result = MachConfigSchema.parse(module.default);
} catch (error) {
logger.error("Invalid config file", chalk.redBright(config));
logger.error(error);
process.exit(1);
}

logger.info("Loaded config file", chalk.cyanBright(config), "\n");

if (actionCommand.getOptionValue("workInConfigDir")) {
process.chdir(path.dirname(config));
}
})
.catch(() => {
signale.error("Unable to load config file", chalk.redBright(config));
.catch((error) => {
logger.error("Unable to load config file", chalk.redBright(config));
logger.error(error);
process.exit(1);
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import path from "node:path";
import chalk from "chalk";
import type { Message } from "esbuild";
import { filesize } from "filesize";
import signale, { type DefaultMethods, type Signale } from "signale";
import signale, { type SignaleOptions, type DefaultMethods, type Signale } from "signale";

import type { BuildResultWithMeta } from "./types";

export class BuildLogger {
private readonly logger: Signale<DefaultMethods | "file" | "errorMessage" | "errorLocation">;
private readonly logger: Signale<
DefaultMethods | "file" | "errorMessage" | "errorLocation" | "warningMessage" | "warningLocation"
>;

private readonly verbose: boolean;

Expand Down

0 comments on commit 622c877

Please sign in to comment.