From fce3b3ce7bfa817ae683bfd2bea7c326a3b81f8b Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 4 May 2023 00:12:00 +0800 Subject: [PATCH] feat: use commit time as version id --- app/components/settings.tsx | 26 ++++++++++++++++++++++++-- app/config/build.ts | 11 ++++------- app/store/update.ts | 5 ++--- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/components/settings.tsx b/app/components/settings.tsx index 3b40f7c5..945149b7 100644 --- a/app/components/settings.tsx +++ b/app/components/settings.tsx @@ -183,6 +183,19 @@ function UserPromptModal(props: { onClose?: () => void }) { ); } +function formatVersionDate(t: string) { + const d = new Date(+t); + const year = d.getUTCFullYear(); + const month = d.getUTCMonth() + 1; + const day = d.getUTCDate(); + + return [ + year.toString(), + month.toString().padStart(2, "0"), + day.toString().padStart(2, "0"), + ].join(""); +} + export function Settings() { const navigate = useNavigate(); const [showEmojiPicker, setShowEmojiPicker] = useState(false); @@ -193,8 +206,8 @@ export function Settings() { const updateStore = useUpdateStore(); const [checkingUpdate, setCheckingUpdate] = useState(false); - const currentVersion = updateStore.version; - const remoteId = updateStore.remoteVersion; + const currentVersion = formatVersionDate(updateStore.version); + const remoteId = formatVersionDate(updateStore.remoteVersion); const hasNewVersion = currentVersion !== remoteId; function checkUpdate(force = false) { @@ -202,6 +215,15 @@ export function Settings() { updateStore.getLatestVersion(force).then(() => { setCheckingUpdate(false); }); + + console.log( + "[Update] local version ", + new Date(+updateStore.version).toLocaleString(), + ); + console.log( + "[Update] remote version ", + new Date(+updateStore.remoteVersion).toLocaleString(), + ); } const usage = { diff --git a/app/config/build.ts b/app/config/build.ts index 49205c9b..79ed5d3e 100644 --- a/app/config/build.ts +++ b/app/config/build.ts @@ -1,13 +1,10 @@ const COMMIT_ID: string = (() => { try { const childProcess = require("child_process"); - return ( - childProcess - // .execSync("git describe --tags --abbrev=0") - .execSync("git rev-parse --short HEAD") - .toString() - .trim() - ); + return childProcess + .execSync('git log -1 --format="%at000" --date=unix') + .toString() + .trim(); } catch (e) { console.error("[Build Config] No git or not from git repo."); return "unknown"; diff --git a/app/store/update.ts b/app/store/update.ts index 888741b8..8d880822 100644 --- a/app/store/update.ts +++ b/app/store/update.ts @@ -53,10 +53,9 @@ export const useUpdateStore = create()( })); try { - // const data = await (await fetch(FETCH_TAG_URL)).json(); - // const remoteId = data[0].name as string; const data = await (await fetch(FETCH_COMMIT_URL)).json(); - const remoteId = (data[0].sha as string).substring(0, 7); + const remoteCommitTime = data[0].commit.committer.date; + const remoteId = new Date(remoteCommitTime).getTime().toString(); set(() => ({ remoteVersion: remoteId, }));