mon-entreprise/source/scripts/rules.js

29 lines
781 B
JavaScript
Raw Normal View History

2020-02-04 17:35:21 +00:00
// In a vanilla NodeJS environment it is not possible to use the "import"
// statement with the Webpack transformer (from yaml to json).
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
2020-04-05 21:27:31 +00:00
const publicodesDir = path.resolve(__dirname, '../rules')
2020-02-04 17:35:21 +00:00
function concatenateFilesInDir(dirPath = publicodesDir) {
return fs
.readdirSync(dirPath)
2020-02-04 17:35:21 +00:00
.map(filename => {
const fullpath = path.join(dirPath, filename)
if (fs.statSync(fullpath).isDirectory()) {
return concatenateFilesInDir(fullpath)
} else {
2020-04-05 21:27:31 +00:00
return filename.endsWith('.yaml') ? fs.readFileSync(fullpath) : ''
}
2020-02-04 17:35:21 +00:00
})
.reduce((acc, cur) => acc + '\n' + cur, '')
}
function readRules() {
return yaml.safeLoad(concatenateFilesInDir())
2020-02-04 17:35:21 +00:00
}
exports.readRules = readRules