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

feat: add deno support #11305

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changes/deno.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:feat'
'@tauri-apps/cli': 'patch:feat'
---

Add Deno support in tauri-cli operations.
6 changes: 3 additions & 3 deletions tooling/cli/node/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions tooling/cli/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -29,6 +29,10 @@ fn completions_for(shell: Shell, manager: &'static str, cmd: Command) -> Vec<u8>
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)
};
Expand All @@ -42,13 +46,15 @@ fn get_completions(shell: Shell, cmd: Command) -> Result<String> {
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
}
Expand Down
17 changes: 5 additions & 12 deletions tooling/cli/src/info/env_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,10 @@ pub fn items(metadata: &VersionMetadata) -> Vec<SectionItem> {
.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()),
]
}
96 changes: 27 additions & 69 deletions tooling/cli/src/info/packages_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum PackageManager {
Pnpm,
Yarn,
YarnBerry,
Deno,
Bun,
}

Expand All @@ -33,6 +34,7 @@ impl Display for PackageManager {
PackageManager::Pnpm => "pnpm",
PackageManager::Yarn => "yarn",
PackageManager::YarnBerry => "yarn berry",
PackageManager::Deno => "deno",
PackageManager::Bun => "bun",
}
)
Expand Down Expand Up @@ -74,7 +76,8 @@ fn npm_latest_version(pm: &PackageManager, name: &str) -> crate::Result<Option<S
Ok(None)
}
}
PackageManager::Npm => {
// 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()?;
Expand All @@ -96,18 +99,6 @@ fn npm_latest_version(pm: &PackageManager, name: &str) -> crate::Result<Option<S
Ok(None)
}
}
// Bun doesn't support `info` command
PackageManager::Bun => {
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)
}
}
}
}

Expand Down Expand Up @@ -135,7 +126,8 @@ fn npm_package_version<P: AsRef<Path>>(
.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)
Expand All @@ -153,16 +145,6 @@ fn npm_package_version<P: AsRef<Path>>(
.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);
Expand All @@ -179,65 +161,49 @@ fn npm_package_version<P: AsRef<Path>>(
}

fn get_package_manager<T: AsRef<str>>(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()
);
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::<Vec<_>>().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::<Vec<_>>().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
}
}

Expand All @@ -251,14 +217,6 @@ pub fn items(app_dir: Option<&PathBuf>, metadata: &VersionMetadata) -> Vec<Secti
package_manager = get_package_manager(&app_dir_entries);
}

if package_manager == PackageManager::Yarn
&& manager_version("yarn")
.map(|v| v.chars().next().map(|c| c > '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 [
Expand Down
Loading