Skip to content

Instantly share code, notes, and snippets.

@vikikamath
Last active August 29, 2015 14:15
Show Gist options
  • Save vikikamath/425d48bc908424a8dcae to your computer and use it in GitHub Desktop.
Save vikikamath/425d48bc908424a8dcae to your computer and use it in GitHub Desktop.
JSON transformation using Oboe
var oboe = require('oboe')
fs = require('fs')
path = require('path')
mkdirp = require('mkdirp')
mv = require('mv')
rootDir = path.join(__dirname, 'dist')
apps = [] // temp store for all apps found in a
urls = {} // temp store for all urls found in a config file
function versionSnakeCase(str){
return 'v' + str.replace(/\./g, '_')
}
function onFileSaved(data){
apps.push(data.filePath)
return function (err) {
if (err) throw err
console.log("%s file saved", data.filePath )
}
}
function onDirectoryCreated(data){
return function (err, created) {
if (err) throw err
console.log("%s path created", created)
data.configs.forEach(function(_app) {
console.log("%s %s", _app, path.join(data.dirPath, path.basename(_app)))
mv(_app, path.join(data.dirPath, path.basename(_app)), function(err) {
if (err) throw err
console.log("Moving %s to %s", _app, data.dirPath)
})
})
}
}
module.exports = function(fileName, cb){
oboe(fs.createReadStream('./data.json'))
.on('node', {
'apps[*].screens[*]': function( obj ) {
if (!urls[obj.url]){
urls[obj.url] = {}
}
urls[obj.url][obj.id] = obj["map"]
},
'![*].apps[*]': function( obj ) {
var filePath = path.join(rootDir, obj.name + '.js')
fs.writeFile(filePath, JSON.stringify(urls), onFileSaved({
"id": obj.name,
"filePath": filePath
}))
urls = {} // deref
},
'![*]': function( obj ) {
var dirPath = path.join(rootDir, obj.session, versionSnakeCase(obj.version))
mkdirp(dirPath, onDirectoryCreated({
"dirPath": dirPath,
"configs": apps.slice()
}))
apps = [] // deref
}
})
.on('done', function(){
console.log('completed')
})
}
[{
"session": "private",
"version": "1.0",
"apps": [
{
"name": "app1",
"screens": [{
"id": "1",
"url": "/#app1/1",
"map": {
"comp1": true,
"comp2": true,
"comp3": false
}
},{
"id": "2",
"url": "/#app1/2",
"map": {
"comp11": true,
"comp21": true,
"comp31": false
}
},{
"id": "3",
"url": "/#app1/3",
"map": {
"comp11": false,
"comp21": true,
"comp31": false
}
},{
"id": "4",
"url": "/#app1/4",
"map": {
"comp11": true,
"comp21": true,
"comp31": true
}
}]
},{
"name": "app2",
"screens": [{
"id": "1",
"url": "/#app2/1",
"map": {
"comp1": true,
"comp2": true,
"comp3": false
}
},{
"id": "2",
"url": "/#app2/2",
"map": {
"comp11": true,
"comp21": true,
"comp31": false
}
}]
}]
},
{
"session": "public",
"version": "0.0",
"apps": [
{
"name": "app12",
"screens": [{
"id": "34",
"url": "/#app12/1",
"map": {
"comp1": true,
"comp2": true,
"comp3": false
}
},{
"id": "21",
"url": "/#app12/2",
"map": {
"comp121": true,
"comp231": true,
"comp331": false
}
}]
},{
"name": "app19",
"screens": [{
"id": "132",
"url": "/#app19/1",
"map": {
"comp11": true,
"comp322": true,
"comp33": false
}
},{
"id": "2",
"url": "/#app19/2",
"map": {
"comp131": true,
"comp213": true,
"comp331": false
}
}]
}]
}]
define( // template
{
'/#dash': {
"1": {
"comp1": true,
"comp2": true,
"comp3": false
}
},
'/': {
"2": {
"comp11": true,
"comp21": true,
"comp31": false
},
"3": {
"comp11": false,
"comp21": true,
"comp31": false
},
"4": {
"comp11": true,
"comp21": true,
"comp31": true
}
}
}
) // template
{
"name": "test-oboe",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha tests"
},
"author": "",
"license": "ISC",
"dependencies": {
"oboe": "^2.1.1",
"mkdirp": "^0.5.0",
"mv": "^2.0.3"
},
"devDependencies": {
"chai": "^2.0.0",
"sinon": "^1.12.2",
"mocha": "^2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment