-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
56 lines (48 loc) · 1.46 KB
/
utils.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
const chalk = require("chalk");
const _ = require("underscore");
const fs = require("fs");
const checkUrl = (url) => {
const regex = /(https?:\/\/(.+?\.)?mercadolibre\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)/g;
return url.match(regex);
};
const showMessage = (msg, color) => {
if (!_.isUndefined(color) || !_.isNull(color)) {
console.log(chalk.keyword(color)(`${msg}`));
} else {
console.log(chalk.green(`${msg}`));
}
};
const errorLogger = (msg) => {
console.log(chalk.red(`Error: ${msg}`));
};
const createJsonFile = (data) => {
createFileReport("results", "json", JSON.stringify(data));
};
const createHtmlFile = (data, info) => {
let rawData = fs.readFileSync("./template.html", "utf8");
rawData = rawData.toString();
rawData = rawData.replace("$data", JSON.stringify(data));
rawData = rawData.replace("$url", info.url);
rawData = rawData.replace("$products", info.products);
rawData = rawData.replace("$items", info.items);
rawData = rawData.replace("$similarity", info.similarity);
createFileReport("report", "html", rawData);
};
const createFileReport = (title, format, data) => {
const fileName = `${title}.${format}`;
fs.writeFile(fileName, data, function (err) {
if (err) {
console.log(err);
} else {
showMessage(`-- Reporte Generado - ${fileName} --`, "white");
}
});
};
module.exports = {
checkUrl,
showMessage,
errorLogger,
createJsonFile,
createHtmlFile,
createFileReport,
};