forked from pkgxdev/pkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg.parse should be able to parse eg
nodejs@16
We are leaving semver.parse itself as more strict however so that we don’t ingest eg. git tags like `16` as v16.0.0
- Loading branch information
Showing
1 changed file
with
8 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { Package, PackageRequirement } from "types" | ||
import SemVer, * as semver from "semver" | ||
import { core } from "https://deno.land/[email protected]/encoding/_yaml/schema/core.ts" | ||
|
||
/// allows inputs `nodejs.org@16` when `semver.parse` would reject | ||
export function parse(input: string): PackageRequirement | Package { | ||
const match = input.match(/^(.*?)([\^=~<>@].+)?$/) | ||
if (!match) throw new Error(`invalid pkgspec: ${input}`) | ||
|
@@ -9,10 +11,13 @@ export function parse(input: string): PackageRequirement | Package { | |
const project = match[1] | ||
|
||
if (match[2].startsWith("@") || match[2].startsWith("=")) { | ||
return { | ||
project, | ||
version: new SemVer(match[2].slice(1)) | ||
let version = semver.parse(match[2].slice(1)) | ||
if (!version) { | ||
const coercion = parseInt(match[2].slice(1)) | ||
if (Number.isNaN(coercion)) throw new Error() | ||
version = new SemVer([coercion, 0, 0]) | ||
} | ||
return { project, version } | ||
} else { | ||
const constraint = new semver.Range(match[2]) | ||
const version = constraint.single() | ||
|