Skip to content

Commit 1836d17

Browse files
authored
fix: handle package managers with a bin array correctly (#20)
* test: fix tests on older versions of node * test: fix tests on Windows * fix: handle package managers with a bin array correctly
1 parent 909c2e7 commit 1836d17

10 files changed

+94
-9
lines changed

.pnp.js

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

babel.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
[`@babel/plugin-proposal-decorators`, {legacy: true}],
77
[`@babel/plugin-proposal-class-properties`, {loose: true}],
88
[`@babel/plugin-transform-modules-commonjs`],
9+
[`@babel/plugin-proposal-nullish-coalescing-operator`],
910
],
1011
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/core": "^7.11.0",
1414
"@babel/plugin-proposal-class-properties": "^7.10.4",
1515
"@babel/plugin-proposal-decorators": "^7.10.5",
16+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
1617
"@babel/plugin-transform-modules-commonjs": "^7.8.3",
1718
"@babel/preset-typescript": "^7.10.4",
1819
"@types/debug": "^4.1.5",

sources/pmmUtils.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ export async function installVersion(installTarget: string, locator: Locator, {s
130130
export async function runVersion(installSpec: { location: string, spec: PackageManagerSpec }, locator: Locator, binName: string, args: Array<string>, context: Context) {
131131
let binPath: string | null = null;
132132
if (Array.isArray(installSpec.spec.bin)) {
133-
binPath = path.join(installSpec.location, `${binName}.js`);
133+
if (installSpec.spec.bin.some(bin => bin === binName)) {
134+
const parsedUrl = new URL(installSpec.spec.url);
135+
const ext = path.posix.extname(parsedUrl.pathname);
136+
if (ext === `.js`) {
137+
binPath = path.join(installSpec.location, path.posix.basename(parsedUrl.pathname));
138+
}
139+
}
134140
} else {
135141
for (const [name, dest] of Object.entries(installSpec.spec.bin)) {
136142
if (name === binName) {
@@ -141,7 +147,7 @@ export async function runVersion(installSpec: { location: string, spec: PackageM
141147
}
142148

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

146152
return new Promise<number>((resolve, reject) => {
147153
process.on(`SIGINT`, () => {

tests/Disable.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {runCli} from './_runCli';
99
const engine = new Engine();
1010

1111
beforeEach(async () => {
12-
process.env.COREPACK_HOME = await xfs.mktempPromise();
12+
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
1313
});
1414

1515
async function makeBin(cwd: PortablePath, name: Filename) {

tests/Enable.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {runCli} from './_runCli';
99
const engine = new Engine();
1010

1111
beforeEach(async () => {
12-
process.env.COREPACK_HOME = await xfs.mktempPromise();
12+
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
1313
});
1414

1515
async function makeBin(cwd: PortablePath, name: Filename) {

tests/main.test.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {Filename, ppath, xfs} from '@yarnpkg/fslib';
1+
import {Filename, ppath, xfs, npath} from '@yarnpkg/fslib';
22

3-
import config from '../config.json';
3+
import config from '../config.json';
44

5-
import {runCli} from './_runCli';
5+
import {runCli} from './_runCli';
66

77
beforeEach(async () => {
8-
process.env.COREPACK_HOME = await xfs.mktempPromise();
8+
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
99
});
1010

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

171171
// Use a new cache
172-
process.env.COREPACK_HOME = await xfs.mktempPromise();
172+
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
173173

174174
// Disable the network to make sure we don't succeed by accident
175175
process.env.COREPACK_ENABLE_NETWORK = `0`;
@@ -193,3 +193,21 @@ it(`should support hydrating package managers from cached archives`, async () =>
193193
}
194194
});
195195
});
196+
197+
it(`should support running package managers with bin array`, async () => {
198+
await xfs.mktempPromise(async cwd => {
199+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
200+
packageManager: `[email protected]`,
201+
});
202+
203+
await expect(runCli(cwd, [`yarn`, `yarnpkg`, `--version`])).resolves.toMatchObject({
204+
stdout: `2.2.2\n`,
205+
exitCode: 0,
206+
});
207+
208+
await expect(runCli(cwd, [`yarn`, `yarn`, `--version`])).resolves.toMatchObject({
209+
stdout: `2.2.2\n`,
210+
exitCode: 0,
211+
});
212+
});
213+
});

yarn.lock

+24
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ __metadata:
403403
languageName: node
404404
linkType: hard
405405

406+
"@babel/plugin-proposal-nullish-coalescing-operator@npm:^7.10.4":
407+
version: 7.10.4
408+
resolution: "@babel/plugin-proposal-nullish-coalescing-operator@npm:7.10.4"
409+
dependencies:
410+
"@babel/helper-plugin-utils": ^7.10.4
411+
"@babel/plugin-syntax-nullish-coalescing-operator": ^7.8.0
412+
peerDependencies:
413+
"@babel/core": ^7.0.0-0
414+
checksum: 5a20d8bcbf2926dde3e9edcf847eaa5485d0d0fea76d0683ef1cafb11e0c35e46620391916283e1a9c0f76351e8c5ecccebf0d3a6bdf24559c5ad381433a0e3a
415+
languageName: node
416+
linkType: hard
417+
406418
"@babel/plugin-syntax-bigint@npm:^7.0.0":
407419
version: 7.8.3
408420
resolution: "@babel/plugin-syntax-bigint@npm:7.8.3"
@@ -425,6 +437,17 @@ __metadata:
425437
languageName: node
426438
linkType: hard
427439

440+
"@babel/plugin-syntax-nullish-coalescing-operator@npm:^7.8.0":
441+
version: 7.8.3
442+
resolution: "@babel/plugin-syntax-nullish-coalescing-operator@npm:7.8.3"
443+
dependencies:
444+
"@babel/helper-plugin-utils": ^7.8.0
445+
peerDependencies:
446+
"@babel/core": ^7.0.0-0
447+
checksum: 4ba03753759a2d9783b792c060147a20f474f76c42edf77cbf89c6669f9f22ffb3cbba4facdd8ce651129db6089a81feca1f7e42da75244eabedecba37bd20be
448+
languageName: node
449+
linkType: hard
450+
428451
"@babel/plugin-syntax-object-rest-spread@npm:^7.0.0":
429452
version: 7.8.3
430453
resolution: "@babel/plugin-syntax-object-rest-spread@npm:7.8.3"
@@ -2212,6 +2235,7 @@ __metadata:
22122235
"@babel/core": ^7.11.0
22132236
"@babel/plugin-proposal-class-properties": ^7.10.4
22142237
"@babel/plugin-proposal-decorators": ^7.10.5
2238+
"@babel/plugin-proposal-nullish-coalescing-operator": ^7.10.4
22152239
"@babel/plugin-transform-modules-commonjs": ^7.8.3
22162240
"@babel/preset-typescript": ^7.10.4
22172241
"@types/debug": ^4.1.5

0 commit comments

Comments
 (0)