59 lines
1.9 KiB
JavaScript
Executable File
59 lines
1.9 KiB
JavaScript
Executable File
/**
|
||
* 生产 11 位构建号:YYYYMMDD(8)+ 当日第几次构建(3 位 001~999)
|
||
* 状态存 build/build-sequence.json,结果写 src/config/buildId.json
|
||
* 在 npm prebuild 时执行(仅正式 vite build 前,dev 不跑)
|
||
*/
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||
const root = path.resolve(__dirname, '..')
|
||
const statePath = path.join(root, 'build', 'build-sequence.json')
|
||
const outPath = path.join(root, 'src', 'config', 'buildId.json')
|
||
|
||
const now = new Date()
|
||
const y = now.getFullYear()
|
||
const m = String(now.getMonth() + 1).padStart(2, '0')
|
||
const d = String(now.getDate()).padStart(2, '0')
|
||
const ymd8 = `${y}${m}${d}`
|
||
const todayKey = `${y}-${m}-${d}`
|
||
|
||
let state = { date: todayKey, daySeq: 0 }
|
||
if (fs.existsSync(statePath)) {
|
||
try {
|
||
const prev = JSON.parse(fs.readFileSync(statePath, 'utf8'))
|
||
if (prev && typeof prev === 'object') {
|
||
state.date = prev.date || todayKey
|
||
state.daySeq = Number.isFinite(prev.daySeq) ? prev.daySeq : 0
|
||
}
|
||
} catch { /* 忽略损坏文件 */ }
|
||
}
|
||
|
||
if (state.date === todayKey) {
|
||
state.daySeq = (state.daySeq || 0) + 1
|
||
} else {
|
||
state.date = todayKey
|
||
state.daySeq = 1
|
||
}
|
||
if (state.daySeq > 999) {
|
||
console.warn('[bump-build-id] 当日构建已超过 999 次,已钳位为 999')
|
||
state.daySeq = 999
|
||
}
|
||
|
||
const buildId = `${ymd8}${String(state.daySeq).padStart(3, '0')}`
|
||
|
||
fs.mkdirSync(path.dirname(statePath), { recursive: true })
|
||
fs.writeFileSync(statePath, JSON.stringify(state, null, 2), 'utf8')
|
||
fs.mkdirSync(path.dirname(outPath), { recursive: true })
|
||
fs.writeFileSync(
|
||
outPath,
|
||
JSON.stringify(
|
||
{ buildId, generatedAt: new Date().toISOString() },
|
||
null,
|
||
2,
|
||
),
|
||
'utf8',
|
||
)
|
||
console.log(`[bump-build-id] buildId=${buildId}`)
|