forked from Laboratoria/BOG004-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
164 lines (148 loc) · 4.3 KB
/
functions.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { fstat } = require("fs");
const path = require("path");
const fs = require("fs");
const { error } = require("console");
const https = require("https");
var colors = require("colors");
function validatePath(pathUser) {
if (!fs.existsSync(pathUser)) {
console.log("☹ ✾ La ruta ingresada no es valida o no existe ✾ ☹".magenta);
process.exit();
} else if (path.isAbsolute(pathUser)) {
return pathUser;
} else {
const pathAbsolute = path.resolve(pathUser).normalize();
return pathAbsolute;
}
}
// Función aplicando recursividad para hacer recorrido de directorios y push de archivos .md
function documentsRoute(pathUser) {
const separator =
process.platform === "win32" || process.platform === "win64" ? "\\" : "/";
let filesPath = [];
if (fs.statSync(pathUser).isFile() && path.extname(pathUser) === ".md") {
filesPath.push(pathUser);
} else {
if (fs.statSync(pathUser).isDirectory()) {
let directory = pathUser;
let contentDirectory = fs.readdirSync(directory);
contentDirectory.forEach((elem) => {
documentsRoute(pathUser + separator + elem).forEach((elem) => {
filesPath.push(elem);
});
});
}
}
if (filesPath.length === 0) {
console.log("No se encontraron archivos markdown".magenta);
process.exit();
}
return filesPath;
}
// esta función lee los archivos md, busca links en cada archivo y guarda los links
const readDocument = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, "utf-8", (error, data) => {
if (error) return reject(error);
else {
resolve({
route: file,
fileContent: data,
});
}
});
});
};
const getObjet = (mdArray) => {
let urlsOnly = [];
let pathOnly = [];
let objets = [];
return Promise.all(mdArray.map(readDocument))
.then((data) => {
const expRegLinks = /!*\[(.+?)\]\((.+?)\)/gi;
data.forEach((item) => {
const linksAll = item.fileContent.match(expRegLinks);
if (linksAll) {
linksAll.forEach((elem) => {
urlsOnly.push(elem);
pathOnly.push(item.route);
});
}
});
objets = urlsOnly.map((links) => {
let index = urlsOnly.indexOf(links);
const splitUrl = links.split("](");
const text = splitUrl[0].slice(1);
const href = splitUrl[1].slice(0, -1);
return {
href,
text: text.substring(0, 50),
file: pathOnly[index],
};
});
return objets;
})
.catch((error) => console.error(error));
};
function validateUrl(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => resolve(res)).on("error", (e) => reject(false));
});
}
function CreateObjectWithvalidateUrl(data, optionsUser) {
let urlValidatedList = data.map((object) =>
validateUrl(object.href)
.then((res) => {
object.status = res.statusCode;
object.ok =
res.statusCode >= 200 && res.statusCode <= 399 ? "ok" : "fail";
})
.catch((error) => {
object.status = error.code;
object.ok = "fail";
})
);
return Promise.all(urlValidatedList).then(() => {
// Para mostrar la tabla con broken se debe esperar a que termine la validacion con .then
if (optionsUser.stats) {
const filterDataWithHref = getTotalLinks(data);
const filterDataWithStatus = data.filter(
(object) => object.ok === "fail"
);
const unique = getUnique(data);
result = {
Total: filterDataWithHref.length,
Unique: unique.length,
Broken: filterDataWithStatus.length,
};
// console.table(result);
return result;
} else {
// (colors.cyan("Estos son los enlaces validados: ",
return data;
}
});
}
function objectfitStat(data) {
const filterDataWithHref = getTotalLinks(data);
const unique = getUnique(data);
result = {
Total: filterDataWithHref.length,
Unique: unique.length,
};
return result;
}
function getUnique(data) {
return [...new Set(data.map((object) => object.href))];
}
function getTotalLinks(data) {
return data.filter((object) => object.hasOwnProperty("href"));
}
module.exports = {
validatePath,
documentsRoute,
validateUrl,
getObjet,
CreateObjectWithvalidateUrl,
objectfitStat,
};