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

code review #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me agrada la exclusion de la carpeta node_modules/ agregaria la carpeta de assets y el archivo package-lock.json

98 changes: 98 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env node
const colors = require('colors');
const path = require('path');
const fs = require('fs');
const markdownLinkExtractor = require('markdown-link-extractor');
const fetch = require('node-fetch');
Comment on lines +2 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

el uso de const esta genial


let inputDoc = process.argv[2];
let options = process.argv[3];

const validateArgvExist = (inputFile) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cuando el valor muta se implementa let

Suggested change
const validateArgvExist = (inputFile) => {
let validateArgvExist = (inputFile) => {

if (inputFile === undefined) {
console.log('You need to enter a correct filename to continue'.brightRed);
} else validateExt(inputFile);
};

const validateExt = (inputFile) => {
let mdExt = path.extname(inputFile);
if (mdExt === '.md') {
readFiles(inputFile);
} else console.log('invalid format, only MD files are supported, try again'.brightRed);
};

const readFiles = (inputFile) => {
fs.readFile(inputFile, 'utf8', (err, data) => {
if (err) {
console.log(err);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto se pudo ver mejor con una promesa

Suggested change
console.log(err);
(res=>{
then
catch

} selectOption(data);
});
};

const selectOption = async (inputFile) => {
let links = markdownLinkExtractor(inputFile);
switch (options) {
case '--validate': {
validateLinks(links);
break;
}
case '--stats': {
stats(links);
break;
}
case '--validate--stats':
case '--stats--validate': {
await stats(links);
await validateLinks(links);
break;
}
default: {
getLinks(links);
break;
}
}
};
Comment on lines +32 to +54

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me agrada el uso de la sentencia de control


const getLinks = (links) => {
links.forEach(link => {
console.log('Path: '.brightBlue + path.resolve(inputDoc).brightWhite + ' Link: '.brightBlue + link.brightWhite)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto se veria mejor con un return y una promesa

Suggested change
console.log('Path: '.brightBlue + path.resolve(inputDoc).brightWhite + ' Link: '.brightBlue + link.brightWhite)
let result= ('Path: '.brightBlue + path.resolve(inputDoc).brightWhite + ' Link: '.brightBlue + link.brightWhite)
return result

})
}

const validateLinks = (links) => {
console.log('VALIDATE'.rainbow.bold)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este console.log puede ser confuso al usuario

Suggested change
console.log('VALIDATE'.rainbow.bold)

links.forEach(link => {
fetch(link).then((res) => {
console.log('STATUS: '.brightBlue + `${res.status} ${res.statusText}`.brightGreen + ' - URL: '.brightBlue + res.url.brightWhite);
Comment on lines +65 to +66

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿que pasa si status es 404

Suggested change
fetch(link).then((res) => {
console.log('STATUS: '.brightBlue + `${res.status} ${res.statusText}`.brightGreen + ' - URL: '.brightBlue + res.url.brightWhite);
fetch(link).then((res) => {
if(res.status===200){
return `${link} true`
}else if(res.status===404){
return `${link} false tu link esta roto`

}).catch((err) => console.log('STATUS: '.brightBlue + '404 Fail'.red + ' - URL: '.brightBlue + link.brightWhite))
});
}

const stats = async (links) => {
console.log('STATS'.rainbow.bold)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('STATS'.rainbow.bold)

let okLinks = []
let brokenLinks = []
console.log('Total: '.brightMagenta + links.length)
let unique = links.filter((item, index, array) => {
return array.indexOf(item) === index;
})
const urls = links.map(async url => {
try {
const response = await fetch(url)
if (response.status < 400) {
okLinks.push(response.url)
} else {
brokenLinks.push(response.url)
await reject(response.statusText)
}
} catch (error) {
brokenLinks.push(url)
}
})
await Promise.all(urls)
console.log('Unique Links: '.brightYellow + unique.length);
console.log('Ok Links: '.brightGreen + okLinks.length);
console.log('Broken Links: '.brightRed + brokenLinks.length);
}

validateArgvExist(inputDoc);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ten cuidado con la invocación

Suggested change
validateArgvExist(inputDoc);
validateArgvExist(inputDoc);

Binary file added imgs/Captura.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading