forked from Laboratoria/BOG004-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
158 lines (151 loc) · 4.82 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
//modulos utilizados de node
const fs = require("fs");
const path = require("path");
const https = require("https");
//Convierte ruta relativa en absoluta
function validatePath(pathUser) {
if (path.isAbsolute(pathUser)) {
//isAbsolute() valida si la rutaingresada es absoluta
return pathUser;
}
if (!fs.existsSync(pathUser)) {
console.log("❌ Ruta ingresada no existe");
process.exit();
} else {
const pathAbsolute = path.resolve(pathUser).normalize(); //mediante el metodo resolve se convierte absoluta y normalize la estandariza
return pathAbsolute;
}
}
//funcion que recorre los directorios en busca de archivos md y los empuja a un array
function throughDirectory(pathUser) {
const backslash =
process.platform === "win32" || process.platform === "win64" ? "\\" : "/"; //nos coloca los backlash dependiendo del sistema operativo
let filesPath = [];
if (fs.statSync(pathUser).isFile() && path.extname(pathUser) === ".md") {
filesPath.push(pathUser);
} else {
if (fs.statSync(pathUser).isDirectory()) {
const directory = pathUser;
let contentDirectory = fs.readdirSync(directory); //ingresa al directorio y lo lee
contentDirectory.forEach((elem) => {
throughDirectory(pathUser + backslash + elem).forEach((elem) => {
filesPath.push(elem);
});
});
}
if (filesPath.length === 0) {
console.log("No se encontraron archivos md:(");
}
}
return filesPath;
}
//promesa para leer archivos en el array
const readMd = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, "utf-8", (error, data) => {
if (error) return reject(error);
else {
resolve({
//Si se resuelve la promesa captura la informaciòn del objeto
route: file,
fileContent: data,
});
}
});
});
};
const getObject = (mdArray) => {
const links = []; //array para guardar los links
const paths = []; //array que guarda las rutas
let object = []; //objecto final
return Promise.all(mdArray.map(readMd)) //recorre los md
.then((data) => {
const regExp = /!*\[(.+?)\]\((.+?)\)/gi;
data.forEach((item) => {
const linkFind = item.fileContent.match(regExp); //compara los archivos que estamos buscando con los simbolos en la expresion
if (linkFind) {
linkFind.forEach((elem) => {
links.push(elem);
paths.push(item.route);
});
}
});
//se construye el objecto
object = links.map((linkAll) => {
let index = links.indexOf(linkAll); //se obtiene la posicion de los link mediante el indexOf()
const cutLink = linkAll.split("]("); //separar donde encuentre "]("
const text = cutLink[0].slice(1); //corta del 0 al 1 corta el signo que abre
const href = cutLink[1].slice(0, -1);
return {
href, //enlace
text: text.substring(0, 50), //caracteres hasta 50
file: paths[index], //ruta
};
});
return object;
})
.catch((error) =>
console.log("❌La ruta del archivo o carpeta es obligatorio", error)
);
}
//Contiene una promesa que por medio del HTTPS valida los links
function validateUrl(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => resolve(res)).on("error", (e) => reject(false));
});
}
function createObjectWithvalidateUrl(data, options) {
let validateUrlList = 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(validateUrlList)
.then(() => {
if (options.stats) {
const contentDataHref = getTotalLinks(data);
const filterDataStats = data.filter((object) => object.ok === "fail");
const unique = getUniqueLinks(data);
result = {
Total: contentDataHref.length,
Unique: unique.length,
Broken: filterDataStats.length,
};
return result;
} else {
// console.log("Links desde promesa:", data);
return data;
}
})
.catch((error) => console.log("❌ERROR", error));
}
function objectOfStats(data) {
const contentDataHref = getTotalLinks(data);
const unique = getUniqueLinks(data);
result = {
Total: contentDataHref.length,
Unique: unique.length,
};
// console.table(result);
return result
}
function getUniqueLinks(data) {
return [...new Set(data.map((object) => object.href))];
}
function getTotalLinks(data) {
return data.filter((object) => object.hasOwnProperty("href"));
}
module.exports = {
validatePath,
throughDirectory,
getObject,
objectOfStats,
createObjectWithvalidateUrl,
};