Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading files #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extrat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const path = require('path');

37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
module.exports = () => {
// ...
};
const fs = require('fs');
const chalk = require('chalk');
const path = require('path');
//const { pathAbsolute } = require('./validapath.js');
const { extraer } = require('./mdLinks');
//const { validaLinks } = require('./validateLinks.js');
//const { validation } = require('./promesa');
//const { resolve } = require('path');
//const { rejects } = require('assert');


const userPath = process.argv[2];
//console.log(userPath);


//valida que el archivo sea md
const mdLinks = (isMd) => {

const mdExtensionFiles = path.extname(isMd) === '.md'
if (mdExtensionFiles === false) {
console.log(chalk.red('ARCHIVO NO ES .md'));
console.log(chalk.red('BYE ..'));

} else {
console.log(chalk.green('ARCHIVO SI ES .md'));
//pathAbsolute = (isMd);
extraer(userPath);
// validaLinks(userPath);
}


}

mdLinks(userPath);
44 changes: 44 additions & 0 deletions mdLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require('fs');
const chalk = require('chalk');
const path = require('path');
//const fetch = require('node-fetch');


const userPath = process.argv[2];
//console.log(userPath);

//const validationLinks = require('./extraerLinks.js')
//validationLinks (['https://nodejs.org/']);


//verificamos si la ruta existe
/*const extraerPath = fs.existsSync;
console.log(extraerPath(userPath) + ' verificamos ruta');*/


//leemos el archivo y mostramos links
const extraer = (userPath) => {

const links = fs.readFileSync(userPath, 'utf-8').match(/\[(.+)\]\((https?:\/\/.+)\)/gi);
let list = links.map((link) => {
const limite = link.indexOf('](');

const obj = {
href: link.substring(limite + 2, link.length - 1),
text: link.substring(1, limite),
file: userPath,
}
return obj;

});

console.log(chalk.magenta('leyendo los links..'));
console.log(list);
return list;
}


module.exports = {
extraer,

}
125 changes: 125 additions & 0 deletions notitas.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
//pediir path y mostarlo
console.log(chalk.cyan("Ingrese path:"));

path.addListener("data", (data) => {

console.log(chalk.magenta("Tu path es: " + data.toString()));
// Require the given module


});*/


fs.readFileSync('README.md', 'utf-8' , (err, data) => {
if(err) {
console.log(chalk.red('error: ', err));
} else {
console.log(data);
}
})
console.log('esto se ejecuta antes que esté el archivo');


let archivo = fs.readFileSync('notitas.txt', 'utf-8');
console.log(archivo);


console.log('Esto se muestra después de haber leído el achivo2.txt (por el readFileSync)');

//prueba poniendo ruta completa
async function main () {
console.log(await findFiles(_dirname + '/test'));
}
main ();

async function findFiles(folderName){
const files = [];
const items = await fs.readdir(folderName,{ withFileTypes: true}) ;

for (const item of items){
if(item.isDirectory()){
files = files.concat( await findFiles(`${folderName}/${item.name}`));
}
} else {
if (item.name === 'README.md'){
files.push(`${folderName}/${item.name}`)
}
}

return files;

}


//leer archivo
const fs = require('fs');

const readFiles = (route) => {
let extractLinks = fs.readFileSync(route, 'utf-8').match(/\[(.+)\]\((https?:\/\/.+)\)/gi);
let result= extractLinks.map((link) => {
let obj = {
href : '',
text : '',
file : route;
}
return obj;
});
console.log(result);
return extractLinks;
}

module.exports = {
readFiles
}


//path absoluto
const chalk = require('chalk'); //colores
const path = require('path');

const absolutePath = (directoryPath) => {
if (!path.isAbsolute(directoryPath)){
return path.resolve(directoryPath);
}else {
return directoryPath;
}
}

module.exports = {
absolutePath
}


//index
/*const path = require('path');
const { readfiles } = require('./readFiles');

const { absolutePath } = require('./path')

const directoryPath = process.argv.slice(2);
console.log(absolutePath(route));

const extMd = (extPath(route));
if (extMd == '.md') {
console.log(readfiles(route));
} else {
console.log('El archivo no es .md');
}*/

//leer archivo solo leer
const fs = require('fs');
const chalk = require('chalk');


fs.readFileSync('README.md', 'utf-8' , (err, data) => {
if(err) {
console.log(chalk.red('error: ', err));
} else {
console.log(data);
}
})
console.log(chalk.blue('Leemos el archivo'));

let archivo = fs.readFileSync('README.md', 'utf-8');
console.log(chalk.magenta(archivo));
24 changes: 24 additions & 0 deletions onlyValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fetch = require('node-fetch');
const fs = require('fs');
const {extraer} = require('./mdLinks.js');

const userPath = process.argv[2];
const objetolink = extraer(userPath);

const unlink = objetolink.map((obj)=>{
const linkobjeto = obj.href;
return linkobjeto

});

/*const onlyVal= (link) => {
return new Promise((resolve, reject)=>{
fetch('https://es.wikipedia.org/wiksdi/Markdo.com')
.then(resp => resolve(resp.status))
.catch(err=> reject('fallo'))
})
}*/

module.exports={
unlink
}
Loading