Skip to content

Commit

Permalink
feat: allow installing peerDeps when using addDependency (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jun 28, 2024
1 parent 0b6ac4f commit 686c31f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"consola": "^3.2.3",
"execa": "^8.0.1",
"pathe": "^1.1.2",
"pkg-types": "^1.1.1",
"ufo": "^1.4.0"
},
"devDependencies": {
Expand Down
43 changes: 27 additions & 16 deletions pnpm-lock.yaml

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

35 changes: 35 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readPackageJSON } from "pkg-types";
import {
executeCommand,
resolveOperationOptions,
Expand Down Expand Up @@ -71,6 +72,40 @@ export async function addDependency(
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});

if (options.installPeerDependencies) {
const existingPkg = await readPackageJSON(resolvedOptions.cwd);
const peerDeps: string[] = [];
const peerDevDeps: string[] = [];
for (const _name of names) {
const pkgName = _name.match(/^(.[^@]+)/)?.[0];
const pkg = await readPackageJSON(pkgName, {
url: resolvedOptions.cwd,
}).catch(() => ({}) as Record<string, undefined>);
if (!pkg.peerDependencies || name !== pkgName) {
continue;
}
for (const [peerDependency, version] of Object.entries(
pkg.peerDependencies,
)) {
if (pkg.peerDependenciesMeta?.[peerDependency]?.optional) {
continue;
}
// TODO: refactor to getSpecifiedPackageInfo later on
if (
existingPkg.dependencies?.[peerDependency] ||
existingPkg.devDependencies?.[peerDependency]
) {
continue;
}
// TODO: Make sure peerDependency is not already installed in user project
const isDev = pkg.peerDependenciesMeta?.[peerDependency]?.dev;
(isDev ? peerDevDeps : peerDeps).push(`${peerDependency}@${version}`);
}
}
await addDependency(peerDeps, { ...resolvedOptions });
await addDevDependency(peerDevDeps, { ...resolvedOptions });
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type OperationOptions = {
cwd?: string;
silent?: boolean;
packageManager?: PackageManager | PackageManagerName;
installPeerDependencies?: boolean;
dev?: boolean;
workspace?: boolean | string;
global?: boolean;
Expand Down
Binary file modified test/fixtures/bun-workspace/bun.lockb
Binary file not shown.

0 comments on commit 686c31f

Please sign in to comment.