-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·52 lines (42 loc) · 1.49 KB
/
index.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
#!/usr/bin/env node
var downloader = require('./lib/downloader'),
converter = require('./lib/converter'),
program = require('commander'),
fs = require('fs');
//Help topic
program
.version('0.1.0')
.option('-o --output [filename]', 'Set output file(output.json by default)')
.option('-q --quiet', 'Do not display output to console')
.description('Parse provided website')
//Main function
.action(function(website) {
var response = downloader.downloadPage(website);
var result = JSON.stringify(converter.convertToJSON(response), null, 2);
if (program.output) {
var filename = (program.output) ? program.output : "output.json";
fs.writeFileSync(filename, result);
}
if(program.quiet === undefined) {
console.log(result);
}
});
// Examples
program.on('--help', function(){
console.log(' Examples:');
console.log('\n #parse and display output');
console.log(' html2json https://google.com\n');
console.log(' #parse and save output to google.json');
console.log(' html2json -o google.json https://google.com\n');
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}
//Export module
module.exports = {
convertPage: function(page) {
var response = downloader.downloadPage(page);
return JSON.stringify(converter.convertToJSON(response, program.verbose), null, 2);
}
};