Skip to content

Commit

Permalink
feat: support Bun package manager
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Aug 30, 2023
1 parent af3268a commit e733cde
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changes/support-bun.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:feat'
'@tauri-apps/cli': 'patch:feat'
---

Support Bun package manager in CLI
2 changes: 1 addition & 1 deletion tooling/cli/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Tauri is a polyglot and generic system that is very composable and allows engine

Tauri apps can have custom menus and have tray-type interfaces. They can be updated, and are managed by the user's operating system as expected. They are very small, because they use the system's webview. They do not ship a runtime, since the final binary is compiled from rust. This makes the reversing of Tauri apps not a trivial task.
## This module
Written in Typescript and packaged such that it can be used with `npm`, `pnpm`, and `yarn`, this library provides a node.js runner for common tasks when using Tauri, like `yarn tauri dev`. For the most part it is a wrapper around [tauri-cli](https://github.com/tauri-apps/tauri/blob/dev/tooling/cli).
Written in Typescript and packaged such that it can be used with `npm`, `pnpm`, `yarn`, and `bun`, this library provides a node.js runner for common tasks when using Tauri, like `yarn tauri dev`. For the most part it is a wrapper around [tauri-cli](https://github.com/tauri-apps/tauri/blob/dev/tooling/cli).

To learn more about the details of how all of these pieces fit together, please consult this [ARCHITECTURE.md](https://github.com/tauri-apps/tauri/blob/dev/ARCHITECTURE.md) document.

Expand Down
4 changes: 0 additions & 4 deletions tooling/cli/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/* tslint:disable */
/* eslint-disable */

Expand Down
4 changes: 0 additions & 4 deletions tooling/cli/node/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
Expand Down
10 changes: 5 additions & 5 deletions tooling/cli/node/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const cli = require('./main')
const path = require('path')

const [bin, script, ...arguments] = process.argv
const [bin, script, ...args] = process.argv
const binStem = path.parse(bin).name.toLowerCase()

// We want to make a helpful binary name for the underlying CLI helper, if we
Expand All @@ -20,7 +20,7 @@ if (bin === '@tauri-apps/cli') {
}
// Even if started by a package manager, the binary will be NodeJS.
// Some distribution still use "nodejs" as the binary name.
else if (binStem.match(/(nodejs|node)\-?([0-9]*)*$/g)) {
else if (binStem.match(/(nodejs|node|bun)\-?([0-9]*)*$/g)) {
const managerStem = process.env.npm_execpath
? path.parse(process.env.npm_execpath).name.toLowerCase()
: null
Expand All @@ -32,7 +32,7 @@ else if (binStem.match(/(nodejs|node)\-?([0-9]*)*$/g)) {
manager = 'npm'
break

// Yarn and pnpm have the same stem name as their bin.
// Yarn, pnpm, and bun have the same stem name as their bin.
// We assume all unknown package managers do as well.
default:
manager = managerStem
Expand All @@ -48,10 +48,10 @@ else if (binStem.match(/(nodejs|node)\-?([0-9]*)*$/g)) {
}
} else {
// We don't know what started it, assume it's already stripped.
arguments.unshift(bin)
args.unshift(bin)
}

cli.run(arguments, binName).catch((err) => {
cli.run(args, binName).catch((err) => {
cli.logError(err.message)
process.exit(1)
})
8 changes: 7 additions & 1 deletion 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"];
const PKG_MANAGERS: &[&str] = &["cargo", "pnpm", "npm", "yarn", "bun"];

#[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 == "bun" {
Command::new(manager)
.bin_name(manager)
.subcommand(Command::new("run").subcommand(tauri))
} else {
Command::new(manager).bin_name(manager).subcommand(tauri)
};
Expand All @@ -47,6 +51,8 @@ fn get_completions(shell: Shell, cmd: Command) -> Result<String> {
"complete -F _cargo -o bashdefault -o default {} tauri\n",
if manager == &"npm" {
"npm run"
} else if manager == &"bun" {
"bun run"
} else {
manager
}
Expand Down
18 changes: 17 additions & 1 deletion tooling/cli/src/helpers/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum PackageManager {
Pnpm,
Yarn,
YarnBerry,
Bun
}

impl Display for PackageManager {
Expand All @@ -23,6 +24,7 @@ impl Display for PackageManager {
PackageManager::Pnpm => "pnpm",
PackageManager::Yarn => "yarn",
PackageManager::YarnBerry => "yarn berry",
PackageManager::Bun => "bun",
}
)
}
Expand All @@ -33,6 +35,7 @@ impl PackageManager {
let mut use_npm = false;
let mut use_pnpm = false;
let mut use_yarn = false;
let mut use_bun = false;

if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
Expand All @@ -44,11 +47,13 @@ impl PackageManager {
use_pnpm = true;
} else if name.as_ref() == "yarn.lock" {
use_yarn = true;
} else if name.as_ref() == "bun.lockb" {
use_bun = true;
}
}
}

if !use_npm && !use_pnpm && !use_yarn {
if !use_npm && !use_pnpm && !use_yarn && !use_bun {
return Vec::new();
}

Expand All @@ -63,6 +68,9 @@ impl PackageManager {
if use_yarn {
found.push(PackageManager::Yarn);
}
if use_bun {
found.push(PackageManager::Bun);
}

found
}
Expand Down Expand Up @@ -101,6 +109,14 @@ impl PackageManager {
.status()
.map_err(Into::into)
}
PackageManager::Bun => {
let mut cmd = cross_command("bun");
cmd
.arg("install")
.args(dependencies)
.status()
.map_err(Into::into)
}
}
}
}
22 changes: 22 additions & 0 deletions tooling/cli/src/info/env_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ pub fn items(metadata: &VersionMetadata) -> (Vec<SectionItem>, Option<String>) {
|| None,
false,
),
SectionItem::new(
|| {
cross_command("bun")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some((
format!("bun: {}", v.split('\n').next().unwrap()),
Status::Neutral,
))
} else {
None
}
})
.ok()
.unwrap_or_default()
},
|| None,
false,
),
SectionItem::new(
move || {
yarn_version_c
Expand Down
22 changes: 22 additions & 0 deletions tooling/cli/src/info/packages_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ 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 @@ -117,6 +129,16 @@ 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 Down
1 change: 1 addition & 0 deletions tooling/cli/src/plugin/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub fn command(options: Options) -> Result<()> {
PackageManager::Pnpm => cross_command("pnpm"),
PackageManager::Yarn => cross_command("yarn"),
PackageManager::YarnBerry => cross_command("yarn"),
PackageManager::Bun => cross_command("bun"),
};

cmd.arg("add").arg(&npm_spec);
Expand Down

0 comments on commit e733cde

Please sign in to comment.