From dfdb6626e86b9c81bff2275ec03944aaa9259c69 Mon Sep 17 00:00:00 2001 From: Tom Lienard Date: Mon, 5 Jun 2023 17:38:51 +0200 Subject: [PATCH] fix(cli): load env file based on current directory (#930) --- .changeset/config.json | 10 +++++++--- .changeset/metal-mangos-watch.md | 5 +++++ crates/cli/src/commands/dev.rs | 15 ++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 .changeset/metal-mangos-watch.md diff --git a/.changeset/config.json b/.changeset/config.json index f0cd9e19a..96a262a5e 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -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" } diff --git a/.changeset/metal-mangos-watch.md b/.changeset/metal-mangos-watch.md new file mode 100644 index 000000000..26f6511ce --- /dev/null +++ b/.changeset/metal-mangos-watch.md @@ -0,0 +1,5 @@ +--- +'@lagon/cli': patch +--- + +Load environment variable file based on current directory diff --git a/crates/cli/src/commands/dev.rs b/crates/cli/src/commands/dev.rs index a32b20be9..bc822519a 100644 --- a/crates/cli/src/commands/dev.rs +++ b/crates/cli/src/commands/dev.rs @@ -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; @@ -25,20 +25,21 @@ use tokio::sync::Mutex; const LOCAL_REGION: &str = "local"; fn parse_environment_variables( - root: &Path, + path: Option, env: Option, ) -> Result> { + 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); } @@ -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)); @@ -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)), };