/** * Pre-build script to automatically increment the patch version * This script runs before each build to bump the patch version */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; // Get current file's directory in ESM const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Path to the version.ts file const versionFilePath = path.resolve(__dirname, '../lib/version.ts'); // Path to package.json const packageJsonPath = path.resolve(__dirname, '../../package.json'); try { // Read the current version.ts file const versionFileContent = fs.readFileSync(versionFilePath, 'utf8'); // Extract current version values const versionMatch = versionFileContent.match(/export const APP_VERSION: Version = {\s*major: (\d+),\s*minor: (\d+),\s*patch: (\d+)\s*};/); if (!versionMatch) { console.error('Could not find version information in version.ts'); process.exit(1); } const currentMajor = parseInt(versionMatch[1]); const currentMinor = parseInt(versionMatch[2]); const currentPatch = parseInt(versionMatch[3]); // Increment patch version const newPatch = currentPatch + 1; console.log(`Incrementing version from ${currentMajor}.${currentMinor}.${currentPatch} to ${currentMajor}.${currentMinor}.${newPatch}`); // Update version.ts const updatedVersionContent = versionFileContent.replace( /export const APP_VERSION: Version = {\s*major: \d+,\s*minor: \d+,\s*patch: \d+\s*};/, `export const APP_VERSION: Version = {\n major: ${currentMajor},\n minor: ${currentMinor},\n patch: ${newPatch}\n};` ); fs.writeFileSync(versionFilePath, updatedVersionContent, 'utf8'); console.log('Updated version.ts successfully'); // Update package.json const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); packageJson.version = `${currentMajor}.${currentMinor}.${newPatch}`; fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8'); console.log('Updated package.json successfully'); } catch (error) { console.error('Error updating version:', error); process.exit(1); }