Create Your Own Reddit CLI With Nodejs

Create Your Own Reddit CLI With Nodejs

We all know CLI stands for "Command Line Interface". Wouldn't it be cool if we could make our own CLI? Well in this post we are going to do just that.

Let's build a CLI that opens a random post in our browser or prints it on the terminal itself when given a '--print' flag. Find my github repo here.

Some Prerequisites

You must have node version>14 installed since we will be using ES6 module syntax instead of the common js module here.

Step 1:

Create a folder (in my case I'm naming it "RedditCLI" only) and then make it a package by typing npm init from inside that folder.

Step 2:

In that folder, create a new file reddit.mjs

// reddit.mjs
#! /usr/bin/env node

console.log('hello from your CLI')

The first line on the file is called a shabang or hashbang. It's needed to tell the machine where the interpreter is located that is needed to execute this file. For us, that will be Node.js.

Step 3:

Now we need to tell Node what the name of our CLI is going to be. With this name, we will be able to use this in our terminal. Add the below code to the package.json.

"bin": {
  "reddit": "./reddit.mjs"
}

Once installed, this package will have its bin command installed into our machine's bin folder allowing us to use the reddit command.

Step 4:

Now, we must install our own package locally so we can test out the CLI. We could just execute the file with the node runtime, but we want to see the CLI actually work. So run the below command from inside the folder:

npm install -g

We can simply install with no args which tells npm to install the current directory. The -g flag means we want to globally install this package vs in a local node_modules. You should now be able to run reddit and see your log print.

Step 5:

Moving onto actually making it print a reddit post, we must install some useful packages:

npm install open node-fetch yargs --save

We'll install just these three packages.

  • open - will open our browser with a URL
  • node-fetch - is a fetch client that we can use to hit the reddit API
  • yargs - will allow us to process any flags or arguments passed to the CLI

Step 6:

Paste this code in the reddit.mjs file.

#! /usr/bin/env node
// import our packages
import open from 'open'
import fetch from 'node-fetch'
import yargs from 'yargs'

// parse env vars
const { argv } = yargs(process.argv)
// init fetch to reddit api
const res = await fetch('https://www.reddit.com/.json')
const data = await res.json()
const randomIndex = Math.floor(Math.random() * data.data.children.length)
// get radom post from reddit api response of all posts on front page
const post = data.data.children[randomIndex]

// log if --print flag is passed
if (argv.print) {
  console.log(`
    Title: ${post.data.title}\n
    Link: ${post.data.permalink}
  `)
} else {
  // open in browser if not
  open(`https://reddit.com${post.data.permalink}`)
}

Your own Reddit CLI is ready. You improvise it more by adding some more flags or sub commands.

Now, when we will type reddit a post will be open up in your default browser.

Screenshot from 2022-03-08 22-58-31.png

And when we'll give --print flag reddit --print we'll be able to see the post in our terminal along with the link.

Screenshot from 2022-03-08 22-57-49.png

Please share it if you like it :)