pmtool / src /scripts /increment-patch.mjs
devarshia5's picture
Upload 487 files
d97b8f9 verified
Raw
History Blame Contribute Delete
2.94 kB
/**
* 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);
// Create a debug log file
const logFile = path.resolve(__dirname, '../../increment-debug.log');
fs.writeFileSync(logFile, `Script started at ${new Date().toISOString()}\n`, 'utf8');
// Log function that writes to both console and file
const log = (message) => {
console.log(message);
fs.appendFileSync(logFile, message + '\n', 'utf8');
};
log('====================================');
log('πŸ“¦ VERSION INCREMENT SCRIPT RUNNING');
log('====================================');
// 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) {
const errorMsg = '❌ Could not find version information in version.ts';
log(errorMsg);
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;
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');
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');
log('βœ… Updated package.json successfully');
log('====================================');
log(`πŸ“ NEW VERSION: ${currentMajor}.${currentMinor}.${newPatch}`);
log('====================================');
} catch (error) {
const errorMsg = `❌ Error updating version: ${error.message}`;
log(errorMsg);
process.exit(1);
}