1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 04:05:01 +00:00
mon-entreprise/scripts/build-rules.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.5 KiB
JavaScript
Raw Normal View History

/* eslint-env node */
2021-12-07 13:46:03 +01:00
import fs from 'fs'
import yaml from 'js-yaml'
import path from 'path'
import Engine from 'publicodes'
const publicodesDir = './règles'
const outDir = './dist'
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir)
}
function recursiveFindYamlFile(dirPath = publicodesDir) {
return fs
.readdirSync(dirPath)
.flatMap((filename) => {
const fullpath = path.join(dirPath, filename)
if (fs.statSync(fullpath).isDirectory()) {
return recursiveFindYamlFile(fullpath)
} else {
return filename.endsWith('.yaml') ? fullpath : false
}
})
.filter(Boolean)
}
function readRules() {
return recursiveFindYamlFile().reduce((rules, filePath) => {
const newRules = yaml.load(fs.readFileSync(filePath, 'utf-8'), {
filename: filePath,
})
const duplicatedRule = Object.keys(newRules).find(
(ruleName) => ruleName in rules
)
if (duplicatedRule) {
throw new Error(
`La règle ${duplicatedRule} a été redéfinie dans dans le fichier ${filePath}, alors qu'elle avait déjà été définie auparavant dans un autre fichier`
)
}
return Object.assign(rules, newRules)
}, {})
}
export default function writeJSFile() {
const rules = readRules()
const names = Object.keys(new Engine(rules).getParsedRules())
2021-12-07 09:12:57 +01:00
const jsString = `export default ${JSON.stringify(rules, null, 2)}`
fs.writeFileSync(path.resolve(outDir, 'index.js'), jsString)
fs.writeFileSync(
path.resolve(outDir, 'names.ts'),
`\nexport type Names = ${names.map((name) => `"${name}"`).join('\n | ')}\n`
)
}
writeJSFile()