Created
February 21, 2019 15:50
-
-
Save jslyonnais/2d9208320c7b20ef6e243e7f72b3c675 to your computer and use it in GitHub Desktop.
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
const fs = require('fs') | |
const YAML = require('json-to-pretty-yaml') | |
const configPath = '/static/admin' | |
const configs = require('require-all')(__dirname + `${configPath}/config`) | |
let obj = { | |
'backend': {}, | |
'collections': [] | |
} | |
const createNewCollectionsType = (category, name) => { | |
obj[category][name] = { | |
'label' : name, | |
'name' : name, | |
'files' : [] | |
} | |
} | |
const createUniqueObject = () => { | |
Object.entries(configs).map((file) => { | |
const infos = file[0] | |
const data = file[1] | |
const category = (infos.indexOf('-') > -1) ? infos.split('-')[0] : infos | |
const type = (infos.indexOf('-') > -1) ? infos.split('-')[1] : null | |
const name = (infos.indexOf('-') > -1) ? infos.split('-')[2] : null | |
if(type && name) { | |
if(!obj[category][type]) { | |
createNewCollectionsType(category, type) | |
} | |
return obj[category][type]['files'].push(data) | |
} else if(type) { | |
return obj[category].push(data) | |
} | |
return obj[category] = data | |
}) | |
return constructFinalObject() | |
} | |
const constructFinalObject = () => { | |
var output = { | |
'collections': [] | |
} | |
Object.entries(obj).map((category) => { | |
(category[0] === 'collections') ? | |
Object.entries(category[1]).map((entry) => { | |
const firstObjectInList = entry[1] | |
return output['collections'].push(firstObjectInList) | |
}) | |
: Object.entries(category[1]).map(entry => { | |
let key = entry[0]; | |
let value = entry[1]; | |
return output[key] = value | |
}); | |
}) | |
saveToFile(output) | |
} | |
const saveToFile = (output) => { | |
const fileName = `.${configPath}/config.yml` | |
fs.writeFile(fileName, YAML.stringify(output), function(err) { | |
if(err) { | |
return console.log(err) | |
} | |
console.log(`⚙️ Config was successfully created!`) | |
}) | |
} | |
createUniqueObject() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment