Skip to content

Instantly share code, notes, and snippets.

@jslyonnais
Created February 21, 2019 15:50
Show Gist options
  • Save jslyonnais/2d9208320c7b20ef6e243e7f72b3c675 to your computer and use it in GitHub Desktop.
Save jslyonnais/2d9208320c7b20ef6e243e7f72b3c675 to your computer and use it in GitHub Desktop.
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