Skip to content

Commit

Permalink
Merge pull request #1 from Shinh18/master
Browse files Browse the repository at this point in the history
updating latest merges
  • Loading branch information
chawlapalak authored Oct 18, 2020
2 parents 59ba5c6 + 80bc876 commit e22d565
Show file tree
Hide file tree
Showing 16 changed files with 890 additions and 46 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@
Command-line tool for finding and reporting dead links (e.g., broken URLs) in a file

## Intsallation
* Fork this repository
* Clone the forked repository to your local device
* Run ```npm i -g https://github.com/Shinh18/urltester.git```

* Clone this repository to your locl device
* Run ' npm i -g https://github.com/Shinh18/urltester.git '
## Usage
* ```urltester <filename>```
* This command enables the tool to search for all the links in the file, make network requests and prints out the url with their respective status code
* ```urltester -v | version ```
* Using either v or version command, you can see the tool name and current version
* ```urltester -j | --json | \j ```
* Using either one of these commands will display the outputs in JSON format
* ```urltester -all | --good | --bad ```
* --all will displays all URLs
* --good will display all good URLs i.e. status code 200
* --bad will display all bad URLs i.e. status code 400 or 404
* ```urltester```
* This command displays a standard usage manual which shows how to use the tool as well as the command line arguments available

## Features
* It looks for and processes all the URLs using ```http://``` or ```https://``` schemes
* URLs are displayed according to the following:
* Green: good, status code 200
* Red: bad, status code 400 or 404
* Gray: unknown, status code
* Optimized to request headers instead of full body downloads
* Additional support is added for timeouts

* Th tools displays a standard usage message which shows
* the commands requird to use it
* arguments that can be passed with it

* The tool prints out good, bad and unknown URLs



117 changes: 78 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const boxen = require("boxen");
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
var displayAll = true;
var displayGood = false;
var displayBad = false;

const boxenOptions = {
padding: 1,
Expand All @@ -14,51 +17,87 @@ const boxenOptions = {
backgroundColor: "#FFFFFF"
}



const version = boxen(chalk.redBright.bold("Urltester 1.0.0"), boxenOptions);

if(process.argv.length == 2 ){

const messageOne = () => {
console.log(chalk.red("\n Standard user manual "));
console.log((chalk.gray("----------------------------------------------------")));
console.log(chalk.redBright("urltester filename -"), "reports good,bad,unknown urls");
console.log(chalk.redBright("urltester v |version -"), "displays tool version");
console.log((chalk.redBright("----------------------------------------------------\n")));
}
};

const version = boxen(chalk.redBright.bold("Urltester 1.0.0"), boxenOptions);

else {
if(process.argv[2] === "v" || process.argv[2] === "version") {
console.log(version);
const coloredOutput = (urlArray) => {
for(var item of urlArray) {
if(item.status == '200' && displayGood) {
console.log(chalk.green.bold(` ${item.status}: ${item.url} `));
}
else if((item.status == '400' || item.status == '404') && displayBad){
console.log(chalk.red.bold(` ${item.status}: ${item.url} `));
}
else if(displayAll){
console.log(chalk.grey.bold(` ${item.status}: ${item.url} `));
}
}
else {
const filePath = path.join(__dirname,process.argv[2]);
fs.readFile(filePath,'utf-8', function(err, data) {
if(err) {
console.log(chalk.red("Unsuccesful to read file"), err)
}
else {
const urlArr = data.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi);

urlArr.forEach((url) => {
//network request of url
fetch(url)
.then(function(response) {
if(response.status == 200) {
console.log(chalk.green(response.status, url));
}
else if(response.status == 400 || response.status == 404) {
console.log(chalk.red(response.status, url));
}
else {
console.log(chalk.gray(response.status, url) );
}
})
.catch(function(error) {
console.log(chalk.redBright("Error occured "), error);
});
});
}
});
}

const checkUrl = async (url) => {
var jsonOutput = [];
var urlElement;
try{
const res = await fetch(url,{method: "HEAD", timeout: 1500});
urlElement = { url: `${url}`, status: `${res.status}` };
jsonOutput.push(urlElement);
} catch(error) {
urlElement = { url: `${url}`, status: '404' }
jsonOutput.push(urlElement)
}
return urlElement
}

if(process.argv.length == 2 ) messageOne();
else if(process.argv[2] === "version" || process.argv[2] === "-v") {
console.log(version);
}
else {
const filePath = path.join(__dirname,process.argv[2]);
fs.readFile(filePath,'utf-8', function(err, data) {
if(err) console.log(chalk.red("Unsuccesful to read file"), err)
else {
const urlArr = data.match(/(http|https)(:\/\/)([\w+\-&@`~#$%^*.=/?:]+)/gi);
const promises = urlArr.map(checkUrl);
Promise
.all(promises)
.then(results => {
if(process.argv[3] === '-j' || process.argv[3] === '--json' || process.argv[3] === '\j' ){
console.log(JSON.stringify(results));
}
else if(process.argv[3] === '--all' ){
displayAll = true;
displayGood = true;
displayBad = true;
coloredOutput(results);
}
else if(process.argv[3] === '--good' ) {
displayGood = true;
displayAll = false;
coloredOutput(results);
}
else if(process.argv[3] === '--bad' ) {
displayBad = true;
displayAll = false;
coloredOutput(results);
}
else {
displayAll = true;
displayGood = true;
displayBad = true;
coloredOutput(results);
}
})
.catch(err =>
console.log("Error message: ", err) );
}
});
}

155 changes: 155 additions & 0 deletions node_modules/dotenv/CHANGELOG.md

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

23 changes: 23 additions & 0 deletions node_modules/dotenv/LICENSE

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

Loading

0 comments on commit e22d565

Please sign in to comment.