Skip to content

Commit

Permalink
fix(cli): properly handle errors
Browse files Browse the repository at this point in the history
- Handle all errors centrally and properly set process exit code
- Use console.warn where appropriate to funnel messaged to stderr
- Do not ignore errors when loading config file
  • Loading branch information
George Taveras committed Jan 2, 2024
1 parent 546dba6 commit 9b10ede
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
7 changes: 6 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import yargs from "yargs";
import { alerts } from "./core";
import { IMPLEMENTATIONS } from "./implementations";
import { main } from "./main";
import { Aliases, NAME_FORMATS } from "./sass";
Expand Down Expand Up @@ -145,4 +146,8 @@ const { _: patterns, ...rest } = yargs
.parseSync();

// eslint-disable-next-line @typescript-eslint/no-floating-promises
main(patterns[0] as string, { ...rest });
main(patterns[0] as string, { ...rest }).catch((error) => {
alerts.error("Encountered an error while generating type definitions.");
alerts.error(error);
process.exitCode = 1;
});
4 changes: 2 additions & 2 deletions lib/core/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const withLogLevelsRestriction =

const error = withLogLevelsRestriction(
["verbose", "error", "info"],
(message: string) => console.log(chalk.red(message))
(message: string) => console.warn(chalk.red(message))
);
const warn = withLogLevelsRestriction(["verbose"], (message: string) =>
console.log(chalk.yellowBright(message))
console.warn(chalk.yellowBright(message))
);
const notice = withLogLevelsRestriction(
["verbose", "info"],
Expand Down
27 changes: 9 additions & 18 deletions lib/load.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bundleRequire } from "bundle-require";
import JoyCon from "joycon";
import path from "path";
import { alerts, CLIOptions, ConfigOptions } from "./core";
import { CLIOptions, ConfigOptions } from "./core";
import { getDefaultImplementation } from "./implementations";
import { nameFormatDefault } from "./sass";
import {
Expand Down Expand Up @@ -39,25 +39,16 @@ export const loadConfig = async (): Promise<
);

if (configPath) {
try {
const configModule = await bundleRequire({
filepath: configPath,
});
const configModule = await bundleRequire({
filepath: configPath,
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const config: ConfigOptions =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
configModule.mod.config || configModule.mod.default || configModule.mod;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const config: ConfigOptions =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
configModule.mod.config || configModule.mod.default || configModule.mod;

return config;
} catch (error) {
alerts.error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`An error occurred loading the config file "${configPath}":\n${error}`
);

return {};
}
return config;
}

return {};
Expand Down

0 comments on commit 9b10ede

Please sign in to comment.