import { exec } from 'child_process';
import * as dotenv from 'dotenv';
/**
* Streams environment variables for a staging environment with dry-run mode.
* @param {string} envFile Path to the .env file.
* @param {boolean} dryRun Whether to only log the variables, not load them. Defaults to true.
*/
async function streamEnvVariables(envFile, dryRun = true) {
try {
// Load environment variables from the .env file.
dotenv.config({ path: envFile });
// Check if the file exists
if (!__filename.includes(envFile)) {
console.error(`Error: .env file not found at ${envFile}`);
process.exit(1);
}
// Iterate through the environment variables and stream them.
for (const key in process.env) {
if (process.env.hasOwnProperty(key)) {
const value = process.env[key];
const envVarString = `${key}=${value}`;
//Output the env var
console.log(envVarString);
}
}
} catch (error) {
console.error('Error loading or streaming environment variables:', error);
process.exit(1);
}
}
/**
* Executes the streamEnvVariables function.
* @param {string} envFile Path to the .env file.
* @param {boolean} dryRun Whether to only log the variables, not load them. Defaults to true.
*/
async function runEnvStream(envFile, dryRun = true) {
streamEnvVariables(envFile, dryRun);
}
// Example Usage:
// runEnvStream('.env', true); // Dry-run mode
// runEnvStream('.env', false); // Load variables
export { runEnvStream };
Add your comment