Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): allow lagon deploy & lagon build to specify files #688

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/smart-hotels-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/cli': patch
'@lagon/docs': patch
---

Allow `lagon deploy` & `lagon build` to specify files and folders
7 changes: 3 additions & 4 deletions crates/cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use std::{fs, path::PathBuf};

use anyhow::{anyhow, Result};

use crate::utils::{bundle_function, debug, get_root, print_progress, success, FunctionConfig};
use crate::utils::{bundle_function, debug, print_progress, resolve_path, success};

pub fn build(
path: Option<PathBuf>,
client: Option<PathBuf>,
public_dir: Option<PathBuf>,
directory: Option<PathBuf>,
) -> Result<()> {
let root = get_root(directory);
let function_config = FunctionConfig::load(&root, client, public_dir)?;
let (root, function_config) = resolve_path(path, client, public_dir)?;
let (index, assets) = bundle_function(&function_config, &root)?;

let end_progress = print_progress("Writting index.js...");
Expand Down
7 changes: 3 additions & 4 deletions crates/cli/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dialoguer::{Confirm, Input, Select};
use serde::{Deserialize, Serialize};

use crate::utils::{
create_deployment, debug, get_root, info, print_progress, Config, FunctionConfig, TrpcClient,
create_deployment, debug, info, print_progress, resolve_path, Config, TrpcClient,
};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -53,10 +53,10 @@ impl Display for Function {
pub type FunctionsResponse = Vec<Function>;

pub async fn deploy(
path: Option<PathBuf>,
client: Option<PathBuf>,
public_dir: Option<PathBuf>,
prod: bool,
directory: Option<PathBuf>,
) -> Result<()> {
let config = Config::new()?;

Expand All @@ -66,8 +66,7 @@ pub async fn deploy(
));
}

let root = get_root(directory);
let mut function_config = FunctionConfig::load(&root, client, public_dir)?;
let (root, mut function_config) = resolve_path(path, client, public_dir)?;

if function_config.function_id.is_empty() {
println!("{}", debug("No deployment config found..."));
Expand Down
37 changes: 3 additions & 34 deletions crates/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Error, Result};
use anyhow::{Error, Result};
use chrono::offset::Local;
use colored::Colorize;
use envfile::EnvFile;
Expand All @@ -16,7 +16,6 @@ use log::{
};
use notify::event::ModifyKind;
use notify::{Config, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use pathdiff::diff_paths;
use std::collections::HashMap;
use std::convert::Infallible;
use std::path::{Path, PathBuf};
Expand All @@ -25,7 +24,7 @@ use std::time::Duration;
use tokio::runtime::Handle;
use tokio::sync::Mutex;

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

const LOCAL_REGION: &str = "local";

Expand Down Expand Up @@ -178,37 +177,7 @@ pub async fn dev(
env: Option<PathBuf>,
allow_code_generation: bool,
) -> Result<()> {
let path = path.unwrap_or_else(|| PathBuf::from("."));

if !path.exists() {
return Err(anyhow!("File or directory not found"));
}

let (root, function_config) = match path.is_file() {
true => {
let root = PathBuf::from(path.parent().unwrap());

let index = diff_paths(&path, &root).unwrap();
let client = client.map(|client| diff_paths(client, &root).unwrap());
let assets = public_dir.map(|public_dir| diff_paths(public_dir, &root).unwrap());

(
root,
FunctionConfig {
function_id: String::new(),
organization_id: String::new(),
index,
client,
assets,
},
)
}
false => (
path.clone(),
FunctionConfig::load(&path, client, public_dir)?,
),
};

let (root, function_config) = resolve_path(path, client, public_dir)?;
let (index, assets) = bundle_function(&function_config, &root)?;

let server_index = index.clone();
Expand Down
20 changes: 10 additions & 10 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ enum Commands {
Logout,
/// Deploy a new or existing Function
Deploy {
/// Path to a file or a directory containing a Function
#[clap(value_parser)]
path: Option<PathBuf>,
/// Path to a client-side script
#[clap(short, long, value_parser)]
client: Option<PathBuf>,
Expand All @@ -42,9 +45,6 @@ enum Commands {
/// Deploy as a production deployment
#[clap(visible_alias = "production", long)]
prod: bool,
/// Path to a directory containing a Function
#[clap(value_parser)]
directory: Option<PathBuf>,
},
/// Delete an existing Function
Rm {
Expand Down Expand Up @@ -78,15 +78,15 @@ enum Commands {
},
/// Build a Function without deploying it
Build {
/// Path to a file or a directory containing a Function
#[clap(value_parser)]
path: Option<PathBuf>,
/// Path to a client-side script
#[clap(short, long, value_parser)]
client: Option<PathBuf>,
/// Path to a public directory to serve assets from
#[clap(short, long, value_parser)]
public_dir: Option<PathBuf>,
/// Path to a directory containing a Function
#[clap(value_parser)]
directory: Option<PathBuf>,
},
/// Link a local Function file to an already deployed Function
Link {
Expand Down Expand Up @@ -127,11 +127,11 @@ async fn main() {
Commands::Login => commands::login().await,
Commands::Logout => commands::logout(),
Commands::Deploy {
path,
client,
public_dir,
prod,
directory,
} => commands::deploy(client, public_dir, prod, directory).await,
} => commands::deploy(path, client, public_dir, prod).await,
Commands::Rm { directory } => commands::rm(directory).await,
Commands::Dev {
path,
Expand All @@ -154,10 +154,10 @@ async fn main() {
.await
}
Commands::Build {
path,
client,
public_dir,
directory,
} => commands::build(client, public_dir, directory),
} => commands::build(path, client, public_dir),
Commands::Link { directory } => commands::link(directory).await,
Commands::Ls { directory } => commands::ls(directory).await,
Commands::Undeploy {
Expand Down
37 changes: 37 additions & 0 deletions crates/cli/src/utils/deployments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ impl FunctionConfig {
}
}

pub fn resolve_path(
path: Option<PathBuf>,
client: Option<PathBuf>,
public_dir: Option<PathBuf>,
) -> Result<(PathBuf, FunctionConfig)> {
let path = path.unwrap_or_else(|| PathBuf::from("."));

if !path.exists() {
return Err(anyhow!("File or directory not found"));
}

match path.is_file() {
true => {
let root = PathBuf::from(path.parent().unwrap());

let index = diff_paths(&path, &root).unwrap();
let client = client.map(|client| diff_paths(client, &root).unwrap());
let assets = public_dir.map(|public_dir| diff_paths(public_dir, &root).unwrap());

Ok((
root,
FunctionConfig {
function_id: String::new(),
organization_id: String::new(),
index,
client,
assets,
},
))
}
false => Ok((
path.clone(),
FunctionConfig::load(&path, client, public_dir)?,
)),
}
}

pub fn get_root(root: Option<PathBuf>) -> PathBuf {
match root {
Some(path) => path,
Expand Down
6 changes: 4 additions & 2 deletions packages/docs/pages/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If you then want to trigger a new Deployment, re-run the same command. By defaul

This command accepts the following arguments and options:

- `[DIRECTORY]` is an optional path to a directory containing the Function. (Default: `.`)
- `[PATH]` is an optional path to a file or directory containing the Function. (Default: `.`)
- `--client, -c <CLIENT>` allows you to specify a path to an additional file to bundle as a client-side script.
- `--public, -p <<PUBLIC_DIR>>` allows you to specify a path to a directory containing assets to be served statically.
- `--production, --prod` allows you to deploy the Function in production mode. (Default: `false`)
Expand All @@ -58,6 +58,8 @@ Examples:
```bash
# Deploy the current directory to Production
lagon deploy --prod
# Deploy the index.ts file
lagon deploy ./index.ts
# Deploy the my-project directory and override the public directory
lagon deploy ./my-project --public ./my-project/assets
```
Expand Down Expand Up @@ -168,7 +170,7 @@ For debugging purposes, you can build a Function and see its output without depl

This command accepts the following arguments and options:

- `[DIRECTORY]` is an optional path to a directory containing the Function. (Default: `.`)
- `[PATH]` is an optional path to a file or directory containing the Function. (Default: `.`)
- `--client, -c <CLIENT>` allows you to specify a path to an additional file to bundle as a client-side script.
- `--public, -p <<PUBLIC_DIR>>` allows you to specify a path to a directory containing assets to be served statically.

Expand Down