-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
get-latest-version.ts
45 lines (40 loc) · 1.4 KB
/
get-latest-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
import {fetchAllVersions, fetchLatestDistVersion} from './npm-fetch'
import {log} from '@blitzjs/display'
import {Fallbackable} from './fallbackable'
import chalk from 'chalk'
export const logFailedVersionFetch = (dependency: string, fallback: string) => {
log.clearLine(
log.withWarning(
`Failed to fetch latest version of '${chalk.bold(dependency)}', falling back to '${chalk.bold(
fallback,
)}'.\n`,
),
)
}
export const getLatestVersion = async (
dependency: string,
templateVersion: string = '',
): Promise<Fallbackable<string>> => {
const major = templateVersion.replace('.x', '')
try {
const [allVersions, latestDistVersion] = await Promise.all([
fetchAllVersions(dependency),
fetchLatestDistVersion(dependency),
])
const latestVersion = allVersions
.filter((version) => version.startsWith(major))
.sort((a, b) => a.localeCompare(b, undefined, {numeric: true}))
.reverse()[0]
// If the latest tagged version matches our pinned major, use that, otherwise use the
// latest untagged which does
if (latestDistVersion.startsWith(major)) {
return {value: latestDistVersion, isFallback: false}
} else {
return {value: latestVersion, isFallback: false}
}
} catch (error) {
const fallback = templateVersion
logFailedVersionFetch(dependency, fallback)
return {value: fallback, isFallback: false}
}
}