Skip to content

Instantly share code, notes, and snippets.

@hughrawlinson
Last active June 16, 2022 12:07
Show Gist options
  • Save hughrawlinson/0d4a0842e29ea2ec87b65e231d0c42ee to your computer and use it in GitHub Desktop.
Save hughrawlinson/0d4a0842e29ea2ec87b65e231d0c42ee to your computer and use it in GitHub Desktop.
Add dates to Wagtail release notes
/*
* version-date-mapping.json is generated with the following shell command (requires ripgrep and jq):
*
* cat CHANGELOG.txt | rg '(?P<v>\d\.(\d)?\d(\.\d\d?)?)( LTS)? \((?P<d>\d\d\.\d\d\.\d\d\d\d)\)' \
* -r '{"version":"$v", "date":"$d"}' --only-matching | jq --slurp . > version-date-mapping.json
*/
import { readFile, writeFile } from 'fs/promises';
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
function dateFormat(dateString) {
const [dayString, monthString, yearString] = dateString.split('.');
const date = new Date();
date.setDate(Number(dayString));
date.setMonth(Number(monthString) - 1);
date.setFullYear(Number(yearString));
return `${months[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`;
}
async function loadVersionMap() {
const versionAndDates = JSON.parse(
await readFile('version-date-mapping.json', 'utf-8'),
);
const versionMap = new Map();
versionAndDates.forEach((map) => {
const { version, date } = map;
versionMap.set(version, dateFormat(date));
});
return versionMap;
}
// Change this to "md" to add dates to the markdown files
const Extension = 'rst';
function insertDateIntoDocument(fileContents, date) {
if (Extension === 'rst') {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [headingLine, title, _, ...rest] = fileContents.split('\n');
return `${headingLine}\n${title}\n${headingLine}\n\n*${date}*\n${rest.join(
'\n',
)}`;
}
if (Extension === 'md') {
const [title, ...rest] = fileContents.split('\n');
return `${title}\n\n*${date}*\n${rest.join('\n')}`;
}
throw new Error(`Unsupported extension: ${Extension}`);
}
async function main() {
const versionMap = await loadVersionMap();
versionMap.forEach(([version, date]) => {
try {
const fileContents = (
await readFile(`docs/releases/${version}.${Extension}`, 'utf-8')
).toString();
await writeFile(
`docs/releases/${version}.${Extension}`,
insertDateIntoDocument(fileContents, date),
);
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Could not find file for version ${version}`);
}
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment