Skip to content

Commit

Permalink
Fix changelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 17, 2024
1 parent eb58131 commit b8d8ff6
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 499 deletions.
143 changes: 7 additions & 136 deletions apps/website/changelog/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import glob from 'glob';
import { readFile, writeFile } from 'node:fs/promises';
import { promisify } from 'node:util';
import { markdown } from 'markdown';
import { format } from 'prettier';
import { NodeHtmlMarkdown } from 'node-html-markdown';
import { parseSemVer, compareSemVer } from 'semver-parser';
import { resolve } from 'node:path';

import { excludeTrashUpdates, groupByVersions } from './lib.mjs';

const files = await promisify(glob)(
'../../{packages,deleted_packages}/*/CHANGELOG.md',
{
Expand All @@ -24,142 +19,18 @@ const changelogs = await Promise.all(
)
);

for (const [release, changelog] of mergeChangelogs(changelogs).entries()) {
const md = await renderChangelog(changelog);

const releaseFile = release.replaceAll('.', '-');
const filePath = resolve('docs/releases', `${releaseFile}.changelog.md`);
for (const { name, content } of changelogs) {
const filePath = resolve('docs', name, 'CHANGELOG.md');

await writeFile(filePath, md);
await writeFile(filePath, content);
}

// --- // ---

async function renderChangelog(tree) {
return makeLinksOnCommits(
await format(
NodeHtmlMarkdown.translate(
markdown.renderJsonML(markdown.toHTMLTree(tree))
),
{
parser: 'markdown',
}
)
);
}

function mergeChangelogs(packages) {
const releases = new Set(
Object.values(packages)
.flatMap(({ changes }) => Object.keys(changes))
.map(getRelease)
);

const log = new Map();

for (const release of releases) {
const currentLog = [[]];

const relatedChanges = packages
.map(({ name, changes }) => ({
name,
changes: Object.fromEntries(
Object.entries(changes).filter(
([version]) => getRelease(version) === release
)
),
}))
.filter(({ changes }) => Object.keys(changes).length > 0);

currentLog.push(['header', { level: 2 }, 'Full changelog']);
for (const { version, packages } of groupByVersions(relatedChanges).sort(
({ version: v1 }, { version: v2 }) => -compareSemVer(v1, v2)
)) {
const logForVersion = [];
for (const { name: packageName, changes: packageChanges } of packages) {
const pacakgeChangesEntries = Object.entries(packageChanges)
.map(([type, items]) => [type, excludeTrashUpdates(items)])
.filter(([, items]) => items.length > 0);

let hasChanges = pacakgeChangesEntries.length > 0;

if (!hasChanges) {
continue;
}

logForVersion.push(['para', `::: details ${packageName}`]);

for (const [type, items] of pacakgeChangesEntries) {
logForVersion.push(['para', ['strong', type]], ...items);
}

logForVersion.push(['para', ':::']);
}

if (logForVersion.length > 0) {
currentLog.push(['header', { level: 3 }, version]);
currentLog.push(...logForVersion);
}
}

log.set(release, currentLog);
}

return log;
}

async function parseChangelog(md) {
const [_1, header, ...rest] = markdown.parse(md);
async function parseChangelog(content) {
const [_1, header] = markdown.parse(content);

const name = header.at(2);

const versions = groupByLevel(2, rest);

const changes = {};
for (const [version, content] of Object.entries(versions)) {
changes[version] = groupByLevel(3, content);
}

return { name, changes };
}

function groupByLevel(targetLevel, data) {
const groups = {};

let currentGroup;
for (const item of data) {
const { level } = item.at(1);

if (level === targetLevel) {
const group = item.at(2);
currentGroup = group;
} else {
if (!groups[currentGroup]) {
continue;
}
groups[currentGroup].push(item);
}
}

return groups;
}

function getRelease(version) {
const { major, minor } = parseSemVer(version);

return `${major}.${minor}`;
}

function makeLinksOnCommits(content) {
function linkToCommit(commitHash) {
return `[${commitHash}](https://github.com/igorkamyshev/farfetched/commit/${commitHash})`;
}

function replacer(all, commitHash) {
return all.replace(commitHash, linkToCommit(commitHash));
}
const name = header.at(2).replace('@withease/', '');

return content
.replaceAll(/- ([0-9a-f]{7}):/gi, replacer)
.replaceAll(/\\\[([0-9a-f]{7})\\\]/gi, replacer);
return { name, content };
}
40 changes: 0 additions & 40 deletions apps/website/changelog/lib.mjs

This file was deleted.

Loading

0 comments on commit b8d8ff6

Please sign in to comment.