From fee967a07ddb969f223f27521e5e2c95fefff9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleks=20R=C5=ABti=C5=86=C5=A1?= Date: Fri, 20 Dec 2024 14:36:45 -0500 Subject: [PATCH] Fully support Deno 2 by default, support `engines.deno` --- docs/pages/docs/providers/deno.md | 2 +- examples/deno1/deno.json | 5 ++ examples/{deno2 => deno1}/src/index.ts | 0 examples/deno2/nixpacks.toml | 2 - src/providers/deno.rs | 55 ++++++++++++++++--- .../snapshots/generate_plan_tests__deno1.snap | 35 ++++++++++++ 6 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 examples/deno1/deno.json rename examples/{deno2 => deno1}/src/index.ts (100%) delete mode 100644 examples/deno2/nixpacks.toml create mode 100644 tests/snapshots/generate_plan_tests__deno1.snap diff --git a/docs/pages/docs/providers/deno.md b/docs/pages/docs/providers/deno.md index e8e8d978d..7badb4164 100644 --- a/docs/pages/docs/providers/deno.md +++ b/docs/pages/docs/providers/deno.md @@ -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 diff --git a/examples/deno1/deno.json b/examples/deno1/deno.json new file mode 100644 index 000000000..d71d5d793 --- /dev/null +++ b/examples/deno1/deno.json @@ -0,0 +1,5 @@ +{ + "engines": { + "deno": "1" + } +} diff --git a/examples/deno2/src/index.ts b/examples/deno1/src/index.ts similarity index 100% rename from examples/deno2/src/index.ts rename to examples/deno1/src/index.ts diff --git a/examples/deno2/nixpacks.toml b/examples/deno2/nixpacks.toml deleted file mode 100644 index b497c2708..000000000 --- a/examples/deno2/nixpacks.toml +++ /dev/null @@ -1,2 +0,0 @@ -[variables] -NIXPACKS_USE_DENO_2 = '1' \ No newline at end of file diff --git a/src/providers/deno.rs b/src/providers/deno.rs index 4a2d587ba..442506171 100644 --- a/src/providers/deno.rs +++ b/src/providers/deno.rs @@ -20,9 +20,21 @@ pub struct DenoTasks { pub start: Option, } +#[derive(Serialize, Deserialize, Default, Debug)] +pub struct DenoEngines { + pub deno: Option, +} + #[derive(Serialize, Deserialize, Default, Debug)] pub struct DenoJson { pub tasks: Option, + pub engines: Option, +} + +#[derive(Serialize, Deserialize, Default, Debug)] +pub struct PackageJson { + pub engines: Option, + pub scripts: Option, } pub struct DenoProvider {} @@ -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::("package.json"); + let deno_json = app.read_json::("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); @@ -121,15 +148,29 @@ 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() @@ -137,10 +178,8 @@ mod tests { .unwrap() .get("setup") .unwrap() - .nixpkgs_archive - .as_ref() - .unwrap(), - &NIXPACKS_ARCHIVE_LATEST_DENO.to_string() + .nixpkgs_archive, + None ); } } diff --git a/tests/snapshots/generate_plan_tests__deno1.snap b/tests/snapshots/generate_plan_tests__deno1.snap new file mode 100644 index 000000000..08f32a3ea --- /dev/null +++ b/tests/snapshots/generate_plan_tests__deno1.snap @@ -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" + } +}