-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (52 loc) · 1.85 KB
/
Copy pathindex.js
File metadata and controls
64 lines (52 loc) · 1.85 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
const chalk= require("chalk");
const fs=require("fs");
function extraiLinks(texto){
const regex = /\[([^\]]*)\]\((https?:\/\/[^$#\s].[^\s]*)\)/gm;
const arrayResultados=[];
let temp;
while((temp = regex.exec(texto))!==null){
arrayResultados.push({ [temp[1]]:temp[2]})
}
return arrayResultados.length === 0 ?'nao ha links':arrayResultados;
}
function trataErro(erro){
throw new Error(chalk.red(erro.code, 'não há um arquivo no caminho'));
}
async function pegaArquivo(caminhoDoArquivo){
const encoding = 'utf-8';
try{
const texto =await fs.promises.readFile(caminhoDoArquivo,encoding);
return extraiLinks(texto);
}catch(erro){
trataErro(chalk.redBright(erro));
}
}
pegaArquivo('./arquivos/texto1.md');
module.exports = pegaArquivo;
//usando then
// const chalk = require('chalk');//lib para alterar cores das msg
// const fs = require('fs'); //filesystem
// function pegaArquivo(caminhoDoArquivo){
// const encoding = 'utf-8';
// fs.promises
// .readFile(caminhoDoArquivo, encoding)
// .then((texto) => chalk.green(console.log(texto)))
// .catch((erro)=>trataErro(erro))
// }
//1 forma - antig
// function pegaArquivo(caminhoDoArquivo){
// const encoding = 'utf-8';
// fs.promises
// .readFile(caminhoDoArquivo,encoding)//colocando o promisse na frente do readFile, estamos avisando para o fs que demora um tempo para realizar toda leitura do arquivo e o fs espera
// .then((texto)=> (console.log(chalk.blueBright(texto))))
// .catch((erro)=>trataErro(erro))
// }
// // function pegaArquivo(caminhoDoArquivo){
// // const encoding= 'utf-8';
// // fs.readFile(caminhoDoArquivo,encoding,(erro,texto)=>{
// // if(erro){
// // trataErro(erro);
// // }
// // console.log(chalk.blueBright(texto));
// // })
// // }