-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrulesToJSON.js
30 lines (27 loc) · 1.02 KB
/
rulesToJSON.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const yaml = require('yaml')
const fs = require('fs')
const glob = require('glob')
// this file is kindof a duplicate of RulesProvider (which serves for the local watched webpack environment) in ecolab-climat
// if it grows more than 20 lines, it should be shared
const destinationPath = process.argv[2] ?? './public/co2.json'
glob('data/**/*.yaml', (err, files) => {
const rules = files.reduce((memo, filename) => {
const data = fs.readFileSync('./' + filename, 'utf8')
const rules = yaml.parse(data)
const splitName = filename.replace('data/', '').split('>.yaml')
const prefixedRuleSet =
splitName.length > 1
? Object.fromEntries(
Object.entries(rules).map(([k, v]) => [
k === 'index' ? splitName[0] : splitName[0] + ' . ' + k,
v,
])
)
: rules
return { ...memo, ...prefixedRuleSet }
}, {})
fs.writeFile(destinationPath, JSON.stringify(rules), function (err) {
if (err) return console.error(err)
console.log('Les règles en JSON ont été écrites avec succès, bravo !')
})
})