Skip to content

Commit

Permalink
feat(cli): automatically load .env from current dir (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Jun 19, 2023
1 parent fe5a96d commit 5dc7bd9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-chicken-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Automatically load .env file from current directory or specified path
30 changes: 21 additions & 9 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ fn parse_environment_variables(
path: Option<PathBuf>,
env: Option<PathBuf>,
) -> Result<HashMap<String, String>> {
let path = path.unwrap_or_else(|| PathBuf::from("."));
let mut environment_variables = HashMap::new();

if let Some(env) = env {
Expand All @@ -43,15 +42,28 @@ fn parse_environment_variables(
}

println!("{}", style("Loaded .env file...").black().bright());
} else if let Ok(envfile) = EnvFile::new(path.join(".env")) {
for (key, value) in envfile.store {
environment_variables.insert(key, value);
}

println!(
"{}",
style("Automatically loaded .env file...").black().bright()
} else {
let path = path.map_or_else(
|| PathBuf::from("."),
|path| {
if path.is_file() {
PathBuf::from(".")
} else {
path
}
},
);

if let Ok(envfile) = EnvFile::new(path.join(".env")) {
for (key, value) in envfile.store {
environment_variables.insert(key, value);
}

println!(
"{}",
style("Automatically loaded .env file...").black().bright()
);
}
}

Ok(environment_variables)
Expand Down

0 comments on commit 5dc7bd9

Please sign in to comment.