forked from Laboratoria/DEV002-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
194 lines (167 loc) · 5.15 KB
/
data.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require('fs');
const path = require('path');
const axios = require('axios');
//si la ruta existe o no
const exist = (route) => fs.existsSync(route);
// regresa un booleano
// console.log(exist)
// metodo para convertir la ruta absoluta
const absolute = (route) => path.resolve(route)
// console.log(absolute)
//-------------- comprobar si es un archivo o un directorio con la clase isDirectory o isFile, regresa un booleano
const isDirectory = (route) => fs.statSync(route).isDirectory();
const isFile = (route) => fs.statSync(route).isFile();
//---------------obtener la extension de un archivo
const ext = (route) => path.extname(route);
// console.log(ext);
// funcion para recorrer los el array de files
const recorrerArrayFiles = (arrayFiles) => {
const newArray = []
return new Promise((resolve, reject) => {
arrayFiles.forEach((file, index) => {
//console.log(arrayFiles.length)
// console.log(index)
fs.readFile(`${file}`, 'utf-8', (err, contenido) => {
if (err) {
reject('error recorreArray');
} else {
newArray.push(arrayLinks(file, contenido));
const merge = [].concat(...newArray)
if (index === (arrayFiles.length - 1)) {
//const newArray = arrayLinks(file, contenido)
//console.log('newArray', newArray)
//console.log('merge',merge)
resolve(merge)
//return merge
}
}
});
});
});
}
const regexLinks = /\[(.+?)\]\((https?:\/\/[^\s]+)(?: "(.+)")?\)|(https?:\/\/[^\s]+)/ig;
const urlRegex = /\((https?:\/\/[^\s]+)(?: "(.+)")?\)|(https?:\/\/[^\s]+)/ig;
const textRegex = /\[(\w+.+?)\]/gi;
// funcion para encontrar links y botar el objeto
const arrayLinks = (file, contenido) => {
const arrayObjetos = []
if (regexLinks.test(contenido) === false) {
console.log('No hay links para verificar en la ruta ' + `${file}`)
return []
} else {
const matches = contenido.match(regexLinks)
//console.log(matches)
matches.forEach((item) => {
const matchestext = item.match(textRegex);
let unidadText = "";
let puroText = ['sin texto']
if (matchestext) {
//console.log(matchestext)
unidadText = matchestext[0];
puroText = unidadText.replace(/\[|\]/g, '').split(',');
}
const matchesLink = item.match(urlRegex)
//console.log(matchesLink)
const unidadLink = matchesLink[0];
const puroLink = unidadLink.replace(/\(|\)/g, '').split(',');
arrayObjetos.push({ href: puroLink[0], text: puroText[0], path: `${file}` })
//console.log('dentro',arrayObjetos)
//return arrayObjetos
})
//console.log('fuera del foreach', arrayObjetos)
return arrayObjetos;
}
}
// validacion de los links - entrega el objeto con status y ok
const allPromise = (arrayLinks) => {
const validate = arrayLinks.map((link) => {
return axios.get(link.href)
.then((result) => {
const objectValidate = {
...link,
status: result.status,
ok: result.statusText
}
//console.log(objectValidate)
return objectValidate
})
.catch((err) => {
//console.log(err.errno);
const objectValidate = {
...link,
status: err.response ? 404 : 'ERROR',
ok: "fail"
}
//console.log(objectValidate)
return objectValidate
})
})
return Promise.all(validate)
}
//obtener el resultado de la opcion stats
const statsResult = (arrayObjeto) => {
const arrayLink = arrayObjeto.map(element => element.href);
const uniqueLink = new Set(arrayLink);
return {
Total: arrayLink.length,
Unique: uniqueLink.size
}
}
//obtener el resultado de la opcion stats y validate
const statsAndValidate = (arrayObjeto) => {
const arrayLink = arrayObjeto.map(element => element.href);
const uniqueLink = new Set(arrayLink);
const brokenLink = arrayObjeto.filter(element => element.ok === 'fail')
return {
Total: arrayLink.length,
Unique: uniqueLink.size,
Broken: brokenLink.length
}
}
// -----------------funcion recursiva
// const arrayOfFiles = []
function readAllFiles(route, newarray = []) {
//const newarray = []
const files = fs.readdirSync(route)
//console.log(files);
files.forEach(file => {
const stat = fs.statSync(`${route}/${file}`)
// console.log(stat.isDirectory());
if (stat.isDirectory()) {
readAllFiles(`${route}/${file}`, newarray)
} else {
//validar una extension .md
if (path.extname(file) === '.md') {
newarray.push(`${route}/${file}`)
}
}
});
// console.log(newarray)
return newarray
}
function readAllFilesRevuersive(route) {
if (isDirectory(route)) {
const files = fs.readdirSync(route)
//readAllFilesRevuersive(files)
return files.map((file) => {
return readAllFilesRevuersive(`${route}/${file}`)
}).flat()
} else {
return [route]
}
}
// arrayOfFiles = []
// console.log(readAllFiles('./carpeta', arrayOfFiles ));
module.exports = {
exist,
absolute,
readAllFiles,
isDirectory,
isFile,
ext,
recorrerArrayFiles,
allPromise,
statsResult,
statsAndValidate,
readAllFilesRevuersive
}