UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var debug_ = require("debug");
5var filehound = require("filehound");
6var fs = require("fs");
7var path = require("path");
8var lcp_1 = require("r2-lcp-js/dist/es5/src/parser/epub/lcp");
9var init_globals_1 = require("r2-opds-js/dist/es5/src/opds/init-globals");
10var init_globals_2 = require("r2-shared-js/dist/es5/src/init-globals");
11var epub_1 = require("r2-shared-js/dist/es5/src/parser/epub");
12var 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"));
17var debug = debug_("r2:streamer#http/server-cli");
18debug("process.cwd(): " + process.cwd());
19debug("__dirname: " + __dirname);
20var 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}
26var argPath = args[0].trim();
27var 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);
43var stats = fs.lstatSync(filePath);
44if (!stats.isFile() && !stats.isDirectory()) {
45 debug("FILEPATH MUST BE FILE OR DIRECTORY.");
46 process.exit(1);
47}
48var 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);
67var isAnEPUB = epub_1.isEPUBlication(filePath);
68if (stats.isDirectory() && (isAnEPUB !== epub_1.EPUBis.LocalExploded)) {
69 debug("Analysing directory...");
70 (function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
71 var files, server, url;
72 return tslib_1.__generator(this, function (_a) {
73 switch (_a.label) {
74 case 0: return [4, filehound.create()
75 .discard("node_modules")
76 .depth(5)
77 .paths(filePath)
78 .ext([".epub", ".epub3", ".cbz", ".audiobook", ".lcpaudiobook", ".lcpa", ".divina", ".lcpdivina"])
79 .find()];
80 case 1:
81 files = _a.sent();
82 server = new server_1.Server({
83 maxPrefetchLinks: maxPrefetchLinks,
84 });
85 server.preventRobots();
86 server.addPublications(files);
87 return [4, server.start(0, false)];
88 case 2:
89 url = _a.sent();
90 debug(url);
91 return [2];
92 }
93 });
94 }); })();
95}
96else {
97 (function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
98 var server, url;
99 return tslib_1.__generator(this, function (_a) {
100 switch (_a.label) {
101 case 0:
102 server = new server_1.Server({
103 maxPrefetchLinks: maxPrefetchLinks,
104 });
105 server.preventRobots();
106 server.addPublications([filePath]);
107 return [4, server.start(0, false)];
108 case 1:
109 url = _a.sent();
110 debug(url);
111 return [2];
112 }
113 });
114 }); })();
115}
116//# sourceMappingURL=server-cli.js.map
\No newline at end of file