From 73b7c42c23badf2094be0962e3ba63d012006664 Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Tue, 25 Oct 2022 11:41:25 -0500 Subject: [PATCH 1/6] reading files --- mdLinks.js | 25 +++++++++ package-lock.json | 139 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 ++++++++ test/mdLinks.js | 13 +++++ test/module.js | 0 test/notitas.txt | 69 +++++++++++++++++++++++ 6 files changed, 269 insertions(+) create mode 100644 mdLinks.js create mode 100644 package-lock.json create mode 100644 test/mdLinks.js create mode 100644 test/module.js create mode 100644 test/notitas.txt diff --git a/mdLinks.js b/mdLinks.js new file mode 100644 index 0000000..9e1611a --- /dev/null +++ b/mdLinks.js @@ -0,0 +1,25 @@ +const fs = require('fs'); +const chalk = require('chalk'); +const path = process.openStdin(); +/* +//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 + + +});*/ + + +//mostar directorio +const items = fs.readdirSync('./test', {withFileTypes: true}); + +for (let item of items) { + const type = item.isDirectory() ? 'folder' : 'file'; + console.log(chalk.blue(`${item.name}: ${type}`)); +} + +process.exit(); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..6ea36c7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,139 @@ +{ + "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" + }, + "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/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" + } + } + }, + "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==" + }, + "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" + } + } + } +} diff --git a/package.json b/package.json index 4d29515..5125410 100644 --- a/package.json +++ b/package.json @@ -3,5 +3,28 @@ "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", + "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" } } diff --git a/test/mdLinks.js b/test/mdLinks.js new file mode 100644 index 0000000..dfba09a --- /dev/null +++ b/test/mdLinks.js @@ -0,0 +1,13 @@ +const chalk = require('chalk'); //colores +const fs = require('fs').promises; + +async function main () { + await findFiles('test'); +} + +main (); + +async function findFiles(folderName) { + const mineFiles = await fs.readdir(folderName); + console.log(mineFiles); +} \ No newline at end of file diff --git a/test/module.js b/test/module.js new file mode 100644 index 0000000..e69de29 diff --git a/test/notitas.txt b/test/notitas.txt new file mode 100644 index 0000000..9e8a4cc --- /dev/null +++ b/test/notitas.txt @@ -0,0 +1,69 @@ +console.log(chalk.cyan("Ingrese path:")); + +path.addListener("data", (data) => { + + console.log(chalk.magenta("Tu path es: " + data.toString())); + // Require the given module + +process.exit(); + +//prueba dos +const path = process.openStdin(); + +console.log(chalk.blue("Ingrese path:")); + + +path.addListener("data", (data) => { + const file = data.toString(); + console.log(chalk.yellow("Tu path es: " + file)); + fs.readFile(file, 'utf-8', (err, data) => { + if(err){ + console.log(chalk.red('error no se encuentra archivo', err)); + }else { + console.log(chalk.magenta(data,toString)); + + } + process.exit(); + }) + + + //console.log(items); + +const items = fs.readdir('stores', {withFileTypes: true}); +for (let item of items) { + const type = item.isDirectory() ? 'folder' : 'file'; + console.log(`${item.name}: ${type}`); +} + +function findFiles(folderName) { + const items = fs.readdir(folderName, {withFileTypes: true}); + items.forEach((item) => { + if (path.extname(item.name) === 'md'){ + console.log(chalk.magenta(`Found file: ${item.name} in folder: ${folderName}`)); + } + else { + findFiles(path.join(folderName, item.name)); + } + }); +} +findFiles('stores'); + + +}) + +//3er prueba +var texto = fs.readFileSync('notitas.txt', {encoding:'utf-8'}); +console.log(chalk.blue(texto)); + + +//prueba video +async function main () { + await findFiles('test'); +} + +main (); + +async function findFiles(folderName) { + const mineFiles = await fs.readdir(folderName); + console.log(mineFiles); +} \ No newline at end of file From 2f78a9946607194e1f2d5b41d425c9cd8b145c33 Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:39:05 -0600 Subject: [PATCH 2/6] sub cambios pero aun no funciona --- extrat.js | 2 + index.js | 26 +++++++- mdLinks.js | 23 +------ test/mdLinks.js | 13 ---- test/module.js | 0 test/notitas.txt | 150 ++++++++++++++++++++++++++++++-------------- test/obtenerpath.js | 11 ++++ test/readFiles.js | 21 +++++++ 8 files changed, 161 insertions(+), 85 deletions(-) create mode 100644 extrat.js delete mode 100644 test/mdLinks.js delete mode 100644 test/module.js create mode 100644 test/obtenerpath.js create mode 100644 test/readFiles.js 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..035d361 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,23 @@ -module.exports = () => { - // ... -}; +const fs = require('fs'); +const chalk = require('chalk'); +const path = require('path'); +const { argv } = require('node:process'); +const {readArchivo} = require('./readFiles.js'); + +const fileName = argv[2]; +console.log(chalk.magenta(fileName)); +const resolverpath = path.resolve(fileName); +console.log(resolverpath); + + +const review = file => { + const review = path.extname(file); +if (review == '.md') { + + console.log(readArchivo(fileName)); + } else { + console.log(chalk.red('NO ES ARCHIVO.md')); + } + +} + diff --git a/mdLinks.js b/mdLinks.js index 9e1611a..ecae957 100644 --- a/mdLinks.js +++ b/mdLinks.js @@ -1,25 +1,4 @@ -const fs = require('fs'); const chalk = require('chalk'); -const path = process.openStdin(); -/* -//pediir path y mostarlo -console.log(chalk.cyan("Ingrese path:")); +const path = require('path'); -path.addListener("data", (data) => { - - console.log(chalk.magenta("Tu path es: " + data.toString())); - // Require the given module - -});*/ - - -//mostar directorio -const items = fs.readdirSync('./test', {withFileTypes: true}); - -for (let item of items) { - const type = item.isDirectory() ? 'folder' : 'file'; - console.log(chalk.blue(`${item.name}: ${type}`)); -} - -process.exit(); diff --git a/test/mdLinks.js b/test/mdLinks.js deleted file mode 100644 index dfba09a..0000000 --- a/test/mdLinks.js +++ /dev/null @@ -1,13 +0,0 @@ -const chalk = require('chalk'); //colores -const fs = require('fs').promises; - -async function main () { - await findFiles('test'); -} - -main (); - -async function findFiles(folderName) { - const mineFiles = await fs.readdir(folderName); - console.log(mineFiles); -} \ No newline at end of file diff --git a/test/module.js b/test/module.js deleted file mode 100644 index e69de29..0000000 diff --git a/test/notitas.txt b/test/notitas.txt index 9e8a4cc..1729a1d 100644 --- a/test/notitas.txt +++ b/test/notitas.txt @@ -1,3 +1,5 @@ +/* +//pediir path y mostarlo console.log(chalk.cyan("Ingrese path:")); path.addListener("data", (data) => { @@ -5,65 +7,119 @@ path.addListener("data", (data) => { console.log(chalk.magenta("Tu path es: " + data.toString())); // Require the given module -process.exit(); -//prueba dos -const path = process.openStdin(); +});*/ -console.log(chalk.blue("Ingrese path:")); +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'); -path.addListener("data", (data) => { - const file = data.toString(); - console.log(chalk.yellow("Tu path es: " + file)); - fs.readFile(file, 'utf-8', (err, data) => { - if(err){ - console.log(chalk.red('error no se encuentra archivo', err)); - }else { - console.log(chalk.magenta(data,toString)); - - } - process.exit(); - }) - - - //console.log(items); - -const items = fs.readdir('stores', {withFileTypes: true}); -for (let item of items) { - const type = item.isDirectory() ? 'folder' : 'file'; - console.log(`${item.name}: ${type}`); + +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 (); -function findFiles(folderName) { - const items = fs.readdir(folderName, {withFileTypes: true}); - items.forEach((item) => { - if (path.extname(item.name) === 'md'){ - console.log(chalk.magenta(`Found file: ${item.name} in folder: ${folderName}`)); - } - else { - findFiles(path.join(folderName, item.name)); - } - }); +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}`) + } } -findFiles('stores'); +return files; -}) +} -//3er prueba -var texto = fs.readFileSync('notitas.txt', {encoding:'utf-8'}); -console.log(chalk.blue(texto)); +//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; +} -//prueba video -async function main () { - await findFiles('test'); +module.exports = { + readFiles } -main (); -async function findFiles(folderName) { - const mineFiles = await fs.readdir(folderName); - console.log(mineFiles); -} \ No newline at end of file +//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/test/obtenerpath.js b/test/obtenerpath.js new file mode 100644 index 0000000..494cb61 --- /dev/null +++ b/test/obtenerpath.js @@ -0,0 +1,11 @@ +const path = require('path'); + +const directoryPath = argv[2]; +console.log(directoryPath); +const resolverpath = path.resolve(directoryPath); +console.log(resolverpath); + + +module.exports = { + absolute +} \ No newline at end of file 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 From 8e800d773fcb048e1ada8192bcf1eb1986aee40a Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:41:42 -0600 Subject: [PATCH 3/6] leyendo links y mostrando directorios --- index.js | 10 +++++----- mdLinks.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 035d361..ba71ca3 100644 --- a/index.js +++ b/index.js @@ -2,15 +2,15 @@ const fs = require('fs'); const chalk = require('chalk'); const path = require('path'); const { argv } = require('node:process'); -const {readArchivo} = require('./readFiles.js'); +const { items } = require('./mdLinks.js'); -const fileName = argv[2]; +/*const fileName = argv[2]; console.log(chalk.magenta(fileName)); const resolverpath = path.resolve(fileName); -console.log(resolverpath); +console.log(resolverpath);*/ -const review = file => { +/*const review = file => { const review = path.extname(file); if (review == '.md') { @@ -19,5 +19,5 @@ if (review == '.md') { console.log(chalk.red('NO ES ARCHIVO.md')); } -} +}*/ diff --git a/mdLinks.js b/mdLinks.js index ecae957..4f907a8 100644 --- a/mdLinks.js +++ b/mdLinks.js @@ -1,4 +1,39 @@ +const fs = require('fs'); const chalk = require('chalk'); -const path = require('path'); +//const path = require ('path'); +//mostramos el directorio completo +const items = fs.readdirSync('./node_modules', { withFileTypes: true }); + +for (let item of items) { + const type = item.isDirectory() ? 'folder' : 'file'; + console.log(chalk.blue(__dirname + `${item.name}: ${type}`)); +} + +//leemos el archivo y mostramos links +const validate = (route) => { + const links = fs.readFileSync(route, 'utf-8').match(/\[(.+)\]\((https?:\/\/.+)\)/gi); + let list = links.map((link) => { + const limite = link.indexOf(']('); + + let obj = { + Title: link.substring(1, limite), + file: route, + Url: link.substring (limite +2, link.length -1 ), + } + return obj; + + }); + + console.log(chalk.magenta('leyendo los links..')); + console.log(list); + return list; +} +validate('README.md') + + + +module.exports = { + items +} \ No newline at end of file From 38f6a0fdb8864f96e46bd96a2c5aab51f23cd511 Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:49:48 -0600 Subject: [PATCH 4/6] links links y directorio --- mdLinks.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mdLinks.js b/mdLinks.js index 4f907a8..8edee7f 100644 --- a/mdLinks.js +++ b/mdLinks.js @@ -30,9 +30,7 @@ const validate = (route) => { console.log(list); return list; } -validate('README.md') - - +validate('README.md'); module.exports = { items From 1c770b934c734558bd38ff7c21d79798616e5d5b Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Tue, 8 Nov 2022 15:27:41 -0600 Subject: [PATCH 5/6] hasta extraer los links md --- index.js | 45 +++++++++++++--------- mdLinks.js | 60 ++++++++++++++++------------- test/notitas.txt => notitas.txt | 0 package-lock.json | 68 ++++++++++++++++++++++++++++++++- package.json | 4 +- promesa.js | 34 +++++++++++++++++ prueba.md | 18 +++++++++ test/obtenerpath.js | 11 ------ validapath.js | 27 +++++++++++++ validateLinks.js | 25 ++++++++++++ 10 files changed, 236 insertions(+), 56 deletions(-) rename test/notitas.txt => notitas.txt (100%) create mode 100644 promesa.js create mode 100644 prueba.md delete mode 100644 test/obtenerpath.js create mode 100644 validapath.js create mode 100644 validateLinks.js diff --git a/index.js b/index.js index ba71ca3..b606a83 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,34 @@ const fs = require('fs'); const chalk = require('chalk'); const path = require('path'); -const { argv } = require('node:process'); -const { items } = require('./mdLinks.js'); - -/*const fileName = argv[2]; -console.log(chalk.magenta(fileName)); -const resolverpath = path.resolve(fileName); -console.log(resolverpath);*/ - - -/*const review = file => { - const review = path.extname(file); -if (review == '.md') { - - console.log(readArchivo(fileName)); - } else { - console.log(chalk.red('NO ES ARCHIVO.md')); +//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 index 8edee7f..0748372 100644 --- a/mdLinks.js +++ b/mdLinks.js @@ -1,37 +1,45 @@ const fs = require('fs'); const chalk = require('chalk'); -//const path = require ('path'); +const path = require('path'); +//const fetch = require('node-fetch'); -//mostramos el directorio completo -const items = fs.readdirSync('./node_modules', { withFileTypes: true }); +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'); -for (let item of items) { - const type = item.isDirectory() ? 'folder' : 'file'; - console.log(chalk.blue(__dirname + `${item.name}: ${type}`)); -} //leemos el archivo y mostramos links -const validate = (route) => { - const links = fs.readFileSync(route, 'utf-8').match(/\[(.+)\]\((https?:\/\/.+)\)/gi); - let list = links.map((link) => { - const limite = link.indexOf(']('); - - let obj = { - Title: link.substring(1, limite), - file: route, - Url: link.substring (limite +2, link.length -1 ), - } - return obj; - - }); - - console.log(chalk.magenta('leyendo los links..')); - console.log(list); - return list; +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; } -validate('README.md'); + module.exports = { - items + userPath, + extraer, + } \ No newline at end of file diff --git a/test/notitas.txt b/notitas.txt similarity index 100% rename from test/notitas.txt rename to notitas.txt diff --git a/package-lock.json b/package-lock.json index 6ea36c7..fd998f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "ISC", "dependencies": { "chalk": "^4.1.2", - "fs": "^0.0.1-security" + "fs": "^0.0.1-security", + "node-fetch": "^2.6.7" }, "engines": { "node": ">=16.x" @@ -74,6 +75,25 @@ "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", @@ -84,6 +104,25 @@ "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": { @@ -127,6 +166,14 @@ "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", @@ -134,6 +181,25 @@ "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 5125410..bfe86e8 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ }, "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" }, @@ -25,6 +26,7 @@ "homepage": "https://github.com/Liz2208/CDMX013-md-links#readme", "dependencies": { "chalk": "^4.1.2", - "fs": "^0.0.1-security" + "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/obtenerpath.js b/test/obtenerpath.js deleted file mode 100644 index 494cb61..0000000 --- a/test/obtenerpath.js +++ /dev/null @@ -1,11 +0,0 @@ -const path = require('path'); - -const directoryPath = argv[2]; -console.log(directoryPath); -const resolverpath = path.resolve(directoryPath); -console.log(resolverpath); - - -module.exports = { - absolute -} \ 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..d7861af --- /dev/null +++ b/validateLinks.js @@ -0,0 +1,25 @@ +const fetch = require('node-fetch'); +const { extraer } = require('./mdLinks'); + +const userPath = process.argv[2]; +//console.log(userPath); + + +const validaLinks = (link) => { + console.log(link); + return fetch(link) + .then((response) => { + console.log(response.statusText); + console.log(response.status); + + // Do something with response + }) + .catch(function (err) { + console.log("Unable to fetch -", err); + }); + +} + +module.exports = { + validaLinks +} \ No newline at end of file From 7766e65b5091b67ddddfe61632b623871c378247 Mon Sep 17 00:00:00 2001 From: Liz2208 <108889788+Liz2208@users.noreply.github.com> Date: Wed, 9 Nov 2022 10:19:23 -0600 Subject: [PATCH 6/6] valida links pero falta promesa --- index.js | 8 +++--- mdLinks.js | 7 +++--- onlyValidation.js | 24 ++++++++++++++++++ validateLinks.js | 62 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 onlyValidation.js diff --git a/index.js b/index.js index b606a83..bd0cdfd 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,8 @@ 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 { validaLinks } = require('./validateLinks.js'); +//const { validation } = require('./promesa'); //const { resolve } = require('path'); //const { rejects } = require('assert'); @@ -20,12 +20,12 @@ const mdLinks = (isMd) => { 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); + // validaLinks(userPath); } diff --git a/mdLinks.js b/mdLinks.js index 0748372..b8b0f7f 100644 --- a/mdLinks.js +++ b/mdLinks.js @@ -5,15 +5,15 @@ const path = require('path'); const userPath = process.argv[2]; -console.log(userPath); +//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'); +/*const extraerPath = fs.existsSync; +console.log(extraerPath(userPath) + ' verificamos ruta');*/ //leemos el archivo y mostramos links @@ -39,7 +39,6 @@ const extraer = (userPath) => { module.exports = { - userPath, extraer, } \ No newline at end of file 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/validateLinks.js b/validateLinks.js index d7861af..714b9fa 100644 --- a/validateLinks.js +++ b/validateLinks.js @@ -1,25 +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); + +}) -const validaLinks = (link) => { - console.log(link); - return fetch(link) - .then((response) => { - console.log(response.statusText); - console.log(response.status); - - // Do something with response - }) - .catch(function (err) { - console.log("Unable to fetch -", err); - }); - -} +/*) => { + 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 -} \ No newline at end of file + validaLinks +} +