Skip to content

Commit

Permalink
Fixed tool to extract changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Jul 9, 2021
1 parent 9c63d0b commit d6127d8
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions scripts/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,60 @@ program
.replace(/{PREVIOUS_VERSION}/g, previousVersion)
.replace(/{CURRENT_VERSION}/g, nextVersion);
// Create a header
const header = `## Release ${nextVersion.substring(1)} (${moment().format('YYYY-MM-DD')})`;
const header = `## Release ${nextVersion.substring(1)} (${moment().format(
'YYYY-MM-DD',
)})`;
// Write back the changelog
const changeLogTransformed = `${introduction}${header}\n\n${releaseNotesTransformed}\n\n${existingReleases}`;
fs.writeFileSync(changeLogPath, changeLogTransformed);
});

program
.command('extract-release-notes <dest>')
.action((destinationPath) => {
// Read the content of the release notes
const changeLog = fs.readFileSync(changeLogPath, 'utf8');
// TODO: Parse the markdown and extract the changelog
const releaseNotes = remark()
.use(() => {
// Find the index of headers in a list of children
function findHeadings(children, depth) {
const result = [];
// Loop through each child and add the right nodes to the results
children.forEach((c, index) => {
if (c.type === 'heading' && c.depth === depth) {
result.push(index);
}
});
// Return the list of heading indices
return result;
}

return function transformer(tree, file) {
const headingsIndex = findHeadings(tree.children, 2);
if (headingsIndex.length < 1) {
throw new Error("Expected at least one release in the changelog");
program.command('extract-release-notes <dest>').action(destinationPath => {
// Read the content of the release notes
const changeLog = fs.readFileSync(changeLogPath, 'utf8');
// TODO: Parse the markdown and extract the changelog
const releaseNotes = remark()
.use(() => {
// Find the index of headers in a list of children
function findHeadings(children, depth) {
const result = [];
// Loop through each child and add the right nodes to the results
children.forEach((c, index) => {
if (c.type === 'heading' && c.depth === depth) {
result.push(index);
}
// The subtree related to the latest release, exluding the depth 1 headers
const releaseChildren = tree.children.slice(headingsIndex[0] + 1, headingsIndex[1]);
// Trim out everything internal
const internalHeaderIndex = releaseChildren
.findIndex(n => n.type === 'heading' && n.children.find(
c => c.type === 'text' && c.value === 'Internals'
));
const trimmedReleaseChildren = internalHeaderIndex === -1 ?
releaseChildren :
releaseChildren.slice(0, internalHeaderIndex);
// Replace the children
tree.children = trimmedReleaseChildren;
});
// Return the list of heading indices
return result;
}

return function transformer(tree, file) {
const headingsIndex = findHeadings(tree.children, 2);
if (headingsIndex.length < 1) {
throw new Error('Expected at least one release in the changelog');
}
})
.processSync(changeLog);
fs.writeFileSync(destinationPath, releaseNotes);
});
// The subtree related to the latest release, exluding the depth 1 headers
const releaseChildren = tree.children.slice(
headingsIndex[0] + 1,
headingsIndex[1],
);
// Trim out everything internal
const internalHeaderIndex = releaseChildren.findIndex(
n =>
n.type === 'heading' &&
n.children.find(c => c.type === 'text' && c.value === 'Internals'),
);
const trimmedReleaseChildren =
internalHeaderIndex === -1
? releaseChildren
: releaseChildren.slice(0, internalHeaderIndex);
// Replace the children
tree.children = trimmedReleaseChildren;
};
})
.processSync(changeLog)
.toString();
fs.writeFileSync(destinationPath, releaseNotes);
});

program.parse(process.argv);

0 comments on commit d6127d8

Please sign in to comment.