Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Support bun partially #6

Merged
merged 1 commit into from
Nov 20, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ For pnpm:
```bash
pnpm dlx vp-update
```

For bun:

```bash
bunx vp-update
```
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const cli = cac("vp-update");

cli
.command("[dir]", "Update VuePress project")
.usage("pnpm dlx vp-update [dir] / npx vp-update [dir]")
.usage(
"pnpm dlx vp-update [dir] / npx vp-update [dir] / bunx vp-update [dir]"
)
.example("docs")
.action(async (targetDir = "") => {
console.log("Upgrading current project...");
Expand Down Expand Up @@ -59,6 +61,8 @@ cli
? `yarn upgrade`
: packageManager === "yarn"
? `yarn up`
: packageManager === "bun"
? `bun update`
: `npm update`;

execaCommandSync(updateCommand, { stdout: "inherit" });
Expand All @@ -68,7 +72,8 @@ cli

cli.help(() => [
{
title: "pnpm dlx vp-update [dir] / npx vp-update [dir]",
title:
"pnpm dlx vp-update [dir] / npx vp-update [dir] / bunx vp-update [dir]",
body: "Update VuePress project in [dir]",
},
]);
Expand Down
17 changes: 16 additions & 1 deletion src/utils/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { dirname, resolve } from "node:path";

import { execaCommandSync } from "execa";

export type PackageManager = "npm" | "yarn" | "yarn1" | "pnpm";
export type PackageManager = "npm" | "yarn" | "yarn1" | "pnpm" | "bun";

const globalCache = new Map<string, boolean>();
const localCache = new Map<string, PackageManager>();

const NPM_LOCK = "package-lock.json";
const YARN_LOCK = "yarn.lock";
const BUN_LOCK = "bun.lockb";
const PNPM_LOCK = "pnpm-lock.yaml";

const isInstalled = (packageManager: PackageManager): boolean => {
Expand Down Expand Up @@ -74,6 +75,12 @@ export const getTypeofLockFile = (
return packageManager;
}

if (existsSync(resolve(cwd, BUN_LOCK))) {
localCache.set(key, "bun");

return "bun";
}

if (existsSync(resolve(cwd, NPM_LOCK))) {
localCache.set(key, "npm");

Expand Down Expand Up @@ -104,6 +111,12 @@ export const getTypeofLockFile = (
return packageManager;
}

if (existsSync(resolve(dir, BUN_LOCK))) {
localCache.set(key, "bun");

return "bun";
}

if (existsSync(resolve(dir, NPM_LOCK))) {
localCache.set(key, "npm");

Expand All @@ -127,6 +140,8 @@ export const detectPackageManager = (
? "pnpm"
: hasGlobalInstallation("yarn")
? "yarn"
: hasGlobalInstallation("bun")
? "bun"
: "npm")
);
};
15 changes: 15 additions & 0 deletions src/utils/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ export const getRegistry = (packageManager: PackageManager): string => {
`${packageManager} config get npmRegistryServer`
).stdout.replace(/\/?$/, "/");

if (
packageManager === "bun" &&
!execaCommandSync(`${packageManager} --version`).exitCode
) {
console.warn(
"bun does not support get registry at the time, using npm global registry instead"
);
return execaCommandSync(
// TODO: wait for bun to support get registry config
`npm config get registry`
).stdout.replace(/\/?$/, "/");
}

return execaCommandSync(
`${packageManager} config get registry`
).stdout.replace(/\/?$/, "/");
Expand All @@ -30,6 +43,8 @@ export const checkTaobaoRegistry = (packageManager: PackageManager): void => {
execaCommandSync(
`${packageManager} config set npmRegistryServer ${NPM_MIRROR_REGISTRY}`
);
} else if (packageManager === "bun") {
execaCommandSync(`npm config set registry ${NPM_MIRROR_REGISTRY}`);
} else {
execaCommandSync(
`${packageManager} config set registry ${NPM_MIRROR_REGISTRY}`
Expand Down