import fs from 'fs'
import lodash from 'lodash'
const _path = process.cwd()
const _logPath = `${_path}/plugins/miao-plugin/CHANGELOG.md`
let logs = {}
let changelogs = []
let currentVersion
let versionCount = 4
let packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const getLine = function (line) {
line = line.replace(/(^\s*\*|\r)/g, '')
line = line.replace(/\s*`([^`]+`)/g, '$1')
line = line.replace(/`\s*/g, '')
line = line.replace(/\s*\*\*([^\*]+\*\*)/g, '$1')
line = line.replace(/\*\*\s*/g, '')
line = line.replace(/ⁿᵉʷ/g, '')
return line
}
try {
if (fs.existsSync(_logPath)) {
logs = fs.readFileSync(_logPath, 'utf8') || ''
logs = logs.split('\n')
let temp = {}
let lastLine = {}
lodash.forEach(logs, (line) => {
if (versionCount <= -1) {
return false
}
let versionRet = /^#\s*([0-9a-zA-Z\\.~\s]+?)\s*$/.exec(line)
if (versionRet && versionRet[1]) {
let v = versionRet[1].trim()
if (!currentVersion) {
currentVersion = v
} else {
changelogs.push(temp)
if (/0\s*$/.test(v) && versionCount > 0) {
versionCount = 0
} else {
versionCount--
}
}
temp = {
version: v,
logs: []
}
} else {
if (!line.trim()) {
return
}
if (/^\*/.test(line)) {
lastLine = {
title: getLine(line),
logs: []
}
temp.logs.push(lastLine)
} else if (/^\s{2,}\*/.test(line)) {
lastLine.logs.push(getLine(line))
}
}
})
}
} catch (e) {
// do nth
}
const yunzaiVersion = packageJson.version
const isV3 = yunzaiVersion[0] === '3'
let Version = {
isV3,
get version () {
return currentVersion
},
get yunzai () {
return yunzaiVersion
},
get changelogs () {
return changelogs
},
runtime () {
console.log(`未能找到e.runtime,请升级至最新版${isV3 ? 'V3' : 'V2'}-Yunzai以使用miao-plugin`)
}
}
export default Version