Skip to content

Commit

Permalink
fix(cli): load env file based on current directory (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Jun 5, 2023
1 parent 28bd4c6 commit dfdb662
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
10 changes: 7 additions & 3 deletions .changeset/config.json
Expand Up @@ -7,10 +7,14 @@
}
],
"commit": false,
"fixed": [["@lagon/runtime", "@lagon/js-runtime"]],
"fixed": [
[
"@lagon/runtime",
"@lagon/js-runtime"
]
],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"privatePackages": { "version": true, "tag": true }
"updateInternalDependencies": "patch"
}
5 changes: 5 additions & 0 deletions .changeset/metal-mangos-watch.md
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Load environment variable file based on current directory
15 changes: 8 additions & 7 deletions crates/cli/src/commands/dev.rs
Expand Up @@ -16,7 +16,7 @@ use notify::event::ModifyKind;
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::convert::Infallible;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::runtime::Handle;
Expand All @@ -25,20 +25,21 @@ use tokio::sync::Mutex;
const LOCAL_REGION: &str = "local";

fn parse_environment_variables(
root: &Path,
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(path) = env {
let envfile = EnvFile::new(root.join(path))?;
if let Some(env) = env {
let envfile = EnvFile::new(env)?;

for (key, value) in envfile.store {
environment_variables.insert(key, value);
}

println!("{}", style("Loaded .env file...").black().bright());
} else if let Ok(envfile) = EnvFile::new(root.join(".env")) {
} else if let Ok(envfile) = EnvFile::new(path.join(".env")) {
for (key, value) in envfile.store {
environment_variables.insert(key, value);
}
Expand Down Expand Up @@ -163,7 +164,7 @@ pub async fn dev(
allow_code_generation: bool,
prod: bool,
) -> Result<()> {
let (root, function_config) = resolve_path(path, client, public_dir)?;
let (root, function_config) = resolve_path(path.clone(), client, public_dir)?;
let (index, assets) = bundle_function(&function_config, &root, prod)?;

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

let environment_variables = match parse_environment_variables(&root, env) {
let environment_variables = match parse_environment_variables(path, env) {
Ok(env) => env,
Err(err) => return Err(anyhow!("Could not load environment variables: {:?}", err)),
};
Expand Down

0 comments on commit dfdb662

Please sign in to comment.