Skip to content
Closed
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
16 changes: 13 additions & 3 deletions src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,25 @@ pub fn count_tokens(text: &str) -> usize {
}

/// Detect the package manager used in the current directory.
/// Returns "pnpm", "yarn", or "npm" based on lockfile presence.
/// Returns "pnpm", "yarn", "bun", or "npm" based on lockfile presence.
///
/// # Examples
/// ```no_run
/// use rtk::utils::detect_package_manager;
/// let pm = detect_package_manager();
/// // Returns "pnpm" if pnpm-lock.yaml exists, "yarn" if yarn.lock, else "npm"
/// // Returns "pnpm" if pnpm-lock.yaml exists, "yarn" if yarn.lock,
/// // "bun" if bun.lockb exists, else "npm"
/// ```
#[allow(dead_code)]
pub fn detect_package_manager() -> &'static str {
if std::path::Path::new("pnpm-lock.yaml").exists() {
"pnpm"
} else if std::path::Path::new("yarn.lock").exists() {
"yarn"
} else if std::path::Path::new("bun.lockb").exists()
|| std::path::Path::new("bun.lock").exists()
{
"bun"
} else {
"npm"
}
Expand All @@ -297,6 +302,11 @@ pub fn package_manager_exec(tool: &str) -> Command {
c.arg("exec").arg("--").arg(tool);
c
}
"bun" => {
let mut c = resolved_command("bunx");
c.arg(tool);
c
}
_ => {
let mut c = resolved_command("npx");
c.arg("--no-install").arg("--").arg(tool);
Expand Down Expand Up @@ -497,7 +507,7 @@ mod tests {
// In the test environment (rtk repo), there's no JS lockfile
// so it should default to "npm"
let pm = detect_package_manager();
assert!(["pnpm", "yarn", "npm"].contains(&pm));
assert!(["pnpm", "yarn", "bun", "npm"].contains(&pm));
}

#[test]
Expand Down
32 changes: 32 additions & 0 deletions src/discover/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,14 @@ mod tests {
);
}

#[test]
fn test_rewrite_bunx_tsc() {
assert_eq!(
rewrite_command("bunx tsc --noEmit", &[]),
Some("rtk tsc --noEmit".into())
);
}

#[test]
fn test_rewrite_cat_file() {
assert_eq!(
Expand Down Expand Up @@ -1895,6 +1903,30 @@ mod tests {
);
}

#[test]
fn test_rewrite_bun_test() {
assert_eq!(
rewrite_command("bun test", &[]),
Some("rtk vitest".into())
);
}

#[test]
fn test_rewrite_bun_test_with_path() {
assert_eq!(
rewrite_command("bun test src/", &[]),
Some("rtk vitest src/".into())
);
}

#[test]
fn test_rewrite_bunx_vitest() {
assert_eq!(
rewrite_command("bunx vitest run", &[]),
Some("rtk vitest run".into())
);
}

#[test]
fn test_classify_prisma() {
assert!(matches!(
Expand Down
8 changes: 4 additions & 4 deletions src/discover/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ pub const RULES: &[RtkRule] = &[
subcmd_status: &[],
},
RtkRule {
pattern: r"^(npx\s+|pnpm\s+)?tsc(\s|$)",
pattern: r"^(npx\s+|pnpm\s+|bunx\s+)?tsc(\s|$)",
rtk_cmd: "rtk tsc",
rewrite_prefixes: &["pnpm tsc", "npx tsc", "tsc"],
rewrite_prefixes: &["pnpm tsc", "npx tsc", "bunx tsc", "tsc"],
category: "Build",
savings_pct: 83.0,
subcmd_savings: &[],
Expand Down Expand Up @@ -150,9 +150,9 @@ pub const RULES: &[RtkRule] = &[
subcmd_status: &[],
},
RtkRule {
pattern: r"^(pnpm\s+|npx\s+)?(vitest|jest|test)(\s|$)",
pattern: r"^(pnpm\s+|npx\s+|bunx\s+)?(vitest|jest|test)(\s|$)|^bun\s+test(\s|$)",
rtk_cmd: "rtk vitest",
rewrite_prefixes: &["pnpm vitest", "npx vitest", "vitest", "jest"],
rewrite_prefixes: &["pnpm vitest", "npx vitest", "bunx vitest", "vitest", "jest", "bun test"],
category: "Tests",
savings_pct: 99.0,
subcmd_savings: &[],
Expand Down