2019-01-12 11:33:27 +08:00
|
|
|
const fs = require('fs');
|
2018-03-18 19:59:38 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
module.exports = function getChangelog(file, version) {
|
|
|
|
const lines = fs
|
|
|
|
.readFileSync(file)
|
|
|
|
.toString()
|
|
|
|
.split('\n');
|
|
|
|
const changeLog = [];
|
|
|
|
const startPattern = new RegExp(`^## ${version}`);
|
|
|
|
const stopPattern = /^## /; // 前一个版本
|
|
|
|
const skipPattern = /^`/; // 日期
|
|
|
|
let begin = false;
|
2018-03-18 19:59:38 +08:00
|
|
|
for (let i = 0; i < lines.length; i += 1) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const line = lines[i];
|
2018-03-18 19:59:38 +08:00
|
|
|
if (begin && stopPattern.test(line)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
break;
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
if (begin && line && !skipPattern.test(line)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
changeLog.push(line);
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
if (!begin) {
|
2019-01-12 11:33:27 +08:00
|
|
|
begin = startPattern.test(line);
|
2018-03-18 19:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return changeLog.join('\n');
|
|
|
|
};
|