mirror of
https://github.com/sern-handler/website
synced 2026-06-24 00:32:24 +00:00
25 lines
724 B
TypeScript
25 lines
724 B
TypeScript
import { basename, dirname } from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
|
|
export function getNewestCommitDate(file: string) {
|
|
const result = spawnSync('git', ['log', '--format=%ct', '--max-count=1', basename(file)], {
|
|
cwd: dirname(file),
|
|
encoding: 'utf-8',
|
|
});
|
|
|
|
if (result.error) {
|
|
throw new Error(`Failed to retrieve the git history for file "${file}"`);
|
|
}
|
|
const output = result.stdout.trim();
|
|
const regex = /^(?<timestamp>\d+)$/;
|
|
const match = output.match(regex);
|
|
|
|
if (!match?.groups?.timestamp) {
|
|
throw new Error(`Failed to validate the timestamp for file "${file}"`);
|
|
}
|
|
|
|
const timestamp = Number(match.groups.timestamp);
|
|
const date = new Date(timestamp * 1000);
|
|
return date;
|
|
}
|