|
| 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'); |
0 commit comments