Skip to content

Commit 94a3f76

Browse files
committed
feat: add Bun
1 parent ecb48ae commit 94a3f76

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/bun.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { execa } from "execa";
2+
3+
/** `Bun` is a wrapper for the Bun package manager. */
4+
export class Bun {
5+
#bunCmd: string;
6+
7+
constructor(bunCmd = "bun") {
8+
this.#bunCmd = bunCmd.trim();
9+
}
10+
11+
/**
12+
`add` installs the given package in the given directory and
13+
returns a list of all installed packages (e.g., `["foo@1.0.0"]`).
14+
15+
@see {@link https://bun.sh/docs/cli/add}
16+
*/
17+
async add(pkg: string, cwd: string): Promise<string[]> {
18+
// Run `bun add <pkg> --verbose` in the `cwd`.
19+
const { stdout } = await execa(this.#bunCmd, ["add", pkg, "--verbose"], { cwd });
20+
21+
// With verbose output on, bun prints one line per installed package
22+
// (e.g., "foo@1.0.0"), including all installed dependencies.
23+
// These lines are between the two delimiting lines found here:
24+
// https://github.com/oven-sh/bun/blob/972a7b7080bd3066b54dcb43e9c91c5dfa26a69c/src/install/lockfile.zig#L5369-L5370.
25+
const lines = stdout.split("\n");
26+
const beginHash = lines.findIndex((line) => line.startsWith("-- BEGIN SHA512/256"));
27+
const endHash = lines.findIndex((line) => line.startsWith("-- END HASH"));
28+
const installedPackages = lines.slice(beginHash + 1, endHash);
29+
return installedPackages;
30+
}
31+
}

0 commit comments

Comments
 (0)