Skip to content
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
10 changes: 5 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
resolveOperationOptions,
getWorkspaceArgs,
doesDependencyExist,
readPackageJSON,
} from "./_utils.ts";
import { dlxCommand } from "./cmd.ts";
import { readPackageJSON } from "./_utils.ts";
import type {
OperationOptions,
OperationResult,
Expand All @@ -15,7 +15,6 @@ import type {
import * as fs from "node:fs";
import { resolve } from "pathe";
import { join } from "node:path";
import type { PackageJson } from "pkg-types";

/**
* Installs project dependencies.
Expand Down Expand Up @@ -140,11 +139,12 @@ export async function addDependency(
const existingPkg = await readPackageJSON(resolvedOptions.cwd);
const peerDeps: string[] = [];
const peerDevDeps: string[] = [];
const _require = createRequire(join(resolvedOptions.cwd, "/_.js"));
for (const _name of names) {
const pkgName = _name.match(/^(.[^@]+)/)?.[0];
const _require = createRequire(join(resolvedOptions.cwd, "/_.js"));
const pkg = _require(`${pkgName}/package.json`) as PackageJson;
if (!pkg.peerDependencies || pkg.name !== pkgName) {
const pkgEntryPath = _require.resolve(pkgName!);
const pkg = await readPackageJSON(pkgEntryPath);
if (!pkg?.peerDependencies || pkg?.name !== pkgName) {
continue;
}
for (const [peerDependency, version] of Object.entries(
Expand Down
18 changes: 18 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ describe("api", () => {
expect(addDependencySpy).toHaveReturned();
}, 60_000);

it.skipIf(fixture.name.includes("deno"))(
"adds dependency with installPeerDependencies option",
async () => {
const addDependencySpy = vi.fn(addDependency);
// consola has restrictive exports and does not export ./package.json
const executeAddDependencySpy = () =>
addDependencySpy("consola", {
cwd: fixture.dir,
silent: !process.env.DEBUG,
installPeerDependencies: true,
});

await executeAddDependencySpy();
expect(addDependencySpy).toHaveReturned();
},
60_000,
);

it("ensures dependency is installed", async () => {
const ensureDependencyInstalledSpy = vi.fn(ensureDependencyInstalled);

Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/npm/package-lock.json

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

2 changes: 1 addition & 1 deletion test/fixtures/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"packageManager": "npm@9.7.2",
"dependencies": {
"consola": "3.2.2"
"consola": "^3.4.2"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/yarn-berry-v4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"test-script-env": "node -e \"fs.writeFileSync('test-file-env.txt', process.env.TEST_CONTENT, 'utf8')\""
},
"dependencies": {
"consola": "3.2.2"
"consola": "^3.4.2"
}
}
10 changes: 5 additions & 5 deletions test/fixtures/yarn-berry-v4/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ __metadata:
version: 6
cacheKey: 8

"consola@npm:3.2.2":
version: 3.2.2
resolution: "consola@npm:3.2.2"
checksum: 3c22102f74b4dc1519ab2a3096b1975a8b767f258027cef9888ca9d999ed79388e390b969474bce0b42aade73cd2689f62ca511e530a06bf842092f8ef1bf7a6
"consola@npm:^3.4.2":
version: 3.4.2
resolution: "consola@npm:3.4.2"
checksum: 32d1339e0505842f033ca34cb4572a841281caa367f438b785d3b284ab2a06134f009e605908480402c5f57f56c1e3210090c37e6417923416f76ce730d39361
languageName: node
linkType: hard

"fixture-yarn-berry-v4@workspace:.":
version: 0.0.0-use.local
resolution: "fixture-yarn-berry-v4@workspace:."
dependencies:
consola: 3.2.2
consola: ^3.4.2
languageName: unknown
linkType: soft
2 changes: 1 addition & 1 deletion test/fixtures/yarn-berry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"packageManager": "yarn@3.6.1",
"dependencies": {
"consola": "3.2.2"
"consola": "^3.4.2"
}
}
10 changes: 5 additions & 5 deletions test/fixtures/yarn-berry/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ __metadata:
version: 6
cacheKey: 8

"consola@npm:3.2.2":
version: 3.2.2
resolution: "consola@npm:3.2.2"
checksum: 3c22102f74b4dc1519ab2a3096b1975a8b767f258027cef9888ca9d999ed79388e390b969474bce0b42aade73cd2689f62ca511e530a06bf842092f8ef1bf7a6
"consola@npm:^3.4.2":
version: 3.4.2
resolution: "consola@npm:3.4.2"
checksum: 32d1339e0505842f033ca34cb4572a841281caa367f438b785d3b284ab2a06134f009e605908480402c5f57f56c1e3210090c37e6417923416f76ce730d39361
languageName: node
linkType: hard

"fixture-yarn-berry@workspace:.":
version: 0.0.0-use.local
resolution: "fixture-yarn-berry@workspace:."
dependencies:
consola: 3.2.2
consola: ^3.4.2
languageName: unknown
linkType: soft
2 changes: 1 addition & 1 deletion test/fixtures/yarn-classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"packageManager": "yarn@1.22.19",
"dependencies": {
"consola": "3.2.2"
"consola": "^3.4.2"
}
}
8 changes: 4 additions & 4 deletions test/fixtures/yarn-classic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# yarn lockfile v1


consola@3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/consola/-/consola-3.2.2.tgz#be9b295e1e556355a09ac475b4394016144e0c32"
integrity sha512-r921u0vbF4lQsoIqYvSSER+yZLPQGijOHrYcWoCNVNBZmn/bRR+xT/DgerTze/nLD9TTGzdDa378TVhx7RDOYg==
consola@^3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/consola/-/consola-3.4.2.tgz#5af110145397bb67afdab77013fdc34cae590ea7"
integrity sha512-5IKcdX0nnYavi6G7TtOhwkYzyjfJlatbjMjuLSfE2kYT5pMDOilZ4OvMhi637CcDICTmz3wARPoyhqyX1Y+XvA==