Skip to content

Commit 040c03d

Browse files
author
George Taveras
committed
fix(cli): properly error handling
- 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
1 parent 546dba6 commit 040c03d

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

lib/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
import yargs from "yargs";
4+
import { alerts } from "./core";
45
import { IMPLEMENTATIONS } from "./implementations";
56
import { main } from "./main";
67
import { Aliases, NAME_FORMATS } from "./sass";
@@ -145,4 +146,8 @@ const { _: patterns, ...rest } = yargs
145146
.parseSync();
146147

147148
// eslint-disable-next-line @typescript-eslint/no-floating-promises
148-
main(patterns[0] as string, { ...rest });
149+
main(patterns[0] as string, { ...rest }).catch((error) => {
150+
alerts.error("Encountered an error while generating type definitions.");
151+
alerts.error(error);
152+
process.exitCode = 1;
153+
});

lib/core/alerts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ const withLogLevelsRestriction =
3434

3535
const error = withLogLevelsRestriction(
3636
["verbose", "error", "info"],
37-
(message: string) => console.log(chalk.red(message))
37+
(message: string) => console.warn(chalk.red(message))
3838
);
3939
const warn = withLogLevelsRestriction(["verbose"], (message: string) =>
40-
console.log(chalk.yellowBright(message))
40+
console.warn(chalk.yellowBright(message))
4141
);
4242
const notice = withLogLevelsRestriction(
4343
["verbose", "info"],

lib/load.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { bundleRequire } from "bundle-require";
22
import JoyCon from "joycon";
33
import path from "path";
4-
import { alerts, CLIOptions, ConfigOptions } from "./core";
4+
import { CLIOptions, ConfigOptions } from "./core";
55
import { getDefaultImplementation } from "./implementations";
66
import { nameFormatDefault } from "./sass";
77
import {
@@ -39,25 +39,16 @@ export const loadConfig = async (): Promise<
3939
);
4040

4141
if (configPath) {
42-
try {
43-
const configModule = await bundleRequire({
44-
filepath: configPath,
45-
});
42+
const configModule = await bundleRequire({
43+
filepath: configPath,
44+
});
4645

47-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
48-
const config: ConfigOptions =
49-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
50-
configModule.mod.config || configModule.mod.default || configModule.mod;
46+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
47+
const config: ConfigOptions =
48+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
49+
configModule.mod.config || configModule.mod.default || configModule.mod;
5150

52-
return config;
53-
} catch (error) {
54-
alerts.error(
55-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
56-
`An error occurred loading the config file "${configPath}":\n${error}`
57-
);
58-
59-
return {};
60-
}
51+
return config;
6152
}
6253

6354
return {};

0 commit comments

Comments
 (0)