Skip to content

Commit

Permalink
chore(build): better error message for bad .env configs
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed Sep 17, 2023
1 parent 68c324e commit 68dd41f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions libs/env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
});
Expand All @@ -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(
Expand All @@ -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"
);

Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 68dd41f

Please sign in to comment.