Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(libs/env): improve error message for bad .env configs #9673

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 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 {
return 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 @@ -31,10 +41,10 @@ export const FOLDERSEARCH = process.env.BUILD_FOLDERSEARCH || "";
export const GOOGLE_ANALYTICS_MEASUREMENT_ID =
process.env.BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID || "";
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 @@ -50,14 +60,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 @@ -130,7 +140,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 @@ -168,7 +178,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
Loading