Update dependencies and code style

This commit is contained in:
Domenic Denicola 2017-08-13 16:38:42 -04:00
commit 0435c45b2e
7 changed files with 108 additions and 140 deletions

View file

@ -3,40 +3,39 @@ const path = require("path");
const fs = require("mz/fs");
const throat = require("throat");
const serializeToXML = require("xmlserializer").serializeToString;
const jsdom = require("./jsdom.js");
const { JSDOM } = require("jsdom");
const substitutions = require("./substitutions.json");
module.exports = (cachePath, manifestPath, contentPath) => {
return fs.readFile(manifestPath, { encoding: "utf-8" }).then(manifestContents => {
const chapters = JSON.parse(manifestContents);
console.log("All chapters downloaded; beginning conversion to EPUB chapters");
module.exports = async (cachePath, manifestPath, contentPath) => {
const manifestContents = await fs.readFile(manifestPath, { encoding: "utf-8" });
const chapters = JSON.parse(manifestContents);
console.log("All chapters downloaded; beginning conversion to EPUB chapters");
const mapper = throat(10, chapter => {
return convertChapter(chapter, cachePath, contentPath);
});
return Promise.all(chapters.map(mapper));
})
.then(() => console.log("All chapters converted"));
const mapper = throat(10, chapter => convertChapter(chapter, cachePath, contentPath));
await Promise.all(chapters.map(mapper));
console.log("All chapters converted");
};
function convertChapter(chapter, cachePath, contentPath) {
async function convertChapter(chapter, cachePath, contentPath) {
const filename = chapter.filename;
const filePath = path.resolve(cachePath, filename);
console.log(`- Reading ${filename}`);
return fs.readFile(filePath, { encoding: "utf-8" }).then(contents => {
console.log(`- Read ${filename}`);
const rawChapterDoc = jsdom(contents);
const output = getChapterString(chapter, rawChapterDoc);
const contents = await fs.readFile(filePath, { encoding: "utf-8" });
console.log(`- Read ${filename}`);
// TODO: this should probably not be necessary... jsdom bug I guess!?
rawChapterDoc.defaultView.close();
const rawChapterJSDOM = new JSDOM(contents);
const output = getChapterString(chapter, rawChapterJSDOM.window.document);
const destFileName = `${path.basename(filename, ".html")}.xhtml`;
const destFilePath = path.resolve(contentPath, destFileName);
return fs.writeFile(destFilePath, output);
})
.then(() => console.log(`- Finished converting ${filename}`));
// TODO: this should probably not be necessary... jsdom bug I guess!?
rawChapterJSDOM.window.close();
const destFileName = `${path.basename(filename, ".html")}.xhtml`;
const destFilePath = path.resolve(contentPath, destFileName);
await fs.writeFile(destFilePath, output);
console.log(`- Finished converting ${filename}`);
}
function getChapterString(chapter, rawChapterDoc) {