81 lines
2.5 KiB
TypeScript
Executable File
81 lines
2.5 KiB
TypeScript
Executable File
#!/usr/bin/env runts -- --allow-all
|
|
|
|
import {
|
|
execCommand,
|
|
formatHumanTime,
|
|
log,
|
|
ProcessBar,
|
|
readFileToString,
|
|
stringifyPretty,
|
|
uint8ArrayToHexString,
|
|
writeStringToFile,
|
|
} 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<string> {
|
|
const data = new TextEncoder().encode(input);
|
|
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
|
return uint8ArrayToHexString(new Uint8Array(hashBuffer));
|
|
}
|
|
|
|
async function getPwd(): Promise<string> {
|
|
return (await execCommand("pwd"))
|
|
.assertSuccess()
|
|
.stdout.trim();
|
|
}
|
|
|
|
interface GitCheckCache {
|
|
path: string;
|
|
lastCheckTime: number;
|
|
}
|
|
|
|
async function main() {
|
|
const pwd = await getPwd();
|
|
const pwdHash = await sha256OfString(pwd);
|
|
const gitCheckCacheFile = `~/.cache/git-check-${
|
|
pwdHash.substring(0, 20)
|
|
}.json`;
|
|
log.debug(`Check git cache file: ${gitCheckCacheFile}`);
|
|
const gitCheckCacheContent = await readFileToString(gitCheckCacheFile);
|
|
const gitCheckCache = gitCheckCacheContent
|
|
? JSON.parse(gitCheckCacheContent) as GitCheckCache
|
|
: null;
|
|
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 ${lastCheckTime}, in ${timeBeforeHuman}, skip check git remote rev`,
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
const localRev = await getGitLocalRev();
|
|
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,
|
|
stringifyPretty(gitCheckCache),
|
|
);
|
|
} else {
|
|
log.error(
|
|
`Check rev failed, local rev: ${localRev} vs remote rev: ${remoteRev}`,
|
|
);
|
|
Deno.exit(1);
|
|
}
|
|
}
|
|
|
|
await main();
|
|
|
|
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T183447+08:00.MEYCIQCizSoZCohVIszX24vf
|
|
// kYa6qKDOpEBl0xwcMz8oY4U4wAIhAM0QzPVsFVZMO5379wA2nQdp0KrjDDzAr2zdd2kVMd7z
|