-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
79 lines (65 loc) · 2.02 KB
/
run.js
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
69
70
71
72
73
74
75
76
77
78
var fs = require('fs');
const { exit } = require('process');
const { hideBin } = require('yargs/helpers')
const yargs = require('yargs');
var collectorMatch = false;
var formatterMatch = false;
var collectorList = [];
var formatterList = [];
var selectedFormatter;
// Get list of available formatters
fs.readdirSync("formatters").forEach(file => {
if (file.endsWith(".js")) {
var formatter = require(`./formatters/${file}`);
formatterList.push(formatter.formatterId);
}
});
// Define Command Line Arguments
const options = yargs
.usage('Usage: $0 [options]')
.option('url', {
describe: 'URL of the work',
type: 'string',
demandOption: true
})
.option('format', {
describe: 'Which formatter to use',
type: 'string',
demandOption: true,
choices: formatterList
})
.option('skip-toc', {
describe: 'Skips generating the Table of Contents',
type: 'boolean',
default: false
})
.help()
.argv;
console.log(options);
// Check to see if the given formatter exists
fs.readdirSync("formatters").forEach(file => {
if (file.endsWith(".js")) {
var formatter = require(`./formatters/${file}`);
if (options.format == formatter.formatterId) {
console.log(`INFO: Matched formatter with ${formatter.formatterName}`);
formatterMatch = true;
selectedFormatter = formatter
}
}
});
// Match the URL with the right collector
fs.readdirSync("collectors").forEach(async file => {
if (file.endsWith(".js")) {
var collector = require(`./collectors/${file}`);
collectorList.push(collector.collectorName);
if (options.url.match(collector.collectorRegex)) {
console.log(`INFO: Matched url with ${collector.collectorName}`);
collectorMatch = true;
var data = await collector.process(options.url);
selectedFormatter.format(data, options);
}
}
});
if (!collectorMatch) {
console.error("ERROR: Count not find a matching collector for url "+options.url)
}