import * as fs from 'fs'
import * as path from 'path'
function validateJSONKeys(basePath) {
const baseJSON = JSON.parse(fs.readFileSync(path.join(basePath, 'en', 'common.json'), 'utf8'))
const baseKeys = Object.keys(baseJSON)
const folders = fs
.readdirSync(basePath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) =>
dirent.name)
console.log('Validation Results:\n')
folders.forEach((folder) => {
if (folder === 'en') return
const filePath = path.join(basePath, folder, 'common.json')
if (!fs.existsSync(filePath)) {
console.log(`[${folder}] common.json not found.`)
return
}
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'))
const keys = Object.keys(json)
const missingKeys = baseKeys.filter((key) => !keys.includes(key))
const extraKeys = keys.filter((key) => !baseKeys.includes(key))
if (missingKeys.length === 0 && extraKeys.length === 0) {
console.log(`[${folder}] ✓ All keys are present.`)
} else {
if (missingKeys.length > 0) {
console.log(`[${folder}] ✗ Missing keys.`)
}
if (extraKeys.length > 0) {
console.log(`[${folder}] ⚠ Warning: Extra keys found.`)
}
}
})
}
// Usage example
const basePath = './public/locales'
validateJSONKeys(basePath)
前端项目要搞 几个 script , 放到 internal 里,类似 site map 生成,npm postbuild 自动跑。