|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | + |
| 4 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 5 | +import { dirname, resolve } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +export interface CorePackageJson { |
| 9 | + name: string; |
| 10 | + version: string; |
| 11 | + catalyst?: { version: string; ref: string }; |
| 12 | + [key: string]: unknown; |
| 13 | +} |
| 14 | + |
| 15 | +// `catalyst.version` mirrors the released version; `catalyst.ref` is the git tag |
| 16 | +// (`@bigcommerce/catalyst-core@<version>`) the version corresponds to, consumed |
| 17 | +// by the future `catalyst upgrade` command. |
| 18 | +export function buildCatalystField( |
| 19 | + pkg: Pick<CorePackageJson, "name" | "version">, |
| 20 | +): { version: string; ref: string } { |
| 21 | + return { version: pkg.version, ref: `${pkg.name}@${pkg.version}` }; |
| 22 | +} |
| 23 | + |
| 24 | +// Returns the package.json text with its `catalyst` field synced to `version`. |
| 25 | +// Re-serializes with the canonical 2-space + trailing-newline format, so a run |
| 26 | +// where nothing changed produces no diff. |
| 27 | +export function syncCatalystField(packageJsonText: string): string { |
| 28 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 29 | + const pkg = JSON.parse(packageJsonText) as CorePackageJson; |
| 30 | + |
| 31 | + pkg.catalyst = buildCatalystField(pkg); |
| 32 | + |
| 33 | + return `${JSON.stringify(pkg, null, 2)}\n`; |
| 34 | +} |
| 35 | + |
| 36 | +function main(): void { |
| 37 | + const corePackageJsonPath = resolve( |
| 38 | + dirname(fileURLToPath(import.meta.url)), |
| 39 | + "../../core/package.json", |
| 40 | + ); |
| 41 | + |
| 42 | + const updated = syncCatalystField(readFileSync(corePackageJsonPath, "utf-8")); |
| 43 | + |
| 44 | + writeFileSync(corePackageJsonPath, updated); |
| 45 | + |
| 46 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions |
| 47 | + const { catalyst } = JSON.parse(updated) as CorePackageJson; |
| 48 | + |
| 49 | + console.log( |
| 50 | + `Synced core/package.json catalyst field → ${catalyst?.version} (${catalyst?.ref})`, |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +const isMain = process.argv[1] === fileURLToPath(import.meta.url); |
| 55 | + |
| 56 | +if (isMain) { |
| 57 | + main(); |
| 58 | +} |
0 commit comments