Heroku, SalesForce framework to build CLIs

Features

Command

Extend Command base class for application's commands

import { Command } from '@oclif/command'

export class MyCommand extends Command {
  static description = 'description of this example command'

  async run() {
    console.log('running my command')
  }
}

Arguments

Arguments are declared on the command level, parsed by oclif and used for documentation generation

LOG_LEVEL=debug note
import { Command, flags } from '@oclif/command'

export default class MyCommand extends Command {
  static flags = {
    logLevel: flags.string({
      description: `Environment variable 'LOG_LEVEL'.\nIt CAN NOT be passed as a flag`,
      env: 'LOG_LEVEL',
    })
  }

  async run() {
    const { flags: { logLevel } } = this.parse(MyCommand)
    
    console.log(`running my command with logLevel ${logLevel}`)
  }
}

Create a new CLI project with oclif generator

npx oclif multi my-oclif-cli
cd my-oclif-cli
npm install -g .
my-oclif-cli hello

Note - Project Management as CLI

Educational Open Source Project to practice with JavaScript, TypeScript, Node, oclif, Git, Web Components, and Project Management

Make a command to send Hello World notification to slack

npm install @slack/webhook
npx oclif command slack

Example of input/output

my-oclif-cli slack "Hello from @username"
# the message "Hello from @username" appears in the slack channel

Configure your Slack

1. How to obtain the WebHook URL for our Slack Node-Edu Channel:

2. Put the Webhook URL to config/.slackrc file as SLACK_WEBHOOK_URL environment variable

export SLACK_WEBHOOK_URL=___WEBHOOK_GOES_HERE___
# or
export SLACK_WEBHOOK_URL=$(echo "aHR0cHM6Ly9ob29rcy5zbGFjay5jb20vc2VydmljZXMvVEwwMzg2V1BOL0JRMzRWREhQVy9DTjg3d2NVYlE4YTkyMmhaZjBaeEgwMVM=" | base64 --decode)
# or 
export SLACK_WEBHOOK_URL=$(workshop slack)

3. Import .slackrc to your shell with source

source config/.slackrc

Install NPM dependencies

npm i @slack/webhook

Write the command

1. Require an IncomingWebhook class from the @slack/webhook

import { IncomingWebhook } from '@slack/webhook'

2. Set a description for your command

static description = 'Send a message to a channel in Slack'

3. The text should be provided as an argument. So, lets define an argument

static args = [
  {
    name: 'text',
    required: true
  }
]

4. Add a definition of the flag to flags section. You may still keep "help" flag since it's quite useful usually

static flags = {
  help: flags.help({
    char: 'h'
  }),
  slackWebhookUrl: flags.string({
    env: 'SLACK_WEBHOOK_URL',
    required: true
  })
}

5. In the very beginning of the run function lets get our flags and args from the input with the following line

const { flags, args } = this.parse(Slack)

6. Next lets create a new instance of IncomingWebhook with a slackWebhookUrl flag

const webhook = new IncomingWebhook(flags.slackWebhookUrl)

7. Call the "send" method with an object containing "text" property with your text. Please bear in mind that this is an async function

await webhook.send({ text: args.text })

spoiler alert

Send Slack message code

my-oclif-cli slack "Hello World!"