Skip to content

Usage | Programmatic

dc edited this page Feb 27, 2016 · 4 revisions

Using Cash programatically

Cash provides the ability to programmatically execute Linux-style commands in your project.

$ npm install cash --save
const $ = require('cash');
const readme = $('cat README.md');

All commands are executed synchronously and return the same stdout that the regular command execution would output.

For development-friendly stdout check out ShellJS. This is a library that implements Linux-based commands suited for development. For instance, ShellJS's ls command will return you an Array of values, whereas Cash will return raw text including ASCII color codes.

Syntax

There are three flavors of syntax available:

Shorthand

cash(command);

This version interprets the command exactly as it would have if it were typed in a Cash environment.

const cash = require('cash');
const dir = cash('ls -lah');
With template literals
cash `echo hello`;

You can make use of ES2015 template literals for a flowing, script-like syntax:

const out = require('cash') `
  cp -R ./src ./dest
  ls | grep *-spec.js | cat
  rm ./specResults.html 
  echo done!
`;

All commands are executed synchronously and stdout is suppressed and returned upon command completion.

Explicit

cash.[command](args, options);

This version exposes each command as a method of Cash, accepts a String or Array in the arguments column and an Object for the command's options.

As an example, to implement ls . ./modules -lah:

const cash = require('cash');
const dir = cash.ls(['.', './modules'], {l: true, all: true, humanreadable: true});

When an Option has a short and long method, always refer to the long method.

Available commands

Other ways to use Cash