forked from Laboratoria/DEV005-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (89 loc) · 3.12 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
const {
isRouteAbs,
readFilesMD,
stats,
readDir,
} = require("./functions");
const Table = require("cli-table");
const mdLinks = (route, options) => {
return new Promise((resolve, reject) => {
// console.log("opciones", options);
const routeUser = isRouteAbs(route);
if (routeUser === "error") {
reject("Error: La ruta no existe");
}
const filesMD = readDir(routeUser);
// const fileContent = readFileMD(filesMD);
readFilesMD(filesMD)
.then((res) => {
// console.log(res)
// resolve(data.toString());
const data = res.flat();
// console.log(obj);
// console.log(options.validate);
// console.log(options.stats);
const table = new Table({
head: ["URL", "Text", "Route"], // Encabezados de la tabla
colWidths: [50, 20, 50], // Ancho de las columnas
});
const tableValidate = new Table({
head: ["URL", "Status", "Status Text", "Text", "Route"], // Encabezados de la tabla
colWidths: [50, 10, 10, 20, 50], // Ancho de las columnas
});
const tableStats = new Table({
head: ["Total", "Unique"], // Encabezados de la tabla
colWidths: [20, 20], // Ancho de las columnas
});
const tableStatsVS = new Table({
head: ["Total", "Unique", "Broken"], // Encabezados de la tabla
colWidths: [20, 20, 20], // Ancho de las columnas
});
if (options.validate === false && options.stats === false) {
data.forEach((e) => {
table.push([e.href, e.text, e.file]);
// console.log("-----------------")
// console.log(e.href);
// console.log(e.text);
// console.log(e.file);
// console.log("-----------------");
});
resolve(table.toString());
}
if (options.validate === true && options.stats === false) {
data.forEach((e) => {
tableValidate.push([
e.href,
e.status,
e.statusText,
e.text,
e.file,
]);
});
resolve(tableValidate.toString());
}
if (options.stats === true && options.validate === false) {
const { totalLinks, uniqueLinksArray, _ } = stats(data);
// console.log("Total enlaces:", totalLinks);
// console.log("Enlaces únicos:", uniqueLinksArray.length);
tableStats.push([totalLinks, uniqueLinksArray.length]);
resolve(tableStats.toString());
}
if (options.stats === true && options.validate === true) {
const { totalLinks, uniqueLinksArray, brokenLinks } = stats(data);
// console.log("Total enlaces:", totalLinks);
// console.log("Enlaces únicos:", uniqueLinksArray.length);
tableStatsVS.push([
totalLinks,
uniqueLinksArray.length,
brokenLinks.length,
]);
resolve(tableStatsVS.toString());
}
// resolve(res.flat());
})
.catch((err) => {
reject(err);
});
});
};
module.exports = { mdLinks };