-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathupdate-versions.js
90 lines (75 loc) · 3 KB
/
update-versions.js
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
82
83
84
85
86
87
88
89
90
const fs = require('fs');
const path = require('path');
// Get the new version from command line argument
const NEW_VERSION = process.argv[2];
if (!NEW_VERSION) {
console.error('Please provide the new version number as an argument.');
console.error('Usage: node script.js <new_version>');
console.error('Example: node script.js 1.31.2');
process.exit(1);
}
const getAllFiles = (dirPath, arrayOfFiles = []) => {
const files = fs.readdirSync(dirPath);
files.forEach(file => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
if (file !== 'preview') {
getAllFiles(fullPath, arrayOfFiles);
}
} else if (path.extname(file) === '.md') {
arrayOfFiles.push(fullPath);
}
});
return arrayOfFiles;
};
function processFiles() {
const files = getAllFiles('.');
let versionToUpdate = null;
// First pass: find the lotus-x.xx.x version
files.forEach(file => {
try {
const content = fs.readFileSync(file, 'utf8');
const lotusVersionMatch = content.match(/lotus-(\d+\.\d+\.\d+)/);
if (lotusVersionMatch) {
versionToUpdate = lotusVersionMatch[1];
console.log(`Found lotus version to update: ${versionToUpdate}`);
return false; // Exit the forEach loop once we find a version
}
} catch (error) {
console.error(`Error reading ${file}:`, error);
}
});
if (!versionToUpdate) {
console.error('No lotus version found in files');
return;
}
// Second pass: update all occurrences of the version
files.forEach(file => {
try {
const content = fs.readFileSync(file, 'utf8');
let processed = content;
// Create regex to match the version with optional lotus- prefix
const versionRegex = new RegExp(`(lotus-)?${versionToUpdate.replace(/\./g, '\\.')}`, 'g');
const matches = content.match(versionRegex);
if (matches) {
matches.forEach(match => {
// If match includes 'lotus-', replace with 'lotus-NEW_VERSION'
// Otherwise just replace with NEW_VERSION
const replacement = match.includes('lotus-') ?
`lotus-${NEW_VERSION}` :
NEW_VERSION;
processed = processed.replace(match, replacement);
});
if (content !== processed) {
fs.writeFileSync(file, processed);
console.log(`Updated in ${file}:`);
console.log(`Found patterns: ${matches.join(', ')}`);
console.log(`Updated to versions containing: ${NEW_VERSION}`);
}
}
} catch (error) {
console.error(`Error processing ${file}:`, error);
}
});
}
processFiles();