UNPKG

3.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const debug_ = require("debug");
5const filehound = require("filehound");
6const fs = require("fs");
7const path = require("path");
8const lcp_1 = require("r2-lcp-js/dist/es7-es2016/src/parser/epub/lcp");
9const init_globals_1 = require("r2-opds-js/dist/es7-es2016/src/opds/init-globals");
10const init_globals_2 = require("r2-shared-js/dist/es7-es2016/src/init-globals");
11const epub_1 = require("r2-shared-js/dist/es7-es2016/src/parser/epub");
12const server_1 = require("./server");
13init_globals_1.initGlobalConverters_OPDS();
14init_globals_2.initGlobalConverters_SHARED();
15init_globals_2.initGlobalConverters_GENERIC();
16lcp_1.setLcpNativePluginPath(path.join(process.cwd(), "LCP", "lcp.node"));
17const debug = debug_("r2:streamer#http/server-cli");
18debug(`process.cwd(): ${process.cwd()}`);
19debug(`__dirname: ${__dirname}`);
20const args = process.argv.slice(2);
21debug("process.argv.slice(2): %o", args);
22if (!args[0]) {
23 debug("FILEPATH ARGUMENT IS MISSING.");
24 process.exit(1);
25}
26const argPath = args[0].trim();
27let filePath = argPath;
28debug(`path: ${filePath}`);
29if (!fs.existsSync(filePath)) {
30 filePath = path.join(__dirname, argPath);
31 debug(`path: ${filePath}`);
32 if (!fs.existsSync(filePath)) {
33 filePath = path.join(process.cwd(), argPath);
34 debug(`path: ${filePath}`);
35 if (!fs.existsSync(filePath)) {
36 debug("FILEPATH DOES NOT EXIST.");
37 process.exit(1);
38 }
39 }
40}
41filePath = fs.realpathSync(filePath);
42debug(`path (normalized): ${filePath}`);
43const stats = fs.lstatSync(filePath);
44if (!stats.isFile() && !stats.isDirectory()) {
45 debug("FILEPATH MUST BE FILE OR DIRECTORY.");
46 process.exit(1);
47}
48let maxPrefetchLinks = server_1.MAX_PREFETCH_LINKS;
49if (args[1]) {
50 args[1] = args[1].trim();
51 if (args[1].length && args[1][0] === "-") {
52 maxPrefetchLinks = -1;
53 }
54 else {
55 try {
56 maxPrefetchLinks = parseInt(args[1], 10);
57 }
58 catch (err) {
59 debug(err);
60 }
61 if (isNaN(maxPrefetchLinks)) {
62 maxPrefetchLinks = server_1.MAX_PREFETCH_LINKS;
63 }
64 }
65}
66debug(`maxPrefetchLinks: ${maxPrefetchLinks}`);
67const isAnEPUB = epub_1.isEPUBlication(filePath);
68if (stats.isDirectory() && (isAnEPUB !== epub_1.EPUBis.LocalExploded)) {
69 debug("Analysing directory...");
70 (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
71 const files = yield filehound.create()
72 .discard("node_modules")
73 .depth(5)
74 .paths(filePath)
75 .ext([".epub", ".epub3", ".cbz", ".audiobook", ".lcpaudiobook", ".lcpa", ".divina", ".lcpdivina"])
76 .find();
77 const server = new server_1.Server({
78 maxPrefetchLinks,
79 });
80 server.preventRobots();
81 server.addPublications(files);
82 const url = yield server.start(0, false);
83 debug(url);
84 }))();
85}
86else {
87 (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
88 const server = new server_1.Server({
89 maxPrefetchLinks,
90 });
91 server.preventRobots();
92 server.addPublications([filePath]);
93 const url = yield server.start(0, false);
94 debug(url);
95 }))();
96}
97//# sourceMappingURL=server-cli.js.map
\No newline at end of file