50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
const fs = require("node:fs");
|
|
const YAML = require("yaml");
|
|
|
|
const ciFilepath = ".github/workflows/ci.yml";
|
|
const envFilepath = "src/env.mjs";
|
|
|
|
const ignoreEnvs = ["NODE_ENV", "SKIP_ENV_VALIDATION"];
|
|
|
|
// ANSI escape codes for colors
|
|
const colors = {
|
|
red: "\x1b[31m%s\x1b[0m",
|
|
green: "\x1b[32m%s\x1b[0m",
|
|
yellow: "\x1b[33m%s\x1b[0m",
|
|
};
|
|
|
|
console.log();
|
|
console.log(colors.yellow, "Environment variables checker:");
|
|
// Read CI file content
|
|
try {
|
|
const ciContent = fs.readFileSync(ciFilepath, "utf8");
|
|
const ciData = YAML.parse(ciContent);
|
|
|
|
// Read env file content
|
|
const envContent = fs.readFileSync(envFilepath, "utf8");
|
|
const envRegex = /process\.env\.([A-Z_0-9]+)/g;
|
|
const declaredEnvs = [];
|
|
|
|
let match = envRegex.exec(envContent);
|
|
while (match !== null) {
|
|
declaredEnvs.push(match[1]);
|
|
match = envRegex.exec(envContent); // Re-assign at the end of the loop
|
|
}
|
|
|
|
// Remove ignored environment variables from the list of declared environment variables
|
|
const filteredEnvs = declaredEnvs.filter((env) => !ignoreEnvs.includes(env));
|
|
|
|
// Check for missing env variables in CI file
|
|
const missingEnvs = filteredEnvs.filter((env) => !ciData.env[env]);
|
|
|
|
if (missingEnvs.length > 0) {
|
|
console.error(
|
|
colors.red,
|
|
`Missing environment variables in ${ciFilepath}: ${missingEnvs.join(", ")}`,
|
|
);
|
|
} else {
|
|
console.log(colors.green, "No missing environment variables found.");
|
|
}
|
|
} catch (error) {
|
|
console.error(colors.red, `Error processing files: ${error.message}`);
|
|
}
|