-
Notifications
You must be signed in to change notification settings - Fork 15
/
check_version.ts
65 lines (52 loc) · 1.47 KB
/
check_version.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
import https from "https"
import chalk from "chalk"
import pkginfo from "pkginfo"
pkginfo(module, "version")
export function getInstalledVersion() {
return module.exports.version as string
}
export const checkVersionAsync = async () => {
const version = getInstalledVersion()
console.info(chalk.whiteBright("ts2gd", "v" + version))
const options = {
hostname: "registry.npmjs.org",
path: "/ts2gd",
}
let response = ""
await new Promise<void>((resolve) => {
const req = https.request(options, (res) => {
res.on("data", (d: Buffer) => {
response += d
})
res.on("end", () => {
resolve()
})
})
req.end()
})
const versionNameDate: [string, Date][] = Object.entries(
JSON.parse(response).time as { [key: string]: string }
)
.sort(
(first: [string, string], second: [string, string]) =>
new Date(second[1]).getTime() - new Date(first[1]).getTime()
)
.map(([a, b]) => [a, new Date(b)])
let latestPublishedVersion = ""
for (const [versionName, date] of versionNameDate) {
if (versionName === "modified") {
continue
}
latestPublishedVersion = versionName
break
}
if (latestPublishedVersion !== version) {
console.info(``)
console.info(
`There is a new version (${latestPublishedVersion}) of ts2gd. (You have ${version}.)`
)
console.info(`Install it with`)
console.info(``)
console.info(chalk.blue(`npm install --global ts2gd`))
}
}