Node.js is an open-source javascript runtime built that execute Javascript code outside of a traditional browser.

JavaScript is a high-level, interpreted scripting language … JavaScript has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions. — Wikipedia

In this post I will demonstrate how to create simple CLI (command line interface) applications that would generate random programmer jokes.

Dependencies

First, use terminal command brew install node to install both Node.js and NPM on your computer.

  • Homebrew is a ruby-based package management system that simplifies the installation of software on Linux computers.
  • NPM, or Node Package Manager, is the default package manager for Node.js.

Now use npm to install the commander package on your computer. The -g argument makes sure that the package globally accessible.

1
npm install commander -g

Set Up

Create a new directory for your CLI.

1
mkdir generator && cd generator && npm init

The terminal will prompt the creation of package.json, which contains all relevant information about this application.

JSON, or JavaScript Object Notation, is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types. — Wikipedia

Opening up the json file and add in the "bin" category. This will later allow the keyword generator to be used in terminal to directly execute the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"generator"
],
"bin": {
"generator": "bin/generator.js"
},
"author": "Rongjia Liu <jackliu234@gmail.com>",
"license": "MIT"
}

Now create a bin folder under the new directory just created, which will contain the javascript executable. The data folder contains the programmer jokes in txt format.

1
mkdir bin && mkdir data

Create a javascript file generator.js as follow. Save under the bin directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env node

var cmd = require('/usr/local/lib/node_modules/commander');
var appInfo = require('../package.json');
var fs = require('fs');
var path = require('path');

var zh = fs.readFileSync(path.join(__dirname, '../data/jokes-zh.txt')).toString().split('\n');
var en = fs.readFileSync(path.join(__dirname, '../data/jokes-en.txt')).toString().split('\n');

/**
* @param type
* @returns {string}
*/
function generator(type) {
switch (type) {
case 'zh':
return zh[Math.floor(Math.random() * zh.length)];
case 'en':
return en[Math.floor(Math.random() * en.length)];
default:
return zh[Math.floor(Math.random() * zh.length)];
}
}

cmd
.version(appInfo.version)
.option('-t, --type <value>', '[zh|en]', 'zh', /^(zh|en)$/i)
.on('--help', function(){}).parse(process.argv);

console.log(generator(cmd.type));

Lastly, install the package locally on your computer.

1
sudo npm install . -g

Result

Now you can type in the generator keyword in terminal to generate random jokes.

1
2
3
4
5
6
7
$ generator

阿里小米皆自主,百度排名最公平;
京东全网最低价,当当爱国很理性;
用户体验看新浪,网易从来少愤青;
豆瓣从来不约炮,人人分享高水平;
从不抄袭数腾讯,开放安全三六零。

The default language is Chinese (ZH). Add an argument -t en will display English jokes.

1
2
$ generator -t en
The box said 'Requires Windows 95 or better'. So I installed LINUX

Display the help menu, which is generated from the commander.js installed in the beginning. It can be customized through the cmd section of the generator.js above.

1
2
3
4
5
6
7
$ generator --help
Usage: generator [options]

Options:
-V, --version output the version number
-t, --type <value> [zh|en] (default: "zh")
-h, --help output usage information




Reference