Skip to content

Commit f7877a5

Browse files
committed
add script version
1 parent a70ad7d commit f7877a5

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

scripts/version.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
console.log('Starting version update process...');
6+
7+
function extractVersion(input) {
8+
// This regex will match 'v1.2.3' or '1.2.3' with or without additional suffixes
9+
const match = input.match(/v?(\d+\.\d+\.\d+)/);
10+
return match ? match[1] : null;
11+
}
12+
13+
function isValidVersion(version) {
14+
const versionRegex = /^\d+\.\d+\.\d+$/;
15+
return versionRegex.test(version);
16+
}
17+
18+
function updateVersion(inputVersion) {
19+
const extractedVersion = extractVersion(inputVersion);
20+
21+
if (!extractedVersion || !isValidVersion(extractedVersion)) {
22+
console.error('Error: Invalid version format. Please use the format v1.2.3 or 1.2.3');
23+
console.error('Additional suffixes after the version number will be ignored.');
24+
console.error('Example: v1.2.3 or 1.2.3 or v1.2.3-beta');
25+
process.exit(1);
26+
}
27+
28+
// Update package.json
29+
const packageJsonPath = path.join(__dirname, '..', 'package.json');
30+
let packageJson;
31+
try {
32+
const fileContent = fs.readFileSync(packageJsonPath, 'utf8');
33+
packageJson = JSON.parse(fileContent);
34+
console.log(`Current version in package.json: ${packageJson.version}`);
35+
} catch (error) {
36+
console.error('Error reading package.json:', error);
37+
process.exit(1);
38+
}
39+
40+
packageJson.version = extractedVersion;
41+
42+
try {
43+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
44+
console.log(`Successfully updated package.json with new version: ${extractedVersion}`);
45+
} catch (error) {
46+
console.error('Error writing to package.json:', error);
47+
process.exit(1);
48+
}
49+
50+
console.log('Version update process completed successfully.');
51+
}
52+
53+
// Check if version is provided as an argument
54+
const providedVersion = process.argv[2];
55+
56+
if (providedVersion) {
57+
updateVersion(providedVersion);
58+
} else {
59+
console.error('Error: Version not provided. Please run the script with a version argument.');
60+
console.error('Example: node scripts/version.js v1.2.3 or node scripts/version.js 1.2.3');
61+
process.exit(1);
62+
}

0 commit comments

Comments
 (0)