Skip to content

Commit

Permalink
json feature
Browse files Browse the repository at this point in the history
  • Loading branch information
klee214 committed Oct 7, 2020
1 parent f8bcbb7 commit 2dc664c
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 100 deletions.
168 changes: 84 additions & 84 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,94 @@ const axios = require("axios");
*/

const fetchFunction = async (url, file) => {
try {
const response = await axios.head(url);
try {
const response = await axios.head(url);

// 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(
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 {
// 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(
console.log(
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) {
"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(
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") {
"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(
chalk.white.bgGrey.bold(
"In " + file + " file, the URL: " + url + " is unknown url: "
)
);
chalk.white(console.log(error.code));
"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));
}
} 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)
);
}

// 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));
}
// 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));
}
};
}
};

module.exports = fetchFunction
module.exports = { fetchFunction };
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
const fetchFunction = require('./fetch')
const { fetchFunction } = require("./fetch");
const fs = require("fs");
const validator = require("validator");
const files = require('./yargs');
const { exit } = require('process');
const { files, arg } = require("./yargs");
const { jsonFetch } = require("./json");
const { exit } = require("process");

let fileData = null;

Expand All @@ -14,7 +15,7 @@ files.map((file) => {
encoding: "utf-8",
});

// wrong file name
// wrong file name
} catch (error) {
console.log(file + " is a WRONG file name");
return process.exit(1);
Expand All @@ -25,7 +26,13 @@ files.map((file) => {

if (findURL) {
findURL.forEach(async (url) => {
if (validator.isURL(url)) await fetchFunction(url, file);
if (validator.isURL(url)) {
if (arg.j) {
await jsonFetch(url);
} else {
await fetchFunction(url, file);
}
}
});
}
});
35 changes: 35 additions & 0 deletions json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const axios = require("axios");
const chalk = require("chalk");
const { arg } = require("./yargs");

const jsonFetch = async (url) => {
try {
const response = await axios.head(url);
if (!arg.b) {
const httpObj = {
url,
status: response.status,
};
console.log(chalk.green(JSON.stringify(httpObj)));
}
} catch (error) {
if (!arg.g) {
// If 404 error :
if (error.response) {
const httpObj = {
url,
status: error.response.status,
};
console.log(chalk.yellow(JSON.stringify(httpObj)));
} else {
const httpObj = {
url,
error: error.code,
};
console.log(chalk.red(JSON.stringify(httpObj)));
}
}
}
};

module.exports = { jsonFetch };
30 changes: 19 additions & 11 deletions yargs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const yargs = require("yargs");
const fs = require("fs");
const chalk = require("chalk");

/*
The purpose of this module:
Expand Down Expand Up @@ -42,17 +43,24 @@ yargs

let files = [];

// decide the option if it is -f or -a
if (yargs.argv.a && typeof yargs.argv.f !== "string") {
const tmpFiles = fs.readdirSync(__dirname, { encoding: "utf-8" });
if (yargs.argv._.length === 1 && yargs.argv._[0] === "start") {
// decide the option if it is -f or -a
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") {
console.log(yargs.argv);
files = [...yargs.argv.f.split(",")];
// 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("Wrong argument: use [start]"));
}
const yargObj = {
files,
arg: yargs.argv,
};

module.exports = files;
module.exports = yargObj;

0 comments on commit 2dc664c

Please sign in to comment.