cloud_bbb/scripts/imports/changelog.js

151 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-02-01 10:57:41 +01:00
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const simpleGit = require('simple-git');
2021-02-01 10:57:41 +01:00
const inquirer = require('inquirer');
const git = simpleGit();
2021-02-24 15:45:55 +01:00
async function generateChangelog(version) {
const latestTag = (await git.tags()).latest;
const title = `v${version}` === latestTag ? '[Unreleased]' : `${version} (${new Date().toISOString().split('T')[0]})`;
const logs = await git.log({
from: latestTag,
to: 'HEAD',
});
const sections = [{
type: 'feat',
label: 'Added',
}, {
type: 'fix',
label: 'Fixed',
}];
const entries = {};
logs.all.forEach(log => {
const match = log.message.match(/^([a-z]+)(?:\((\w+)\))?: (.+)/);
if (!match) {
return;
}
const [, type, scope, description] = match;
2021-02-24 15:45:55 +01:00
const entry = { type, scope, description, issues: [] };
if(log.body) {
const matches = log.body.match(/(?:fix|fixes|closes?|refs?) #(\d+)/g) || [];
for (const match of matches) {
2021-02-24 15:45:55 +01:00
const [, number] = match.match(/(\d+)$/);
entry.issues.push(number);
}
}
if (!entries[type]) {
entries[type] = [];
}
entries[type].push(entry);
});
let changeLog = `## ${title}\n`;
function stringifyEntry(entry) {
const issues = entry.issues.map(issue => {
return `[#${issue}](https://github.com/sualko/cloud_bbb/issues/${issue})`;
}).join('');
return `- ${issues}${issues.length > 0 ? ' ' : ''}${entry.description}\n`;
}
sections.forEach(section => {
if (!entries[section.type]) {
return;
}
changeLog += `### ${section.label}\n`;
entries[section.type].forEach(entry => {
changeLog += stringifyEntry(entry);
});
delete entries[section.type];
changeLog += '\n';
});
const miscKeys = Object.keys(entries);
if (miscKeys && miscKeys.length > 0) {
changeLog += '### Misc\n';
miscKeys.forEach(type => {
entries[type].forEach(entry => {
changeLog += stringifyEntry(entry);
});
});
}
return changeLog;
}
2021-02-24 15:45:55 +01:00
async function editChangeLog(changeLog) {
const answers = await inquirer.prompt([{
type: 'editor',
name: 'changeLog',
message: 'You have now the possibility to edit the change log',
default: changeLog,
}]);
return answers.changeLog;
}
2021-02-24 15:45:55 +01:00
async function hasChangeLogEntry(version) {
2021-02-01 10:57:41 +01:00
if (!version) {
return false;
}
const entry = await getChangelogEntry(version);
2021-02-01 10:57:41 +01:00
if (entry.split('\n').filter(line => !!line.trim()).length < 2) {
throw `Found no change log entry for ${version}`;
}
return true;
}
2021-02-24 15:45:55 +01:00
function getChangelogEntry(version) {
return new Promise(resolve => {
2021-02-01 10:57:41 +01:00
fs.readFile(path.join(__dirname, '..', '..', 'CHANGELOG.md'), 'utf8', function (err, data) {
if (err) throw err;
const releaseHeader = /^\d+\.\d+\.\d+$/.test(version) ? `## ${version}` : '## [Unreleased]';
const lines = data.split('\n');
2021-02-24 15:45:55 +01:00
const entry = [];
let inEntry = false;
for(const line of lines) {
if (line.startsWith(releaseHeader)) {
inEntry = true;
} else if (line.startsWith('## ') && entry.length > 0) {
inEntry = false;
break;
}
if (inEntry) {
entry.push(line);
}
}
resolve(entry.join('\n'));
});
});
}
2021-02-24 15:45:55 +01:00
2021-02-24 17:36:08 +01:00
module.exports = {generateChangelog, editChangeLog, hasChangeLogEntry, getChangelogEntry};