Skip to content

Instantly share code, notes, and snippets.

@libetl
Created March 2, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libetl/26ca19f495ff167ac415f867448fd797 to your computer and use it in GitHub Desktop.
Save libetl/26ca19f495ff167ac415f867448fd797 to your computer and use it in GitHub Desktop.
yamlChecker
{
"name": "yamlchecker",
"version": "0.0.1",
"private": true,
"dependencies": {
"yamljs": "latest"
},
"devDependencies": {
"webpack": "latest"
}
}
const path = require('path');
module.exports = {
entry: './yamlChecker.js',
target: 'node',
output: {
filename: 'yamlChecker.bin.js',
path: path.resolve(__dirname, 'dist')
},
node: {
fs: 'empty'
}
};
const yaml = require('yamljs')
const fs = require('fs')
const path = require('path')
const keepKeysOf = (object, name) => Object.entries(object)
.map(([key, value]) => typeof value === 'object' ? {[key]: keepKeysOf(value, name)} : {[key]:name})
.reduce((acc, value) => Object.assign(acc, value), {})
const deepMerge = (obj1, obj2) => !obj1 ? obj2 : !obj2 ? obj1 :
Object.assign({}, obj1, Object.entries(obj2).map(([key, value]) => typeof value !== 'object' ? {[key]: value} :
{[key]: deepMerge(obj1[key], obj2[key])}).reduce((acc, value) => Object.assign({}, acc, value), {}))
const flatten = (flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten)
const listProperties = (data, where, prefix = '') => Object.entries(data).map(
([key, value]) => typeof value === 'object' ?
listProperties(data[key], where, (prefix.length ? prefix + '.' : '') + key) :
where(value) ? [prefix + '.' + key]: []).reduce(flatten, []).filter(prop => prop)
const getStatus = data => {
const allYamls = data.toString().split('---')
.map(oneYaml => yaml.parse(oneYaml))
.reduce((acc, value) => Object.assign(acc, value.spring ? {[(value.spring.profiles) || 'default']: value} : {}), {})
const allKeys = Object.entries(allYamls).map(([key, oneYaml]) => ({[key]: keepKeysOf(oneYaml, key)}))
.reduce((acc, value) => Object.assign(acc, value), {})
const defaultValues = allKeys.default
return Object.entries(allKeys).map(([key, oneEnvKeys]) => ({[key]: deepMerge(defaultValues, oneEnvKeys)}))
.reduce((acc, value) => Object.assign(acc, value), {})
}
const addedProfileTo = ({file, data}) => {
const suffix = (file.split('/').slice(-1)[0].match(/^[^.]+-([^.]+)\.yml$/)||[])[1]
return !suffix ? data :
`spring:
profiles: ${suffix}
${data}`
}
const check = files => Promise.all(files.map(file => new Promise((resolve, reject) => fs.readFile(file, (err, data) => err ? reject(err) : resolve({data, file})))))
.then(allFiles => allFiles.reduce((acc, value) => acc.length ? acc + '\n---\n' + addedProfileTo(value) : addedProfileTo(value), ''))
.then(dump => {
const status = dump.length ? getStatus(dump) : {}
const notDefaultEnvs = Object.keys(status).filter(env => env !== 'default')
notDefaultEnvs.map(oneEnv => console.log(`These ${oneEnv} properties in your YAML are read from 'default' profile :\n- ${listProperties(status[oneEnv], value => value === 'default').join('\n- ')}\n\n`))
})
check(process.argv.slice(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment