update git-check

This commit is contained in:
2026-02-08 18:35:00 +08:00
parent 4dd0ea4050
commit 9a9262524c
3 changed files with 27 additions and 30 deletions

View File

@@ -885,3 +885,7 @@ export function stringifySorted<T extends Record<string, any>>(
return value; return value;
}, space); }, space);
} }
export function stringifyPretty(object: any): string {
return JSON.stringify(object, null, 2);
}

View File

@@ -98,12 +98,12 @@
}, },
"git-check.ts": { "git-check.ts": {
"script_name": "git-check.ts", "script_name": "git-check.ts",
"script_length": 2683, "script_length": 2543,
"script_sha256": "1267cfdaeace4c5379caf5171be48b0769e5432de9fca9008379c1f0087ffa66", "script_sha256": "88e15260ebcef098c737c49eb229adf3649da10bba69f757792fe1223d9b31cb",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/git-check.ts", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/git-check.ts",
"single_script_file": true, "single_script_file": true,
"publish_time": 1770541896791, "publish_time": 1770541896791,
"update_time": 1770542007498 "update_time": 1770546895115
}, },
"helloworld-bun.ts": { "helloworld-bun.ts": {
"script_name": "helloworld-bun.ts", "script_name": "helloworld-bun.ts",

View File

@@ -6,22 +6,22 @@ import {
log, log,
ProcessBar, ProcessBar,
readFileToString, readFileToString,
stringifyPretty,
uint8ArrayToHexString,
writeStringToFile, 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"; import {getGitLocalRev, getGitRemoteRev,} from "https://script.hatter.ink/@0/deno-git-mod.ts";
async function sha256OfString(input: string): Promise<string> { async function sha256OfString(input: string): Promise<string> {
const data = new TextEncoder().encode(input); const data = new TextEncoder().encode(input);
const hashBuffer = await crypto.subtle.digest("SHA-256", data); const hashBuffer = await crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer)); return uint8ArrayToHexString(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
"",
);
} }
async function getPwd(): Promise<string> { async function getPwd(): Promise<string> {
const pwd = await execCommand("pwd"); return (await execCommand("pwd"))
pwd.assertSuccess(); .assertSuccess()
return pwd.stdout.trim(); .stdout.trim();
} }
interface GitCheckCache { interface GitCheckCache {
@@ -43,35 +43,28 @@ async function main() {
if (gitCheckCache) { if (gitCheckCache) {
const timeBefore = Date.now() - gitCheckCache.lastCheckTime; const timeBefore = Date.now() - gitCheckCache.lastCheckTime;
if (timeBefore < 60 * 60 * 1000) { if (timeBefore < 60 * 60 * 1000) {
const lastCheckTime = new Date(gitCheckCache.lastCheckTime);
const timeBeforeHuman = formatHumanTime(timeBefore);
log.info( log.info(
`Last check at ${new Date( `Last check at ${lastCheckTime}, in ${timeBeforeHuman}, skip check git remote rev`,
gitCheckCache.lastCheckTime,
)}, in ${
formatHumanTime(timeBefore)
}, skip check git remote rev`,
); );
return; return;
} }
} }
const localRev = await getGitLocalRev(); const localRev = await getGitLocalRev();
const remoteRev = await new ProcessBar("Checking rev").call( const remoteRev = await new ProcessBar("Checking remote rev").call(
async (): Promise<string> => { getGitRemoteRev,
return await getGitRemoteRev();
},
); );
if (localRev === remoteRev) { if (localRev === remoteRev) {
log.success(`Check rev successfully, rev: ${localRev}`); log.success(`Check rev successfully, rev: ${localRev}`);
const gitCheckCache: GitCheckCache = {
path: pwd,
lastCheckTime: Date.now(),
};
await writeStringToFile( await writeStringToFile(
gitCheckCacheFile, gitCheckCacheFile,
JSON.stringify( stringifyPretty(gitCheckCache),
{
path: pwd,
lastCheckTime: Date.now(),
} as GitCheckCache,
null,
2,
),
); );
} else { } else {
log.error( log.error(
@@ -83,5 +76,5 @@ async function main() {
await main(); await main();
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T171322+08:00.MEYCIQDvczD6qSfdlDkZCiN+ // @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T183447+08:00.MEYCIQCizSoZCohVIszX24vf
// 9VKrEX6Fqd7/yZQLXjuWpOOX9AIhAIF/bVRRxFcawTy8DIt6G5Zxv3UqB82KgM0aQIAESMM5 // kYa6qKDOpEBl0xwcMz8oY4U4wAIhAM0QzPVsFVZMO5379wA2nQdp0KrjDDzAr2zdd2kVMd7z