forked from annotorious/annotorious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-version.js
58 lines (46 loc) · 1.46 KB
/
update-version.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
const fs = require('fs');
const path = require('path');
const searchDirectories = (dir, fileList = []) => {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
if (file !== 'node_modules')
fileList = searchDirectories(filePath, fileList);
} else {
if (file === 'package.json') {
fileList.push(filePath);
}
}
});
return fileList;
};
const updateVersionNumbers = (filePath, newVersion) => {
try {
let packageJson = fs.readFileSync(filePath, 'utf8');
const packageData = JSON.parse(packageJson);
packageData.version = newVersion;
['dependencies', 'peerDependencies'].forEach(depType => {
if (packageData[depType]) {
for (const dep in packageData[depType]) {
if (dep.startsWith('@annotorious/')) {
packageData[depType][dep] = newVersion;
}
}
}
});
fs.writeFileSync(filePath, JSON.stringify(packageData, null, 2), 'utf8');
console.log(`Updated ${filePath}`);
} catch (error) {
console.error(`Error updating ${filePath}: ${error}`);
}
}
const main = () => {
const newVersion = process.argv[2];
const packageJsonFiles = searchDirectories('.');
packageJsonFiles.forEach(filePath => {
updateVersionNumbers(filePath, newVersion);
});
console.log(`All package.json files updated to version ${newVersion}`);
};
main();