How I Created an Interview CLI with Node.js for self-interview preparation
Tags: javascript, node, react, webdev
Published: 2021-10-02 19:04:34

Hi Guys, In this article, we will focus on topics of Node.js, CLI, and Interview Preparation.

I have created a self-interview preparation CLI toolkit, which will show me random questions whenever I run a CLI command called "interview".

Sounds cool right.
## Purpose
I felt I would require a real-time experience of attending an interview, but I wanted to have control over what I could be expecting, and still having the random unexpectedness of attending an interview.

This simple project I felt was in the right direction.

## Prerequisites/Tools required.
1. Latest version of  [Node.js](https://nodejs.org/) installed(I used v14.18.0)
2. Text editor - [Visual Studio Code](https://code.visualstudio.com/) 

Next, open your project folder and enter the below commands to create and initiate the node.js project setup.

```
mkdir interview-cli
cd interview-cli
npm init -y
``` 

Next, open the interview-cli folder and create a bin folder and an index.js file within it.
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633186344733/tne5Iyehm.png)


```js
//bin/index.js
#!/usr/bin/env node

console.log("Welcome to the Mock Interview!!");
``` 
A string that begins with `#!` is called a “shebang”. With this string, we are explicitly stating the command line/terminal to run our script with `node`.

Now open the `package.json` file and update the `main` key value to the path `"bin/index.js"` and append a new key `bin` with the below code.
```json
"bin": {
    "interview": "./bin/index.js"
 }
```
> Make sure you add a comma(",") to the end of the previous line for the above step.

Your `package.json` must look like this.
```json
{
  "name": "interview-cli",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {},
  "author": "",
  "license": "ISC",
  "bin": {
    "interview": "./bin/index.js"
  }
}
```

At this point, we can run the application like any other node application with the below command.
```
node .
```
But since our goal is to run the application from anywhere, I want to simply open my computer/laptop and hit the command line to prepare for the interview. For achieving that, we install our application globally with the below command.
```
npm install -g .
```
This installs our script globally. This means the "interview" command mentioned in the `bin` key section in the `package.json` file is made available to the command line.

Let's run our script globally with the below command
```
interview
```
Output:
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633187935207/gI-b6Y2Af.png)

Let's now create few questions for the interview in a file called `mock-interview-questions.txt` at the root of the project
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633191536251/YJhAV-vRl.png)

Now let's add the script to show a random interview question when we enter "interview" in the command line.
```js
//bin/index.js
#!/usr/bin/env node

const fs = require("fs");
const crypto = require("crypto");

try {
  let data = fs.readFileSync("mock-interview-questions.txt", "utf8");
  let questions = data.split("\r\n");

  const n = crypto.randomInt(0, questions.length);

  for (let i = 0; i < 10; i++) console.log();
  console.log(questions[n]);
  for (let i = 0; i < 10; i++) console.log();
} catch (err) {
  console.error(err);
}

```

> For the tutorial I have kept the number of questions to 10, but in reality, I would suggest you add at least 50 questions for keeping the interview interesting and helpful for you

## Final Output

1. 
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633198127126/wYsvK3N4D.png)

2. 
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1633198067346/azbklHLAh.png)


## If you would like to support my work: 
<a href="https://www.buymeacoffee.com/karthikdevblog" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>
and Follow my Blog at(https://karthikdevarticles.com/)