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

Pull request Alejandra #53

Open
wants to merge 5 commits into
base: master
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
109 changes: 109 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env node
const fs= require ("fs");
const path = require("path");
const fetch= require("node-fetch");
let options= process.argv;

let index = process.argv.indexOf("--file");
let pathFileArg = process.argv[index +1];


function validateExt(pathFile) {
try {
let ext= (path.extname(pathFile));
return ext === '.md'
} catch (e) {
return false
}
}


function readMdFile(pathFile) {
const data = fs.readFileSync(pathFile, 'utf-8')
return data;
}


function getLinks(data) {
let string= data;
let regExp= (/https?:\S+\w/gi);
let links= string.match(regExp);
return links
}

let linkok =[]
let okLinks = []
let blink=[]
let broken = []

function statusLink(links) {
let i=0
let j=0
let promises =links.map( link =>fetch(link)
.then(res=> {
linkok= {
href:res.url,
statusText:res.statusText,
status:res.status,
file:pathFileArg
}

if (linkok.status===200){
okLinks.push(linkok)
return i++
}
if (linkok.status!==200){
broken.push(linkok)
return j++
}
})
.catch(err=>{
blink={
url:link,
error:err.code,
status:err.status,
file:pathFileArg
}
if (blink.status!==200){
broken.push(blink)
return j++
}
}))

return Promise.all(promises)
.then(results => ({
Total: results.length,
Ok: i,
Broken: j,
})
)
}


function main (pathFile) {
if (validateExt(pathFile)) {
const data = readMdFile(pathFile);
const links= getLinks(data);
console.log("Links encontrados:",links)
return statusLink(links)
} else {
return Promise.reject('El archivo no está en formato md');
}
}

main(pathFileArg)
.then((result) => {
if (options.includes('--validate') && options.includes('--stats')) {
console.log(result)
console.log("OK Links:",okLinks)
console.log("Broken Links:",broken)
}else if(options.includes('--validate')){
console.log("OK Links:",okLinks)
console.log("Broken Links:",broken)
} else if(options.includes ('--stats')){
console.log(result)
}
})
.catch(e=> {
console.log(e)
});;
413 changes: 413 additions & 0 deletions node_modules/axios/CHANGELOG.md

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions node_modules/axios/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading