diff --git a/.github/scripts/check-yaml.cjs b/.github/scripts/check-yaml.cjs new file mode 100644 index 0000000..5d04d0c --- /dev/null +++ b/.github/scripts/check-yaml.cjs @@ -0,0 +1,63 @@ +const fs = require("fs"); +const yaml = require("js-yaml"); +const glob = require("glob"); + +const SNAKE_CASE_REGEX = /^[a-z][a-z0-9_]*$/; + +let hasError = false; +const parsedFiles = []; + +function checkKeys(obj, file, path = "") { + if (typeof obj !== "object" || obj === null) return; + + for (const key of Object.keys(obj)) { + const fullPath = path ? `${path}.${key}` : key; + + if (!SNAKE_CASE_REGEX.test(key)) { + console.error( + `❌ ${file} → clé invalide "${fullPath}" (snake_case requis)` + ); + hasError = true; + } + + checkKeys(obj[key], file, fullPath); + } +} + +const files = glob.sync("**/*.{yml,yaml}", { + ignore: ["node_modules/**"] +}); + +if (files.length === 0) { + console.log("ℹ️ Aucun fichier YAML trouvé"); + process.exit(0); +} + +for (const file of files) { + try { + const content = fs.readFileSync(file, "utf8"); + const data = yaml.load(content); + + checkKeys(data, file); + + parsedFiles.push({ + file, + data + }); + } catch (err) { + console.error(`❌ Erreur YAML dans ${file}`); + console.error(err.message); + hasError = true; + } +} + +if (hasError) { + console.error("\n⛔ Validation YAML échouée"); + process.exit(1); +} + +/** + * OUTPUT FINAL + * Tu peux le parser dans un autre step GitHub Actions + */ +console.log(JSON.stringify(parsedFiles, null, 2)); diff --git a/.github/workflows/yaml-lint.yml b/.github/workflows/yaml-lint.yml new file mode 100644 index 0000000..0383a44 --- /dev/null +++ b/.github/workflows/yaml-lint.yml @@ -0,0 +1,31 @@ +name: YAML format & snake_case check + +on: + push: + paths: + - "**/*.yml" + - "**/*.yaml" + pull_request: + paths: + - "**/*.yml" + - "**/*.yaml" + +jobs: + yaml-check: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm install js-yaml glob + + - name: Validate YAML files and keys + run: node .github/scripts/check-yaml.cjs +