Skip to content

Instantly share code, notes, and snippets.

@ajainarayanan
Created January 5, 2016 23:49
Show Gist options
  • Save ajainarayanan/612d54d00f7675272c80 to your computer and use it in GitHub Desktop.
Save ajainarayanan/612d54d00f7675272c80 to your computer and use it in GitHub Desktop.
Automatically change artifact name & version from predefined apps
// Usage: node --harmony <filename>.js path=./templates/apps/predefined/ --artifact=core-plugins --version=1.2.0
// Templates from <3.2 to new template structure in 3.3
var uuid = require('node-uuid');
var fs = require('fs');
var paths = [];
var fileExt = require('path');
var yargs = require('yargs').argv;
var path = yargs.path;
var artifact = yargs.artifact;
var version = yargs.version;
console.log('VERSION: ', version);
if (!path || !artifact || !version) {
console.log('Path, Artifact & Version are mandatory.');
console.log('Usage');
console.log('node --harmony apps-artifact-version-change.js --path=<some-path> --artifact=<artifact-name> --version=<artifact-version>');
process.exit(1);
}
fetchAndConvertJsons(path);
function fetchAndConvertJsons(path) {
fs.readdir(path, (err, files) => {
files.forEach(file => {
if (fileExt.extname(file) === '.json' && file !== 'config.json') {
fs.readFile(path + '/' + file , 'utf-8', (err, content) => {
if (err) throw err;
content = JSON.parse(content);
var source = content.config.source;
var sinks = content.config.sinks;
var transforms = content.config.transforms;
console.log('===================');
console.log('[FILE]: ' + file);
console.log('===================');
console.log('[SOURCE]:' + source.plugin.label + ' - [' + source.plugin.artifact.name + ', ' + source.plugin.artifact.version + '] -> [' + artifact + ', ' + version + ']');
source.plugin.artifact.name = artifact;
source.plugin.artifact.version = version;
sinks.forEach( sink => {
console.log('[SINK]:' + sink.plugin.label + ' - [' + sink.plugin.artifact.name + ', ' + sink.plugin.artifact.version + '] -> [' + artifact + ', ' + version + ']')
sink.plugin.artifact.name = artifact;
sink.plugin.artifact.version = version;
});
transforms.forEach( transform => {
console.log('[TRANSFORM]:' + transform.plugin.label + ' - [' + transform.plugin.artifact.name + ', ' + transform.plugin.artifact.version + '] -> [' + artifact + ', ' + version + ']')
transform.plugin.artifact.name = artifact;
transform.plugin.artifact.version = version;
});
fs.writeFile(path + '/' + file, JSON.stringify(content, null, 2), (err) => {
if (err) {
console.log('Write to file: ' + path + '/' + file + ' failed');
return;
}
console.log('File ' + path + '/' + file + ' written successfully');
});
});
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment