Skip to content

add corepack project install command #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion sources/commands/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ export abstract class BaseCommand extends Command<Context> {
previousPackageManager,
} = await specUtils.setLocalPackageManager(this.context.cwd, info);

await this.installLocalPackageManager(info, previousPackageManager);
}

async installLocalPackageManager(info: PreparedPackageManagerInfo, previousPackageManager?: string) {
const command = this.context.engine.getPackageManagerSpecFor(info.locator).commands?.use ?? null;
if (command === null)
return 0;

// Adding it into the environment avoids breaking package managers that
// don't expect those options.
process.env.COREPACK_MIGRATE_FROM = previousPackageManager;
if (previousPackageManager)
process.env.COREPACK_MIGRATE_FROM = previousPackageManager;

this.context.stdout.write(`\n`);

const [binaryName, ...args] = command;
Expand Down
46 changes: 46 additions & 0 deletions sources/commands/Project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Command, UsageError} from 'clipanion';
import semverValid from 'semver/functions/valid';
import semverValidRange from 'semver/ranges/valid';

import {BaseCommand} from './Base';

// modified from ./Enable.ts
// https://github.com/nodejs/corepack/issues/505
export class ProjectInstallCommand extends BaseCommand {
static paths = [
[`project`, `install`],
];

static usage = Command.Usage({
description: `Add the Corepack shims to the install directories, and run the install command of the specified package manager`,
details: `
When run, this command will check whether the shims for the specified package managers can be found with the correct values inside the install directory. If not, or if they don't exist, they will be created.

Then, it will run the install command of the specified package manager. If no package manager is specified, it will default to NPM.

By default it will locate the install directory by running the equivalent of \`which corepack\`, but this can be tweaked by explicitly passing the install directory via the \`--install-directory\` flag.
`,
examples: [[
`Enable all shims and install, putting shims next to the \`corepack\` binary`,
`$0 project install`,
]],
});

async execute() {
const [descriptor] = await this.resolvePatternsToDescriptors({
patterns: [],
});

if (!semverValid(descriptor.range) && !semverValidRange(descriptor.range))
throw new UsageError(`The 'corepack project install' command can only be used when your project's packageManager field is set to a semver version or semver range`);

const resolved = await this.context.engine.resolveDescriptor(descriptor, {useCache: true});
if (!resolved)
throw new UsageError(`Failed to successfully resolve '${descriptor.range}' to a valid ${descriptor.name} release`);

this.context.stdout.write(`Installing ${resolved.name}@${resolved.reference} in the project...\n`);

const packageManagerInfo = await this.context.engine.ensurePackageManager(resolved);
await this.installLocalPackageManager(packageManagerInfo);
}
}
2 changes: 2 additions & 0 deletions sources/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {EnableCommand} from './commands/Enable';
import {InstallGlobalCommand} from './commands/InstallGlobal';
import {InstallLocalCommand} from './commands/InstallLocal';
import {PackCommand} from './commands/Pack';
import {ProjectInstallCommand} from './commands/Project';
import {UpCommand} from './commands/Up';
import {UseCommand} from './commands/Use';
import {HydrateCommand} from './commands/deprecated/Hydrate';
Expand Down Expand Up @@ -62,6 +63,7 @@ export async function runMain(argv: Array<string>) {
cli.register(PackCommand);
cli.register(UpCommand);
cli.register(UseCommand);
cli.register(ProjectInstallCommand);

// Deprecated commands
cli.register(HydrateCommand);
Expand Down
9 changes: 9 additions & 0 deletions tests/Project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {describe, it} from 'vitest';

describe(`ProjectCommand`, () => {
describe(`InstallSubcommand`, () => {
it(`should add the binaries in the folder found in the PATH`, async () => {
// todo
});
});
});