Skip to content

Commit e9edbb2

Browse files
committed
feat: port compatibility table script to NodeJS
1 parent fdc1667 commit e9edbb2

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

.github/workflows/compatibility.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ jobs:
2525
path: dependency
2626
fetch-depth: 0
2727

28-
- name: Setup Python
29-
uses: actions/setup-python@v5
28+
- name: Setup Node.js
29+
uses: actions/setup-node@v4
3030
with:
31-
python-version: '3.13'
31+
node-version: "22.x"
32+
33+
- name: Install dependencies
34+
uses: borales/actions-yarn@v5
35+
with:
36+
cmd: install
3237

3338
- name: Update compatibility table
34-
run: python ./source/compatibility-table.py --source-version v${{ needs.versioning.outputs.next-version }} -d ./dependency -r ./source/README.md
39+
run: node ./source/compatibility-table.mjs --source-version v${{ needs.versioning.outputs.next-version }} -d ./dependency -r ./source/README.md
3540

3641
- name: Commit changes
3742
working-directory: ./source

compatibility-table.mjs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
import { execSync } from 'child_process';
6+
import { Command } from 'commander';
7+
8+
const program = new Command();
9+
10+
program
11+
.description('Update the compatibility table in a README file by adding a new row based on repo versions')
12+
.option('-s, --source-repo <path>', 'Source git repository')
13+
.option('--source-version <version>', 'Source version')
14+
.option('-r, --readme <file>', 'Readme file to update', './README.md')
15+
.option('-d, --dependency-repo <path>', 'Dependency git repository')
16+
.option('--dependency-version <version>', 'Dependency version');
17+
18+
program.parse();
19+
20+
const options = program.opts();
21+
22+
if (!options.sourceRepo && !options.sourceVersion && !options.dependencyRepo && !options.dependencyVersion) {
23+
program.help();
24+
}
25+
26+
if (!fs.existsSync(options.readme)) {
27+
console.error(`Cannot find readme file: "${options.readme}"`);
28+
process.exit(1);
29+
}
30+
31+
function getVersionFromGitRepo(repoPath) {
32+
if (!fs.existsSync(repoPath)) {
33+
console.error('Source repo cannot be found');
34+
process.exit(1);
35+
}
36+
if (!fs.statSync(repoPath).isDirectory()) {
37+
console.error('Source repo is not a directory');
38+
process.exit(1);
39+
}
40+
if (!fs.existsSync(path.join(repoPath, '.git'))) {
41+
console.error('Source repo does not contain a .git directory');
42+
process.exit(1);
43+
}
44+
45+
try {
46+
const output = execSync('git describe --tags --abbrev=0', { cwd: repoPath, encoding: 'utf8' });
47+
return output.trim();
48+
} catch (error) {
49+
console.error(`Getting latest tag from git repo "${repoPath}" failed:`, error.message);
50+
process.exit(1);
51+
}
52+
}
53+
54+
let sourceVersion = options.sourceVersion;
55+
if (!sourceVersion) {
56+
sourceVersion = getVersionFromGitRepo(options.sourceRepo);
57+
}
58+
console.log(`Source version: ${sourceVersion}`);
59+
60+
let dependencyVersion = options.dependencyVersion;
61+
if (!dependencyVersion) {
62+
dependencyVersion = getVersionFromGitRepo(options.dependencyRepo);
63+
}
64+
console.log(`Dependency version: ${dependencyVersion}`);
65+
66+
const readmeLines = fs.readFileSync(options.readme, 'utf-8').split(/\r?\n/);
67+
68+
const tableLineIndex = readmeLines.findIndex((line) => /<!-- COMPATIBILITY_TABLE skip:\d+ -->/.test(line));
69+
70+
if (tableLineIndex === -1) {
71+
console.error('Compatibility table marker not found in the README.');
72+
process.exit(1);
73+
}
74+
75+
console.log(`Found compatibility marker on line ${tableLineIndex + 1} in ${options.readme}`);
76+
77+
const markerLine = readmeLines[tableLineIndex];
78+
const markerMatch = markerLine.match(/<!-- COMPATIBILITY_TABLE skip:(\d+)/);
79+
80+
if (!markerMatch) {
81+
console.error('Failed to parse compatibility table marker.');
82+
process.exit(1);
83+
}
84+
85+
const skipLines = parseInt(markerMatch[1], 10);
86+
const addLineIndex = tableLineIndex + skipLines + 1;
87+
88+
const lastLine = readmeLines[addLineIndex];
89+
if (lastLine && lastLine.startsWith('|')) {
90+
const lastVersion = lastLine.slice(2).split(' | ')[0];
91+
if (lastVersion === sourceVersion) {
92+
console.log(`Most recent version in compatibility table is already ${lastVersion}`);
93+
process.exit(1);
94+
}
95+
}
96+
97+
const newLine = `| ${sourceVersion} | ${dependencyVersion}+ |`;
98+
console.log(`Insert "${newLine}" at line ${addLineIndex} in ${options.readme}`);
99+
100+
readmeLines.splice(addLineIndex, 0, newLine);
101+
fs.writeFileSync(options.readme, readmeLines.join('\n'), 'utf-8');
102+
103+
console.log('Finish');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@primevue/themes": "^4.3.2",
2323
"@tailwindcss/postcss": "^4.1.4",
2424
"@vee-validate/yup": "^4.15.0",
25+
"commander": "^14.0.0",
2526
"cronstrue": "^2.59.0",
2627
"dotenv": "^16.5.0",
2728
"pinia": "^3.0.2",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ commander@^13.1.0:
16761676
resolved "https://registry.yarnpkg.com/commander/-/commander-13.1.0.tgz#776167db68c78f38dcce1f9b8d7b8b9a488abf46"
16771677
integrity sha512-/rFeCpNJQbhSZjGVwO9RFV3xPqbnERS8MmIQzCtD/zl6gpJuV/bMLuN92oG3F7d8oDEHHRrujSXNUr8fpjntKw==
16781678

1679+
commander@^14.0.0:
1680+
version "14.0.0"
1681+
resolved "https://registry.yarnpkg.com/commander/-/commander-14.0.0.tgz#f244fc74a92343514e56229f16ef5c5e22ced5e9"
1682+
integrity sha512-2uM9rYjPvyq39NwLRqaiLtWHyDC1FvryJDa2ATTVims5YAS4PupsEQsDvP14FqhFr0P49CYDugi59xaxJlTXRA==
1683+
16791684
commands-events@1.0.4:
16801685
version "1.0.4"
16811686
resolved "https://registry.yarnpkg.com/commands-events/-/commands-events-1.0.4.tgz#772123d7f175b2984474bce37a722f4c2d3d2830"

0 commit comments

Comments
 (0)