-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (99 loc) · 2.55 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const inputUrl = require('./input-url');
const remoteHtml = require('./remote-html');
const actions = require('./actions');
const fs = require('fs');
const Spinner = require('cli-spinner').Spinner;
let loading;
/**
* Shows CLI loading text
*/
function showLoading(){
loading = new Spinner("A carregar...");
loading.setSpinnerString(18);
loading.start();
}
/**
* Hides CLI loading text if it's showing
*/
function hideLoading(){
if( loading && loading.isSpinning() ){
loading.stop(true);
}
}
/**
* Write JSON output into a file and exit
* @param obj - JSON object to write in output file
*/
function writeOutput(obj){
fs.writeFile('./output.json', JSON.stringify(obj, null, 2), function(err){
if(err){
console.log("Ocurreu um erro a criar o ficheiro de output");
} else {
console.log("Ficheiro output.json criado");
}
process.exit();
});
}
/**
* Get, parse and analyze HTML from given url
* @param {String} url - URL to get the HTML from
*/
function start(url){
if(!url){
console.log("Por favor insira um URL");
getData(true);
return;
}
showLoading();
remoteHtml.getBody(url).then(
async (body) => {
const parsedBody = remoteHtml.parseHTML(body);
if( !parsedBody ){
console.log("Ocurreu um erro a ler o HTML");
getData(true);
return;
};
let finObj = {};
finObj.treeDepth = actions.getTreeDepth(parsedBody);
finObj.elementCount = actions.countElements(parsedBody);
finObj.attributesCount = actions.countAtributesByTag(parsedBody);
finObj.childrenCount = actions.getChildrenByTag(parsedBody);
finObj.loadedResources = await actions.getLoadedResources(parsedBody, url, true);
hideLoading();
console.log("-----JSON----");
console.log(JSON.stringify(finObj, null, 2));
console.log("-------------");
writeOutput(finObj);
},
(err) => {
getData(true);
}
);
}
/**
* Starter function to get the URL
* @param {Boolean} ignoreArg - Choose if program should ignore the URL provided via CLI
*/
function getData(ignoreArg = false){
hideLoading();
// Check if url was provided
if( process.argv.length > 2 && inputUrl.validateUrl(process.argv[2]) && !ignoreArg ){
start(process.argv[2]);
} else {
inputUrl.getUrlInput()
.then(
(url) => {
if( !url || !inputUrl.validateUrl(url) ){
console.log("URL Inválido");
getData(true);
} else {
start(url);
}
},
(err) => {
getData(true);
}
);
}
}
getData();