revealmd.js

Building a Node.js Command Line App

Josh Finnie

Who Am I?

  • Josh Finnie
  • Software Maven @ TrackMaven
  • Purveyor of @NodeDC Hack Nights / Office Hours

Why a Node.js CLA?

  • I tried to write Bash
  • I was terrible at it
  • Then this happened:

Bad Bash

source: Optimizing with Bash!

Rage

And

We know Node...

So how?

  • Write in node.js
  • ...
  • Profit?

Setup

  • Tell the app you're using node.js
#!/usr/bin/env node

console.log("Hello World!")
  • Make it executable
$ chmod u+x myCLA
  • Run it
$ ./myCLA
Hello World!

Example 1

ex1

#!/usr/bin/env node

var userArgs = process.argv.slice(2);
var name = userArgs[0];

console.log("Hello " + name + "!");

output

$ ./ex1 Josh
Hello Josh!

Running a CLA

  • Run using the node interpreter:
$ node bin/myCLA
  • Making it executible:
$ ./bin/myCLA
  • Install globally:
$ myCLA

Making this better

var userArgs = process.argv.slice(2);
var name = userArgs[0];

Example 2

Using Optimist

(Pushfile as an example...)

#!/usr/bin/env node

var argv = optimist
  .usage('Pushes file to S3 using a shortened name.\n\nVersion: ' + version + '\n\nUsage: $0 [options] <file>')
  .boolean('u')
  .alias('u', 'unique')
  .describe('u', 'Gives a unique, timestamped hash for uploaded filename.')
  .boolean('c')
  .alias('c', 'configure')
  .describe('c', 'Create a configuration file.')
  .string('h')
  .alias('h', 'help')
  .describe('h', 'Print usage info')
  .argv;

Ouput of Optimist

╭ ▶ joshfinnie@trackmaven  ~
╰ ▶ $ pushfile -h
Pushes file to S3 using a shortened name.

Version: 0.1.3

Usage: /usr/local/bin/pushfile [options] <file>

Options:
  -u, --unique     Gives a unique, timestamped hash for uploaded filename.
  -c, --configure  Create a configuration file.
  -h, --help       Print usage info

Testing your CLA

  • It's Node.js!

Node Testing Options

(source)

Releasing your CLA

  • Update your package.json
    • Set preferGlobal to true
    • Set location of executible using bin
    "preferGlobal": true,
    "bin": {"app": "bin/app"}

Setting up NPM

  • Create a user on NPM
  • Run npm adduser in your terminal
$ npm adduser
Username: joshfinnie
Password:
Email: (this IS public) josh@jfin.us
npm http PUT https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie
npm http 409 https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie
npm http GET https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie?write=true
npm http 200 https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie?write=true
npm http PUT https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie/-rev/3-5cbc34433444234f5e75d385d5ffe960
npm http 201 https://registry.npmjs.org/-/user/org.couchdb.user:joshfinnie/-rev/3-5cbc34433444234f5e75d385d5ffe960

Publish to NPM

  • cd into your CLA directory
  • Run the following command:
    npm publish .
    
  • Yes, it is that easy...

Conclusion

That's a super high-level talk about writing a command-line application in Node.js

  • Remember:
    • It's just Node.js
    • Arg parsing is a pain
    • Test your app
    • Publish often!

Connect with me!

We're Hiring

Twerking Maven

Questions

?