Skip to content

Commit

Permalink
Refactoring Utest.js to improve code maintainability:
Browse files Browse the repository at this point in the history
*Rename version variable to make it more clear
*Remove comments that are not necessary
*Move styling of the output into style.js file in the styling folder
*Rename variables that holds response status to make it more clear
  • Loading branch information
katyasichov committed Oct 20, 2020
1 parent e9e8c02 commit aca94df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
34 changes: 13 additions & 21 deletions Utest.js
Expand Up @@ -5,18 +5,11 @@ const boxen = require("boxen");
const yargs = require("yargs");
const fetch = require("node-fetch");
const fs = require("fs");
const v = require("./package.json").version;
const version = require("./package.json").version;
const { boxenOptions } = require("./styling/style");

//styling of the box for the tool name and version
const boxenOptions = {
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "green",
backgroundColor: "#555555",
};

const vmessage = chalk.white.bold(`Utest version: ${v}`);
const vmessage = chalk.white.bold(`Utest version: ${version}`);
const msgBox = boxen(vmessage, boxenOptions);

const argv = yargs
Expand Down Expand Up @@ -72,31 +65,30 @@ function checkStatus(data) {
}

s.on("end", async () => {
var jsonResponse = [];
var jsonU;
var responseStatusByUrl = [];
var statusResponseForUrl;

//Iterate through the links and check their status

await Promise.all(
urlList.map(async (url) => {
try {
const urlTest = await fetch(url, { method: "head", timeout: 1500 });
jsonU = { url: `${url}`, status: `${urlTest.status}` };
jsonResponse.push(jsonU);
statusResponseForUrl = { url: `${url}`, status: `${urlTest.status}` };
responseStatusByUrl.push(statusResponseForUrl);
} catch (error) {
jsonU = { url: `${url}`, status: "UNKNOWN" };
jsonResponse.push(jsonU);
statusResponseForUrl = { url: `${url}`, status: "UNKNOWN" };
responseStatusByUrl.push(statusResponseForUrl);
}
})
);
if (argv.json) {
console.log(JSON.stringify(jsonResponse));
console.log(JSON.stringify(responseStatusByUrl));
} else {
printResponse(jsonResponse);
printResponse(responseStatusByUrl);
}
//var errCode=checkStatus(jsonResponse);
//console.log(checkStatus(jsonResponse));
process.exit(checkStatus(jsonResponse));

process.exit(checkStatus(responseStatusByUrl));
});
process.on("exit", function (code) {
return console.log(`About to exit with code ${code}`);
Expand Down
11 changes: 11 additions & 0 deletions styling/style.js
@@ -0,0 +1,11 @@
//styling of the box for the tool name and version
const boxenOptions = {
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "green",
backgroundColor: "#555555",
};


module.exports.boxenOptions = boxenOptions;

0 comments on commit aca94df

Please sign in to comment.