Skip to content

Commit

Permalink
Improved parsing of js config with promises, if empty and formatError
Browse files Browse the repository at this point in the history
Netfloex committed May 1, 2022
1 parent 06d81a5 commit cc7759a
Showing 3 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -62,12 +62,12 @@ const main = async (): Promise<ExitCode> => {
if (!stopping) {
results = await parseUserConfig(configFilePath!);

if (!results) {
if (results === false) {
logger.configError({ config: configFilePath! });
stopping = true;
}

if (Object.keys(results).length == 0) {
if (!results || Object.keys(results).length == 0) {
logger.configEmpty({ config: configFilePath! });
stopping = true;
}
29 changes: 21 additions & 8 deletions src/lib/logMessages.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,14 @@ export const defineLogList = <
list: T
): T => list;

const formatError = (error: unknown): string =>
error instanceof Error
? chalk`{dim ${error.message}}\n{dim ${error.stack
?.split("\n")
.slice(0, 3)
.join("\n")}}`
: chalk.dim(error);

export const logMessages = defineLogList({
// Global

@@ -250,15 +258,15 @@ export const logMessages = defineLogList({
Tag.config,
chalk`Yaml Error: {dim ${error.reason}}:\n${error.mark.snippet}`
],
configJSONError: ({ error = new Error() }: { error: Error }) => [
configJSONError: ({ error }: { error: unknown }) => [
Log.error,
Tag.config,
chalk`JSON Error: {dim ${error.message}}`
chalk`JSON Error: ${formatError(error)}`
],
configJSError: ({ error = new Error() }: { error: Error }) => [
configJSError: ({ error }: { error: unknown }) => [
Log.error,
Tag.config,
chalk`JS Error: {dim ${error.message}}\n${error.stack?.split("\n")[1]}`
chalk`JS Error: ${formatError(error)}`
],
configJSInvalidType: ({
type,
@@ -304,6 +312,11 @@ export const logMessages = defineLogList({
Tag.config,
chalk`Config contains {dim ${envKey}}, while {dim process.env.${env}} was not defined.`
],
configPromise: () => [
Log.info,
Tag.config,
chalk`Config is a promise, waiting until it resolves.`
],

// CSS

@@ -332,13 +345,13 @@ export const logMessages = defineLogList({
error
}: {
fileName: string;
error: Error;
error: unknown;
}) => [
Log.error,
Tag.css,
chalk`{red Could not save the CSS file} {dim ${fileName}}\n{dim ${
error?.message ?? error
}}`
chalk`{red Could not save the CSS file} {dim ${fileName}}\n${formatError(
error
)}`
],

// JS
18 changes: 12 additions & 6 deletions src/utils/parseUserConfig.ts
Original file line number Diff line number Diff line change
@@ -10,14 +10,20 @@ const parseUserConfig = async (
): Promise<Record<string, unknown> | false> => {
const ext = extname(configFilePath);

if (ext.match(/^\.js$/)) {
if (ext == ".js") {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const data: unknown = require(configFilePath);
const imported = await import(configFilePath);
const data = imported.default;
if (typeof data == "object" && data) {
return data as Record<string, unknown>;
} else if (typeof data == "function") {
return await data();
const generatedData = data();

if (generatedData instanceof Promise) {
logger.configPromise();
}

return await generatedData;
} else {
logger.configJSInvalidType({
type: typeof data,
@@ -26,7 +32,7 @@ const parseUserConfig = async (
return false;
}
} catch (error) {
logger.configJSError({ error: error as Error });
logger.configJSError({ error });
return false;
}
}
@@ -69,7 +75,7 @@ const parseUserConfig = async (
return value;
});
} catch (error) {
logger.configJSONError({ error: error as Error });
logger.configJSONError({ error });
return false;
}
}

0 comments on commit cc7759a

Please sign in to comment.