From 9a9262524c3ecb2cefc94b6bb4c8d9d6567a9091 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 8 Feb 2026 18:35:00 +0800 Subject: [PATCH] update git-check --- libraries/deno-commons-mod.ts | 4 +++ script-meta-v2.json | 6 ++--- single-scripts/git-check.ts | 47 +++++++++++++++-------------------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 9647a6a..943b6ff 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -885,3 +885,7 @@ export function stringifySorted>( return value; }, space); } + +export function stringifyPretty(object: any): string { + return JSON.stringify(object, null, 2); +} diff --git a/script-meta-v2.json b/script-meta-v2.json index 232edef..44ec7ab 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -98,12 +98,12 @@ }, "git-check.ts": { "script_name": "git-check.ts", - "script_length": 2683, - "script_sha256": "1267cfdaeace4c5379caf5171be48b0769e5432de9fca9008379c1f0087ffa66", + "script_length": 2543, + "script_sha256": "88e15260ebcef098c737c49eb229adf3649da10bba69f757792fe1223d9b31cb", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/git-check.ts", "single_script_file": true, "publish_time": 1770541896791, - "update_time": 1770542007498 + "update_time": 1770546895115 }, "helloworld-bun.ts": { "script_name": "helloworld-bun.ts", diff --git a/single-scripts/git-check.ts b/single-scripts/git-check.ts index 6993b78..72651e6 100755 --- a/single-scripts/git-check.ts +++ b/single-scripts/git-check.ts @@ -6,22 +6,22 @@ import { log, ProcessBar, readFileToString, + stringifyPretty, + uint8ArrayToHexString, writeStringToFile, -} from "https://script.hatter.ink/@29/deno-commons-mod.ts"; +} from "https://script.hatter.ink/@34/deno-commons-mod.ts"; import {getGitLocalRev, getGitRemoteRev,} from "https://script.hatter.ink/@0/deno-git-mod.ts"; async function sha256OfString(input: string): Promise { const data = new TextEncoder().encode(input); const hashBuffer = await crypto.subtle.digest("SHA-256", data); - const hashArray = Array.from(new Uint8Array(hashBuffer)); - return hashArray.map((b) => b.toString(16).padStart(2, "0")).join( - "", - ); + return uint8ArrayToHexString(new Uint8Array(hashBuffer)); } + async function getPwd(): Promise { - const pwd = await execCommand("pwd"); - pwd.assertSuccess(); - return pwd.stdout.trim(); + return (await execCommand("pwd")) + .assertSuccess() + .stdout.trim(); } interface GitCheckCache { @@ -43,35 +43,28 @@ async function main() { if (gitCheckCache) { const timeBefore = Date.now() - gitCheckCache.lastCheckTime; if (timeBefore < 60 * 60 * 1000) { + const lastCheckTime = new Date(gitCheckCache.lastCheckTime); + const timeBeforeHuman = formatHumanTime(timeBefore); log.info( - `Last check at ${new Date( - gitCheckCache.lastCheckTime, - )}, in ${ - formatHumanTime(timeBefore) - }, skip check git remote rev`, + `Last check at ${lastCheckTime}, in ${timeBeforeHuman}, skip check git remote rev`, ); return; } } const localRev = await getGitLocalRev(); - const remoteRev = await new ProcessBar("Checking rev").call( - async (): Promise => { - return await getGitRemoteRev(); - }, + const remoteRev = await new ProcessBar("Checking remote rev").call( + getGitRemoteRev, ); if (localRev === remoteRev) { log.success(`Check rev successfully, rev: ${localRev}`); + const gitCheckCache: GitCheckCache = { + path: pwd, + lastCheckTime: Date.now(), + }; await writeStringToFile( gitCheckCacheFile, - JSON.stringify( - { - path: pwd, - lastCheckTime: Date.now(), - } as GitCheckCache, - null, - 2, - ), + stringifyPretty(gitCheckCache), ); } else { log.error( @@ -83,5 +76,5 @@ async function main() { await main(); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T171322+08:00.MEYCIQDvczD6qSfdlDkZCiN+ -// 9VKrEX6Fqd7/yZQLXjuWpOOX9AIhAIF/bVRRxFcawTy8DIt6G5Zxv3UqB82KgM0aQIAESMM5 +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T183447+08:00.MEYCIQCizSoZCohVIszX24vf +// kYa6qKDOpEBl0xwcMz8oY4U4wAIhAM0QzPVsFVZMO5379wA2nQdp0KrjDDzAr2zdd2kVMd7z