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/promise');
|
|
|
|
const inquirer = require('inquirer');
|
2021-01-31 18:40:35 +01:00
|
|
|
|
|
|
|
const git = simpleGit();
|
|
|
|
|
2021-02-24 15:45:55 +01:00
|
|
|
async function generateChangelog(version) {
|
2021-01-31 18:40:35 +01:00
|
|
|
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: [] };
|
2021-01-31 18:40:35 +01:00
|
|
|
|
|
|
|
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+)$/);
|
2021-01-31 18:40:35 +01:00
|
|
|
|
|
|
|
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) {
|
2021-01-31 18:40:35 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:40:35 +01:00
|
|
|
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-01-31 18:40:35 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-01-31 18:40:35 +01:00
|
|
|
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 = [];
|
2021-01-31 18:40:35 +01:00
|
|
|
|
|
|
|
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};
|