Skip to content

Commit

Permalink
pkg.parse should be able to parse eg nodejs@16
Browse files Browse the repository at this point in the history
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
mxcl committed Sep 26, 2022
1 parent b97f856 commit a415052
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/utils/pkg.ts
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}`)
Expand All @@ -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()
Expand Down

0 comments on commit a415052

Please sign in to comment.