Skip to content

Commit

Permalink
merge issue1
Browse files Browse the repository at this point in the history
  • Loading branch information
klee214 committed Oct 8, 2020
2 parents 2dc664c + 145fb45 commit 5cc4ecb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 77 deletions.
155 changes: 80 additions & 75 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chalk = require("chalk");
const axios = require("axios");
const { arg } = require("./yargs");

/*
The purpose of this module:
Expand All @@ -14,86 +15,90 @@ const fetchFunction = async (url, file) => {
// 300 series... redirect status. However axios does redirect to the changed URL automatically
// So status will be normal 200 in the most of the time.
// Most of the case this will not be run. I will implement this here just in case.
if (response.status === 301) {
chalk.black.bgYellow(
if (!arg.b) {
if (response.status === 301) {
chalk.black.bgYellow(
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 301, Moved Permanently to: " +
response.headers[`Location`]
)
);
} else if (response.status === 307) {
chalk.black.bgYellow(
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 307, Temporary Redirect to: " +
response.headers[`Location`]
)
);
} else if (response.status === 308) {
chalk.black.bgYellow(
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 308, Permanent Redirect to: " +
response.headers[`Location`]
)
);
// Success status : 200
} else {
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 301, Moved Permanently to: " +
response.headers[`Location`]
)
);
} else if (response.status === 307) {
chalk.black.bgYellow(
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 307, Temporary Redirect to: " +
response.headers[`Location`]
)
);
} else if (response.status === 308) {
chalk.black.bgYellow(
console.log(
"In " +
file +
" file, the URL: " +
url +
". Status code: 308, Permanent Redirect to: " +
response.headers[`Location`]
)
);
// Success status : 200
} else {
console.log(
chalk.black.bgGreen.bold(
"In " + file + " file, the URL: " + url + " is success: "
)
);
console.log(chalk.green.underline.bold("STATUS: " + response.status));
chalk.black.bgGreen.bold(
"In " + file + " file, the URL: " + url + " is success: "
)
);
console.log(chalk.green.underline.bold("STATUS: " + response.status));
}
}
} catch (error) {
// If 404 error :
if (error.response) {
console.log(
chalk.white.bgRed.bold(
"In " + file + " file, the URL: " + url + " is a bad url: "
)
);
return console.log(
chalk.red.underline.bold("STATUS: " + error.response.status)
);
}
if (!arg.g) {
// If 404 error :
if (error.response) {
console.log(
chalk.white.bgRed.bold(
"In " + file + " file, the URL: " + url + " is a bad url: "
)
);
return console.log(
chalk.red.underline.bold("STATUS: " + error.response.status)
);
}

// non-exist URL
if (error.code == "ENOTFOUND") {
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " is unknown url: "
)
);
chalk.white(console.log(error.code));
// non-exist URL
if (error.code == "ENOTFOUND") {
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " is unknown url: "
)
);
chalk.white(console.log(error.code));

// timeout error
} else if (error.code == "ETIMEDOUT") {
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " is TIMEOUT: "
)
);
chalk.white.underline(console.log(error.code));
} else {
// server error or other error : error.code will indicate which error it has
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " has following issue: "
)
);
chalk.white.underline(console.log(error.code));
// timeout error
} else if (error.code == "ETIMEDOUT") {
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " is TIMEOUT: "
)
);
chalk.white.underline(console.log(error.code));
} else {
// server error or other error : error.code will indicate which error it has
console.log(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " has following issue: "
)
);
chalk.white.underline(console.log(error.code));
}
}
}
};
Expand Down
10 changes: 8 additions & 2 deletions yargs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const yargs = require("yargs");
const chalk = require('chalk')
const fs = require("fs");
const chalk = require("chalk");

/*
The purpose of this module:
Expand All @@ -26,15 +26,18 @@ yargs
" Display all results as JSON format{ url: 'https://...': status '200' }, ... "
)
.example("url-tester start -f=foo1.html -g", " Display only good URL")
.example("url-tester start -f=foo1.html -b", " Display only baad URL")
.alias("f", "file")
.alias("j", "json")
.alias("a", "all")
.alias("g", "good")
.alias("b", "bad")
.demandOption(["f"])
.describe("f", "Load all specified files (delimiter is ',')")
.describe("a", "Load all HTML files in the current dir")
.describe("j", "Display all results as JSON format")
.describe("g", "Display only good URL")
.describe("g", "Display only GOOD URL")
.describe("g", "Display only BAD URL")
.version()
.alias("v", "version")
.help()
Expand All @@ -48,12 +51,15 @@ if (yargs.argv._.length === 1 && yargs.argv._[0] === "start") {
if (yargs.argv.a && typeof yargs.argv.f !== "string") {
const tmpFiles = fs.readdirSync(__dirname, { encoding: "utf-8" });


// if -a, store all files into the files variable
files = tmpFiles.filter((file) => {
return file.toLowerCase().endsWith(".html");
});
} else if (typeof yargs.argv.f === "string") {
files = [...yargs.argv.f.split(",")];
} else{
console.log(chalk.yellow("Please enter filenames or -a following -f"))
}
} else {
console.log(chalk.yellow("Wrong argument: use [start]"));
Expand Down

0 comments on commit 5cc4ecb

Please sign in to comment.