Skip to content

Commit

Permalink
perf!: call executePackageManagerRequest directly
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Mar 15, 2024
1 parent 47f1e2f commit 8f7203f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 64 deletions.
81 changes: 33 additions & 48 deletions sources/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {BaseContext, Builtins, Cli, Command, Option} from 'clipanion';

import {version as corepackVersion} from '../package.json';

import {Engine, PackageManagerRequest} from './Engine';
import {CacheCommand} from './commands/Cache';
import {DisableCommand} from './commands/Disable';
import {EnableCommand} from './commands/Enable';
import {InstallGlobalCommand} from './commands/InstallGlobal';
import {InstallLocalCommand} from './commands/InstallLocal';
import {PackCommand} from './commands/Pack';
import {UpCommand} from './commands/Up';
import {UseCommand} from './commands/Use';
import {HydrateCommand} from './commands/deprecated/Hydrate';
import {PrepareCommand} from './commands/deprecated/Prepare';
import {BaseContext, Builtins, Cli} from 'clipanion';

import {version as corepackVersion} from '../package.json';

import {Engine, PackageManagerRequest} from './Engine';
import {CacheCommand} from './commands/Cache';
import {DisableCommand} from './commands/Disable';
import {EnableCommand} from './commands/Enable';
import {InstallGlobalCommand} from './commands/InstallGlobal';
import {InstallLocalCommand} from './commands/InstallLocal';
import {PackCommand} from './commands/Pack';
import {UpCommand} from './commands/Up';
import {UseCommand} from './commands/Use';
import {HydrateCommand} from './commands/deprecated/Hydrate';
import {PrepareCommand} from './commands/deprecated/Prepare';

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

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

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

const [, binaryName, binaryVersion] = match;
const packageManager = context.engine.getPackageManagerFor(binaryName)!;
const packageManager = engine.getPackageManagerFor(binaryName)!;

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

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

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

const [firstArg, ...restArgs] = argv;
const request = getPackageManagerRequestFromCli(firstArg, context);

let code: number;
const request = getPackageManagerRequestFromCli(firstArg, engine);

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

code = await cli.run(argv, context);
} else {
// Otherwise, we create a single-command CLI to run the specified package manager (we still use Clipanion in order to pretty-print usage errors).
const cli = new Cli({
binaryLabel: `'${request.binaryName}', via Corepack`,
binaryName: request.binaryName,
binaryVersion: `corepack/${corepackVersion}`,
});

cli.register(class BinaryCommand extends Command<Context> {
proxy = Option.Proxy();
async execute() {
return this.context.engine.executePackageManagerRequest(request, {
cwd: this.context.cwd,
args: this.proxy,
});
}
});
const context = {
...Cli.defaultContext,
cwd: process.cwd(),
engine,
};

code = await cli.run(restArgs, context);
}
const code = await cli.run(argv, context);

if (code !== 0) {
process.exitCode ??= code;
if (code !== 0) {
process.exitCode ??= code;
}
} else {
await engine.executePackageManagerRequest(request, {
cwd: process.cwd(),
args: restArgs,
});
}
}
33 changes: 17 additions & 16 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ it(`should refuse to download a package manager if the hash doesn't match`, asyn

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
stdout: /Mismatch hashes/,
stderr: /Mismatch hashes/,
stdout: ``,
});
});
});
Expand All @@ -34,8 +34,8 @@ it(`should refuse to download a known package manager from a URL`, async () => {
// Package managers known by Corepack cannot be loaded from a URL.
await expect(runCli(cwd, [`yarn@https://registry.npmjs.com/yarn/-/yarn-1.22.21.tgz`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
stdout: /Illegal use of URL for known package manager/,
stderr: /Illegal use of URL for known package manager/,
stdout: ``,
});

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

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

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

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

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

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

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

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
stdout: /expected a semver version/,
stderr: /expected a semver version/,
stdout: ``,
});
});
});
Expand Down Expand Up @@ -271,7 +271,7 @@ it(`shouldn't allow using regular Yarn commands on npm-configured projects`, asy

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: ``,
stderr: expect.stringContaining(`This project is configured to use npm`),
});
});
});
Expand Down Expand Up @@ -418,7 +418,8 @@ it(`should refuse to run a different package manager within a configured project
process.env.FORCE_COLOR = `0`;

await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
stdout: `Usage Error: This project is configured to use yarn\n\n$ pnpm ...\n`,
stdout: ``,
stderr: expect.stringContaining(`This project is configured to use yarn`),
exitCode: 1,
});

Expand Down Expand Up @@ -468,8 +469,8 @@ it(`should support disabling the network accesses from the environment`, async (
});

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: expect.stringContaining(`Network access disabled by the environment`),
stderr: ``,
stdout: ``,
stderr: expect.stringContaining(`Network access disabled by the environment`),
exitCode: 1,
});
});
Expand Down

0 comments on commit 8f7203f

Please sign in to comment.