From fb6348500ffc827b3aaca3e8299e1a3964be43c1 Mon Sep 17 00:00:00 2001 From: HasanAlrimawi <141642411+HasanAlrimawi@users.noreply.github.com> Date: Wed, 10 Jul 2024 00:33:41 +0300 Subject: feat(compile): support --env (#24166) Supported the use of --env flag with the compile subcommand, so that the generated executable/binary file can access the passed env file. --- cli/args/mod.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'cli/args') diff --git a/cli/args/mod.rs b/cli/args/mod.rs index e0eff6171..553af51a1 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -793,19 +793,7 @@ impl CliOptions { ) .with_context(|| "Resolving node_modules folder.")?; - if let Some(env_file_name) = &flags.env_file { - match from_filename(env_file_name) { - Ok(_) => (), - Err(error) => { - match error { - dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}",colors::yellow("Warning"), env_file_name, index, line), - dotenvy::Error::Io(_)=> log::info!("{} The `--env` flag was used, but the environment file specified '{}' was not found.",colors::yellow("Warning"),env_file_name), - dotenvy::Error::EnvVar(_)=>log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}",colors::yellow("Warning"),env_file_name), - _ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name), - } - } - } - } + load_env_variables_from_env_file(flags.env_file.as_ref()); let disable_deprecated_api_warning = flags.log_level == Some(log::Level::Error) @@ -1118,6 +1106,10 @@ impl CliOptions { } } + pub fn env_file_name(&self) -> Option<&String> { + self.flags.env_file.as_ref() + } + pub fn enable_future_features(&self) -> bool { *DENO_FUTURE } @@ -1871,6 +1863,23 @@ pub fn config_to_deno_graph_workspace_member( }) } +fn load_env_variables_from_env_file(filename: Option<&String>) { + let Some(env_file_name) = filename else { + return; + }; + match from_filename(env_file_name) { + Ok(_) => (), + Err(error) => { + match error { + dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}",colors::yellow("Warning"), env_file_name, index, line), + dotenvy::Error::Io(_)=> log::info!("{} The `--env` flag was used, but the environment file specified '{}' was not found.",colors::yellow("Warning"),env_file_name), + dotenvy::Error::EnvVar(_)=> log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}",colors::yellow("Warning"),env_file_name), + _ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name), + } + } + } +} + #[cfg(test)] mod test { use pretty_assertions::assert_eq; -- cgit v1.2.3