forked from wowsims/mop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-locales.mjs
More file actions
55 lines (45 loc) · 1.45 KB
/
test-locales.mjs
File metadata and controls
55 lines (45 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Ajv from 'ajv';
import path from 'node:path';
import { fileURLToPath } from 'url';
import fs from 'node:fs/promises';
import { glob } from 'glob';
const localesPath = 'assets/locales';
const ajv = new Ajv();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const loadSchemaFiles = async () => {
const schemas = {};
const schemaFiles = await fs.readdir(path.join(__dirname, 'schemas'));
for (const file of schemaFiles) {
const data = await fs.readFile(path.join(__dirname, 'schemas', file), 'utf-8');
const schemaName = file.split('.')[0];
schemas[schemaName] = JSON.parse(data);
}
return schemas;
};
const validateSchemas = async () => {
const schemas = await loadSchemaFiles();
let hasError = false;
for (const [name, schema] of Object.entries(schemas)) {
const validate = ajv.compile(schema);
const filePaths = await glob(path.join(__dirname, localesPath, `**/${name}.json`));
for (const filePath of filePaths) {
const relativePath = filePath.split(localesPath)[1].replace('/', '');
const data = await fs.readFile(filePath, 'utf-8');
const valid = validate(JSON.parse(data));
if (valid) {
console.log(`✅ ${relativePath} is valid`);
} else {
if (!hasError) hasError = true;
console.log(
`❌ ${relativePath} is invalid:`,
ajv.errorsText(validate.errors, {
dataVar: 'schema',
}),
);
}
}
}
if (hasError) process.exit(1);
};
validateSchemas();