|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 4 | +import { dirname, resolve } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +// Anchor paths relative to repo root so the script works from any cwd. |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = dirname(__filename); |
| 10 | +const repoRoot = resolve(__dirname, '..'); |
| 11 | + |
| 12 | +// Matches the exported version constant in packages/constants/src/lib/version.ts. |
| 13 | +const VERSION_EXPORT_PATTERN = /export const version = ['"]([^'\"]+)['"];?/; |
| 14 | + |
| 15 | +async function main() { |
| 16 | + const litClientPackageJsonPath = resolve( |
| 17 | + repoRoot, |
| 18 | + 'packages', |
| 19 | + 'lit-client', |
| 20 | + 'package.json' |
| 21 | + ); |
| 22 | + const versionFilePath = resolve( |
| 23 | + repoRoot, |
| 24 | + 'packages', |
| 25 | + 'constants', |
| 26 | + 'src', |
| 27 | + 'lib', |
| 28 | + 'version.ts' |
| 29 | + ); |
| 30 | + |
| 31 | + const litClientPackageJson = JSON.parse( |
| 32 | + await readFile(litClientPackageJsonPath, 'utf8') |
| 33 | + ); |
| 34 | + const litClientVersion = litClientPackageJson.version; |
| 35 | + |
| 36 | + if (!litClientVersion) { |
| 37 | + throw new Error(`Version not found in ${litClientPackageJsonPath}`); |
| 38 | + } |
| 39 | + |
| 40 | + const versionFileContents = await readFile(versionFilePath, 'utf8'); |
| 41 | + const match = versionFileContents.match(VERSION_EXPORT_PATTERN); |
| 42 | + |
| 43 | + if (!match) { |
| 44 | + throw new Error( |
| 45 | + `Could not find exported version constant in ${versionFilePath}` |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + const currentVersion = match[1]; |
| 50 | + |
| 51 | + if (currentVersion === litClientVersion) { |
| 52 | + console.log(`version.ts already in sync (${currentVersion})`); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Rewrite the version export so it mirrors the lit-client package version. |
| 57 | + const updatedContents = versionFileContents.replace( |
| 58 | + VERSION_EXPORT_PATTERN, |
| 59 | + `export const version = '${litClientVersion}';` |
| 60 | + ); |
| 61 | + |
| 62 | + await writeFile(versionFilePath, updatedContents); |
| 63 | + console.log(`Updated version.ts to ${litClientVersion}`); |
| 64 | +} |
| 65 | + |
| 66 | +main().catch((error) => { |
| 67 | + console.error(error); |
| 68 | + process.exitCode = 1; |
| 69 | +}); |
0 commit comments