"use strict"; const fs = require("mz/fs"); const path = require("path"); const cpr = require("thenify")(require("cpr")); const BOOK_TITLE = "Worm"; const BOOK_AUTHOR = "wildbow"; const BOOK_PUBLISHER = "Domenic Denicola"; const BOOK_ID = "urn:uuid:e7f3532d-8db6-4888-be80-1976166b7059"; // First paragraph of https://parahumans.wordpress.com/about/ const BOOK_DESCRIPTION = ` An introverted teenage girl with an unconventional superpower, Taylor goes out in costume to find escape from a deeply unhappy and frustrated civilian life. Her first attempt at taking down a supervillain sees her mistaken for one, thrusting her into the midst of the local ‘cape’ scene’s politics, unwritten rules, and ambiguous morals. As she risks life and limb, Taylor faces the dilemma of having to do the wrong things for the right reasons.`; const NCX_FILENAME = "toc.ncx"; const COVER_IMG_FILENAME = "cover.png"; const COVER_XHTML_FILENAME = "cover.xhtml"; const COVER_MIMETYPE = "image/png"; module.exports = async (scaffoldingPath, bookPath, contentPath, chaptersPath, manifestPath) => { await Promise.all([ cpr(scaffoldingPath, bookPath, { overwrite: true, confirm: true, filter: noThumbs }), getChapters(contentPath, chaptersPath, manifestPath).then(chapters => { return Promise.all([ writeOPF(chapters, contentPath), writeNcx(chapters, contentPath) ]); }) ]); }; function noThumbs(filePath) { // Thumbs.db causes the strangest errors as Windows has it locked a lot of the time. return path.basename(filePath) !== "Thumbs.db"; } function writeOPF(chapters, contentPath) { const manifestChapters = chapters.map(c => { return ``; }).join("\n"); const spineChapters = chapters.map(c => { return ``; }).join("\n"); const contents = ` ${BOOK_TITLE} en ${BOOK_ID} ${BOOK_AUTHOR} ${BOOK_PUBLISHER} ${BOOK_DESCRIPTION} ${manifestChapters} ${spineChapters} `; return fs.writeFile(path.resolve(contentPath, "content.opf"), contents); } function writeNcx(chapters, contentPath) { const navPoints = chapters.map((c, i) => { return ` ${c.title} `; }).join("\n"); const contents = ` ${BOOK_TITLE} ${BOOK_AUTHOR} ${navPoints} `; return fs.writeFile(path.resolve(contentPath, NCX_FILENAME), contents); } async function getChapters(contentPath, chaptersPath, manifestPath) { const hrefPrefix = `${path.relative(contentPath, chaptersPath)}/`; const manifestContents = await fs.readFile(manifestPath, { encoding: "utf-8" }); const manifestChapters = JSON.parse(manifestContents); const filenames = await fs.readdir(chaptersPath); return filenames .filter(f => path.extname(f) === ".xhtml") .sort() .map((f, i) => { return { id: path.basename(f), title: manifestChapters[i].title, href: `${hrefPrefix}${f}` }; }); }