From 600de13951c1b013b667716a4db4c1a0640758aa Mon Sep 17 00:00:00 2001 From: Johan Girod Date: Tue, 25 Jan 2022 15:54:59 +0100 Subject: [PATCH] Remplace le parseur yaml de modele-social par un autre qui supporte les ancres --- modele-social/build.js | 23 +++++++++++++++-------- modele-social/package.json | 4 ++-- yarn.lock | 6 +++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/modele-social/build.js b/modele-social/build.js index c7595cfd7..cbba72364 100644 --- a/modele-social/build.js +++ b/modele-social/build.js @@ -2,7 +2,7 @@ const fs = require('fs') const path = require('path') -const yaml = require('yaml') +const yaml = require('js-yaml') const publicodesDir = path.resolve(__dirname, './règles') const outDir = path.resolve(__dirname, './dist') @@ -11,22 +11,29 @@ if (!fs.existsSync(outDir)) { fs.mkdirSync(outDir) } -function concatenateFilesInDir(dirPath = publicodesDir) { +function recursiveFindYamlFile(dirPath = publicodesDir) { return fs .readdirSync(dirPath) - .map((filename) => { + .flatMap((filename) => { const fullpath = path.join(dirPath, filename) if (fs.statSync(fullpath).isDirectory()) { - return concatenateFilesInDir(fullpath) + return recursiveFindYamlFile(fullpath) } else { - return filename.endsWith('.yaml') ? fs.readFileSync(fullpath) : '' + return filename.endsWith('.yaml') ? fullpath : false } - }) - .reduce((acc, cur) => acc + '\n' + cur, '') + }).filter(Boolean) } function readRules() { - return yaml.parse(concatenateFilesInDir()) + 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) + }, {}) + } // Note: we can't put the output file in the fs.watched directory diff --git a/modele-social/package.json b/modele-social/package.json index 100ca7dee..5b88f375c 100644 --- a/modele-social/package.json +++ b/modele-social/package.json @@ -16,8 +16,8 @@ "dist/index.js" ], "devDependencies": { - "publicodes": "^1.0.0-beta.23", - "yaml": "^1.10.0" + "js-yaml": "^4.1.0", + "publicodes": "^1.0.0-beta.23" }, "peerDependencies": { "publicodes": "^1.0.0-beta.23" diff --git a/yarn.lock b/yarn.lock index 7e55f9ceb..75024fff2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9156,9 +9156,9 @@ js-tokens@^3.0.2: resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz" integrity sha1-mGbfOVECEw449/mWvOtlRDIJwls= -js-yaml@4.1.0: +js-yaml@4.1.0, js-yaml@^4.1.0: version "4.1.0" - resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602" integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA== dependencies: argparse "^2.0.1" @@ -14974,7 +14974,7 @@ yaml-loader@^0.5.0: dependencies: js-yaml "^3.5.2" -yaml@^1.10.0, yaml@^1.9.2: +yaml@^1.9.2: version "1.10.2" resolved "https://registry.npmjs.org/yaml/-/yaml-1.10.2.tgz" integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==