forked from HausDAO/monorepo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathincrementVersion.js
70 lines (57 loc) · 1.95 KB
/
incrementVersion.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
const fs = require('fs');
const path = require('path');
function incrementVersion(version, increment) {
const parts = version.split('.');
const major = parseInt(parts[0]);
const minor = parseInt(parts[1]);
const patch = parseInt(parts[2]);
if (increment === 'major') {
return `${major + 1}.0.0`;
} else if (increment === 'minor') {
return `${major}.${minor + 1}.0`;
} else if (increment === 'patch') {
return `${major}.${minor}.${patch + 1}`;
} else {
throw new Error(`Invalid increment: ${increment}`);
}
}
function updatePackageJsonVersion(filePath, increment) {
const fileContents = fs.readFileSync(filePath, 'utf8');
const packageJson = JSON.parse(fileContents);
if (!packageJson.version) {
throw new Error(`No 'version' key found in ${filePath}`);
}
const newVersion = incrementVersion(packageJson.version, increment);
packageJson.version = newVersion;
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2));
console.log(`Updated version in ${filePath} to ${newVersion}`);
}
function processDirectory(dirPath, increment) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
files.forEach((file) => {
const filePath = path.join(dirPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
processDirectory(filePath, increment);
} else if (file === 'package.json') {
try {
updatePackageJsonVersion(filePath, increment);
} catch (error) {
console.error(`Error updating version in ${filePath}: ${error}`);
}
}
});
});
}
// Usage: node updateVersion.js <directoryPath> <increment>
const directoryPath = process.argv[2];
const increment = process.argv[3];
if (!directoryPath || !increment) {
console.error('Usage: node updateVersion.js <directoryPath> <increment>');
process.exit(1);
}
processDirectory(directoryPath, increment);