diff --git a/extrat.js b/extrat.js new file mode 100644 index 0000000..64fda7d --- /dev/null +++ b/extrat.js @@ -0,0 +1,2 @@ +const path = require('path'); + diff --git a/index.js b/index.js index a4e4a45..bd0cdfd 100644 --- a/index.js +++ b/index.js @@ -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); \ No newline at end of file diff --git a/mdLinks.js b/mdLinks.js new file mode 100644 index 0000000..b8b0f7f --- /dev/null +++ b/mdLinks.js @@ -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, + +} \ No newline at end of file diff --git a/notitas.txt b/notitas.txt new file mode 100644 index 0000000..1729a1d --- /dev/null +++ b/notitas.txt @@ -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)); diff --git a/onlyValidation.js b/onlyValidation.js new file mode 100644 index 0000000..dd33142 --- /dev/null +++ b/onlyValidation.js @@ -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 +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..fd998f8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,205 @@ +{ + "name": "md-links", + "version": "0.1.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "md-links", + "version": "0.1.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2", + "fs": "^0.0.1-security", + "node-fetch": "^2.6.7" + }, + "engines": { + "node": ">=16.x" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, + "node_modules/node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + }, + "dependencies": { + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, + "chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, + "node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "requires": { + "whatwg-url": "^5.0.0" + } + }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } +} diff --git a/package.json b/package.json index 4d29515..bfe86e8 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,30 @@ "version": "0.1.0", "engines": { "node": ">=16.x" + }, + "description": "* [1. Preámbulo](#1-preámbulo)\r * [2. Resumen del proyecto](#2-resumen-del-proyecto)\r * [3. Objetivos de aprendizaje](#3-objetivos-de-aprendizaje)\r * [4. Consideraciones generales](#4-consideraciones-generales)\r * [5. Criterios de aceptación mínimos del proyecto](#5-criterios-de-aceptación-mínimos-del-proyecto)\r * [6. Entregables](#6-entregables)\r * [7. Hacker edition](#7-hacker-edition)\r * [8. Pistas, tips y lecturas complementarias](#8-pistas-tips-y-lecturas-complementarias)\r * [9. Checklist](#9-checklist)\r * [10. Achicando el problema](#10-achicando-el-problema)", + "main": "index.js", + "type": "commonjs", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Liz2208/CDMX013-md-links.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Liz2208/CDMX013-md-links/issues" + }, + "homepage": "https://github.com/Liz2208/CDMX013-md-links#readme", + "dependencies": { + "chalk": "^4.1.2", + "fs": "^0.0.1-security", + "node-fetch": "^2.6.7" } } diff --git a/promesa.js b/promesa.js new file mode 100644 index 0000000..0d97a87 --- /dev/null +++ b/promesa.js @@ -0,0 +1,34 @@ +const { closeSync } = require('fs') +const { validaLinks } = require('./validateLinks') + +const validation = (links) => { + + let allLinks = links.map(link => { + return validaLinks(link.href).then(respuesta => { + link.status = respuesta + if (respuesta < 300) { + link.message = 'ok' + } else { + link.message = 'fail' + } + // stauts code + return link + }).catch(err => { + link.status = err + + // status code + return link + }) + + }) + return new Promise((resolve, reject) => { + Promise.all(allLinks).then(fin => { + resolve(fin) + }); + }); +} + + +module.exports = { + validation +} \ No newline at end of file diff --git a/prueba.md b/prueba.md new file mode 100644 index 0000000..8ff71ff --- /dev/null +++ b/prueba.md @@ -0,0 +1,18 @@ +## 1. Preámbulo + +[Markdown](https://es.wikipedia.org/wiki/Markdown) es un lenguaje de marcado +ligero muy popular entre developers. Es usado en muchísimas plataformas que +manejan texto plano (GitHub, foros, blogs, ...) y es muy común +encontrar varios archivos en ese formato en cualquier tipo de repositorio +(empezando por el tradicional `README.md`). + +Estos archivos `Markdown` normalmente contienen _links_ (vínculos/ligas) que +muchas veces están rotos o ya no son válidos y eso perjudica mucho el valor de +la información que se quiere compartir. + +Dentro de una comunidad de código abierto, nos han propuesto crear una +herramienta usando [Node.js](https://nodejs.org/), que lea y analice archivos +en formato `Markdown`, para verificar los links que contengan y reportar +algunas estadísticas. + +![md-links](https://user-images.githubusercontent.com/110297/42118443-b7a5f1f0-7bc8-11e8-96ad-9cc5593715a6.jpg) diff --git a/test/readFiles.js b/test/readFiles.js new file mode 100644 index 0000000..c0b6777 --- /dev/null +++ b/test/readFiles.js @@ -0,0 +1,21 @@ +const fs = require('node.fs'); +const path = require('path'); + +const readArchivo = (directoryPath) => { + let extractLinks = fs.readFileSync(directoryPath, 'utf-8').match(/\[(.+)\]\((https?:\/\/.+)\)/gi); +console.log(extractLinks); + let result= extractLinks.map((link) => { + let obj = { + href : '', + text : '', + file : directoryPath, + } + return obj; +}); + console.log(result); + return extractLinks; +} + +module.exports = { + readArchivo +} \ No newline at end of file diff --git a/validapath.js b/validapath.js new file mode 100644 index 0000000..30a89d5 --- /dev/null +++ b/validapath.js @@ -0,0 +1,27 @@ +const fs = require('fs'); +const chalk = require('chalk'); +const path = require('path'); +const { resolve } = require('path'); + +const userPath = process.argv[2]; + + + +// Verifica si la ruta es absoluta, y si no la convierte +const pathAbsolute = (userPath) => path.isAbsolute(userPath) ? userPath : path.resolve(userPath); +console.log(chalk.yellow('ESTA ES TU RUTA ABSOLUTA : ' + pathAbsolute(userPath))); + + +//mostramos el directorio completo +/*/const items = fs.readdirSync('./test', { withFileTypes: true }); + +for (let item of items) { + const type = item.isDirectory() ? 'folder' : 'file'; + console.log(chalk.yellow('ESTE ES EL ARCHIVO DENTRO DE TU CARPETA .. ')); + console.log(chalk.blue(__dirname + `${item.name}: ${type}`)); +}*/ + + +module.exports = { + pathAbsolute +} \ No newline at end of file diff --git a/validateLinks.js b/validateLinks.js new file mode 100644 index 0000000..714b9fa --- /dev/null +++ b/validateLinks.js @@ -0,0 +1,53 @@ +const fetch = require('node-fetch'); +const chalk = require('chalk'); +const { onlyVal } = require('./onlyValidation'); +const { extraer } = require('./mdLinks'); +const { link } = require('fs'); +const {unlink} = require('./onlyValidation'); +const userPath = process.argv[2]; +//console.log(userPath); +const validaLinks = (link) => { //nunca modificar es parametro de mi funcion + fetch(link) + .then((response) => { + console.log(chalk.green('si funciona')); + console.log(response.status); + // Do something with response + }) + .catch(function (err) { + console.log(chalk.red('no funciona link')); + console.log("Unable to fetch -", err); + }); +} + + Promise.all(unlink.map(validaLinks)).then((resultado) => { + console.log(resultado); + +}) + +/*) => { + let allValue = link.map(link => { + return onlyVal(link.href).then(respuesta => { + link.status = respuesta + if (respuesta < 300) { + link.message = 'funciona' + } else { + link.message = 'no funciona' + } + return link + }).catch(err =>{ + link.status = err + return link + }) + }) +return new Promise((resolve, reject)=> { + Promise.all(allValue).then(fin => { + resolve(fin) + }) +}) + +}*/ + +module.exports = { + validaLinks +} +