Skip to content

Commit

Permalink
feat(whats-new): add script for generating whats new pages
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Jun 17, 2024
1 parent 41d52e9 commit 25f0a38
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions scripts/create-whats-new/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dotenv from 'dotenv';
import { WhatsNewCreator } from './whats-new-creator.js';

dotenv.config({ path: '.env.local' });

const reactFlowCreator = await WhatsNewCreator({
site: 'reactflow.dev',
packageName: 'reactflow',
});
const reactFlowUpdated = await reactFlowCreator.start();

if (reactFlowUpdated) {
console.log('updated React Flow');
}

// no Svelte Flow releases yet
// const svelteFlowCreator = await WhatsNewCreator({
// site: 'svelteflow.dev',
// packageName: '@xyflow/svelte',
// });
// const svelteFlowUpdated = await svelteFlowCreator.start();

// if (svelteFlowUpdated) {
// console.log('updated Svelte Flow');
// }
20 changes: 20 additions & 0 deletions scripts/create-whats-new/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "create-whats-new",
"version": "1.0.0",
"private": true,
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "xyflow",
"license": "MIT",
"dependencies": {
"@actions/exec": "^1.1.1",
"dotenv": "^16.4.5",
"envfile": "^7.1.0",
"latest-version": "^9.0.0"
}
}
80 changes: 80 additions & 0 deletions scripts/create-whats-new/whats-new-creator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import { writeFile, stat } from 'fs/promises';

import latestVersion from 'latest-version';

const __dirname = dirname(fileURLToPath(import.meta.url));

/**
* This is a helper function for checking the latest version on npm against the version in the .env file for a specific site.
* You need to create the helper and then call the start method to check the version.
*/
export async function WhatsNewCreator({ site, packageName }) {
const sitePath = resolve(__dirname, `../../sites/${site}`);
const latestNpmVersion = await latestVersion(packageName);
const ghApiUrl = `https://api.github.com/repos/xyflow/xyflow/releases/tags/${latestNpmVersion}`;

async function getReleaseNotes() {
console.log('request release notes for', ghApiUrl);

const releaseNotes = await fetch(ghApiUrl, {
headers: {
Authorization: process.env.GITHUB_TOKEN,
Accept: 'application/vnd.github+json',
},
});
const releaseJson = await releaseNotes.json();

return releaseJson.body;
}

async function writeWhatsNew(fileName, content) {
console.log(`write whats new page for ${site}`);

const whatsNewPath = resolve(
sitePath,
'src',
'pages',
'whats-new',
`${fileName}.mdx`,
);

if (await fileExists(whatsNewPath)) {
console.log(
`Can't write file. What's new file ${fileName} already exists!`,
);
return null;
}

await writeFile(whatsNewPath, content);
return true;
}

function getMdContent(date, body) {
return `---
title: New Release ${latestNpmVersion}
description: What's new in ${packageName} ${latestNpmVersion}
authors: [moklick]
date: ${date}
---
# ${latestNpmVersion}
${body}`;
}

return {
async start() {
const releaseNotes = await getReleaseNotes();
const today = new Date().toISOString().slice(0, 10);
const mdContent = getMdContent(today, releaseNotes);
const createdWhatsNew = await writeWhatsNew(today, mdContent);

return createdWhatsNew;
},
};
}

const fileExists = async (pathName) =>
!!(await stat(pathName).catch((e) => false));

0 comments on commit 25f0a38

Please sign in to comment.