Skip to content

Commit

Permalink
feat: add support for rangeless commands (#338)
Browse files Browse the repository at this point in the history
This diff makes it possible to run the CLI commands without an explicit range, in which case we use an implicit `*`, e.g.: `corepack install -g yarn`.
  • Loading branch information
arcanis authored Dec 29, 2023
1 parent 0717c6a commit 9bee415
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ This command doesn't change the global version used when running the package
manager from outside the project (use the \`-g,--global\` flag if you wish
to do this).

### `corepack install <-g,--global> [--all] [... name@version]`
### `corepack install <-g,--global> [--all] [... name[@<version>]]`

| Option | Description |
| --------------------- | ------------------------------------------ |
Expand All @@ -189,7 +189,7 @@ Package managers thus installed will be configured as the new default when
calling their respective binaries outside of projects defining the
`packageManager` field.

### `corepack pack [--all] [... name@version]`
### `corepack pack [--all] [... name[@<version>]]`

| Option | Description |
| --------------------- | ------------------------------------------ |
Expand All @@ -200,7 +200,7 @@ calling their respective binaries outside of projects defining the
Download the selected package managers and store them inside a tarball
suitable for use with `corepack install -g`.

### `corepack use <name@version>`
### `corepack use <name[@<version>]>`

When run, this command will retrieve the latest release matching the provided
descriptor, assign it to the project's package.json file, and automatically
Expand All @@ -215,7 +215,7 @@ it.
Unlike `corepack use` this command doesn't take a package manager name nor a
version range, as it will always select the latest available version from the
same major line. Should you need to upgrade to a new major, use an explicit
`corepack use {name}@latest` call.
`corepack use {name}@latest` call (or simply `corepack use {name}`).

## Environment Variables

Expand Down
12 changes: 6 additions & 6 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export function parseSpec(raw: unknown, source: string, {enforceExactVersion = t
if (typeof raw !== `string`)
throw new UsageError(`Invalid package manager specification in ${source}; expected a string`);

const match = raw.match(/^(?!_)(.+)@(.+)$/);
if (match === null || (enforceExactVersion && !semver.valid(match[2])))
throw new UsageError(`Invalid package manager specification in ${source}; expected a semver version${enforceExactVersion ? `` : `, range, or tag`}`);
const match = raw.match(/^(?!_)([^@]+)(?:@(.+))?$/);
if (match === null || (enforceExactVersion && (!match[2] || !semver.valid(match[2]))))
throw new UsageError(`Invalid package manager specification in ${source} (${raw}); expected a semver version${enforceExactVersion ? `` : `, range, or tag`}`);

if (!isSupportedPackageManager(match[1]))
throw new UsageError(`Unsupported package manager specification (${match})`);

return {
name: match[1],
range: match[2],
range: match[2] ?? `*`,
};
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
case `NoProject`:
case `NoSpec`: {
return fallbackLocator;
} break;
}

case `Found`: {
if (result.spec.name !== locator.name) {
Expand All @@ -69,7 +69,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
} else {
return result.spec;
}
} break;
}
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ it(`should allow to call "corepack install -g" with a tag`, async () => {
});
});

it(`should allow to call "corepack install -g" without any range`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`install`, `-g`, `yarn`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});

await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
// empty package.json file
});

await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
stdout: expect.not.stringMatching(/^[123]\./),
stderr: ``,
exitCode: 0,
});
});
});

it(`should allow to call "corepack install" without arguments within a configured project`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
Expand Down
Binary file added tests/nock/8LXMft4IyEWeaqoiynS5FA-1.dat
Binary file not shown.
Binary file added tests/nock/8LXMft4IyEWeaqoiynS5FA-2.dat
Binary file not shown.
Binary file modified tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat
Binary file not shown.
Binary file modified tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat
Binary file not shown.

0 comments on commit 9bee415

Please sign in to comment.