File size: 2,561 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
59
60
61
62
63
/**

 * Version Update Utility

 * 

 * This script helps update the version in the version.ts file.

 * It automatically handles the logic for incrementing the version

 * according to the rules (e.g., 2.10.0 becomes 3.0.0).

 * 

 * Run this script using ts-node or similar:

 * npx ts-node src/scripts/update-version.ts [major|minor|patch]

 */

import * as fs from 'fs';
import * as path from 'path';
import { APP_VERSION, incrementVersion, Version } from '../lib/version';

// Get the version update type from command line arguments
const updateType = process.argv[2] as 'major' | 'minor' | 'patch' | undefined;

if (!updateType || !['major', 'minor', 'patch'].includes(updateType)) {
  console.error('Please specify a valid version update type: major, minor, or patch');
  console.log('Example: npx ts-node src/scripts/update-version.ts minor');
  process.exit(1);
}

// Calculate the new version
const newVersion: Version = incrementVersion(APP_VERSION, updateType);
console.log(`Updating version from ${APP_VERSION.major}.${APP_VERSION.minor}.${APP_VERSION.patch} to ${newVersion.major}.${newVersion.minor}.${newVersion.patch}`);

// Path to the version.ts file
const versionFilePath = path.resolve(__dirname, '../lib/version.ts');

// Read the current content
let content: string;
try {
  content = fs.readFileSync(versionFilePath, 'utf8');
} catch (error) {
  console.error('Error reading version.ts file:', error);
  process.exit(1);
}

// Update the version in the file
const updatedContent = content.replace(
  /export const APP_VERSION: Version = \{\s*major: \d+,\s*minor: \d+,\s*patch: \d+\s*\};/,
  `export const APP_VERSION: Version = {\n  major: ${newVersion.major},\n  minor: ${newVersion.minor},\n  patch: ${newVersion.patch}\n};`
);

// Write the updated content back to the file
try {
  fs.writeFileSync(versionFilePath, updatedContent, 'utf8');
  console.log('Version updated successfully!');
  
  // Also update package.json
  const packageJsonPath = path.resolve(__dirname, '../../package.json');
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
  packageJson.version = `${newVersion.major}.${newVersion.minor}.${newVersion.patch}`;
  fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8');
  console.log('package.json updated successfully!');
  
  console.log('\nRemember to rebuild the application for changes to take effect.');
} catch (error) {
  console.error('Error writing to version.ts file:', error);
  process.exit(1);
}