Files

58 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

// 從 git log 產生更新紀錄資料,輸出到 src/data/changelog.json
// 正式版(build 後)沒有 git 也能直接讀 JSON 顯示;dev 與 build 前會自動重新產生。
import { execFileSync } from 'node:child_process'
import { mkdirSync, writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const here = dirname(fileURLToPath(import.meta.url))
const outFile = resolve(here, '../src/data/changelog.json')
const UNIT = '\x1f' // 欄位分隔(unit separator
const RECORD = '\x1e' // 紀錄分隔(record separator
function readGitLog() {
const format = ['%H', '%h', '%ad', '%s', '%b'].join(UNIT) + RECORD
const raw = execFileSync(
'git',
['log', `--pretty=format:${format}`, '--date=format:%Y-%m-%d'],
{ cwd: resolve(here, '..'), encoding: 'utf8', maxBuffer: 1024 * 1024 * 16 },
)
return raw
.split(RECORD)
.map((chunk) => chunk.replace(/^\s+/, ''))
.filter(Boolean)
.map((chunk) => {
const [hash, shortHash, date, subject, body = ''] = chunk.split(UNIT)
return {
hash,
shortHash,
date,
subject: subject.trim(),
body: body.trim(),
}
})
}
function main() {
let entries = []
try {
entries = readGitLog()
} catch (error) {
console.warn('[generate-changelog] 讀取 git log 失敗,輸出空清單:', error.message)
}
const payload = {
generatedAt: new Date().toISOString(),
entries,
}
mkdirSync(dirname(outFile), { recursive: true })
writeFileSync(outFile, JSON.stringify(payload, null, 2) + '\n', 'utf8')
console.log(`[generate-changelog] 已輸出 ${entries.length} 筆更新紀錄到 ${outFile}`)
}
main()