-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathloadVariables.ts
More file actions
39 lines (32 loc) · 1.32 KB
/
loadVariables.ts
File metadata and controls
39 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import { ADDRESSES_DIR, GATEWAY_ADDRESSES_ENV_FILE_NAME } from "../../hardhat.config";
import { pascalCaseToAddressEnvVar } from "../utils";
// Get the required environment variable, throw an error if it's not set or empty
export function getRequiredEnvVar(name: string): string {
if (!(name in process.env)) {
throw new Error(`"${name}" env variable is not set`);
}
const value = process.env[name]!;
if (value.trim() === "") {
throw new Error(`"${name}" env variable is set but empty`);
}
return value;
}
// Get the required address from the environment variable, throw an error if it's not set or empty
export function getRequiredAddressEnvVar(name: string): string {
const addressEnvVarName = pascalCaseToAddressEnvVar(name);
return getRequiredEnvVar(addressEnvVarName);
}
// Load the addresses as environment variables from the env file
export function loadAddressEnvVarsFromFile(fileName: string) {
const envFilePath = path.join(ADDRESSES_DIR, fileName);
if (!fs.existsSync(envFilePath)) {
throw new Error(`Environment file for addresses not found: ${envFilePath}`);
}
dotenv.config({ path: envFilePath, override: true });
}
export function loadGatewayAddresses() {
loadAddressEnvVarsFromFile(GATEWAY_ADDRESSES_ENV_FILE_NAME);
}