mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: update checker
This commit is contained in:
46
apps/expo/src/lib/update.ts
Normal file
46
apps/expo/src/lib/update.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import * as Application from "expo-application";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
|
||||
function isVersionHigher(newVersion: string, currentVersion: string): boolean {
|
||||
const parseVersion = (version: string) =>
|
||||
version
|
||||
.replace(/^v/, "")
|
||||
.split(".")
|
||||
.map((part) => parseInt(part, 10));
|
||||
|
||||
const newParts = parseVersion(newVersion);
|
||||
const currentParts = parseVersion(currentVersion);
|
||||
const maxLength = Math.max(newParts.length, currentParts.length);
|
||||
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const newPart = newParts[i] ?? 0;
|
||||
const currentPart = currentParts[i] ?? 0;
|
||||
|
||||
if (newPart !== currentPart) {
|
||||
return newPart > currentPart;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function checkForUpdate(): Promise<string | undefined> {
|
||||
const octokit = new Octokit();
|
||||
|
||||
const res = await octokit.repos
|
||||
.getLatestRelease({
|
||||
owner: "movie-web",
|
||||
repo: "native-app",
|
||||
})
|
||||
.catch(() => undefined);
|
||||
|
||||
if (!res) return;
|
||||
|
||||
const latestVersion: string = res.data.tag_name;
|
||||
const currentVersion: string =
|
||||
Application.nativeApplicationVersion ?? "0.0.0";
|
||||
|
||||
if (isVersionHigher(latestVersion, currentVersion)) {
|
||||
return res.data.html_url;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user