Skip to content

Commit

Permalink
feat: add support for tags and ranges in prepare command (#136)
Browse files Browse the repository at this point in the history
Fixes: #72
  • Loading branch information
aduh95 authored Jul 8, 2022
1 parent 9a153d2 commit 29da06c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions sources/commands/Prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class PrepareCommand extends Command<Context> {
], [
`Prepare a specific Yarn version`,
`$0 prepare [email protected]`,
], [
`Prepare the latest available pnpm version`,
`$0 prepare pnpm@latest --activate`,
], [
`Generate an archive for a specific Yarn version`,
`$0 prepare [email protected] -o`,
Expand All @@ -50,7 +53,7 @@ export class PrepareCommand extends Command<Context> {
tolerateBoolean: true,
});

specs = Option.Rest()
specs = Option.Rest();

async execute() {
if (this.all && this.specs.length > 0)
Expand Down Expand Up @@ -79,10 +82,10 @@ export class PrepareCommand extends Command<Context> {

for (const request of specs) {
const spec = typeof request === `string`
? specUtils.parseSpec(request, `CLI arguments`)
? specUtils.parseSpec(request, `CLI arguments`, {enforceExactVersion: false})
: request;

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

Expand Down
6 changes: 3 additions & 3 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {Descriptor, Locator, isSupportedPackageManager} from './types';

const nodeModulesRegExp = /[\\/]node_modules[\\/](@[^\\/]*[\\/])?([^@\\/][^\\/]*)$/;

export function parseSpec(raw: unknown, source?: string): Descriptor {
export function parseSpec(raw: unknown, source: string, {enforceExactVersion = true} = {}): Descriptor {
if (typeof raw !== `string`)
throw new UsageError(`Invalid package manager specification in ${source}; expected a string`);

const match = raw.match(/^(?!_)(.+)@(.+)$/);
if (match === null || !semver.valid(match[2]))
throw new UsageError(`Invalid package manager specification in ${source}; expected a semver version`);
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`}`);

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

0 comments on commit 29da06c

Please sign in to comment.