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

Cache deno deps #67

Merged
merged 2 commits into from
May 6, 2022
Merged
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
31 changes: 25 additions & 6 deletions src/providers/deno.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -17,14 +17,36 @@ impl Provider for DenoProvider {

fn detect(&self, app: &App, _env: &Environment) -> Result<bool> {
let re = Regex::new(r##"(?m)^import .+ from "https://deno.land/[^"]+\.ts";?$"##).unwrap();
app.find_match(&re, "**/*.ts")
Ok(app.includes_file("deno.json")
|| app.includes_file("deno.jsonc")
|| app.find_match(&re, "**/*.ts")?)
}

fn setup(&self, _app: &App, _env: &Environment) -> Result<Option<SetupPhase>> {
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() {
@@ -33,9 +55,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
@@ -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())