Skip to content

Commit

Permalink
cache deno deps
Browse files Browse the repository at this point in the history
  • Loading branch information
coffee-cup committed May 6, 2022
1 parent caa814a commit 2d1ab26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/providers/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::nixpacks::{
app::App,
environment::Environment,
nix::Pkg,
phase::{SetupPhase, StartPhase},
phase::{BuildPhase, SetupPhase, StartPhase},
};
use anyhow::Result;
use regex::Regex;
Expand All @@ -24,7 +24,27 @@ impl Provider for DenoProvider {
Ok(Some(SetupPhase::new(vec![Pkg::new("deno")])))
}

fn build(&self, app: &App, _env: &Environment) -> Result<Option<BuildPhase>> {
match DenoProvider::get_start_file(app)? {
Some(start_file) => Ok(Some(BuildPhase::new(format!("deno cache {}", start_file)))),
None => Ok(None),
}
}

fn start(&self, app: &App, _env: &Environment) -> Result<Option<StartPhase>> {
match DenoProvider::get_start_file(app)? {
Some(start_file) => Ok(Some(StartPhase::new(format!(
"deno run --allow-all {}",
start_file
)))),
None => Ok(None),
}
}
}

impl DenoProvider {
// Find the first index.ts or index.js file to run
fn get_start_file(app: &App) -> Result<Option<String>> {
// Find the first index.ts or index.js file to run
let matches = app.find_files("**/index.[tj]s")?;
let path_to_index = match matches.first() {
Expand All @@ -33,9 +53,6 @@ impl Provider for DenoProvider {
};

let relative_path_to_index = app.strip_source_path(&path_to_index)?;
return Ok(Some(StartPhase::new(format!(
"deno run --allow-all {}",
relative_path_to_index
))));
Ok(Some(relative_path_to_index))
}
}
5 changes: 4 additions & 1 deletion tests/generate_plan_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ fn test_go_mod() -> Result<()> {
#[test]
fn test_deno() -> Result<()> {
let plan = gen_plan("./examples/deno", Vec::new(), None, None, Vec::new(), false)?;
assert_eq!(plan.build.unwrap().cmd, None);
assert_eq!(
plan.build.unwrap().cmd,
Some("deno cache src/index.ts".to_string())
);
assert_eq!(
plan.start.unwrap().cmd,
Some("deno run --allow-all src/index.ts".to_string())
Expand Down

0 comments on commit 2d1ab26

Please sign in to comment.