|
| 1 | +import { compare as semverCompare, parse as semverParse } from "npm:semver"; |
| 2 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Syncs the version between package.json and a version file, |
| 6 | + * using the higher version number |
| 7 | + */ |
| 8 | +export function syncLoroVersion( |
| 9 | + packageJsonPath: string, |
| 10 | + versionFilePath: string, |
| 11 | + checkVersion: string = "", |
| 12 | +) { |
| 13 | + // Read package.json |
| 14 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); |
| 15 | + const packageVersion = packageJson.version; |
| 16 | + |
| 17 | + // Read version file |
| 18 | + const versionFileContent = readFileSync(versionFilePath, "utf-8"); |
| 19 | + const versionFileVersion = versionFileContent.trim(); |
| 20 | + |
| 21 | + // Parse and compare versions |
| 22 | + const parsedPackageVersion = semverParse(packageVersion); |
| 23 | + const parsedFileVersion = semverParse(versionFileVersion); |
| 24 | + |
| 25 | + if (!parsedPackageVersion || !parsedFileVersion) { |
| 26 | + throw new Error("Invalid version format found"); |
| 27 | + } |
| 28 | + |
| 29 | + // Compare versions |
| 30 | + const comparison = semverCompare(packageVersion, versionFileVersion); |
| 31 | + |
| 32 | + if (comparison > 0) { |
| 33 | + // package.json version is higher |
| 34 | + writeFileSync(versionFilePath, packageVersion); |
| 35 | + console.log(`Updated version file to ${packageVersion}`); |
| 36 | + if (checkVersion && checkVersion !== packageVersion) { |
| 37 | + throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`) |
| 38 | + } |
| 39 | + } else if (comparison < 0) { |
| 40 | + // version file version is higher |
| 41 | + packageJson.version = versionFileVersion; |
| 42 | + writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n"); |
| 43 | + console.log(`Updated package.json to ${versionFileVersion}`); |
| 44 | + if (checkVersion && checkVersion !== versionFileVersion) { |
| 45 | + throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`) |
| 46 | + } |
| 47 | + } else { |
| 48 | + console.log("Versions are already in sync"); |
| 49 | + if (checkVersion && checkVersion !== versionFileVersion) { |
| 50 | + throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +export function runSyncLoroVersion(checkVersion: string = "") { |
| 56 | + syncLoroVersion( |
| 57 | + "./crates/loro-wasm/package.json", |
| 58 | + "./crates/loro-internal/VERSION", |
| 59 | + checkVersion |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +if (import.meta.main) { |
| 64 | + runSyncLoroVersion(); |
| 65 | +} |
0 commit comments