Skip to content

Commit

Permalink
Fully support Deno 2 by default, support engines.deno
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksrutins committed Dec 20, 2024
1 parent bfef7f4 commit fee967a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/pages/docs/providers/deno.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Deno is detected if there is a `deno.{json,jsonc}` file found or if any `.{ts,ts

Apps built with [Deno Fresh](https://fresh.deno.dev/) should work out of the box.

Deno 2 will be installed if the `NIXPACKS_USE_DENO_2` environment variable is truthy; otherwise, Deno 1 will be used.
Deno 1 will be installed if the `NIXPACKS_USE_DENO_1` environment variable is truthy or `engines.deno` is set to some variant of `1` (e.g. `1`, `v1`, `^1.0`) in either `package.json` or `deno.json`; otherwise, Deno 2 will be used.

## Install

Expand Down
5 changes: 5 additions & 0 deletions examples/deno1/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"engines": {
"deno": "1"
}
}
File renamed without changes.
2 changes: 0 additions & 2 deletions examples/deno2/nixpacks.toml

This file was deleted.

55 changes: 47 additions & 8 deletions src/providers/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ pub struct DenoTasks {
pub start: Option<String>,
}

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct DenoEngines {
pub deno: Option<String>,
}

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct DenoJson {
pub tasks: Option<DenoTasks>,
pub engines: Option<DenoEngines>,
}

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct PackageJson {
pub engines: Option<DenoEngines>,
pub scripts: Option<DenoTasks>,
}

pub struct DenoProvider {}
Expand All @@ -46,7 +58,22 @@ impl Provider for DenoProvider {
let mut plan = BuildPlan::default();

let mut setup = Phase::setup(Some(vec![Pkg::new("deno")]));
if env.is_config_variable_truthy("USE_DENO_2") {

let package_json = app.read_json::<PackageJson>("package.json");
let deno_json = app.read_json::<DenoJson>("deno.json");
let v1_regex = Regex::new(r"^((>=)|\^)?v?1")?;
if !(env.is_config_variable_truthy("USE_DENO_1")
|| package_json
.map(|p| p.engines.map(|e| e.deno.map(|d| v1_regex.is_match(&d))))
.unwrap_or(Some(Some(false)))
.unwrap_or(Some(false))
.unwrap_or(false)
|| deno_json
.map(|p| p.engines.map(|e| e.deno.map(|d| v1_regex.is_match(&d))))
.unwrap_or(Some(Some(false)))
.unwrap_or(Some(false))
.unwrap_or(false))
{
setup.pin(Some(NIXPACKS_ARCHIVE_LATEST_DENO.to_string()));
}
plan.add_phase(setup);
Expand Down Expand Up @@ -121,26 +148,38 @@ impl DenoProvider {

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_deno2() {
fn test_deno_versions() {
let deno = DenoProvider {};
assert_eq!(
deno.get_build_plan(&App::new("examples/deno").unwrap(), &Environment::default())
.unwrap()
.unwrap()
.phases
.unwrap()
.get("setup")
.unwrap()
.nixpkgs_archive
.as_ref()
.unwrap(),
&NIXPACKS_ARCHIVE_LATEST_DENO.to_string()
);
assert_eq!(
deno.get_build_plan(
&App::new("examples/deno2").unwrap(),
&Environment::from_envs(vec!["NIXPACKS_USE_DENO_2=1"]).unwrap()
&App::new("examples/deno1").unwrap(),
&Environment::default()
)
.unwrap()
.unwrap()
.phases
.unwrap()
.get("setup")
.unwrap()
.nixpkgs_archive
.as_ref()
.unwrap(),
&NIXPACKS_ARCHIVE_LATEST_DENO.to_string()
.nixpkgs_archive,
None
);
}
}
35 changes: 35 additions & 0 deletions tests/snapshots/generate_plan_tests__deno1.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: tests/generate_plan_tests.rs
expression: plan
snapshot_kind: text
---
{
"providers": [],
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "deno"
},
"phases": {
"build": {
"name": "build",
"dependsOn": [
"install",
"setup"
],
"cmds": [
"deno cache src/index.ts"
]
},
"setup": {
"name": "setup",
"nixPkgs": [
"deno"
],
"nixOverlays": [],
"nixpkgsArchive": "[archive]"
}
},
"start": {
"cmd": "deno run --allow-all src/index.ts"
}
}

0 comments on commit fee967a

Please sign in to comment.