Skip to content

Commit 92b52f6

Browse files
authored
feat: add COREPACK_ENABLE_STRICT env variable (#167)
1 parent 11d24f8 commit 92b52f6

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,18 @@ This command will retrieve the given package manager from the specified archive
111111

112112
## Environment Variables
113113

114-
- `COREPACK_ENABLE_NETWORK` can be set to `0` to prevent Corepack from accessing the network (in which case you'll be responsible for hydrating the package manager versions that will be required for the projects you'll run, using `corepack hydrate`).
115-
116114
- `COREPACK_DEFAULT_TO_LATEST` can be set to `0` in order to instruct Corepack
117115
not to lookup on the remote registry for the latest version of the selected
118116
package manager.
119117

118+
- `COREPACK_ENABLE_NETWORK` can be set to `0` to prevent Corepack from accessing
119+
the network (in which case you'll be responsible for hydrating the package
120+
manager versions that will be required for the projects you'll run, using
121+
`corepack hydrate`).
122+
123+
- `COREPACK_ENABLE_STRICT` can be set to `0` to prevent Corepack from checking
124+
if the package manager corresponds to the one defined for the current project.
125+
120126
- `COREPACK_HOME` can be set in order to define where Corepack should install
121127
the package managers. By default it is set to `%LOCALAPPDATA%\node\corepack`
122128
on Windows, and to `$HOME/.cache/node/corepack` everywhere else.

sources/specUtils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
4444
// A locator is a valid descriptor (but not the other way around)
4545
const fallbackLocator = {name: locator.name, range: locator.reference};
4646

47+
if (process.env.COREPACK_ENABLE_STRICT === `0`) return fallbackLocator;
48+
4749
while (true) {
4850
const result = await loadSpec(initialCwd);
4951

tests/main.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,34 @@ it(`should allow to call "prepare" without arguments within a configured project
219219
});
220220
});
221221

222+
it(`should refuse to run a different package manager within a configured project`, async () => {
223+
await xfs.mktempPromise(async cwd => {
224+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
225+
packageManager: `yarn@1.0.0`,
226+
});
227+
228+
process.env.FORCE_COLOR = `0`;
229+
230+
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
231+
stdout: `Usage Error: This project is configured to use yarn\n\n$ pnpm ...\n`,
232+
exitCode: 1,
233+
});
234+
235+
// Disable strict checking to workaround the UsageError.
236+
process.env.COREPACK_ENABLE_STRICT = `0`;
237+
238+
try {
239+
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
240+
stdout: `${config.definitions.pnpm.default.split(`+`, 1)[0]}\n`,
241+
exitCode: 0,
242+
});
243+
} finally {
244+
delete process.env.COREPACK_ENABLE_STRICT;
245+
delete process.env.FORCE_COLOR;
246+
}
247+
});
248+
});
249+
222250
it(`should allow to call "prepare" with --all to prepare all package managers`, async () => {
223251
await xfs.mktempPromise(async cwd => {
224252
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {

0 commit comments

Comments
 (0)