30 lines
868 B
JavaScript
30 lines
868 B
JavaScript
import { writeFileSync, mkdirSync } from 'node:fs';
|
|
import { execSync } from 'node:child_process';
|
|
import { resolve } from 'node:path';
|
|
|
|
function safe(cmd, fallback = '') {
|
|
try {
|
|
return execSync(cmd, { stdio: ['ignore', 'pipe', 'ignore'] })
|
|
.toString()
|
|
.trim();
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
const now = new Date().toISOString();
|
|
const commit = safe('git rev-parse --short HEAD', 'local');
|
|
const branch = safe('git rev-parse --abbrev-ref HEAD', '');
|
|
const buildId = `${now.replace(/[-:TZ.]/g, '').slice(0, 14)}.${commit}`;
|
|
|
|
const payload = {
|
|
version: now, // 比對用(每次 build 都不同)
|
|
commit,
|
|
branch,
|
|
buildId
|
|
};
|
|
|
|
mkdirSync(resolve('public'), { recursive: true });
|
|
writeFileSync(resolve('public/version.json'), JSON.stringify(payload, null, 2), 'utf-8');
|
|
console.log('[write-version] public/version.json ->', payload);
|