Last active
December 10, 2015 22:14
-
-
Save ajainarayanan/4d8ce7788697901faa74 to your computer and use it in GitHub Desktop.
This is for automatically converting old predefined app templates to new ones.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: node --harmony <filename>.js path=./templates/apps/predefined/ | |
// 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'); | |
process.argv | |
.filter( (arg, i) => { | |
if (arg.indexOf('path') !== -1) { | |
return true; | |
} | |
return false; | |
}) | |
.forEach((pathArg) => paths = pathArg.split('=')[1].split(',').concat(paths)); | |
paths.forEach(fetchAndConvertJsons); | |
function fetchAndConvertJsons(path) { | |
console.log('pathL ', path); | |
var files = fs.readdir(path, (err, files) => { | |
console.log('files: ', files); | |
files.forEach(file => { | |
if (fileExt.extname(file) === '.json' && file !== 'config.json') { | |
fs.readFile(path + '/' + file , 'utf-8', (err, content) => { | |
if (err) throw err; | |
fs.writeFile(path + '/' + file, JSON.stringify(convertOldAppTemplates(content), null, 2), (err) => { | |
console.log('File ' + path + '/' + file + ' written successfully'); | |
}); | |
}); | |
} | |
}); | |
}); | |
} | |
function convertOldAppTemplates(content) { | |
var newContent = JSON.parse(content); | |
var config = newContent.config; | |
var source = config.source; | |
var sinks = config.sinks; | |
var transforms = config.transforms; | |
var defaultArtifact = { | |
name: 'cdap-etl-lib', | |
scope: 'SYSTEM', | |
version: '3.3.0-SNAPSHOT' | |
}; | |
source.label = source.label || source.name; | |
source.artifact = source.artifact || defaultArtifact; | |
var modifiedSource = { name: source.name + '-' + uuid.v4(), plugin: source }; | |
var modifiedSinks = sinks.map( (sink) => { | |
sink.label = sink.label || sink.name; | |
sink.artifact = sink.artifact || defaultArtifact; | |
return { name: sink.name + '-' + uuid.v4(), plugin : sink }; | |
}); | |
var modifiedTransforms = transforms.map( (transform) => { | |
transform.label = transform.label || transform.name; | |
transform.artifact = transform.artifact || defaultArtifact; | |
return { name: transform.name + '-' + uuid.v4(), plugin: transform }; | |
}); | |
config.source = modifiedSource; | |
config.sinks = modifiedSinks; | |
config.transforms = modifiedTransforms; | |
if (newContent.ui) { | |
delete newContent.ui; | |
} | |
return addConnections(newContent); | |
} | |
function addConnections(configJson) { | |
var sourceNode = configJson.config.source; | |
var connections = []; | |
var nodes = configJson.config.transforms.concat(configJson.config.sinks); | |
var i; | |
connections.push({ | |
from: sourceNode.name, | |
to: nodes[0].name | |
}) | |
for (i =0; i< nodes.length - 1 ; i++) { | |
connections.push({ | |
from: nodes[i].name, | |
to: nodes[i+1].name | |
}); | |
} | |
configJson.config.connections = connections; | |
return configJson; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment