My first small basic terminal using Oclif & TypeScript

Few days ago I was thinking what I love to do and can I pull it off? I had this thought because I realize that I suck at frontend and it's fine because I don't like to design websites or anything. But I love to do backend. So I was just browsing and came to know about SDKs. I thought "hmm how can I build my own SDKs, APIs, CLIs?" and after few days I got to know about a framework that can be used to build CLIs : The Open CLI network oclif.

I made a small simple CLI using oclif and typescript.

 import {Command, Flags, ux} from '@oclif/core'
import {get} from 'node:http'

export default class HumansCommand extends Command {
  static description = 'Get the number of humans currently in space.'

  static examples = [
    '$ space-cli\nNumber of humans currently in space: 7',
  ]

  static flags = {
    table: Flags.boolean({char: 't', description: 'display who is in space and where with a table'}),
  }

  public async run(): Promise<void> {
    const {flags} = await this.parse(HumansCommand)

    get('http://api.open-notify.org/astros.json', res => {
      res.on('data', d => {
        const details = JSON.parse(d)
        this.log(`Number of humans currently in space: ${details.number}`)
        if (flags.table) {
          ux.table(details.people, {name: {}, craft: {}})
        }
      })
    }).on('error', e => {
      this.error(e)
    })
  }
}

What the above code does is basically gets the data from an API (open notify) and the data returned is in JSON format. We convert it into a textual format and log the number of people in space. Also the "ux" is used to display data in tabular format. The description and example variables are nothing but description of the CLI working example is a dummy example of the result.

You can learn more about oclif https://oclif.io/docs/introduction

Good resource to start https://levelup.gitconnected.com/how-to-build-a-simple-cli-with-oclif-af9051f56903

Thank you !