From 68dd41f42b85b25e0ab11b65f479c62bcae601ff Mon Sep 17 00:00:00 2001 From: OnkarRuikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Sun, 17 Sep 2023 08:37:52 +0530 Subject: [PATCH] chore(build): better error message for bad .env configs --- libs/env/index.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/libs/env/index.js b/libs/env/index.js index accd2e289781..36e7923a3c4d 100644 --- a/libs/env/index.js +++ b/libs/env/index.js @@ -10,6 +10,16 @@ import { VALID_FLAW_CHECKS } from "../constants/index.js"; const dirname = fileURLToPath(new URL(".", import.meta.url)); const ROOT = path.join(dirname, "..", ".."); +function parse(value) { + try { + JSON.parse(value); + } catch (e) { + throw new Error(`Error parsing value '${value}' in .env file`, { + cause: e, + }); + } +} + dotenv.config({ path: path.join(cwd(), process.env.ENV_FILE || ".env"), }); @@ -30,14 +40,14 @@ export const FILES = process.env.BUILD_FILES || ""; export const FOLDERSEARCH = process.env.BUILD_FOLDERSEARCH || ""; export const GOOGLE_ANALYTICS_ACCOUNT = process.env.BUILD_GOOGLE_ANALYTICS_ACCOUNT || ""; -export const GOOGLE_ANALYTICS_DEBUG = JSON.parse( +export const GOOGLE_ANALYTICS_DEBUG = parse( process.env.BUILD_GOOGLE_ANALYTICS_DEBUG || "false" ); export const NO_PROGRESSBAR = Boolean( - JSON.parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false") + parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false") ); -export const FIX_FLAWS = JSON.parse(process.env.BUILD_FIX_FLAWS || "false"); -export const FIX_FLAWS_DRY_RUN = JSON.parse( +export const FIX_FLAWS = parse(process.env.BUILD_FIX_FLAWS || "false"); +export const FIX_FLAWS_DRY_RUN = parse( process.env.BUILD_FIX_FLAWS_DRY_RUN || "false" ); export const FIX_FLAWS_TYPES = new Set( @@ -53,14 +63,14 @@ if ([...FIX_FLAWS_TYPES].some((flawType) => !VALID_FLAW_CHECKS.has(flawType))) { ); } -export const FIX_FLAWS_VERBOSE = JSON.parse( +export const FIX_FLAWS_VERBOSE = parse( // It's on by default because it's such a sensible option to always have // on. process.env.BUILD_FIX_FLAWS_VERBOSE || "true" ); // See explanation in docs/envvars.md -export const ALWAYS_ALLOW_ROBOTS = JSON.parse( +export const ALWAYS_ALLOW_ROBOTS = parse( process.env.BUILD_ALWAYS_ALLOW_ROBOTS || "false" ); @@ -123,7 +133,7 @@ function correctContentPathFromEnv(envVarName) { // filecheck // --------- -export const MAX_FILE_SIZE = JSON.parse( +export const MAX_FILE_SIZE = parse( process.env.FILECHECK_MAX_FILE_SIZE || 500 * 1024 // 500KiB ); @@ -161,7 +171,7 @@ export const PROXY_HOSTNAME = export const CONTENT_HOSTNAME = process.env.SERVER_CONTENT_HOST; export const OFFLINE_CONTENT = process.env.SERVER_OFFLINE_CONTENT === "true"; -export const FAKE_V1_API = JSON.parse(process.env.SERVER_FAKE_V1_API || false); +export const FAKE_V1_API = parse(process.env.SERVER_FAKE_V1_API || false); // ---- // tool