Skip to content

Commit 0f9b748

Browse files
merceyzaduh95
andauthored
fix!: call executePackageManagerRequest directly (#430)
* fix!: call `executePackageManagerRequest` directly * Apply suggestions from code review Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> * test: update assertions --------- Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent df1b773 commit 0f9b748

2 files changed

Lines changed: 74 additions & 88 deletions

File tree

sources/main.ts

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import {BaseContext, Builtins, Cli, Command, Option} from 'clipanion';
2-
3-
import {version as corepackVersion} from '../package.json';
4-
5-
import {Engine, PackageManagerRequest} from './Engine';
6-
import {CacheCommand} from './commands/Cache';
7-
import {DisableCommand} from './commands/Disable';
8-
import {EnableCommand} from './commands/Enable';
9-
import {InstallGlobalCommand} from './commands/InstallGlobal';
10-
import {InstallLocalCommand} from './commands/InstallLocal';
11-
import {PackCommand} from './commands/Pack';
12-
import {UpCommand} from './commands/Up';
13-
import {UseCommand} from './commands/Use';
14-
import {HydrateCommand} from './commands/deprecated/Hydrate';
15-
import {PrepareCommand} from './commands/deprecated/Prepare';
1+
import {BaseContext, Builtins, Cli} from 'clipanion';
2+
3+
import {version as corepackVersion} from '../package.json';
4+
5+
import {Engine, PackageManagerRequest} from './Engine';
6+
import {CacheCommand} from './commands/Cache';
7+
import {DisableCommand} from './commands/Disable';
8+
import {EnableCommand} from './commands/Enable';
9+
import {InstallGlobalCommand} from './commands/InstallGlobal';
10+
import {InstallLocalCommand} from './commands/InstallLocal';
11+
import {PackCommand} from './commands/Pack';
12+
import {UpCommand} from './commands/Up';
13+
import {UseCommand} from './commands/Use';
14+
import {HydrateCommand} from './commands/deprecated/Hydrate';
15+
import {PrepareCommand} from './commands/deprecated/Prepare';
1616

1717
export type CustomContext = {cwd: string, engine: Engine};
1818
export type Context = BaseContext & CustomContext;
1919

20-
function getPackageManagerRequestFromCli(parameter: string | undefined, context: CustomContext & Partial<Context>): PackageManagerRequest | null {
20+
function getPackageManagerRequestFromCli(parameter: string | undefined, engine: Engine): PackageManagerRequest | null {
2121
if (!parameter)
2222
return null;
2323

@@ -26,7 +26,7 @@ function getPackageManagerRequestFromCli(parameter: string | undefined, context:
2626
return null;
2727

2828
const [, binaryName, binaryVersion] = match;
29-
const packageManager = context.engine.getPackageManagerFor(binaryName)!;
29+
const packageManager = engine.getPackageManagerFor(binaryName)!;
3030

3131
if (packageManager == null && binaryVersion == null) return null;
3232

@@ -38,17 +38,10 @@ function getPackageManagerRequestFromCli(parameter: string | undefined, context:
3838
}
3939

4040
export async function runMain(argv: Array<string>) {
41-
// Because we load the binaries in the same process, we don't support custom contexts.
42-
const context = {
43-
...Cli.defaultContext,
44-
cwd: process.cwd(),
45-
engine: new Engine(),
46-
};
41+
const engine = new Engine();
4742

4843
const [firstArg, ...restArgs] = argv;
49-
const request = getPackageManagerRequestFromCli(firstArg, context);
50-
51-
let code: number;
44+
const request = getPackageManagerRequestFromCli(firstArg, engine);
5245

5346
if (!request) {
5447
// If the first argument doesn't match any supported package manager, we fallback to the standard Corepack CLI
@@ -74,29 +67,21 @@ export async function runMain(argv: Array<string>) {
7467
cli.register(HydrateCommand);
7568
cli.register(PrepareCommand);
7669

77-
code = await cli.run(argv, context);
78-
} else {
79-
// Otherwise, we create a single-command CLI to run the specified package manager (we still use Clipanion in order to pretty-print usage errors).
80-
const cli = new Cli({
81-
binaryLabel: `'${request.binaryName}', via Corepack`,
82-
binaryName: request.binaryName,
83-
binaryVersion: `corepack/${corepackVersion}`,
84-
});
85-
86-
cli.register(class BinaryCommand extends Command<Context> {
87-
proxy = Option.Proxy();
88-
async execute() {
89-
return this.context.engine.executePackageManagerRequest(request, {
90-
cwd: this.context.cwd,
91-
args: this.proxy,
92-
});
93-
}
94-
});
70+
const context = {
71+
...Cli.defaultContext,
72+
cwd: process.cwd(),
73+
engine,
74+
};
9575

96-
code = await cli.run(restArgs, context);
97-
}
76+
const code = await cli.run(argv, context);
9877

99-
if (code !== 0) {
100-
process.exitCode ??= code;
78+
if (code !== 0) {
79+
process.exitCode ??= code;
80+
}
81+
} else {
82+
await engine.executePackageManagerRequest(request, {
83+
cwd: process.cwd(),
84+
args: restArgs,
85+
});
10186
}
10287
}

tests/main.test.ts

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ it(`should refuse to download a package manager if the hash doesn't match`, asyn
2424

2525
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
2626
exitCode: 1,
27-
stderr: ``,
28-
stdout: /Mismatch hashes/,
27+
stderr: /Mismatch hashes/,
28+
stdout: ``,
2929
});
3030
});
3131
});
@@ -35,8 +35,8 @@ it(`should refuse to download a known package manager from a URL`, async () => {
3535
// Package managers known by Corepack cannot be loaded from a URL.
3636
await expect(runCli(cwd, [`yarn@https://registry.npmjs.com/yarn/-/yarn-1.22.21.tgz`, `--version`])).resolves.toMatchObject({
3737
exitCode: 1,
38-
stderr: ``,
39-
stdout: /Illegal use of URL for known package manager/,
38+
stderr: /Illegal use of URL for known package manager/,
39+
stdout: ``,
4040
});
4141

4242
// Unknown package managers can be loaded from a URL.
@@ -57,8 +57,8 @@ it.failing(`should refuse to download a known package manager from a URL in pack
5757

5858
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
5959
exitCode: 1,
60-
stderr: ``,
61-
stdout: /Illegal use of URL for known package manager/,
60+
stderr: /Illegal use of URL for known package manager/,
61+
stdout: ``,
6262
});
6363

6464
// Unknown package managers can be loaded from a URL.
@@ -82,8 +82,8 @@ it(`should require a version to be specified`, async () => {
8282

8383
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
8484
exitCode: 1,
85-
stderr: ``,
86-
stdout: /expected a semver version/,
85+
stderr: /expected a semver version/,
86+
stdout: ``,
8787
});
8888

8989
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
@@ -92,8 +92,8 @@ it(`should require a version to be specified`, async () => {
9292

9393
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
9494
exitCode: 1,
95-
stderr: ``,
96-
stdout: /expected a semver version/,
95+
stderr: /expected a semver version/,
96+
stdout: ``,
9797
});
9898

9999
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
@@ -102,8 +102,8 @@ it(`should require a version to be specified`, async () => {
102102

103103
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
104104
exitCode: 1,
105-
stderr: ``,
106-
stdout: /expected a semver version/,
105+
stderr: /expected a semver version/,
106+
stdout: ``,
107107
});
108108
});
109109
});
@@ -272,7 +272,7 @@ it(`shouldn't allow using regular Yarn commands on npm-configured projects`, asy
272272

273273
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
274274
exitCode: 1,
275-
stderr: ``,
275+
stderr: /This project is configured to use npm/,
276276
});
277277
});
278278
});
@@ -419,9 +419,10 @@ it(`should refuse to run a different package manager within a configured project
419419
process.env.FORCE_COLOR = `0`;
420420

421421
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
422-
stdout: `Usage Error: This project is configured to use yarn because ${
422+
stdout: ``,
423+
stderr: expect.stringContaining(`This project is configured to use yarn because ${
423424
npath.fromPortablePath(ppath.join(cwd, `package.json` as Filename))
424-
} has a "packageManager" field\n\n$ pnpm ...\n`,
425+
} has a "packageManager" field`),
425426
exitCode: 1,
426427
});
427428

@@ -471,8 +472,8 @@ it(`should support disabling the network accesses from the environment`, async (
471472
});
472473

473474
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
474-
stdout: expect.stringContaining(`Network access disabled by the environment`),
475-
stderr: ``,
475+
stdout: ``,
476+
stderr: /Network access disabled by the environment/,
476477
exitCode: 1,
477478
});
478479
});
@@ -998,13 +999,13 @@ describe(`handle integrity checks`, () => {
998999
await xfs.mktempPromise(async cwd => {
9991000
await expect(runCli(cwd, [`pnpm@1.x`, `--version`], true)).resolves.toMatchObject({
10001001
exitCode: 1,
1001-
stdout: /Signature does not match/,
1002-
stderr: ``,
1002+
stderr: /Signature does not match/,
1003+
stdout: ``,
10031004
});
10041005
await expect(runCli(cwd, [`yarn@stable`, `--version`], true)).resolves.toMatchObject({
10051006
exitCode: 1,
1006-
stdout: /Signature does not match/,
1007-
stderr: ``,
1007+
stderr: /Signature does not match/,
1008+
stdout: ``,
10081009
});
10091010
});
10101011
});
@@ -1014,19 +1015,19 @@ describe(`handle integrity checks`, () => {
10141015
await xfs.mktempPromise(async cwd => {
10151016
await expect(runCli(cwd, [`pnpm`, `--version`], true)).resolves.toMatchObject({
10161017
exitCode: 1,
1017-
stdout: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1018-
stderr: ``,
1018+
stderr: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1019+
stdout: ``,
10191020
});
10201021
// A second time to validate the invalid version was not cached.
10211022
await expect(runCli(cwd, [`pnpm`, `--version`], true)).resolves.toMatchObject({
10221023
exitCode: 1,
1023-
stdout: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1024-
stderr: ``,
1024+
stderr: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1025+
stdout: ``,
10251026
});
10261027
await expect(runCli(cwd, [`yarn`, `--version`], true)).resolves.toMatchObject({
10271028
exitCode: 1,
1028-
stdout: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1029-
stderr: ``,
1029+
stderr: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1030+
stdout: ``,
10301031
});
10311032
await expect(runCli(cwd, [`use`, `pnpm`], true)).resolves.toMatchObject({
10321033
exitCode: 1,
@@ -1041,19 +1042,19 @@ describe(`handle integrity checks`, () => {
10411042
await xfs.mktempPromise(async cwd => {
10421043
await expect(runCli(cwd, [`pnpm`, `--version`], true)).resolves.toMatchObject({
10431044
exitCode: 1,
1044-
stdout: /Signature does not match/,
1045-
stderr: ``,
1045+
stderr: /Signature does not match/,
1046+
stdout: ``,
10461047
});
10471048
// A second time to validate the invalid version was not cached.
10481049
await expect(runCli(cwd, [`pnpm`, `--version`], true)).resolves.toMatchObject({
10491050
exitCode: 1,
1050-
stdout: /Signature does not match/,
1051-
stderr: ``,
1051+
stderr: /Signature does not match/,
1052+
stdout: ``,
10521053
});
10531054
await expect(runCli(cwd, [`yarn`, `--version`], true)).resolves.toMatchObject({
10541055
exitCode: 1,
1055-
stdout: /Signature does not match/,
1056-
stderr: ``,
1056+
stderr: /Signature does not match/,
1057+
stdout: ``,
10571058
});
10581059
await expect(runCli(cwd, [`use`, `pnpm`], true)).resolves.toMatchObject({
10591060
exitCode: 1,
@@ -1068,8 +1069,8 @@ describe(`handle integrity checks`, () => {
10681069
await xfs.mktempPromise(async cwd => {
10691070
await expect(runCli(cwd, [`yarn@1.9998.9999`, `--version`], true)).resolves.toMatchObject({
10701071
exitCode: 1,
1071-
stdout: /Signature does not match/,
1072-
stderr: ``,
1072+
stderr: /Signature does not match/,
1073+
stdout: ``,
10731074
});
10741075
await expect(runCli(cwd, [`use`, `yarn@1.9998.9999`], true)).resolves.toMatchObject({
10751076
exitCode: 1,
@@ -1084,8 +1085,8 @@ describe(`handle integrity checks`, () => {
10841085
await xfs.mktempPromise(async cwd => {
10851086
await expect(runCli(cwd, [`yarn@1.9998.9999`, `--version`], true)).resolves.toMatchObject({
10861087
exitCode: 1,
1087-
stdout: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1088-
stderr: ``,
1088+
stderr: /Mismatch hashes. Expected [a-f0-9]{128}, got [a-f0-9]{128}/,
1089+
stdout: ``,
10891090
});
10901091
await expect(runCli(cwd, [`use`, `yarn@1.9998.9999`], true)).resolves.toMatchObject({
10911092
exitCode: 1,
@@ -1101,10 +1102,10 @@ describe(`handle integrity checks`, () => {
11011102
const result = await runCli(cwd, [`yarn@1.9998.9999+sha1.deadbeef`, `--version`], true);
11021103
expect(result).toMatchObject({
11031104
exitCode: 1,
1104-
stderr: ``,
1105+
stdout: ``,
11051106
});
1106-
const match = /Mismatch hashes. Expected deadbeef, got ([a-f0-9]{40})/.exec(result.stdout);
1107-
if (match == null) throw new Error(`Invalid output`, {cause: result.stdout});
1107+
const match = /Mismatch hashes. Expected deadbeef, got ([a-f0-9]{40})/.exec(result.stderr);
1108+
if (match == null) throw new Error(`Invalid output`, {cause: result.stderr});
11081109
await expect(runCli(cwd, [`yarn@1.9998.9999+sha1.${match[1]}`, `--version`], true)).resolves.toMatchObject({
11091110
exitCode: 0,
11101111
stdout: `yarn: Hello from custom registry\n`,

0 commit comments

Comments
 (0)