-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathversion.ts
More file actions
63 lines (56 loc) · 1.83 KB
/
version.ts
File metadata and controls
63 lines (56 loc) · 1.83 KB
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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { IS_CHROME, IS_EDGE, IS_FIREFOX } from "@util/constant/environment"
import { fetchGet } from "./http"
/**
* @since 0.1.8
*/
type FirefoxDetail = {
current_version: {
// Like 0.1.5
version: string
}
// Like '2021-06-11T08:45:32Z'
last_updated: string
}
/**
* @since 0.1.8
*/
type EdgeDetail = {
// Version like 0.1.5, without 'v' prefix
version: string
// Like '1619432502.5944779'
lastUpdateDate: string
}
async function getFirefoxVersion(): Promise<string | undefined> {
const response = await fetchGet('https://addons.mozilla.org/api/v3/addons/addon/2690100')
const detail: FirefoxDetail = await response.json()
return detail?.current_version?.version
}
async function getEdgeVersion(): Promise<string | undefined> {
const response = await fetchGet('https://microsoftedge.microsoft.com/addons/getproductdetailsbycrxid/fepjgblalcnepokjblgbgmapmlkgfahc')
const detail: EdgeDetail = await response.json()
return detail?.version
}
async function getChromeVersion(): Promise<string | undefined> {
// Get info from shields.io
const response = await fetchGet('https://img.shields.io/chrome-web-store/v/dkdhhcbjijekmneelocdllcldcpmekmm?label=Google%20Chrome')
const data = await response.text()
const pattern = /:\sv(\d+\.\d+\.\d+)/
const matchResult = pattern.exec(data)
return matchResult?.length === 2 ? matchResult?.[1] : undefined
}
export function getLatestVersion(): Promise<string | undefined> {
if (IS_FIREFOX) {
return getFirefoxVersion()
} else if (IS_CHROME) {
return getChromeVersion()
} else if (IS_EDGE) {
return getEdgeVersion()
}
return Promise.resolve(undefined)
}