-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathupdateVersion.js
More file actions
81 lines (69 loc) · 1.76 KB
/
updateVersion.js
File metadata and controls
81 lines (69 loc) · 1.76 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable no-console */
const fs = require('fs');
const version = process.env.VERSION;
if (!version) {
console.error('Error: VERSION environment variable is not set.');
process.exit(1);
}
const updateJsonFile = (filePath, label = filePath) => {
if (!fs.existsSync(filePath)) {
return;
}
const jsonData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
if (jsonData.version === version) {
console.log(
`No update needed for ${label} (already version ${version}).`
);
return;
}
jsonData.version = version;
fs.writeFileSync(
filePath,
JSON.stringify(jsonData, null, 2) + '\n',
'utf8'
);
console.log(`Updated version in ${label} to ${version}`);
};
const updateTextFile = (filePath, regex, replacement, label = filePath) => {
if (!fs.existsSync(filePath)) {
return;
}
let fileContent = fs.readFileSync(filePath, 'utf8');
const match = fileContent.match(regex);
if (match) {
const currentVersion = match[2];
if (currentVersion === version) {
console.log(
`No update needed for ${label} (already version ${version}).`
);
return;
}
fileContent = fileContent.replace(regex, replacement);
fs.writeFileSync(filePath, fileContent, 'utf8');
console.log(`Updated version in ${label} to ${version}`);
} else {
console.warn(`Version pattern not found in ${label}, skipping update.`);
}
};
// Update all relevant files.
updateTextFile(
'./style.css',
/(Version:\s*)([^\n]+)/,
`$1${version}`,
'style.css'
);
updateTextFile(
'./README.md',
/(## Version:\s*)([^\n]+)/,
`$1${version}`,
'README.md'
);
updateTextFile(
'./readme.txt',
/(Stable tag:\s*)([^\n]+)/,
`$1${version}`,
'readme.txt'
);
updateJsonFile('./package.json', 'package.json');
console.log('Version update process completed.');
/* eslint-enable no-console */