forked from Laboratoria/DEV004-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
108 lines (96 loc) · 2.88 KB
/
Copy pathapi.js
File metadata and controls
108 lines (96 loc) · 2.88 KB
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
import fs, { stat, Stats } from 'fs';
import path from 'path';
import chalk from 'chalk';
//* ****¿La ruta existe?*****
// Si no existe la ruta, rechaza la promesa
const routeExists = (route) => {
if (fs.existsSync(route)) {
return true;
}
return false;
};
//* ****Checar si es absoluta o convertirla*****
const isAbsolute = (route) => {
if (path.isAbsolute(route)) {
return route;
}
return path.resolve(route);
};
//* ****Checar si es archivo*****
const isItFile = (route) => fs.lstatSync(route).isFile();
// fs.stat(route, (err, stats) => stats.isFile()
//* ****Checar si hay archivos MD*****
const isMD = (route) => {
if (path.extname(route) === ('.md')) {
// console.log('si es MD api');
return true;
}
// console.log('NO es MD api');
return false;
};
//* ****Leer un archivo*****
const readFileApi = (route) => new Promise((resolve, reject) => {
fs.readFile(route, 'utf-8', (err, data) => { // 'utf-8', después de path también sirve para cmabiar buffer a texto string
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
//* ****Buscar URLs*****/
const findURLs = (text, route) => {
const dataURLs = text.match(/\[(.*?)\]\((.*?)\)/g);
const finalData = [];
// eslint-disable-next-line no-restricted-syntax
for (const data of dataURLs) {
finalData.push({ text: data.match(/\[(.*)\]/)[1], href: data.match(/https*?:([^"')\s]+)/)[0], file: route });
}
return finalData;
// return dataURLs;
};
// const finalData = findURLs(textE, rutado);
// console.log('datos', finalData);
// const justLinks = finalData.map((obj) => obj.href);
// const getStatus = (url) => fetch(url);
// const readStatus = justLinks.map((url) => getStatus(url));
const validateLinks = (array) => {
const justLinks = array.map((obj) => obj.href);
const getStatus = (url) => fetch(url);
const readStatus = justLinks.map((url) => getStatus(url));
return Promise.all(readStatus)
.then((responses) => responses.map((res, index) => {
const status = { status: res.status };
const code = { code: res.statusText };
const arrayIndex = array[index];
return { ...arrayIndex, ...status, ...code };
}))
.catch((error) => {
console.log('error: ', error);
throw error;
});
};
// console.log('status: ', res.status, res.statusText));
// Promise.all(readStatus).then((answ) => {
// answ.forEach((res) => console.log('res: ', res.status, 'text: ', res.statusText));
// });
// Otra opcion
// return Promise.all(readStatus)
// .then((answ) => answ.forEach((res, index) => {
// const status = { status: res.status };
// const code = { code: res.statusText };
// const arrayIndex = array[index];
// return { ...arrayIndex, ...status, ...code };
// }))
// .catch((error) => {
// console.log('error: ', error);
// });
export {
readFileApi,
routeExists,
isAbsolute,
isItFile,
isMD,
findURLs,
validateLinks,
};