chore: use @publicodes/tools/compilation for compiling rules

pull/2877/head
Johan Girod 2024-01-25 17:46:31 +01:00
parent 8bd3365414
commit 71da738c73
3 changed files with 44 additions and 42 deletions

View File

@ -35,6 +35,7 @@
"@actions/github": "^5.1.1",
"@ianvs/prettier-plugin-sort-imports": "^4.1.0",
"@publicodes/codemod": "^1.1.0",
"@publicodes/tools": "^0.4.6",
"@types/got": "^9.6.12",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",

View File

@ -1,55 +1,20 @@
/* eslint-env node */
import fs from 'fs'
import { writeFileSync } from 'fs'
import path from 'path'
import yaml from 'js-yaml'
import { getModelFromSource } from '@publicodes/tools/compilation'
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)
}, {})
}
const rules = getModelFromSource(path.resolve('./règles/**/*.yaml'), {
verbose: true,
})
export default function writeJSFile() {
const rules = readRules()
const json = JSON.stringify(JSON.stringify(rules))
const names = Object.keys(new Engine(rules).getParsedRules())
const jsString = `export const json = /*@__PURE__*/ ${json};\nexport default /*@__PURE__*/ JSON.parse(json);`
fs.writeFileSync(path.resolve(outDir, 'index.js'), jsString)
fs.writeFileSync(
writeFileSync(path.resolve(outDir, 'index.js'), jsString)
writeFileSync(
path.resolve(outDir, 'names.ts'),
`\nexport type Names = ${names.map((name) => `"${name}"`).join('\n | ')}\n`
)

View File

@ -7608,6 +7608,16 @@ __metadata:
languageName: node
linkType: hard
"@publicodes/tools@npm:^0.4.6":
version: 0.4.6
resolution: "@publicodes/tools@npm:0.4.6"
dependencies:
"@types/node": ^18.11.18
publicodes: ^1.0.0-beta.77
checksum: cb131f4fe8b18550538c7f395763f547118f9a3d9e13b71f86f1940db8c1921312777c3dbbc432f12f1fa8e1ac9999df845ffc195a6b849d043c0130be3dfcd0
languageName: node
linkType: hard
"@radix-ui/number@npm:1.0.1":
version: 1.0.1
resolution: "@radix-ui/number@npm:1.0.1"
@ -12570,6 +12580,15 @@ __metadata:
languageName: node
linkType: hard
"@types/node@npm:^18.11.18":
version: 18.19.9
resolution: "@types/node@npm:18.19.9"
dependencies:
undici-types: ~5.26.4
checksum: 8ffd4043ada193cf32d4c13eb850e8c510d4c96504c62fdc5326b3bb2c7ddc2c81892c351b2c0c403c29df3ab13c864a370f0282daf6d3380ba3c2001728a643
languageName: node
linkType: hard
"@types/node@npm:^20.6.3":
version: 20.6.3
resolution: "@types/node@npm:20.6.3"
@ -26633,6 +26652,15 @@ __metadata:
languageName: node
linkType: hard
"publicodes@npm:^1.0.0-beta.77":
version: 1.0.0-rfc.2
resolution: "publicodes@npm:1.0.0-rfc.2"
peerDependencies:
"@types/mocha": ^9.0.0
checksum: 67c677154b1942257cb329294ca52e94a0cb961bcc64ab4aa5c9530e14687cae066cab8a612c05b758a6d29f603aba7846094c01d12d520f87106ccc3e843fc4
languageName: node
linkType: hard
"pump@npm:3.0.0, pump@npm:^3.0.0":
version: 3.0.0
resolution: "pump@npm:3.0.0"
@ -28312,6 +28340,7 @@ __metadata:
"@actions/github": ^5.1.1
"@ianvs/prettier-plugin-sort-imports": ^4.1.0
"@publicodes/codemod": ^1.1.0
"@publicodes/tools": ^0.4.6
"@types/got": ^9.6.12
"@typescript-eslint/eslint-plugin": ^6.7.2
"@typescript-eslint/parser": ^6.7.2
@ -30945,6 +30974,13 @@ __metadata:
languageName: node
linkType: hard
"undici-types@npm:~5.26.4":
version: 5.26.5
resolution: "undici-types@npm:5.26.5"
checksum: 3192ef6f3fd5df652f2dc1cd782b49d6ff14dc98e5dced492aa8a8c65425227da5da6aafe22523c67f035a272c599bb89cfe803c1db6311e44bed3042fc25487
languageName: node
linkType: hard
"unenv@npm:^1.7.4":
version: 1.8.0
resolution: "unenv@npm:1.8.0"