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 support for bun #87

Merged
merged 8 commits into from
Aug 24, 2023
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
2 changes: 2 additions & 0 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: corepack enable
- uses: oven-sh/setup-bun@v1
- uses: actions/setup-node@v3
with:
node-version: 18
cache: "pnpm"
- run: pnpm install
- name: Fix lint issues
run: pnpm run lint:fix && pnpm vitest run --update
- run: git checkout HEAD test/fixtures/bun
- uses: autofix-ci/action@8caa572fd27b0019a65e4c695447089c8d3138b9
with:
commit-message: "chore: apply automated lint fixes"
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: corepack enable
- uses: oven-sh/setup-bun@v1
if: ${{ matrix.os != 'windows-latest' }}
- uses: actions/setup-node@v3
with:
node-version: 18
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@

## What does **nypm** do?

βœ… Supports **npm, yarn and pnpm** out of the box with a unified API
βœ… Supports **npm, yarn, pnpm and bun** out of the box with a unified API

βœ… Provides an **API interface** to interact with package managers

βœ… **Autodetects** project's package manager using package.json and known lockfiles

βœ… **Auto-installs and use exactly expected version** of package manager using [nodejs/corepack](https://github.com/nodejs/corepack)
βœ… **Auto-installs and use exactly expected version** of supported package managers using [nodejs/corepack](https://github.com/nodejs/corepack)

βœ… **Minimal** implementation

nypm, detects package manager type and version and converts command into package manager CLI arguments. It then uses corepack to execute package manager's command (and download it if necessary).

```
+------------------------------------+
| nypm |
+------------------------------------+
+------------------------------------+
| Corepack |
+------------------------------------+
+---------+ +---------+ +---------+
| npm | | yarn | | pnpm |
+---------+ +---------+ +---------+
+------------------------------------+
| Node.js project |
+------------------------------------+
+------------------------------------------------+
| nypm |
+------------------------------------------------+
+-----------------------------------+
| Corepack |
+-----------------------------------+
+---------+ +---------+ +---------+ +---------+
| npm | | yarn | | pnpm | | bun |
+---------+ +---------+ +---------+ +---------+
+------------------------------------------------+
| Node.js project |
+------------------------------------------------+
```

## CLI Usage
Expand All @@ -55,6 +55,9 @@ pnpm install nypm

# yarn
yarn add nypm

# bun
bun install nypm
```

Import:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"jiti": "^1.19.1",
"pathe": "^1.1.1",
"prettier": "^3.0.2",
"std-env": "^3.4.3",
"typescript": "^5.1.6",
"unbuild": "^1.2.1",
"vitest": "^0.34.1"
Expand Down
13 changes: 8 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export async function executeCommand(
// work around issue with segmentation fault when using corepack with npm
// on ubuntu-latest
const execaArgs: [string, string[]] =
command === "npm" ? [command, args] : ["corepack", [command, ...args]];
command === "npm" || command === "bun"
? [command, args]
: ["corepack", [command, ...args]];

await execa(execaArgs[0], execaArgs[1], {
cwd: resolve(options.cwd || process.cwd()),
Expand Down Expand Up @@ -102,6 +104,10 @@ export function getWorkspaceArgs(
return ["-F", options.workspace];
}

case "bun": {
return [];
}

case "yarn": {
return ["workspace", options.workspace];
}
Expand Down
5 changes: 5 additions & 0 deletions src/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const _packageManagers: PackageManager[] = [
lockFile: "pnpm-lock.yaml",
files: ["pnpm-workspace.yaml"],
},
{
name: "bun",
command: "bun",
lockFile: "bun.lockb",
},
{
name: "yarn",
command: "yarn",
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type PackageManagerName = "npm" | "yarn" | "pnpm";
export type PackageManagerName = "npm" | "yarn" | "pnpm" | "bun";

export type PackageManager = {
name: PackageManagerName;
Expand Down
Binary file added test/fixtures/bun/bun.lockb
Binary file not shown.
8 changes: 8 additions & 0 deletions test/fixtures/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "fixture-bun",
"version": "0.0.0",
"private": true,
"workspaces": [
"packages/*"
]
}
8 changes: 8 additions & 0 deletions test/fixtures/bun/packages/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "fixture-bun/workspace-a",
"private": true,
"version": "0.0.0",
"devDependencies": {
"ufo": "^1.1.1"
}
}
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fileURLToPath } from "node:url";
import { expect, it, describe, vi } from "vitest";
import { isWindows } from "std-env";
import {
installDependencies,
addDependency,
Expand Down Expand Up @@ -32,6 +33,10 @@ const fixtures = [
name: "pnpm",
packageManager: "pnpm",
},
{
name: "bun",
packageManager: "bun",
},
{
name: "yarn-classic",
packageManager: "yarn",
Expand Down Expand Up @@ -123,6 +128,11 @@ describe("detectPackageManager", () => {

describe("api", () => {
for (const fixture of fixtures) {
// bun is not yet supported on Windows
if (isWindows && fixture.name === "bun") {
continue;
}

describe(fixture.name, () => {
const fixtureDirectory = resolveFixtureDirectory(fixture.name);

Expand Down