diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-08-09 21:00:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-09 21:00:37 -0400 |
commit | 7b1838a282f4ac5c50e0ea36122751c3454266aa (patch) | |
tree | 1fb73929e4ce6ae23842027f45d305335632c351 /dev | |
parent | 486d44f7197a2dedc5ea487345e598c668d47638 (diff) |
Create dev utility class for some shared functionality (#724)
Diffstat (limited to 'dev')
-rw-r--r-- | dev/build.js | 50 | ||||
-rw-r--r-- | dev/yomichan-util.js | 79 |
2 files changed, 84 insertions, 45 deletions
diff --git a/dev/build.js b/dev/build.js index 6b62083e..efa04bd5 100644 --- a/dev/build.js +++ b/dev/build.js @@ -19,27 +19,9 @@ const fs = require('fs'); const path = require('path'); const readline = require('readline'); const childProcess = require('child_process'); -const yomichanTest = require('../test/yomichan-test'); - - -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; -} +const util = require('./yomichan-util'); +const {getAllFiles, getDefaultManifestAndVariants, createManifestString} = util; + async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=null) { for (const exe of sevenZipExes) { @@ -64,7 +46,7 @@ async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=nu } async function createJSZip(directory, outputFileName, onUpdate) { - const JSZip = yomichanTest.JSZip; + const JSZip = util.JSZip; const files = getAllFiles(directory, directory); const zip = new JSZip(); for (const fileName of files) { @@ -134,24 +116,9 @@ function getObjectProperties(object, path2, count) { 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() { - const {manifest, variants} = loadDefaultManifestAndVariants(); + const {manifest, variants} = getDefaultManifestAndVariants(); const rootDir = path.join(__dirname, '..'); const extDir = path.join(rootDir, 'ext'); @@ -204,11 +171,4 @@ async function main() { } -module.exports = { - loadDefaultManifest, - loadDefaultManifestAndVariants, - createManifestString -}; - - if (require.main === module) { main(); } diff --git a/dev/yomichan-util.js b/dev/yomichan-util.js new file mode 100644 index 00000000..2d8f31ea --- /dev/null +++ b/dev/yomichan-util.js @@ -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 +}; |