Skip to content

Commit c96bf1e

Browse files
committed
add script version
1 parent f7877a5 commit c96bf1e

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

scripts/version.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
1-
/* eslint-disable @typescript-eslint/no-var-requires */
2-
const fs = require('fs');
3-
const path = require('path');
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
47

58
console.log('Starting version update process...');
69

710
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+
// This regex will match 'v1.2.3' or '1.2.3' with or without additional suffixes
12+
const match = input.match(/v?(\d+\.\d+\.\d+)/);
13+
return match ? match[1] : null;
1114
}
1215

1316
function isValidVersion(version) {
14-
const versionRegex = /^\d+\.\d+\.\d+$/;
15-
return versionRegex.test(version);
17+
const versionRegex = /^\d+\.\d+\.\d+$/;
18+
return versionRegex.test(version);
1619
}
1720

1821
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.');
22+
const extractedVersion = extractVersion(inputVersion);
23+
24+
if (!extractedVersion || !isValidVersion(extractedVersion)) {
25+
console.error('Error: Invalid version format. Please use the format v1.2.3 or 1.2.3');
26+
console.error('Additional suffixes after the version number will be ignored.');
27+
console.error('Example: v1.2.3 or 1.2.3 or v1.2.3-beta');
28+
process.exit(1);
29+
}
30+
31+
// Update package.json
32+
const packageJsonPath = path.join(__dirname, '..', 'package.json');
33+
let packageJson;
34+
try {
35+
const fileContent = fs.readFileSync(packageJsonPath, 'utf8');
36+
packageJson = JSON.parse(fileContent);
37+
console.log(`Current version in package.json: ${packageJson.version}`);
38+
} catch (error) {
39+
console.error('Error reading package.json:', error);
40+
process.exit(1);
41+
}
42+
43+
packageJson.version = extractedVersion;
44+
45+
try {
46+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
47+
console.log(`Successfully updated package.json with new version: ${extractedVersion}`);
48+
} catch (error) {
49+
console.error('Error writing to package.json:', error);
50+
process.exit(1);
51+
}
52+
53+
console.log('Version update process completed successfully.');
5154
}
5255

5356
// Check if version is provided as an argument
5457
const providedVersion = process.argv[2];
5558

5659
if (providedVersion) {
57-
updateVersion(providedVersion);
60+
updateVersion(providedVersion);
5861
} 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+
console.error('Error: Version not provided. Please run the script with a version argument.');
63+
console.error('Example: node scripts/version.js v1.2.3 or node scripts/version.js 1.2.3');
64+
process.exit(1);
6265
}

0 commit comments

Comments
 (0)