1. import { exec } from 'child_process';
  2. import * as dotenv from 'dotenv';
  3. /**
  4. * Streams environment variables for a staging environment with dry-run mode.
  5. * @param {string} envFile Path to the .env file.
  6. * @param {boolean} dryRun Whether to only log the variables, not load them. Defaults to true.
  7. */
  8. async function streamEnvVariables(envFile, dryRun = true) {
  9. try {
  10. // Load environment variables from the .env file.
  11. dotenv.config({ path: envFile });
  12. // Check if the file exists
  13. if (!__filename.includes(envFile)) {
  14. console.error(`Error: .env file not found at ${envFile}`);
  15. process.exit(1);
  16. }
  17. // Iterate through the environment variables and stream them.
  18. for (const key in process.env) {
  19. if (process.env.hasOwnProperty(key)) {
  20. const value = process.env[key];
  21. const envVarString = `${key}=${value}`;
  22. //Output the env var
  23. console.log(envVarString);
  24. }
  25. }
  26. } catch (error) {
  27. console.error('Error loading or streaming environment variables:', error);
  28. process.exit(1);
  29. }
  30. }
  31. /**
  32. * Executes the streamEnvVariables function.
  33. * @param {string} envFile Path to the .env file.
  34. * @param {boolean} dryRun Whether to only log the variables, not load them. Defaults to true.
  35. */
  36. async function runEnvStream(envFile, dryRun = true) {
  37. streamEnvVariables(envFile, dryRun);
  38. }
  39. // Example Usage:
  40. // runEnvStream('.env', true); // Dry-run mode
  41. // runEnvStream('.env', false); // Load variables
  42. export { runEnvStream };

Add your comment