Skip to content

Commit

Permalink
feat(@angular/cli): add support to bun package manager
Browse files Browse the repository at this point in the history
This commit adds support to use `bun` as a package manager.

Closes #26837
  • Loading branch information
alan-agius4 committed Jan 25, 2024
1 parent bac79d4 commit b3e2067
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/angular/cli/lib/config/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"packageManager": {
"description": "Specify which package manager tool to use.",
"type": "string",
"enum": ["npm", "cnpm", "yarn", "pnpm"]
"enum": ["npm", "cnpm", "yarn", "pnpm", "bun"]
},
"warnings": {
"description": "Control CLI specific console warnings",
Expand Down Expand Up @@ -101,7 +101,7 @@
"packageManager": {
"description": "Specify which package manager tool to use.",
"type": "string",
"enum": ["npm", "cnpm", "yarn", "pnpm"]
"enum": ["npm", "cnpm", "yarn", "pnpm", "bun"]
},
"warnings": {
"description": "Control CLI specific console warnings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"description": "The preferred package manager configuration files to use for registry settings.",
"type": "string",
"default": "npm",
"enum": ["npm", "yarn", "cnpm", "pnpm"]
"enum": ["npm", "yarn", "cnpm", "pnpm", "bun"]
}
},
"required": []
Expand Down
24 changes: 21 additions & 3 deletions packages/angular/cli/src/utilities/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ export class PackageManagerUtils {
prefix: '--prefix',
noLockfile: '--no-lockfile',
};
case PackageManager.Bun:
return {
saveDev: '--development',
install: 'add',
installAll: 'install',
prefix: '--cwd',
noLockfile: '',
};
default:
return {
saveDev: '--save-dev',
Expand Down Expand Up @@ -218,14 +226,15 @@ export class PackageManagerUtils {
const hasNpmLock = this.hasLockfile(PackageManager.Npm);
const hasYarnLock = this.hasLockfile(PackageManager.Yarn);
const hasPnpmLock = this.hasLockfile(PackageManager.Pnpm);
const hasBunLock = this.hasLockfile(PackageManager.Bun);

// PERF NOTE: `this.getVersion` spawns the package a the child_process which can take around ~300ms at times.
// Therefore, we should only call this method when needed. IE: don't call `this.getVersion(PackageManager.Pnpm)` unless truly needed.
// The result of this method is not stored in a variable because it's memoized.

if (hasNpmLock) {
// Has NPM lock file.
if (!hasYarnLock && !hasPnpmLock && this.getVersion(PackageManager.Npm)) {
if (!hasYarnLock && !hasPnpmLock && !hasBunLock && this.getVersion(PackageManager.Npm)) {
// Only NPM lock file and NPM binary is available.
return PackageManager.Npm;
}
Expand All @@ -237,18 +246,24 @@ export class PackageManagerUtils {
} else if (hasPnpmLock && this.getVersion(PackageManager.Pnpm)) {
// PNPM lock file and PNPM binary is available.
return PackageManager.Pnpm;
} else if (hasBunLock && this.getVersion(PackageManager.Bun)) {
// Bun lock file and Bun binary is available.
return PackageManager.Bun;
}
}

if (!this.getVersion(PackageManager.Npm)) {
// Doesn't have NPM installed.
const hasYarn = !!this.getVersion(PackageManager.Yarn);
const hasPnpm = !!this.getVersion(PackageManager.Pnpm);
const hasBun = !!this.getVersion(PackageManager.Bun);

if (hasYarn && !hasPnpm) {
if (hasYarn && !hasPnpm && !hasBun) {
return PackageManager.Yarn;
} else if (!hasYarn && hasPnpm) {
} else if (hasPnpm && !hasYarn && !hasBun) {
return PackageManager.Pnpm;
} else if (hasBun && !hasYarn && !hasPnpm) {
return PackageManager.Bun;
}
}

Expand All @@ -266,6 +281,9 @@ export class PackageManagerUtils {
case PackageManager.Pnpm:
lockfileName = 'pnpm-lock.yaml';
break;
case PackageManager.Bun:
lockfileName = 'bun.lockb';
break;
case PackageManager.Npm:
default:
lockfileName = 'package-lock.json';
Expand Down

0 comments on commit b3e2067

Please sign in to comment.