-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·68 lines (54 loc) · 1.82 KB
/
index.js
File metadata and controls
executable file
·68 lines (54 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
// import modules
import Cli from './src/Cli.js';
import Crawler from './src/Crawler.js';
import PDFMerger from 'pdf-merger-js';
import PdfGenerator from './src/PdfGenerator.js';
import commandExists from 'command-exists';
import log from './src/tools.js';
import fs from 'fs';
// get the arguments
const argv = Cli.argv;
// parse the url
const url = Cli.argv.url?.replace(/\/$/, '') || 'https://docusaurus.io/docs';
// Output file
argv.dest = argv.dest || process.cwd() + '/pdf';
const parsedUrl = new URL(url);
const baseUrl = parsedUrl.origin;
const scope = parsedUrl.pathname;
const scopeName = scope !== '/' ? `-${scope.replace(/\/$/, '').replace(/^\//, '').replace(/\//, '-')}` : '';
const listFile = argv.file || `${argv.dest}/${parsedUrl.hostname}${scopeName}.txt`;
const pdfFile = argv.output || `${argv.dest}/${parsedUrl.hostname}${scopeName}.pdf`;
const merger = new PDFMerger();
const pdfGenerator = new PdfGenerator(merger);
const crawler = new Crawler(pdfGenerator, parsedUrl, listFile, pdfFile);
// do a sanity check for required software
let commands = [
'wkhtmltopdf',
];
if (argv.compress) {
commands.push('gs')
}
let promises = [];
commands.forEach((command) => {
promises.push(commandExists(command));
});
if (Cli.argv.sdtout) {
Cli.argv.quiet = true;
}
Promise.all(promises).then(() => {
// All is good. Make output folder
!fs.existsSync(argv.dest) && fs.mkdirSync(argv.dest);
if (argv.pdfOnly) {
// generate the pdf without crawling
pdfGenerator.generate(listFile, pdfFile);
} else {
// crawl the starting page
crawler.requestPage(`${baseUrl}${scope}`);
}
}).catch(err => {
// throw error and exit
log(`Error: the following software must be installed on this machine: ` + commands);
log(err);
process.exit(11);
});