Making a CLI app in REPL with persistent data using node.js
Tags: node, javascript, tutorial, replit
Published: 2020-11-19 06:49:09

### **What is a CLI app?**
CLI stands for Command Line Interface. A CLI app is also known as a console application. It uses a text interface for input and ouput. 

### **What is node.js?**
Node.js is a javascript runtime environment for developing server-side and networking applications. It is open source and cross-platform.

### **What is REPL?**
[REPL](https://repl.it) is an online IDE. It allows users to write code, and build apps and websites using a browser. Good thing about it is that you can share your projects easily and it can be linked with your GitHub account. REPL stands for *read–evaluate–print loop*. It also provides a *free* plan.

### **How to make a node.js app using REPL?**
1. Make an account on [REPL](https://repl.it/signup).
2. Click on **New repl** on the top-left corner of your homepage.
![Creating a new repl](https://dev-to-uploads.s3.amazonaws.com/i/as2sxtp8o2x77pa4ey8g.png)
3. Select the programming language (Node.js in this case) and give it a name (or REPL will give it for you), then click on **Create repl**.
![Selecting programming language and file name for the new repl](https://dev-to-uploads.s3.amazonaws.com/i/6qjso22p5z6ra9juf49j.png)
4. You will see 3 sections. The left-most is the **Navigator** area, the middle area is the **Editor** (where you write your code) and the right-most is the **Console** (where your app will execute when you click on **Run**). You're now ready to code in Node.js.
![REPL interface](https://dev-to-uploads.s3.amazonaws.com/i/dmd7aqzkthnp05x0who8.png)


### **Packages used for the project**
* [readlineSync](https://www.npmjs.com/package/readline-sync)

Get user's input synchronously.
![readlineSync](https://dev-to-uploads.s3.amazonaws.com/i/wl4r9tj9qttrz7kzye2q.png)
* [Chalk](https://www.npmjs.com/package/chalk)

Beautify the CLI with colors.
![Chalk](https://dev-to-uploads.s3.amazonaws.com/i/cpos96il8pzfk2xg94hq.png)
* [jsonbase](https://www.npmjs.com/package/jsonbase.com)

Provides persistent data storage.
![jsonbase](https://dev-to-uploads.s3.amazonaws.com/i/dcmhl1nppanme2mapf1w.png)

### **Taking input from the user with 'readline-sync'**
First, we have to include the package:
```javascript
var rs = require("readline-sync")
```
REPL will automatically add this package once you run the app. If you still face an error, you can add it manually from the navigation panel on the left by searching in *Packages*
**Take input:**
```javascript
var name = rs.question("What is your name? ")
```
**Process input:** (use backtick when using a variable inside the string)
```javascript
var message = `Hello ${name}!`
```
else you could also do 
```javascript
var message = "Hello " + name + "!"
```
**Print Output:**
```javascript
console.log(message)
```
All together, this is how you take input from the user, process it and print the output.
```javascript
var rs = require("readline-sync")
var name = rs.question("What is your name? ")
var message = `Hello ${name}!`
console.log(message)
```


### **Coloring the console with 'chalk'**
Chalk is a great package when it comes down to beautifying the console.

**Include the package:**
```javascript
let chalk = require('chalk')
```
You can use it in the code as easily as:
```javascript
var name = rs.question(chalk.green("What is your name? "))
```


### **Data persistence with 'jsonbase'**
jsonbase.com is great when it comes down to persisting small data, especially when you're dealing with json data.

**Include the package:**
```javascript
let jsonbase = require('jsonbase.com')
```

**Create your Token:**
```javascript
// Replace this with your token (any random string)
let TOKEN = 'pJdQYebgaFSSBUXxQhtgYKVxnUzvUCkXFHXBXadg'
```

**Create your Store:**
```javascript
let store = jsonbase(TOKEN)
```

**To write data:** (I have used the custom key 'user' to save user data, you can choose your own)
```javascript
var name = rs.question(chalk.green("What is your name? "))
var age = rs.question(chalk.green("What is your age? "))

let user = {
  "name": name,
  "age": age
}

store.write('user',user).then( () => {
  console.log("Data saved successfully!")
})
```

**To read data:** (use the same key that you used while writing data)
```javascript
store.read('user').then( resp => {
  console.log(resp.data)
})
```

Yes, it's that simple to use!

### **You can check out this CLI app I made which tests your knowledge on India and keeps track of your highscore as well:**
* [Click here to play](https://repl.it/@iRohitGaur/1-Do-You-Know-India?embed=1&output=1)
* [Click here to check out the code](https://github.com/iRohitGaur/1-Do-You-Know-India)

#### **If you have any queries, get in touch with me on [Twitter](https://twitter.com/iRohitGaur)**