Skip to content

Commit

Permalink
feat(cli,docs): automatically load .env files with lagon dev (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Apr 22, 2023
1 parent 54df73e commit b92e0de
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-bears-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/cli': patch
'@lagon/docs': patch
---

Automatically load .env files with `lagon dev` if present in root
18 changes: 15 additions & 3 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Error, Result};
use anyhow::{anyhow, Error, Result};
use chrono::offset::Local;
use colored::Colorize;
use envfile::EnvFile;
Expand All @@ -21,7 +21,9 @@ use std::time::Duration;
use tokio::runtime::Handle;
use tokio::sync::Mutex;

use crate::utils::{bundle_function, error, info, input, resolve_path, success, warn, Assets};
use crate::utils::{
bundle_function, debug, error, info, input, resolve_path, success, warn, Assets,
};

const LOCAL_REGION: &str = "local";

Expand All @@ -37,6 +39,12 @@ fn parse_environment_variables(
for (key, value) in envfile.store {
environment_variables.insert(key, value);
}
} else if let Ok(envfile) = EnvFile::new(root.join(".env")) {
for (key, value) in envfile.store {
environment_variables.insert(key, value);
}

println!("{}", debug("Automatically loaded .env file..."));
}

Ok(environment_variables)
Expand Down Expand Up @@ -172,7 +180,11 @@ pub async fn dev(
.assets
.as_ref()
.map(|assets| root.join(assets));
let environment_variables = parse_environment_variables(&root, env)?;

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

let (tx, rx) = flume::unbounded();
let (index_tx, index_rx) = flume::unbounded();
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ enum Commands {
/// Hostname to start dev server on
#[clap(long)]
hostname: Option<String>,
/// Path to a env file to parse
/// Path to a custom environment variables file to use
#[clap(short, long, value_parser)]
env: Option<PathBuf>,
/// Allow code generation from strings using `eval` / `new Function`
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/pages/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ This command accepts the following arguments and options:
- `--public, -p <<PUBLIC_DIR>>` allows you to specify a path to a directory containing assets to be served statically.
- `--hostname <HOSTNAME>` allows you to specify a custom hostname to start the server on. (Default: `127.0.0.1`)
- `--port <PORT>` allows you to specify a custom port to start the server on. (Default: `1234`)
- `--env <FILE>` allows you to specify an environment file (typically `.env`) to use to inject environment variables.
- `--env <FILE>` allows you to specify a custom path to an environment file to inject [environment variables](/cloud/environment-variables). (Default: `.env`)
- `--allow-code-generation` allows you to enable code generation from strings (`eval` / `new Function`)

<Callout type="warning">
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/pages/cloud/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function handler(request: Request): Response {

## Development

During development, you can use the `--env` flag of the [`dev` command](http://localhost:3000/cli#lagon-dev) to specify a `.env` file to load.
During development, `.env` files are automatically detected and loaded. You can also manually specify the `--env` flag of the [`dev` command](http://localhost:3000/cli#lagon-dev) to use a custom path for your environment file.

## Adding environment variables

Expand Down

1 comment on commit b92e0de

@vercel
Copy link

@vercel vercel bot commented on b92e0de Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./packages/docs

docs-lagon.vercel.app
docs-git-main-lagon.vercel.app
lagon-docs.vercel.app
docs.lagon.app

Please sign in to comment.