Table of contents
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.
- Latest version of Node.js installed(I used v14.18.0)
- Text editor - Visual Studio Code
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.
//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.
"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.
{
"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:
Let's now create few questions for the interview in a file called mock-interview-questions.txt
at the root of the project
Now let's add the script to show a random interview question when we enter "interview" in the command line.
//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