File size: 2,223 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**

 * 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);
}