Create yomichan-test script to reduce repeated code
This commit is contained in:
parent
7cbebf6897
commit
06480751a9
@ -1,13 +1,9 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const yomichanTest = require('./yomichan-test');
|
||||||
|
|
||||||
process.noDeprecation = true; // Suppress a warning about JSZip
|
const JSZip = yomichanTest.JSZip;
|
||||||
const JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js'));
|
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
|
||||||
process.noDeprecation = false;
|
|
||||||
|
|
||||||
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
|
|
||||||
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
|
|
||||||
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
|
|
||||||
|
|
||||||
|
|
||||||
function readSchema(relativeFileName) {
|
function readSchema(relativeFileName) {
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const yomichanTest = require('./yomichan-test');
|
||||||
|
|
||||||
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
|
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
|
||||||
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
|
|
||||||
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
|
|
||||||
|
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const yomichanTest = require('./yomichan-test');
|
||||||
|
|
||||||
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
|
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
|
||||||
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
|
|
||||||
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
|
|
||||||
|
|
||||||
|
|
||||||
function testValidate1() {
|
function testValidate1() {
|
||||||
|
33
test/yomichan-test.js
Normal file
33
test/yomichan-test.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
|
||||||
|
let JSZip = null;
|
||||||
|
|
||||||
|
function requireScript(fileName, exportNames, variables) {
|
||||||
|
const absoluteFileName = path.join(__dirname, '..', fileName);
|
||||||
|
const source = fs.readFileSync(absoluteFileName, {encoding: 'utf8'});
|
||||||
|
const exportNamesString = Array.isArray(exportNames) ? exportNames.join(',') : '';
|
||||||
|
const variablesArgumentName = '__variables__';
|
||||||
|
let variableString = '';
|
||||||
|
if (typeof variables === 'object' && variables !== null) {
|
||||||
|
variableString = Object.keys(variables).join(',');
|
||||||
|
variableString = `const {${variableString}} = ${variablesArgumentName};`;
|
||||||
|
}
|
||||||
|
return Function(variablesArgumentName, `'use strict';${variableString}${source}\n;return {${exportNamesString}};`)(variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
requireScript,
|
||||||
|
get JSZip() { return getJSZip(); }
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user