Skip to content

Commit dfdb662

Browse files
committed
fix(cli): load env file based on current directory (#930)
1 parent 28bd4c6 commit dfdb662

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

.changeset/config.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
}
88
],
99
"commit": false,
10-
"fixed": [["@lagon/runtime", "@lagon/js-runtime"]],
10+
"fixed": [
11+
[
12+
"@lagon/runtime",
13+
"@lagon/js-runtime"
14+
]
15+
],
1116
"linked": [],
1217
"access": "public",
1318
"baseBranch": "main",
14-
"updateInternalDependencies": "patch",
15-
"privatePackages": { "version": true, "tag": true }
19+
"updateInternalDependencies": "patch"
1620
}

.changeset/metal-mangos-watch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lagon/cli': patch
3+
---
4+
5+
Load environment variable file based on current directory

crates/cli/src/commands/dev.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use notify::event::ModifyKind;
1616
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
1717
use std::collections::HashMap;
1818
use std::convert::Infallible;
19-
use std::path::{Path, PathBuf};
19+
use std::path::PathBuf;
2020
use std::sync::Arc;
2121
use std::time::Duration;
2222
use tokio::runtime::Handle;
@@ -25,20 +25,21 @@ use tokio::sync::Mutex;
2525
const LOCAL_REGION: &str = "local";
2626

2727
fn parse_environment_variables(
28-
root: &Path,
28+
path: Option<PathBuf>,
2929
env: Option<PathBuf>,
3030
) -> Result<HashMap<String, String>> {
31+
let path = path.unwrap_or_else(|| PathBuf::from("."));
3132
let mut environment_variables = HashMap::new();
3233

33-
if let Some(path) = env {
34-
let envfile = EnvFile::new(root.join(path))?;
34+
if let Some(env) = env {
35+
let envfile = EnvFile::new(env)?;
3536

3637
for (key, value) in envfile.store {
3738
environment_variables.insert(key, value);
3839
}
3940

4041
println!("{}", style("Loaded .env file...").black().bright());
41-
} else if let Ok(envfile) = EnvFile::new(root.join(".env")) {
42+
} else if let Ok(envfile) = EnvFile::new(path.join(".env")) {
4243
for (key, value) in envfile.store {
4344
environment_variables.insert(key, value);
4445
}
@@ -163,7 +164,7 @@ pub async fn dev(
163164
allow_code_generation: bool,
164165
prod: bool,
165166
) -> Result<()> {
166-
let (root, function_config) = resolve_path(path, client, public_dir)?;
167+
let (root, function_config) = resolve_path(path.clone(), client, public_dir)?;
167168
let (index, assets) = bundle_function(&function_config, &root, prod)?;
168169

169170
let index = Arc::new(Mutex::new(index));
@@ -183,7 +184,7 @@ pub async fn dev(
183184
.as_ref()
184185
.map(|assets| root.join(assets));
185186

186-
let environment_variables = match parse_environment_variables(&root, env) {
187+
let environment_variables = match parse_environment_variables(path, env) {
187188
Ok(env) => env,
188189
Err(err) => return Err(anyhow!("Could not load environment variables: {:?}", err)),
189190
};

0 commit comments

Comments
 (0)