2020-02-23 18:05:48 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 Yomichan Authors
|
2020-02-23 18:05:48 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-02 03:41:02 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2020-09-19 23:04:28 +00:00
|
|
|
const {JSZip} = require('./util');
|
|
|
|
const {VM} = require('./vm');
|
2020-02-02 03:41:02 +00:00
|
|
|
|
2020-03-03 03:20:47 +00:00
|
|
|
const vm = new VM();
|
2020-06-28 16:38:34 +00:00
|
|
|
vm.execute([
|
|
|
|
'mixed/js/core.js',
|
2020-08-09 18:18:59 +00:00
|
|
|
'mixed/js/cache-map.js',
|
2020-06-28 16:38:34 +00:00
|
|
|
'bg/js/json-schema.js'
|
|
|
|
]);
|
2020-08-15 21:23:09 +00:00
|
|
|
const JsonSchemaValidator = vm.get('JsonSchemaValidator');
|
2020-02-02 03:41:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
function readSchema(relativeFileName) {
|
|
|
|
const fileName = path.join(__dirname, relativeFileName);
|
|
|
|
const source = fs.readFileSync(fileName, {encoding: 'utf8'});
|
|
|
|
return JSON.parse(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function validateDictionaryBanks(zip, fileNameFormat, schema) {
|
|
|
|
let index = 1;
|
|
|
|
while (true) {
|
2020-02-18 03:39:25 +00:00
|
|
|
const fileName = fileNameFormat.replace(/\?/, index);
|
2020-02-02 03:41:02 +00:00
|
|
|
|
|
|
|
const file = zip.files[fileName];
|
|
|
|
if (!file) { break; }
|
|
|
|
|
|
|
|
const data = JSON.parse(await file.async('string'));
|
2020-08-15 21:23:09 +00:00
|
|
|
new JsonSchemaValidator().validate(data, schema);
|
2020-02-02 03:41:02 +00:00
|
|
|
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:38:48 +00:00
|
|
|
async function validateDictionary(archive, schemas) {
|
|
|
|
const indexFile = archive.files['index.json'];
|
2020-02-02 03:41:02 +00:00
|
|
|
if (!indexFile) {
|
|
|
|
throw new Error('No dictionary index found in archive');
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = JSON.parse(await indexFile.async('string'));
|
|
|
|
const version = index.format || index.version;
|
|
|
|
|
2020-08-15 21:23:09 +00:00
|
|
|
new JsonSchemaValidator().validate(index, schemas.index);
|
2020-02-02 03:41:02 +00:00
|
|
|
|
2020-02-18 03:39:25 +00:00
|
|
|
await validateDictionaryBanks(archive, 'term_bank_?.json', version === 1 ? schemas.termBankV1 : schemas.termBankV3);
|
|
|
|
await validateDictionaryBanks(archive, 'term_meta_bank_?.json', schemas.termMetaBankV3);
|
|
|
|
await validateDictionaryBanks(archive, 'kanji_bank_?.json', version === 1 ? schemas.kanjiBankV1 : schemas.kanjiBankV3);
|
|
|
|
await validateDictionaryBanks(archive, 'kanji_meta_bank_?.json', schemas.kanjiMetaBankV3);
|
|
|
|
await validateDictionaryBanks(archive, 'tag_bank_?.json', schemas.tagBankV3);
|
2020-02-18 03:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSchemas() {
|
|
|
|
return {
|
|
|
|
index: readSchema('../ext/bg/data/dictionary-index-schema.json'),
|
|
|
|
kanjiBankV1: readSchema('../ext/bg/data/dictionary-kanji-bank-v1-schema.json'),
|
|
|
|
kanjiBankV3: readSchema('../ext/bg/data/dictionary-kanji-bank-v3-schema.json'),
|
|
|
|
kanjiMetaBankV3: readSchema('../ext/bg/data/dictionary-kanji-meta-bank-v3-schema.json'),
|
|
|
|
tagBankV3: readSchema('../ext/bg/data/dictionary-tag-bank-v3-schema.json'),
|
|
|
|
termBankV1: readSchema('../ext/bg/data/dictionary-term-bank-v1-schema.json'),
|
|
|
|
termBankV3: readSchema('../ext/bg/data/dictionary-term-bank-v3-schema.json'),
|
|
|
|
termMetaBankV3: readSchema('../ext/bg/data/dictionary-term-meta-bank-v3-schema.json')
|
|
|
|
};
|
2020-02-02 03:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const dictionaryFileNames = process.argv.slice(2);
|
|
|
|
if (dictionaryFileNames.length === 0) {
|
|
|
|
console.log([
|
|
|
|
'Usage:',
|
|
|
|
' node dictionary-validate <dictionary-file-names>...'
|
|
|
|
].join('\n'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:38:48 +00:00
|
|
|
const schemas = getSchemas();
|
2020-02-02 03:41:02 +00:00
|
|
|
|
|
|
|
for (const dictionaryFileName of dictionaryFileNames) {
|
|
|
|
try {
|
|
|
|
console.log(`Validating ${dictionaryFileName}...`);
|
2020-02-18 03:38:48 +00:00
|
|
|
const source = fs.readFileSync(dictionaryFileName);
|
|
|
|
const archive = await JSZip.loadAsync(source);
|
|
|
|
await validateDictionary(archive, schemas);
|
2020-02-02 03:41:02 +00:00
|
|
|
console.log('No issues found');
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-18 03:30:16 +00:00
|
|
|
if (require.main === module) { main(); }
|
2020-02-18 03:38:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getSchemas,
|
|
|
|
validateDictionary
|
|
|
|
};
|