Make a nicer command-line interface
This commit is contained in:
parent
46e38a2859
commit
59b82e7ab4
8 changed files with 101 additions and 28 deletions
|
|
@ -2,37 +2,100 @@
|
|||
const path = require("path");
|
||||
const mkdirp = require("mkdirp-then");
|
||||
const rimraf = require("rimraf-then");
|
||||
const yargs = require("yargs");
|
||||
const cpr = require("thenify")(require("cpr"));
|
||||
|
||||
const packageJson = require("../package.json");
|
||||
const download = require("./download.js");
|
||||
const convert = require("./convert.js");
|
||||
const extras = require("./extras.js");
|
||||
const scaffold = require("./scaffold.js");
|
||||
|
||||
require("./track-rejections.js");
|
||||
|
||||
const START_CHAPTER_URL = "https://parahumans.wordpress.com/2011/06/11/1-1/";
|
||||
const argv = yargs
|
||||
.usage(`${packageJson.description}\n\n${packageJson.name} [<command1> [<command2> [<command3> ...]]]\n\n` +
|
||||
"Each command will fail if the previously-listed one has not yet been run (with matching options).")
|
||||
.command("download", "download all chapters by crawling parahumans.wordpress.com")
|
||||
.command("convert", "convert the raw chapter HTML files into cleaned-up ebook chapters")
|
||||
.command("scaffold", "assemble the table of contents, etc. to complete the EPUB")
|
||||
.command("zip", "zip up the EPUB files into a .epub output")
|
||||
.option("s", {
|
||||
alias: "start-url",
|
||||
default: "https://parahumans.wordpress.com/2011/06/11/1-1/",
|
||||
describe: "the URL from which to start crawling, for the download command",
|
||||
requiresArg: true
|
||||
})
|
||||
.option("c", {
|
||||
alias: "cache-directory",
|
||||
default: "cache",
|
||||
describe: "cache directory, for the download and convert commands",
|
||||
requiresArg: true
|
||||
})
|
||||
.option("b", {
|
||||
alias: "book-directory",
|
||||
default: "book",
|
||||
describe: "directory in which to assemble the EPUB files before zipping, for the convert, scaffold, and zip " +
|
||||
"commands",
|
||||
requiresArg: true
|
||||
})
|
||||
.option("o", {
|
||||
alias: "out",
|
||||
default: "Worm.epub",
|
||||
describe: "output file destination, for the zip command",
|
||||
requiresArg: true
|
||||
})
|
||||
.require(1) // TODO remove and allow all
|
||||
.addHelpOpt("help")
|
||||
.version(packageJson.version, "version")
|
||||
.argv;
|
||||
|
||||
const cachePath = path.resolve("cache");
|
||||
const outPath = path.resolve("out");
|
||||
const contentPath = path.resolve(outPath, "OEBPS");
|
||||
const chaptersPath = path.resolve(contentPath, "chapters");
|
||||
const cachePath = path.resolve(argv.cacheDirectory);
|
||||
const manifestPath = path.resolve(cachePath, "manifest.json");
|
||||
|
||||
Promise.resolve()
|
||||
// .then(function () {
|
||||
// return download(START_CHAPTER_URL, cachePath, manifestPath);
|
||||
// })
|
||||
// .then(function () {
|
||||
// return rimraf(chaptersPath);
|
||||
// })
|
||||
// .then(function () {
|
||||
// return mkdirp(chaptersPath);
|
||||
// })
|
||||
.then(function () {
|
||||
return convert(cachePath, manifestPath, chaptersPath);
|
||||
})
|
||||
.then(function () {
|
||||
return extras(contentPath, chaptersPath, manifestPath);
|
||||
})
|
||||
.then(function () {
|
||||
console.log("All done!");
|
||||
const scaffoldingPath = path.resolve(__dirname, "../scaffolding");
|
||||
const outPath = path.resolve(argv.bookDirectory);
|
||||
const contentPath = path.resolve(outPath, "OEBPS");
|
||||
const chaptersPath = path.resolve(contentPath, "chapters");
|
||||
|
||||
const commands = [];
|
||||
|
||||
if (argv._.indexOf("download") !== -1) {
|
||||
commands.push(function () {
|
||||
return download(argv.startUrl, cachePath, manifestPath);
|
||||
});
|
||||
}
|
||||
|
||||
if (argv._.indexOf("convert") !== -1) {
|
||||
commands.push(function () {
|
||||
return rimraf(chaptersPath).then(function () {
|
||||
return mkdirp(chaptersPath);
|
||||
})
|
||||
.then(function () {
|
||||
return convert(cachePath, manifestPath, chaptersPath);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (argv._.indexOf("scaffold") !== -1) {
|
||||
commands.push(function () {
|
||||
return cpr(scaffoldingPath, outPath).then(function () {
|
||||
return scaffold(contentPath, chaptersPath, manifestPath);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (argv._.indexOf("zip") !== -1) {
|
||||
commands.push(function () {
|
||||
console.log("Not yet implemented!");
|
||||
});
|
||||
}
|
||||
|
||||
commands.reduce(function (previous, command) {
|
||||
return previous.then(command);
|
||||
}, Promise.resolve())
|
||||
.then(function () {
|
||||
console.log("All done!");
|
||||
})
|
||||
.catch(function (e) {
|
||||
console.error(e.stack);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue