Files
xiaojibeier 0e21232b38 create
2026-07-13 16:45:11 +08:00

57 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 生产 11 位构建号:YYYYMMDD(8)+ 当日第几次构建(3 位 001~999)
*/
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}`)