forked from alertlogic/paws-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-collector-versions.js
51 lines (39 loc) · 2.04 KB
/
update-collector-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
const fs = require('fs');
const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
// usage
if (process.argv.some(e => e === '--help' || e === '-h')){
console.log("node update-paws-version.js [<paws-version>]");
}
// check if arg is a semver.
if (process.argv[2] && !process.argv[2].match(semverRegex)){
console.log("Please provide valid semver version. i.e 1.0.0")
return
}
// the current version derived from the package.json
const currentPaws = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
// this is the version we will update the collectors to.
// If it is not explicitly set, then we'll grab the current version from the main package.json
const newPawsVersion = process.argv[2] ? process.argv[2] : currentPaws;
// get the collector directories and filter out the template dir.
// we'll handle the template dir as a special case later
const directories = fs.readdirSync('collectors')
.filter(dir => fs.lstatSync(`collectors/${dir}`).isDirectory())
.filter(dir => dir !== 'template');
directories.forEach(dir => updatePackageJson(dir, 'package.json'));
updatePackageJson('template', 'package.json.template', false);
function updatePackageJson(folder, fileName, updateVersion = true) {
const file = `collectors/${folder}/${fileName}`;
const package = JSON.parse(fs.readFileSync(file, 'utf8'));
// if the version is already what we want to upgrade to, there is nothing to do
if(package.dependencies['@alertlogic/paws-collector'].includes(newPawsVersion)){
console.log(`${folder} is already up to date`);
return
}
package.dependencies['@alertlogic/paws-collector'] = `^${newPawsVersion}`;
if(updateVersion){
const version = package.version.split('.').map(e => parseInt(e))
version[2]++;
package.version = version.map(e => e.toString()).join('.');
}
fs.writeFileSync(file, JSON.stringify(package, null, 2), 'utf8');
}