-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from joey2031/issue-2
Issue 2- Added v and version argument features
- Loading branch information
Showing
2 changed files
with
35 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
const fs=require('fs') | ||
const fetch=require('node-fetch') | ||
const path=require('path') | ||
const packageJson = require('./package.json'); | ||
|
||
|
||
console.log(process.argv) | ||
const greetingMessage=()=>{ | ||
console.log("This is command line tool"); | ||
console.log("if red links are not working"); | ||
console.log("if green links are working") | ||
console.log("Run with a file name to process the file, run with the argument v or version to get the version of this tool") | ||
} | ||
|
||
|
||
if(process.argv.length==2){ | ||
if(process.argv.length == 2){ | ||
greetingMessage(); | ||
console.log("Hello"); | ||
} | ||
else{ | ||
const filePath=path.join(__dirname,process.argv[2]) | ||
fs.readFile(filePath,'utf-8',(err,data)=>{ | ||
if(err){ | ||
console.log("Fail to read file",err) | ||
} | ||
else{ | ||
const validUrl=data.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi) | ||
validUrl.forEach((url)=>{ | ||
fetch(url,{method:'HEAD',timeout:2000}) | ||
.then((res)=>{ | ||
if(res.status==200) | ||
console.log(res.status,url) | ||
else if(res.status==400||res.status==404) | ||
console.log(res.status,url) | ||
else console.log(res.status,url) | ||
}) | ||
.catch((error)=>{ | ||
console.log("404",url) | ||
else{ // more then 2 | ||
|
||
if (process.argv[2] == "v" || process.argv[2] == "version") { | ||
console.log(packageJson.name + " Version " + packageJson.version); | ||
} else{ | ||
|
||
const filePath=path.join(__dirname,process.argv[2]) | ||
fs.readFile(filePath,'utf-8',(err,data)=>{ | ||
if(err){ | ||
console.log("Fail to read file",err) | ||
} | ||
else{ | ||
const validUrl=data.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi) | ||
validUrl.forEach((url)=>{ | ||
fetch(url,{method:'HEAD',timeout:2000}) | ||
.then((res)=>{ | ||
if(res.status==200) | ||
console.log(res.status,url) | ||
else if(res.status==400||res.status==404) | ||
console.log(res.status,url) | ||
else console.log(res.status,url) | ||
}) | ||
.catch((error)=>{ | ||
console.log("404",url) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters