Files
Melo/publish/utils/updateChangeLog.js
2025-11-29 16:27:28 +08:00

61 lines
2.0 KiB
JavaScript

const fs = require('fs')
const { jp, formatTime } = require('./index')
const pkgDir = '../../package.json'
const pkg = require(pkgDir)
const version = require('../version.json')
const chalk = require('chalk')
const pkg_bak = JSON.stringify(pkg, null, 2)
const version_bak = JSON.stringify(version, null, 2)
const changelogPath = jp('../../CHANGELOG.md')
const { parseChangelog } = require('./parseChangelog')
// const md_renderer = markdownStr => new (require('markdown-it'))({
// html: true,
// linkify: true,
// typographer: true,
// breaks: true,
// }).render(markdownStr)
const getPrevVer = () => parseChangelog(fs.readFileSync(changelogPath, 'utf-8').toString()).then(versions => {
if (!versions.length) throw new Error('CHANGELOG 无法解析到版本号')
return versions[0].version
})
const updateChangeLog = async(newVerNum, newChangeLog) => {
let changeLog = fs.readFileSync(changelogPath, 'utf-8')
const prevVer = await getPrevVer()
const log = `## [${newVerNum}](${pkg.repository.url.replace(/^git\+(http.+)\.git$/, '$1')}/compare/v${prevVer}...v${newVerNum}) - ${formatTime()}\n\n${newChangeLog}`
fs.writeFileSync(changelogPath, changeLog.replace(/(## \[(?:\d+\.))/, log + '\n$1'), 'utf-8')
}
// const renderChangeLog = md => md_renderer(md)
module.exports = async newVerNum => {
if (!newVerNum) newVerNum = pkg.version
const newMDChangeLog = fs.readFileSync(jp('../changeLog.md'), 'utf-8')
// const newChangeLog = renderChangeLog(newMDChangeLog)
version.history.unshift({
version: version.version,
desc: version.desc,
})
version.version = newVerNum
version.desc = newMDChangeLog.replace(/(?:^|(\n))#{1,6} (.+)\n/g, '$1$2').trim()
pkg.version = newVerNum
console.log(chalk.blue('new version: ') + chalk.green(newVerNum))
fs.writeFileSync(jp('../version.json'), JSON.stringify(version) + '\n', 'utf-8')
fs.writeFileSync(jp(pkgDir), JSON.stringify(pkg, null, 2) + '\n', 'utf-8')
await updateChangeLog(newVerNum, newMDChangeLog)
return {
pkg_bak,
version_bak,
// changeLog: newChangeLog,
}
}