#!/usr/bin/env node /** * Sync the Android app version from package.json into android/app/build.gradle. * * Single source of truth = package.json "version" (semver, e.g. "1.0.92"). * - versionName ← the semver string verbatim ("1.0.93") * - versionCode ← monotonic integer. For the common 1.0.x line this is just * the patch number (1.0.93 → 93), matching the existing * published scheme. For any other major/minor it falls back * to major*10000 + minor*100 + patch to stay monotonic. * * This runs automatically before `npm run android:build`, so you only ever * bump ONE number (package.json) and never hit Zapstore's "already exists". * * If android/ hasn't been generated yet (fresh checkout), it exits quietly. */ import { readFileSync, writeFileSync, existsSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; const root = join(dirname(fileURLToPath(import.meta.url)), '..'); const gradlePath = join(root, 'android', 'app', 'build.gradle'); if (!existsSync(gradlePath)) { console.log('[sync-android-version] android/ not generated yet — skipping.'); process.exit(0); } const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')); const version = String(pkg.version || '').trim(); const m = version.match(/^(\d+)\.(\d+)\.(\d+)/); if (!m) { console.error(`[sync-android-version] package.json version "${version}" is not semver (x.y.z).`); process.exit(1); } const [, major, minor, patch] = m.map(Number); const versionName = version; // Keep continuity with the already-published 1.0.x scheme (versionCode = patch). // Any future major/minor change switches to a wider monotonic formula. const versionCode = major === 1 && minor === 0 ? patch : major * 10000 + minor * 100 + patch; let gradle = readFileSync(gradlePath, 'utf8'); const beforeCode = gradle.match(/versionCode\s+(\d+)/)?.[1]; const beforeName = gradle.match(/versionName\s+"([^"]+)"/)?.[1]; gradle = gradle .replace(/versionCode\s+\d+/, `versionCode ${versionCode}`) .replace(/versionName\s+"[^"]+"/, `versionName "${versionName}"`); writeFileSync(gradlePath, gradle); console.log( `[sync-android-version] ${beforeName} (${beforeCode}) → ${versionName} (${versionCode})`, );