DEV Community

KiminLee
KiminLee

Posted on

Added new features to findBreakURL CLI program

Getting start

findBreakURL is a wonderful CLI program supporting to find wrong URLs in files. The command is very straightforward and powerful! However, I had to admit that the program still needed to be enhanced. So, this time, I have added some more interesting features to the program.

What features was added

Nowadays, AJAX is literally everywhere! What if our CLI program returns JSON instead of just meaningless txt files? It would be a much useful tool to find wrong URL for other web apps. To do that, The JSON output should look like

[ 
  { url: 'https://www.google.com': status '200' },
  { url: 'https://bad-link.com': status: '404' } 
]

Also, most of the case, all we need to is either bad URLs or good URLS only, not both. So it only displays good or bad URLs. The --good flag causes only good URLs to get displayed; the --bad flag causes only bad URLs to get displayed.

Coding Time! -first-

Let's start the first feature. First of all, it needs new argument options! Thanks to yargs npm, this is super easy. .alias("j", "json"), .describe("j", "Display all results as JSON format").

Then we need to check the -j flag is true or false. Only it is true we are going to JSON.stringfy().

arg = yargs.argv;
// ...
if (arg.j) {
      await jsonFetch(url);
    } else {
      await fetchFunction(url, file);
    }
}

Inside the jsonFetch(url)

try {
    const response = await axios.head(url);
    // ...
      const httpObj = {
        url,
        status: response.status,
      };
      console.log(chalk.green(JSON.stringify(httpObj)));
    // ...
  } catch (error) {
    // ...
        const httpObj = {
          // ...
        };
        console.log(chalk.yellow(JSON.stringify(httpObj)));
      } 
    // ...
  }

After commit this change.

Coding Time! -second-

Now, let's do the second feature. Same as -j flag we can get benefit from "yargs" npm.

After that it is relatively simple. Just add conditions before displaying output! if (!arg.b) or if (!arg.g). These returns true if the flags exist or vice versa.

jsonFecth(url) function looks like

// ...
if (!arg.b) {
      const httpObj = {
        url,
        status: response.status,
      };
      console.log(chalk.green(JSON.stringify(httpObj)));
    }

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

And, normal fetchFunction(url,file)

// ...
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`]
          )
        );
      // ...
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: "
          )
        );
      //...

After commit this change.

Conclusing

Because I used two different branch, issue-7 and issue-8. In the process of merging, I had to manually re-code some of the part. For example, I have added -g -b flags later in the code, and changed some of the variable names differently... This minor clumsy and unorganized thing ended up bothering me. So if you do the three-way recursive merging, be aware of this and pay fully attention to small details.

You can find more info on my Github!

Contributing

I always welcome to your contribution! Just visit my github anytime.

Top comments (0)