UNPKG

@braineet/eslint-plugin

Version:

51 lines (45 loc) 1.52 kB
/* eslint-disable import/no-extraneous-dependencies */ const fastGlob = require('fast-glob'); const flat = require('flat'); const fs = require('fs'); const has = require('lodash.has'); function getTranslations(cwd, location) { const translations = Object.fromEntries( location.flatMap(source => { const filePaths = fastGlob.sync(source, { cwd: process.cwd() }); return filePaths.map(filePath => { const raw = fs.readFileSync(filePath, 'utf-8'); let parsed; try { parsed = JSON.parse(raw); } catch { throw new Error( "Invalid translation file: the file's content is not a valid JSON document", ); } if (!parsed || typeof parsed !== 'object') { throw new Error( "Invalid translation file: the file's content should contain a JSON object", ); } return [filePath, parsed]; }); }), ); return translations; } function getKeysFromTranslations(translations) { return new Set( Object.values(translations).flatMap(translation => Object.keys(flat(translation)), ), ); } function hasKeyInTranslation(translation, key) { return has(translation, key); } module.exports = { getTranslations, getKeysFromTranslations, hasKeyInTranslation, };