Command Line Deskera API Client in Node.js | Part 1: Hello, World!

Engineering Jul 08, 2020

Command-Line Interface (CLI) applications are usually the core of the task automation. If one happen to do same things over and over again, one can very well automate these steps with a CLI script  and save time.

Node.js is a great tool for writing CLI applications. Node.js has built-in libraries for file I/O, launching applications, and network communication. Furthermore, Node package manager - npm is full of packages for any task under the sun.

Over the course of the next few posts, I plan to write a basic Command Line Deskera API client in Node.js.

Part 1 — Hello, World! (this post)

Part 2 — Hello, You!

Part 3 — Call an API Endpoint

Part 4 — OAuth 2.0 Authentication

Part 5 —Show Me the Data!

(Note: The GitHub links for this part are: Browse, Zip, Diff)

Prerequisites

Install the latest (or recent) version of Node.js. The specific steps for installation may differ across the operating systems.

We will also need basic knowledge of running command in Command Prompt (Windows) on Terminal (MacOS/Linux). We will refer to Command Prompt/Terminal as Console henceforth.

Hello, World

Open Console, and create a folder for the project.

mkdir deskera-cli
mkdir deskera-cli/bin
cd deskera-cli
npm init

npm init will open an interactive interface to create the package.json file for this project.

% npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (deskera-cli)
version: (1.0.0)
description:
entry point: (index.js) bin/index.js
test command: echo "Error: no tests" && exit 1
git repository:
keywords:
author: Deskera
license: (ISC) MIT
About to write to ~/Documents/GitHub/deskera-cli/package.json:
{
  "name": "deskera-cli",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {
    "test": "echo \"Error: no tests\" && exit 1"
  },
  "author": "Deskera",
  "license": "MIT"
}
Is this OK? (yes)

This should generate a file package.json in the root of the folder.

Open a Text Editor (I recommend Sublime Text on MacOS, if vi/vim is not your thing) and create a file index.js inside deskera-cli/bin folder with the following contents

#!/usr/bin/env node

console.log( "Hello World!" );

(The first line starting with #! is called Shebang, and helps the operating system identify and install a script correctly.)

Open package.json in the Text Editor and append the following lines after license.

"bin": {   
  "hello": "./bin/index.js" 
}

Our package.json should look like this

{
  "name": "deskera-cli",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {
    "test": "echo \"Error: no tests\" && exit 1"
  },
  "author": "Deskera",
  "license": "MIT",
  "bin": {
    "hello": "./bin/index.js"
  }
}

Now, run the application

node .

To install the application “globally”

npm install -g .

With this, the commands listed in the bin section of the package.json file will be made available as command line. Try

hello

It should return the output as:

% hello

Hello!

To uninstall the application at any time, you may run:

npm uninstall -g deskera-cli

To view the list of all installed Node.js packages, try:

 npm ls -g --depth=0

Next up: Part 2 — Hello, You!

Brajesh Sachan

Brajesh, drives direction of Deskera’s future technology and shapes Deskera as the technology leader. With his expertise and over 15 years of experience, he has significantly contributed to Deskera

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.