-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.ts
126 lines (114 loc) · 3.57 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
export function getDeps(text: string) {
const deps: Array<{
type: "npm" | "github" | "std" | "unknown"
url: string
name: string
version: string
emptyVersion: boolean
}> = []
const SCOPED_PKG_RE = /^\/(@[^\/]+)\/([^\/]+)/
const PKG_RE = /^\/([^\/]+)/
text.replace(/\s+from\s+['"`](.+)['"`]/g, (_, p1) => {
if (/^https:\/\//.test(p1)) {
const { hostname, pathname } = new URL("", p1)
let m: RegExpExecArray | null = null
if (hostname === "unpkg.com" || hostname === "cdn.pika.dev") {
if ((m = SCOPED_PKG_RE.exec(pathname))) {
const version = m[2].split("@")[1]
deps.push({
url: p1,
type: "npm",
name: m[1] + "/" + m[2].split("@")[0],
version: version || "latest",
emptyVersion: !version,
})
} else if ((m = PKG_RE.exec(pathname))) {
const version = m[1].split("@")[1]
deps.push({
type: "npm",
url: p1,
name: m[1].split("@")[0],
version: version || "latest",
emptyVersion: !version,
})
} else {
handlerUnkown(p1)
}
} else if (hostname === "deno.land") {
if ((m = /^\/std(?:@([^\/]+))?/.exec(pathname))) {
deps.push({
type: "std",
url: p1,
name: "denoland/deno",
version: m[1] || "master",
emptyVersion: !m[1],
})
} else {
handlerUnkown(p1)
}
} else if (hostname === "denopkg.com") {
if ((m = /\/([^\/]+)\/([^\/]+)/.exec(pathname))) {
const version = m[2].split("@")[1]
deps.push({
type: "github",
url: p1,
name: `${m[1]}/${m[2].split("@")[0]}`,
version: version || "master",
emptyVersion: !version,
})
}
} else {
handlerUnkown(p1)
}
}
return ""
})
function handlerUnkown(url: string) {
deps.push({
type: "unknown",
url,
name: "",
version: "",
emptyVersion: true,
})
}
return deps
}
export async function getPkgLatestVersion(name: string) {
const json = await fetch(`https://registry.npmjs.org/${name}`).then((res) =>
res.json()
)
return json["dist-tags"]["latest"]
}
// https://github.com/substack/semver-compare/blob/master/index.js
export function semverCompare(a: string, b: string) {
const pa = a.split(".")
const pb = b.split(".")
for (let i = 0; i < 3; i++) {
const na = Number(pa[i])
const nb = Number(pb[i])
if (na > nb) return 1
if (nb > na) return -1
if (!isNaN(na) && isNaN(nb)) return 1
if (isNaN(na) && !isNaN(nb)) return -1
}
return 0
}
// https://github.com/sindresorhus/semver-regex/blob/master/index.js
const isSemver = (v: string) =>
/(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(?=$|\s)/gi
export async function getLatestGitTag(repo: string) {
const json = await fetch(
`https://api.github.com/repos/${repo}/git/refs/tags`
).then((res) => res.json())
const tags = json
.map((v: any) => v.ref.replace(/^refs\/tags\/v?/, ""))
.filter((v: string) => isSemver(v))
return tags.sort((a: string, b: string) => -semverCompare(a, b))[0]
}
export async function getLatestStdVersion() {
const text = await fetch(
`https://denopkg.com/denoland/deno_std@main/version.ts`
).then((res) => res.text())
return text.match(/const VERSION = "(.+)"/)![1]
}