Skip to content

Commit

Permalink
fix: handle package managers with a bin array correctly (#20)
Browse files Browse the repository at this point in the history
* test: fix tests on older versions of node

* test: fix tests on Windows

* fix: handle package managers with a bin array correctly
  • Loading branch information
merceyz authored Sep 30, 2020
1 parent 909c2e7 commit 1836d17
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 9 deletions.
35 changes: 35 additions & 0 deletions .pnp.js

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

Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
[`@babel/plugin-proposal-decorators`, {legacy: true}],
[`@babel/plugin-proposal-class-properties`, {loose: true}],
[`@babel/plugin-transform-modules-commonjs`],
[`@babel/plugin-proposal-nullish-coalescing-operator`],
],
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@babel/core": "^7.11.0",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-proposal-decorators": "^7.10.5",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
"@babel/plugin-transform-modules-commonjs": "^7.8.3",
"@babel/preset-typescript": "^7.10.4",
"@types/debug": "^4.1.5",
Expand Down
10 changes: 8 additions & 2 deletions sources/pmmUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ export async function installVersion(installTarget: string, locator: Locator, {s
export async function runVersion(installSpec: { location: string, spec: PackageManagerSpec }, locator: Locator, binName: string, args: Array<string>, context: Context) {
let binPath: string | null = null;
if (Array.isArray(installSpec.spec.bin)) {
binPath = path.join(installSpec.location, `${binName}.js`);
if (installSpec.spec.bin.some(bin => bin === binName)) {
const parsedUrl = new URL(installSpec.spec.url);
const ext = path.posix.extname(parsedUrl.pathname);
if (ext === `.js`) {
binPath = path.join(installSpec.location, path.posix.basename(parsedUrl.pathname));
}
}
} else {
for (const [name, dest] of Object.entries(installSpec.spec.bin)) {
if (name === binName) {
Expand All @@ -141,7 +147,7 @@ export async function runVersion(installSpec: { location: string, spec: PackageM
}

if (!binPath)
throw new Error(`Assertion failed: Unable to locate bin path`);
throw new Error(`Assertion failed: Unable to locate path for bin '${binName}'`);

return new Promise<number>((resolve, reject) => {
process.on(`SIGINT`, () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/Disable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {runCli} from './_runCli';
const engine = new Engine();

beforeEach(async () => {
process.env.COREPACK_HOME = await xfs.mktempPromise();
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
});

async function makeBin(cwd: PortablePath, name: Filename) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Enable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {runCli} from './_runCli';
const engine = new Engine();

beforeEach(async () => {
process.env.COREPACK_HOME = await xfs.mktempPromise();
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
});

async function makeBin(cwd: PortablePath, name: Filename) {
Expand Down
28 changes: 23 additions & 5 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Filename, ppath, xfs} from '@yarnpkg/fslib';
import {Filename, ppath, xfs, npath} from '@yarnpkg/fslib';

import config from '../config.json';
import config from '../config.json';

import {runCli} from './_runCli';
import {runCli} from './_runCli';

beforeEach(async () => {
process.env.COREPACK_HOME = await xfs.mktempPromise();
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
});

for (const [name, version] of [[`yarn`, `1.22.4`], [`yarn`, `2.0.0-rc.30`], [`pnpm`, `4.11.6`], [`npm`, `6.14.2`]]) {
Expand Down Expand Up @@ -169,7 +169,7 @@ it(`should support hydrating package managers from cached archives`, async () =>
});

// Use a new cache
process.env.COREPACK_HOME = await xfs.mktempPromise();
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());

// Disable the network to make sure we don't succeed by accident
process.env.COREPACK_ENABLE_NETWORK = `0`;
Expand All @@ -193,3 +193,21 @@ it(`should support hydrating package managers from cached archives`, async () =>
}
});
});

it(`should support running package managers with bin array`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
packageManager: `yarn@2.2.2`,
});

await expect(runCli(cwd, [`yarn`, `yarnpkg`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
exitCode: 0,
});

await expect(runCli(cwd, [`yarn`, `yarn`, `--version`])).resolves.toMatchObject({
stdout: `2.2.2\n`,
exitCode: 0,
});
});
});
24 changes: 24 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-proposal-nullish-coalescing-operator@npm:^7.10.4":
version: 7.10.4
resolution: "@babel/plugin-proposal-nullish-coalescing-operator@npm:7.10.4"
dependencies:
"@babel/helper-plugin-utils": ^7.10.4
"@babel/plugin-syntax-nullish-coalescing-operator": ^7.8.0
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: 5a20d8bcbf2926dde3e9edcf847eaa5485d0d0fea76d0683ef1cafb11e0c35e46620391916283e1a9c0f76351e8c5ecccebf0d3a6bdf24559c5ad381433a0e3a
languageName: node
linkType: hard

"@babel/plugin-syntax-bigint@npm:^7.0.0":
version: 7.8.3
resolution: "@babel/plugin-syntax-bigint@npm:7.8.3"
Expand All @@ -425,6 +437,17 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-syntax-nullish-coalescing-operator@npm:^7.8.0":
version: 7.8.3
resolution: "@babel/plugin-syntax-nullish-coalescing-operator@npm:7.8.3"
dependencies:
"@babel/helper-plugin-utils": ^7.8.0
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: 4ba03753759a2d9783b792c060147a20f474f76c42edf77cbf89c6669f9f22ffb3cbba4facdd8ce651129db6089a81feca1f7e42da75244eabedecba37bd20be
languageName: node
linkType: hard

"@babel/plugin-syntax-object-rest-spread@npm:^7.0.0":
version: 7.8.3
resolution: "@babel/plugin-syntax-object-rest-spread@npm:7.8.3"
Expand Down Expand Up @@ -2212,6 +2235,7 @@ __metadata:
"@babel/core": ^7.11.0
"@babel/plugin-proposal-class-properties": ^7.10.4
"@babel/plugin-proposal-decorators": ^7.10.5
"@babel/plugin-proposal-nullish-coalescing-operator": ^7.10.4
"@babel/plugin-transform-modules-commonjs": ^7.8.3
"@babel/preset-typescript": ^7.10.4
"@types/debug": ^4.1.5
Expand Down

0 comments on commit 1836d17

Please sign in to comment.