From 343b11d7c775fc01aa6ff8ab517fd1edd7e483ca Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Fri, 11 Oct 2024 22:47:55 +0300 Subject: [PATCH] feat: add deno support (#11305) * feat: add deno support ref: denoland/deno#20738 * Update packages_nodejs.rs * typo --------- Co-authored-by: Lucas Nogueira --- .changes/deno.md | 6 ++ tooling/cli/node/tauri.js | 6 +- tooling/cli/src/completions.rs | 14 ++-- tooling/cli/src/info/env_nodejs.rs | 17 ++--- tooling/cli/src/info/packages_nodejs.rs | 96 +++++++------------------ 5 files changed, 51 insertions(+), 88 deletions(-) create mode 100644 .changes/deno.md diff --git a/.changes/deno.md b/.changes/deno.md new file mode 100644 index 000000000000..cdd0a1a8d6c3 --- /dev/null +++ b/.changes/deno.md @@ -0,0 +1,6 @@ +--- +'tauri-cli': 'patch:feat' +'@tauri-apps/cli': 'patch:feat' +--- + +Add Deno support in tauri-cli operations. diff --git a/tooling/cli/node/tauri.js b/tooling/cli/node/tauri.js index 8ee5b93cd5ba..3bf72eadbdd4 100755 --- a/tooling/cli/node/tauri.js +++ b/tooling/cli/node/tauri.js @@ -14,9 +14,9 @@ const binStem = path.parse(bin).name.toLowerCase() // can successfully detect what command likely started the execution. let binName -// deno run -A --unstable --node-modules-dir npm:@tauri-apps/cli -if (bin === '@tauri-apps/cli') { - binName = '@tauri-apps/cli' +// deno run -A npm:@tauri-apps/cli or deno task tauri +if (globalThis.navigator?.userAgent?.includes('Deno')) { + binName = bin } // Even if started by a package manager, the binary will be NodeJS. // Some distribution still use "nodejs" as the binary name. diff --git a/tooling/cli/src/completions.rs b/tooling/cli/src/completions.rs index 585ee6f508a4..42ad9a94dbb3 100644 --- a/tooling/cli/src/completions.rs +++ b/tooling/cli/src/completions.rs @@ -10,7 +10,7 @@ use log::info; use std::{fs::write, path::PathBuf}; -const PKG_MANAGERS: &[&str] = &["cargo", "pnpm", "npm", "yarn", "bun"]; +const PKG_MANAGERS: &[&str] = &["cargo", "pnpm", "npm", "yarn", "bun", "deno"]; #[derive(Debug, Clone, Parser)] #[clap(about = "Shell completions")] @@ -29,6 +29,10 @@ fn completions_for(shell: Shell, manager: &'static str, cmd: Command) -> Vec Command::new(manager) .bin_name(manager) .subcommand(Command::new("run").subcommand(tauri)) + } else if manager == "deno" { + Command::new(manager) + .bin_name(manager) + .subcommand(Command::new("task").subcommand(tauri)) } else { Command::new(manager).bin_name(manager).subcommand(tauri) }; @@ -42,13 +46,15 @@ fn get_completions(shell: Shell, cmd: Command) -> Result { let completions = if shell == Shell::Bash { let mut completions = String::from_utf8_lossy(&completions_for(shell, "cargo", cmd)).into_owned(); - for manager in PKG_MANAGERS { + for &manager in PKG_MANAGERS { completions.push_str(&format!( "complete -F _cargo -o bashdefault -o default {} tauri\n", - if manager == &"npm" { + if manager == "npm" { "npm run" - } else if manager == &"bun" { + } else if manager == "bun" { "bun run" + } else if manager == "deno" { + "deno task" } else { manager } diff --git a/tooling/cli/src/info/env_nodejs.rs b/tooling/cli/src/info/env_nodejs.rs index 9d8583ee2a86..5b145f294fa9 100644 --- a/tooling/cli/src/info/env_nodejs.rs +++ b/tooling/cli/src/info/env_nodejs.rs @@ -60,17 +60,10 @@ pub fn items(metadata: &VersionMetadata) -> Vec { .ok() .unwrap_or_default() }), - SectionItem::new().action(|| { - manager_version("pnpm") - .map(|v| format!("pnpm: {}", v)) - .into() - }), - SectionItem::new().action(|| { - manager_version("yarn") - .map(|v| format!("yarn: {}", v)) - .into() - }), - SectionItem::new().action(|| manager_version("npm").map(|v| format!("npm: {}", v)).into()), - SectionItem::new().action(|| manager_version("bun").map(|v| format!("bun: {}", v)).into()), + SectionItem::new().action(|| manager_version("pnpm").map(|v| format!("pnpm: {v}")).into()), + SectionItem::new().action(|| manager_version("yarn").map(|v| format!("yarn: {v}")).into()), + SectionItem::new().action(|| manager_version("npm").map(|v| format!("npm: {v}")).into()), + SectionItem::new().action(|| manager_version("bun").map(|v| format!("bun: {v}")).into()), + SectionItem::new().action(|| manager_version("deno").map(|v| format!("deno: {v}")).into()), ] } diff --git a/tooling/cli/src/info/packages_nodejs.rs b/tooling/cli/src/info/packages_nodejs.rs index 13cbb7e80115..0535a23abf45 100644 --- a/tooling/cli/src/info/packages_nodejs.rs +++ b/tooling/cli/src/info/packages_nodejs.rs @@ -20,6 +20,7 @@ enum PackageManager { Pnpm, Yarn, YarnBerry, + Deno, Bun, } @@ -33,6 +34,7 @@ impl Display for PackageManager { PackageManager::Pnpm => "pnpm", PackageManager::Yarn => "yarn", PackageManager::YarnBerry => "yarn berry", + PackageManager::Deno => "deno", PackageManager::Bun => "bun", } ) @@ -74,7 +76,8 @@ fn npm_latest_version(pm: &PackageManager, name: &str) -> crate::Result { + // Bun and Deno don't support `info` command + PackageManager::Npm | PackageManager::Bun | PackageManager::Deno => { let mut cmd = cross_command("npm"); let output = cmd.arg("show").arg(name).arg("version").output()?; @@ -96,18 +99,6 @@ fn npm_latest_version(pm: &PackageManager, name: &str) -> crate::Result { - let mut cmd = cross_command("npm"); - - let output = cmd.arg("show").arg(name).arg("version").output()?; - if output.status.success() { - let stdout = String::from_utf8_lossy(&output.stdout); - Ok(Some(stdout.replace('\n', ""))) - } else { - Ok(None) - } - } } } @@ -135,7 +126,8 @@ fn npm_package_version>( .output()?, Some(regex::Regex::new("\"Version\":\"([\\da-zA-Z\\-\\.]+)\"").unwrap()), ), - PackageManager::Npm => ( + // Bun and Deno don't support `list` command + PackageManager::Npm | PackageManager::Bun | PackageManager::Deno => ( cross_command("npm") .arg("list") .arg(name) @@ -153,16 +145,6 @@ fn npm_package_version>( .output()?, None, ), - // Bun doesn't support `list` command - PackageManager::Bun => ( - cross_command("npm") - .arg("list") - .arg(name) - .args(["version", "--depth", "0"]) - .current_dir(app_dir) - .output()?, - None, - ), }; if output.status.success() { let stdout = String::from_utf8_lossy(&output.stdout); @@ -179,24 +161,23 @@ fn npm_package_version>( } fn get_package_manager>(app_dir_entries: &[T]) -> PackageManager { - let mut use_npm = false; - let mut use_pnpm = false; - let mut use_yarn = false; - let mut use_bun = false; + let mut found = Vec::new(); for name in app_dir_entries { if name.as_ref() == "package-lock.json" { - use_npm = true; + found.push(PackageManager::Npm); } else if name.as_ref() == "pnpm-lock.yaml" { - use_pnpm = true; + found.push(PackageManager::Pnpm); } else if name.as_ref() == "yarn.lock" { - use_yarn = true; + found.push(PackageManager::Yarn); } else if name.as_ref() == "bun.lockb" { - use_bun = true; + found.push(PackageManager::Bun); + } else if name.as_ref() == "deno.lock" { + found.push(PackageManager::Deno); } } - if !use_npm && !use_pnpm && !use_yarn && !use_bun { + if found.is_empty() { println!( "{}: no lock files found, defaulting to npm", "WARNING".yellow() @@ -204,40 +185,25 @@ fn get_package_manager>(app_dir_entries: &[T]) -> PackageManager { return PackageManager::Npm; } - let mut found = Vec::new(); - - if use_npm { - found.push(PackageManager::Npm); - } - if use_pnpm { - found.push(PackageManager::Pnpm); - } - if use_yarn { - found.push(PackageManager::Yarn); - } - if use_bun { - found.push(PackageManager::Bun); - } + let pkg_manager = found[0]; if found.len() > 1 { - let pkg_manger = found[0]; println!( - "{}: Only one package manager should be used, but found {}.\n Please remove unused package manager lock files, will use {} for now!", - "WARNING".yellow(), - found.iter().map(ToString::to_string).collect::>().join(" and "), - pkg_manger - ); - return pkg_manger; + "{}: Only one package manager should be used, but found {}.\n Please remove unused package manager lock files, will use {} for now!", + "WARNING".yellow(), + found.iter().map(ToString::to_string).collect::>().join(" and "), + pkg_manager + ); } - if use_npm { - PackageManager::Npm - } else if use_pnpm { - PackageManager::Pnpm - } else if use_bun { - PackageManager::Bun + if pkg_manager == PackageManager::Yarn + && manager_version("yarn") + .map(|v| v.chars().next().map(|c| c > '1').unwrap_or_default()) + .unwrap_or(false) + { + PackageManager::YarnBerry } else { - PackageManager::Yarn + pkg_manager } } @@ -251,14 +217,6 @@ pub fn items(app_dir: Option<&PathBuf>, metadata: &VersionMetadata) -> Vec '1').unwrap_or_default()) - .unwrap_or(false) - { - package_manager = PackageManager::YarnBerry; - } - let mut items = Vec::new(); if let Some(app_dir) = app_dir { for (package, version) in [