Skip to content

Instantly share code, notes, and snippets.

@BSFishy
Created June 5, 2023 17:51
Show Gist options
  • Save BSFishy/4a27893beb6f226774da9a59ad7f56ea to your computer and use it in GitHub Desktop.
Save BSFishy/4a27893beb6f226774da9a59ad7f56ea to your computer and use it in GitHub Desktop.
Updates a package (defaults to OUI) in all of OSD
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
function updateVersion(input, pkg, version) {
var regex = new RegExp(`^([ \t]+"${pkg.replace('/', '\\/')}"): "([^"]*)"(,?)`, "gm");
function replacer(match, group1, group2, group3) {
return `${group1}: "${version}"${group3}`;
}
return input.replace(regex, replacer);
}
function recurseFiles(dir, filename, callback) {
var files = fs.readdirSync(dir);
for (var i = 0; i < files.length; i++) {
var file = path.join(dir, files[i]);
var scan = fs.lstatSync(file);
if (scan.isFile()) {
if (!file.endsWith(filename)) {
continue;
}
callback(file);
} else if (scan.isDirectory()) {
recurseFiles(file, filename, callback);
}
}
}
var packageName = process.argv[2];
var packageVersion = process.argv[3];
if (typeof packageName === 'undefined') {
packageName = 'npm:@bsfishy/oui@1.0.1-pink.1';
}
if (typeof packageVersion === 'undefined') {
if (typeof packageName === 'undefined') {
packageName = '@elastic/eui';
packageVersion = 'npm:@bsfishy/oui@1.0.1-pink.1';
} else {
packageVersion = packageName;
packageName = '@elastic/eui';
}
}
recurseFiles(__dirname, 'package.json', function callback(file) {
fs.writeFileSync(file, updateVersion(fs.readFileSync(file).toString(), packageName, packageVersion));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment