-
-
Notifications
You must be signed in to change notification settings - Fork 226
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
afcca3d
4b92973
793bbb1
e2d6567
2f790c8
1510e37
a5d870d
ca88ec3
6a0aed2
6ccc639
3e938cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
mmkal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
It will locate the install directory by running the equivalent of \`which corepack\`. | ||
`, | ||
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); | ||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {ppath, xfs, npath} from '@yarnpkg/fslib'; | ||
import process from 'node:process'; | ||
import {describe, beforeEach, it, expect} from 'vitest'; | ||
|
||
import {runCli} from './_runCli'; | ||
|
||
beforeEach(async () => { | ||
// `process.env` is reset after each tests in setupTests.js. | ||
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise()); | ||
process.env.COREPACK_DEFAULT_TO_LATEST = `0`; | ||
}); | ||
|
||
describe(`ProjectCommand`, () => { | ||
describe(`InstallSubcommand`, () => { | ||
it(`should install with npm`, async () => { | ||
await xfs.mktempPromise(async cwd => { | ||
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), { | ||
packageManager: `[email protected]`, | ||
dependencies: { | ||
ms: `2.1.3`, | ||
}, | ||
}); | ||
|
||
await expect(runCli(cwd, [`project`, `install`])).resolves.toMatchObject({ | ||
Check failure on line 24 in tests/Project.test.ts
|
||
exitCode: 0, | ||
stderr: ``, | ||
}); | ||
|
||
const dir = await xfs.readdirPromise(cwd); | ||
expect(dir).toContain(`package-lock.json`); | ||
expect(dir).toContain(`node_modules`); | ||
}); | ||
}); | ||
|
||
it(`should install with pnpm`, async () => { | ||
await xfs.mktempPromise(async cwd => { | ||
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), { | ||
packageManager: `[email protected]`, | ||
dependencies: { | ||
ms: `2.1.3`, | ||
}, | ||
}); | ||
|
||
await expect(runCli(cwd, [`project`, `install`])).resolves.toMatchObject({ | ||
Check failure on line 44 in tests/Project.test.ts
|
||
exitCode: 0, | ||
stderr: ``, | ||
}); | ||
|
||
const dir = await xfs.readdirPromise(cwd); | ||
expect(dir).toContain(`pnpm-lock.yaml`); | ||
expect(dir).toContain(`node_modules`); | ||
}); | ||
}); | ||
|
||
it(`should install with yarn`, async () => { | ||
await xfs.mktempPromise(async cwd => { | ||
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), { | ||
packageManager: `[email protected]`, | ||
dependencies: { | ||
ms: `2.1.3`, | ||
}, | ||
}); | ||
|
||
await expect(runCli(cwd, [`project`, `install`])).resolves.toMatchObject({ | ||
Check failure on line 64 in tests/Project.test.ts
|
||
exitCode: 0, | ||
stderr: ``, | ||
}); | ||
|
||
const dir = await xfs.readdirPromise(cwd); | ||
expect(dir).toContain(`yarn.lock`); | ||
expect(dir).toContain(`.pnp.js`); | ||
}); | ||
}); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.