2
0
mirror of https://github.com/ctrlcvs/xiaoyao-cvs-plugin.git synced 2024-12-23 11:31:01 +08:00
xiaoyao-cvs-plugin/components/Changelog.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-07-24 11:46:05 +08:00
import fs from 'fs'
import lodash from 'lodash'
2022-07-24 11:46:05 +08:00
const _path = process.cwd()
const _logPath = `${_path}/plugins/xiaoyao-cvs-plugin/CHANGELOG.md`
2022-07-24 11:46:05 +08:00
let logs = {}
let changelogs = []
let currentVersion
let versionCount = 4
2022-07-24 11:46:05 +08:00
let packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const getLine = function (line) {
2022-07-24 11:46:05 +08:00
line = line.replace(/(^\s*\*|\r)/g, '')
line = line.replace(/\s*`([^`]+`)/g, '<span class="cmd">$1')
line = line.replace(/`\s*/g, '</span>')
2022-06-26 19:31:18 +08:00
line = line.replace(/\s*\*\*([^\*]+\*\*)/g, '<span class="strong">$1')
2022-07-24 11:46:05 +08:00
line = line.replace(/\*\*\s*/g, '</span>')
line = line.replace(/ⁿᵉʷ/g, '<span class="new"></span>')
return line
}
try {
if (fs.existsSync(_logPath)) {
2022-07-24 11:46:05 +08:00
logs = fs.readFileSync(_logPath, 'utf8') || ''
logs = logs.split('\n')
let temp = {};
let lastLine = {}
lodash.forEach(logs, (line) => {
if (versionCount <= -1) {
2022-07-24 11:46:05 +08:00
return false
}
2022-07-24 11:46:05 +08:00
let versionRet = /^#\s*([0-9\\.~\s]+?)\s*$/.exec(line)
if (versionRet && versionRet[1]) {
2022-07-24 11:46:05 +08:00
let v = versionRet[1].trim()
if (!currentVersion) {
2022-07-24 11:46:05 +08:00
currentVersion = v
} else {
2022-07-24 11:46:05 +08:00
changelogs.push(temp)
if (/0\s*$/.test(v) && versionCount > 0) {
2022-07-24 11:46:05 +08:00
versionCount = 0
} else {
2022-07-24 11:46:05 +08:00
versionCount--
}
}
temp = {
version: v,
logs: []
}
} else {
if (!line.trim()) {
2022-07-24 11:46:05 +08:00
return
}
if (/^\*/.test(line)) {
lastLine = {
title: getLine(line),
logs: []
}
2022-07-24 11:46:05 +08:00
temp.logs.push(lastLine)
} else if (/^\s{2,}\*/.test(line)) {
lastLine.logs.push(getLine(line))
}
}
2022-07-24 11:46:05 +08:00
})
}
} catch (e) {
// do nth
}
2022-07-24 11:46:05 +08:00
const yunzaiVersion = packageJson.version
2022-10-13 16:19:09 +08:00
const isV3 = yunzaiVersion[0]*1 > 2
2022-07-24 11:46:05 +08:00
export { currentVersion, yunzaiVersion, isV3, changelogs }