Skip to content

Commit 68dd41f

Browse files
committed
chore(build): better error message for bad .env configs
1 parent 68c324e commit 68dd41f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

Diff for: libs/env/index.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { VALID_FLAW_CHECKS } from "../constants/index.js";
1010
const dirname = fileURLToPath(new URL(".", import.meta.url));
1111
const ROOT = path.join(dirname, "..", "..");
1212

13+
function parse(value) {
14+
try {
15+
JSON.parse(value);
16+
} catch (e) {
17+
throw new Error(`Error parsing value '${value}' in .env file`, {
18+
cause: e,
19+
});
20+
}
21+
}
22+
1323
dotenv.config({
1424
path: path.join(cwd(), process.env.ENV_FILE || ".env"),
1525
});
@@ -30,14 +40,14 @@ export const FILES = process.env.BUILD_FILES || "";
3040
export const FOLDERSEARCH = process.env.BUILD_FOLDERSEARCH || "";
3141
export const GOOGLE_ANALYTICS_ACCOUNT =
3242
process.env.BUILD_GOOGLE_ANALYTICS_ACCOUNT || "";
33-
export const GOOGLE_ANALYTICS_DEBUG = JSON.parse(
43+
export const GOOGLE_ANALYTICS_DEBUG = parse(
3444
process.env.BUILD_GOOGLE_ANALYTICS_DEBUG || "false"
3545
);
3646
export const NO_PROGRESSBAR = Boolean(
37-
JSON.parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false")
47+
parse(process.env.BUILD_NO_PROGRESSBAR || process.env.CI || "false")
3848
);
39-
export const FIX_FLAWS = JSON.parse(process.env.BUILD_FIX_FLAWS || "false");
40-
export const FIX_FLAWS_DRY_RUN = JSON.parse(
49+
export const FIX_FLAWS = parse(process.env.BUILD_FIX_FLAWS || "false");
50+
export const FIX_FLAWS_DRY_RUN = parse(
4151
process.env.BUILD_FIX_FLAWS_DRY_RUN || "false"
4252
);
4353
export const FIX_FLAWS_TYPES = new Set(
@@ -53,14 +63,14 @@ if ([...FIX_FLAWS_TYPES].some((flawType) => !VALID_FLAW_CHECKS.has(flawType))) {
5363
);
5464
}
5565

56-
export const FIX_FLAWS_VERBOSE = JSON.parse(
66+
export const FIX_FLAWS_VERBOSE = parse(
5767
// It's on by default because it's such a sensible option to always have
5868
// on.
5969
process.env.BUILD_FIX_FLAWS_VERBOSE || "true"
6070
);
6171

6272
// See explanation in docs/envvars.md
63-
export const ALWAYS_ALLOW_ROBOTS = JSON.parse(
73+
export const ALWAYS_ALLOW_ROBOTS = parse(
6474
process.env.BUILD_ALWAYS_ALLOW_ROBOTS || "false"
6575
);
6676

@@ -123,7 +133,7 @@ function correctContentPathFromEnv(envVarName) {
123133
// filecheck
124134
// ---------
125135

126-
export const MAX_FILE_SIZE = JSON.parse(
136+
export const MAX_FILE_SIZE = parse(
127137
process.env.FILECHECK_MAX_FILE_SIZE || 500 * 1024 // 500KiB
128138
);
129139

@@ -161,7 +171,7 @@ export const PROXY_HOSTNAME =
161171
export const CONTENT_HOSTNAME = process.env.SERVER_CONTENT_HOST;
162172
export const OFFLINE_CONTENT = process.env.SERVER_OFFLINE_CONTENT === "true";
163173

164-
export const FAKE_V1_API = JSON.parse(process.env.SERVER_FAKE_V1_API || false);
174+
export const FAKE_V1_API = parse(process.env.SERVER_FAKE_V1_API || false);
165175

166176
// ----
167177
// tool

0 commit comments

Comments
 (0)