loading...
Cover image for Making a CLI app in REPL with persistent data using node.js

Making a CLI app in REPL with persistent data using node.js

irohitgaur profile image Rohit Gaur Updated on ・3 min read

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 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.
  2. Click on New repl on the top-left corner of your homepage. Creating a new repl
  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
  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

Packages used for the project

Get user's input synchronously.
readlineSync

Beautify the CLI with colors.
Chalk

Provides persistent data storage.
jsonbase

Taking input from the user with 'readline-sync'

First, we have to include the package:

var rs = require("readline-sync")
Enter fullscreen mode Exit fullscreen mode

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:

var name = rs.question("What is your name? ")
Enter fullscreen mode Exit fullscreen mode

Process input: (use backtick when using a variable inside the string)

var message = `Hello ${name}!`
Enter fullscreen mode Exit fullscreen mode

else you could also do

var message = "Hello " + name + "!"
Enter fullscreen mode Exit fullscreen mode

Print Output:

console.log(message)
Enter fullscreen mode Exit fullscreen mode

All together, this is how you take input from the user, process it and print the output.

var rs = require("readline-sync")
var name = rs.question("What is your name? ")
var message = `Hello ${name}!`
console.log(message)
Enter fullscreen mode Exit fullscreen mode

Coloring the console with 'chalk'

Chalk is a great package when it comes down to beautifying the console.

Include the package:

let chalk = require('chalk')
Enter fullscreen mode Exit fullscreen mode

You can use it in the code as easily as:

var name = rs.question(chalk.green("What is your name? "))
Enter fullscreen mode Exit fullscreen mode

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:

let jsonbase = require('jsonbase.com')
Enter fullscreen mode Exit fullscreen mode

Create your Token:

// Replace this with your token (any random string)
let TOKEN = 'pJdQYebgaFSSBUXxQhtgYKVxnUzvUCkXFHXBXadg'
Enter fullscreen mode Exit fullscreen mode

Create your Store:

let store = jsonbase(TOKEN)
Enter fullscreen mode Exit fullscreen mode

To write data: (I have used the custom key 'user' to save user data, you can choose your own)

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!")
})
Enter fullscreen mode Exit fullscreen mode

To read data: (use the same key that you used while writing data)

store.read('user').then( resp => {
  console.log(resp.data)
})
Enter fullscreen mode Exit fullscreen mode

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:

If you have any queries, get in touch with me on Twitter

Discussion

pic
Editor guide