Create dev utility class for some shared functionality (#724)
This commit is contained in:
parent
486d44f719
commit
7b1838a282
48
dev/build.js
48
dev/build.js
@ -19,28 +19,10 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const readline = require('readline');
|
const readline = require('readline');
|
||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
const yomichanTest = require('../test/yomichan-test');
|
const util = require('./yomichan-util');
|
||||||
|
const {getAllFiles, getDefaultManifestAndVariants, createManifestString} = util;
|
||||||
|
|
||||||
|
|
||||||
function getAllFiles(directory, relativeTo) {
|
|
||||||
const results = [];
|
|
||||||
const directories = [directory];
|
|
||||||
for (const dir of directories) {
|
|
||||||
const fileNames = fs.readdirSync(dir);
|
|
||||||
for (const fileName of fileNames) {
|
|
||||||
const fullFileName = path.join(dir, fileName);
|
|
||||||
const relativeFileName = path.relative(relativeTo, fullFileName);
|
|
||||||
const stats = fs.lstatSync(fullFileName);
|
|
||||||
if (stats.isFile()) {
|
|
||||||
results.push(relativeFileName);
|
|
||||||
} else if (stats.isDirectory()) {
|
|
||||||
directories.push(fullFileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=null) {
|
async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=null) {
|
||||||
for (const exe of sevenZipExes) {
|
for (const exe of sevenZipExes) {
|
||||||
try {
|
try {
|
||||||
@ -64,7 +46,7 @@ async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=nu
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createJSZip(directory, outputFileName, onUpdate) {
|
async function createJSZip(directory, outputFileName, onUpdate) {
|
||||||
const JSZip = yomichanTest.JSZip;
|
const JSZip = util.JSZip;
|
||||||
const files = getAllFiles(directory, directory);
|
const files = getAllFiles(directory, directory);
|
||||||
const zip = new JSZip();
|
const zip = new JSZip();
|
||||||
for (const fileName of files) {
|
for (const fileName of files) {
|
||||||
@ -134,24 +116,9 @@ function getObjectProperties(object, path2, count) {
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadDefaultManifest() {
|
|
||||||
const {manifest} = loadDefaultManifestAndVariants();
|
|
||||||
return manifest;
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadDefaultManifestAndVariants() {
|
|
||||||
const fileName = path.join(__dirname, 'data', 'manifest-variants.json');
|
|
||||||
const {manifest, variants} = JSON.parse(fs.readFileSync(fileName));
|
|
||||||
return {manifest, variants};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createManifestString(manifest) {
|
|
||||||
return JSON.stringify(manifest, null, 4) + '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const {manifest, variants} = loadDefaultManifestAndVariants();
|
const {manifest, variants} = getDefaultManifestAndVariants();
|
||||||
|
|
||||||
const rootDir = path.join(__dirname, '..');
|
const rootDir = path.join(__dirname, '..');
|
||||||
const extDir = path.join(rootDir, 'ext');
|
const extDir = path.join(rootDir, 'ext');
|
||||||
@ -204,11 +171,4 @@ async function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
loadDefaultManifest,
|
|
||||||
loadDefaultManifestAndVariants,
|
|
||||||
createManifestString
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
if (require.main === module) { main(); }
|
if (require.main === module) { main(); }
|
||||||
|
79
dev/yomichan-util.js
Normal file
79
dev/yomichan-util.js
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2020 Yomichan Authors
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
|
||||||
|
let JSZip = null;
|
||||||
|
|
||||||
|
|
||||||
|
function getJSZip() {
|
||||||
|
if (JSZip === null) {
|
||||||
|
process.noDeprecation = true; // Suppress a warning about JSZip
|
||||||
|
JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js'));
|
||||||
|
process.noDeprecation = false;
|
||||||
|
}
|
||||||
|
return JSZip;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getAllFiles(baseDirectory, relativeTo=null, predicate=null) {
|
||||||
|
const results = [];
|
||||||
|
const directories = [baseDirectory];
|
||||||
|
while (directories.length > 0) {
|
||||||
|
const directory = directories.shift();
|
||||||
|
const fileNames = fs.readdirSync(directory);
|
||||||
|
for (const fileName of fileNames) {
|
||||||
|
const fullFileName = path.join(directory, fileName);
|
||||||
|
const relativeFileName = (relativeTo !== null ? path.relative(relativeTo, fullFileName) : fullFileName);
|
||||||
|
const stats = fs.lstatSync(fullFileName);
|
||||||
|
if (stats.isFile()) {
|
||||||
|
if (typeof predicate !== 'function' || predicate(fullFileName, directory, baseDirectory)) {
|
||||||
|
results.push(relativeFileName);
|
||||||
|
}
|
||||||
|
} else if (stats.isDirectory()) {
|
||||||
|
directories.push(fullFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefaultManifest() {
|
||||||
|
const {manifest} = getDefaultManifestAndVariants();
|
||||||
|
return manifest;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefaultManifestAndVariants() {
|
||||||
|
const fileName = path.join(__dirname, 'data', 'manifest-variants.json');
|
||||||
|
const {manifest, variants} = JSON.parse(fs.readFileSync(fileName));
|
||||||
|
return {manifest, variants};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createManifestString(manifest) {
|
||||||
|
return JSON.stringify(manifest, null, 4) + '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
get JSZip() { return getJSZip(); },
|
||||||
|
getAllFiles,
|
||||||
|
getDefaultManifest,
|
||||||
|
getDefaultManifestAndVariants,
|
||||||
|
createManifestString
|
||||||
|
};
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const {JSZip} = require('./yomichan-test');
|
const {JSZip} = require('../dev/yomichan-util');
|
||||||
const {VM} = require('./yomichan-vm');
|
const {VM} = require('./yomichan-vm');
|
||||||
|
|
||||||
const vm = new VM();
|
const vm = new VM();
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const {getAllFiles} = require('../yomichan-test');
|
const {getAllFiles} = require('../../dev/yomichan-util');
|
||||||
|
|
||||||
|
|
||||||
function countOccurences(string, pattern) {
|
function countOccurences(string, pattern) {
|
||||||
@ -116,7 +116,7 @@ function main() {
|
|||||||
const directory = path.resolve(__dirname, '..', '..', 'ext');
|
const directory = path.resolve(__dirname, '..', '..', 'ext');
|
||||||
const pattern = /\.js$/;
|
const pattern = /\.js$/;
|
||||||
const ignorePattern = /[\\/]ext[\\/]mixed[\\/]lib[\\/]/;
|
const ignorePattern = /[\\/]ext[\\/]mixed[\\/]lib[\\/]/;
|
||||||
const fileNames = getAllFiles(directory, (f) => pattern.test(f) && !ignorePattern.test(f));
|
const fileNames = getAllFiles(directory, null, (f) => pattern.test(f) && !ignorePattern.test(f));
|
||||||
for (const fileName of fileNames) {
|
for (const fileName of fileNames) {
|
||||||
if (!validateGlobals(fileName, fix)) {
|
if (!validateGlobals(fileName, fix)) {
|
||||||
process.exit(-1);
|
process.exit(-1);
|
||||||
|
@ -19,7 +19,8 @@ const fs = require('fs');
|
|||||||
const url = require('url');
|
const url = require('url');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const yomichanTest = require('./yomichan-test');
|
const {JSZip} = require('../dev/yomichan-util');
|
||||||
|
const {createTestDictionaryArchive} = require('./yomichan-test');
|
||||||
const {VM} = require('./yomichan-vm');
|
const {VM} = require('./yomichan-vm');
|
||||||
require('fake-indexeddb/auto');
|
require('fake-indexeddb/auto');
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ const vm = new VM({
|
|||||||
fetch,
|
fetch,
|
||||||
indexedDB: global.indexedDB,
|
indexedDB: global.indexedDB,
|
||||||
IDBKeyRange: global.IDBKeyRange,
|
IDBKeyRange: global.IDBKeyRange,
|
||||||
JSZip: yomichanTest.JSZip,
|
JSZip,
|
||||||
addEventListener() {
|
addEventListener() {
|
||||||
// NOP
|
// NOP
|
||||||
}
|
}
|
||||||
@ -169,7 +170,7 @@ function clearDatabase(timeout) {
|
|||||||
|
|
||||||
async function testDatabase1() {
|
async function testDatabase1() {
|
||||||
// Load dictionary data
|
// Load dictionary data
|
||||||
const testDictionary = yomichanTest.createTestDictionaryArchive('valid-dictionary1');
|
const testDictionary = createTestDictionaryArchive('valid-dictionary1');
|
||||||
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
||||||
const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string'));
|
const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string'));
|
||||||
|
|
||||||
@ -853,7 +854,7 @@ async function testFindTagForTitle1(database, title) {
|
|||||||
|
|
||||||
async function testDatabase2() {
|
async function testDatabase2() {
|
||||||
// Load dictionary data
|
// Load dictionary data
|
||||||
const testDictionary = yomichanTest.createTestDictionaryArchive('valid-dictionary1');
|
const testDictionary = createTestDictionaryArchive('valid-dictionary1');
|
||||||
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
||||||
const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string'));
|
const testDictionaryIndex = JSON.parse(await testDictionary.files['index.json'].async('string'));
|
||||||
|
|
||||||
@ -910,7 +911,7 @@ async function testDatabase3() {
|
|||||||
await dictionaryDatabase.prepare();
|
await dictionaryDatabase.prepare();
|
||||||
|
|
||||||
for (const invalidDictionary of invalidDictionaries) {
|
for (const invalidDictionary of invalidDictionaries) {
|
||||||
const testDictionary = yomichanTest.createTestDictionaryArchive(invalidDictionary);
|
const testDictionary = createTestDictionaryArchive(invalidDictionary);
|
||||||
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
const testDictionarySource = await testDictionary.generateAsync({type: 'string'});
|
||||||
|
|
||||||
let error = null;
|
let error = null;
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const yomichanTest = require('./yomichan-test');
|
const {createTestDictionaryArchive} = require('./yomichan-test');
|
||||||
const dictionaryValidate = require('./dictionary-validate');
|
const dictionaryValidate = require('./dictionary-validate');
|
||||||
|
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ async function main() {
|
|||||||
const schemas = dictionaryValidate.getSchemas();
|
const schemas = dictionaryValidate.getSchemas();
|
||||||
|
|
||||||
for (const {name, valid} of dictionaries) {
|
for (const {name, valid} of dictionaries) {
|
||||||
const archive = yomichanTest.createTestDictionaryArchive(name);
|
const archive = createTestDictionaryArchive(name);
|
||||||
|
|
||||||
let error = null;
|
let error = null;
|
||||||
try {
|
try {
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const {loadDefaultManifest, createManifestString} = require('../dev/build');
|
const {getDefaultManifest, createManifestString} = require('../dev/yomichan-util');
|
||||||
|
|
||||||
|
|
||||||
function loadManifestString() {
|
function loadManifestString() {
|
||||||
@ -28,7 +28,7 @@ function loadManifestString() {
|
|||||||
|
|
||||||
function validateManifest() {
|
function validateManifest() {
|
||||||
const manifest1 = loadManifestString();
|
const manifest1 = loadManifestString();
|
||||||
const manifest2 = createManifestString(loadDefaultManifest());
|
const manifest2 = createManifestString(getDefaultManifest());
|
||||||
assert.strictEqual(manifest1, manifest2, 'Manifest data does not match.');
|
assert.strictEqual(manifest1, manifest2, 'Manifest data does not match.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,23 +19,12 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
|
|
||||||
let JSZip = null;
|
|
||||||
|
|
||||||
|
|
||||||
function getJSZip() {
|
|
||||||
if (JSZip === null) {
|
|
||||||
process.noDeprecation = true; // Suppress a warning about JSZip
|
|
||||||
JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js'));
|
|
||||||
process.noDeprecation = false;
|
|
||||||
}
|
|
||||||
return JSZip;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createTestDictionaryArchive(dictionary, dictionaryName) {
|
function createTestDictionaryArchive(dictionary, dictionaryName) {
|
||||||
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary);
|
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary);
|
||||||
const fileNames = fs.readdirSync(dictionaryDirectory);
|
const fileNames = fs.readdirSync(dictionaryDirectory);
|
||||||
|
|
||||||
const archive = new (getJSZip())();
|
const {JSZip} = require('../dev/yomichan-util');
|
||||||
|
const archive = new JSZip();
|
||||||
|
|
||||||
for (const fileName of fileNames) {
|
for (const fileName of fileNames) {
|
||||||
if (/\.json$/.test(fileName)) {
|
if (/\.json$/.test(fileName)) {
|
||||||
@ -54,29 +43,7 @@ function createTestDictionaryArchive(dictionary, dictionaryName) {
|
|||||||
return archive;
|
return archive;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllFiles(baseDirectory, predicate=null) {
|
|
||||||
const results = [];
|
|
||||||
const directories = [path.resolve(baseDirectory)];
|
|
||||||
while (directories.length > 0) {
|
|
||||||
const directory = directories.shift();
|
|
||||||
for (const fileName of fs.readdirSync(directory)) {
|
|
||||||
const fullFileName = path.resolve(directory, fileName);
|
|
||||||
const stats = fs.statSync(fullFileName);
|
|
||||||
if (stats.isFile()) {
|
|
||||||
if (typeof predicate !== 'function' || predicate(fullFileName, directory, baseDirectory)) {
|
|
||||||
results.push(fullFileName);
|
|
||||||
}
|
|
||||||
} else if (stats.isDirectory()) {
|
|
||||||
directories.push(fullFileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createTestDictionaryArchive,
|
createTestDictionaryArchive
|
||||||
getAllFiles,
|
|
||||||
get JSZip() { return getJSZip(); }
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user