This episode is for subscribers only. Sign Up or Log in to watch it.

30. Building Command Line Applications with Lumo - part 2

Published 28 April 17

Now that the “birch” command is working, it’s time to release it to the world. In this episode you’ll learn how to turn your Lumo script into an NPM package. You’ll also learn how to use packages from NPM (cli-colors) and Clojure library jars (tools.cli) to make your script even more awesome.

Show notes

In episode 29 I showed you how to write a Lumo script, a re-implementation of the Unix tree command, using several of Node’s built-in packages.

In this episode you’ll learn how to publish your script as an NPM package, and how to make use of third party libraries from NPM and Clojars.

So far this script is just a single file, and it has no dependencies apart from what ClojureScript and Node provide out of the box.

Let’s try to make it more interesting, by using an npm package to add some color to the output.

First create a package.json inside the birch directory. There’s a handy npm init command to help you with that. The defaults are fine for now, just press enter a bunch of times, or you can add --yes to accept all defaults.

$ 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 json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (birch)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /tmp/birch/package.json:

{
  "name": "birch",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes)

Now you can install the cli-color npm package. Add the --save flag, this way cli-color gets added as a dependency in package.json. This also adds a node_modules directory which contains the downloaded package. If you’re planning to make a git repo for this project then make sure to exclude this directory from version control.

browse source code

You can browse the code the way it looks at the end of this episode in the ep30-end branch.

  • Unravel and Mach are two Lumo CLI apps distributed as NPM packages. You can check out their source for inspiration.

  • Abio and cljs-node-io are both libraries that provide ClojureScript wrappers for built-in node API’s. If you’re planning to do non-trivial IO or host access you should check these out.

  • Calvin provides a build tool that uses Lumo to compile your ClojureScript project. Even though young and immature it’s definitely a tool to watch. Interestingly it’s not packaged as a Lumo script, but instead uses lein-cljsbuild to compile to JavaScript, after which it’s packaged as a regular Node package.

  • StackOverflow on how to get the location of a bash script.