From cc7759a2c8db98e0a2af32d6b27169aa90488019 Mon Sep 17 00:00:00 2001 From: Netfloex <38650595+Netfloex@users.noreply.github.com> Date: Sun, 1 May 2022 14:38:33 +0200 Subject: [PATCH] Improved parsing of js config with promises, if empty and formatError --- src/index.ts | 4 ++-- src/lib/logMessages.ts | 29 +++++++++++++++++++++-------- src/utils/parseUserConfig.ts | 18 ++++++++++++------ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index a66ce33..8b16955 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,12 +62,12 @@ const main = async (): Promise => { 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; } diff --git a/src/lib/logMessages.ts b/src/lib/logMessages.ts index d053310..21f0d9b 100644 --- a/src/lib/logMessages.ts +++ b/src/lib/logMessages.ts @@ -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 diff --git a/src/utils/parseUserConfig.ts b/src/utils/parseUserConfig.ts index ce0ae5b..450aca3 100644 --- a/src/utils/parseUserConfig.ts +++ b/src/utils/parseUserConfig.ts @@ -10,14 +10,20 @@ const parseUserConfig = async ( ): Promise | 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; } 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; } }