Skip to content

Commit

Permalink
feat(cli): allow lagon deploy & lagon build to specify files (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Mar 25, 2023
1 parent 9da1136 commit 0dffdb2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-hotels-camp.md
@@ -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
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
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
@@ -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
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
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
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

1 comment on commit 0dffdb2

@vercel
Copy link

@vercel vercel bot commented on 0dffdb2 Mar 25, 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

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

Please sign in to comment.