Skip to content

Commit 9bee415

Browse files
authored
feat: add support for rangeless commands (#338)
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`.
1 parent 0717c6a commit 9bee415

7 files changed

+29
-10
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ This command doesn't change the global version used when running the package
177177
manager from outside the project (use the \`-g,--global\` flag if you wish
178178
to do this).
179179

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

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

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

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

203-
### `corepack use <name@version>`
203+
### `corepack use <name[@<version>]>`
204204

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

220220
## Environment Variables
221221

sources/specUtils.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ export function parseSpec(raw: unknown, source: string, {enforceExactVersion = t
1111
if (typeof raw !== `string`)
1212
throw new UsageError(`Invalid package manager specification in ${source}; expected a string`);
1313

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

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

2121
return {
2222
name: match[1],
23-
range: match[2],
23+
range: match[2] ?? `*`,
2424
};
2525
}
2626

@@ -57,7 +57,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
5757
case `NoProject`:
5858
case `NoSpec`: {
5959
return fallbackLocator;
60-
} break;
60+
}
6161

6262
case `Found`: {
6363
if (result.spec.name !== locator.name) {
@@ -69,7 +69,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
6969
} else {
7070
return result.spec;
7171
}
72-
} break;
72+
}
7373
}
7474
}
7575
}

tests/main.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,25 @@ it(`should allow to call "corepack install -g" with a tag`, async () => {
267267
});
268268
});
269269

270+
it(`should allow to call "corepack install -g" without any range`, async () => {
271+
await xfs.mktempPromise(async cwd => {
272+
await expect(runCli(cwd, [`install`, `-g`, `yarn`])).resolves.toMatchObject({
273+
exitCode: 0,
274+
stderr: ``,
275+
});
276+
277+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
278+
// empty package.json file
279+
});
280+
281+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
282+
stdout: expect.not.stringMatching(/^[123]\./),
283+
stderr: ``,
284+
exitCode: 0,
285+
});
286+
});
287+
});
288+
270289
it(`should allow to call "corepack install" without arguments within a configured project`, async () => {
271290
await xfs.mktempPromise(async cwd => {
272291
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
5.4 MB
Binary file not shown.
7 Bytes
Binary file not shown.
-912 KB
Binary file not shown.
-74.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)