Skip to content

What does it look like?

Guilherme Vieira edited this page Dec 10, 2017 · 1 revision
let { cat, echo, rm, vim } = require('blastoise-shell');

async function main() {
    let fileName = 'hello.txt';

    await echo(`Hello, world!`)
        .map(x => x.replace('Hello', 'Hi'))
        .sed('s/world/folks/')
        .writeTo(fileName);

    await vim(fileName);

    await echo(`File contents after editing:`);
    await cat(fileName);

    await rm(fileName);
}

main().catch(console.error);

A couple things are worth noting:

  • We're very easily using system commands (echo, sed, vim, cat, rm).
  • Those commands need not be previously registered with Blastoise Shell. Blastoise Shell's exported object is actually a proxy object with a default getter that invokes any existing system command based on the property name being accessed.
  • We can very easily pipe them using a fluent interface, familiar to most JavaScript programmers.
  • We can very easily write line filters using the map function, much like Bash functions, only optionally inline and with the power of JavaScript.
  • We can use JavaScript variables, of course. It's perfectly safe to pass them as arguments to system commands: They're passed to the programs as individual arguments that match the function call argument positions, so escaping is not required. No combination of characters can:
    • Make a single argument be mistaken for two or more arguments.
    • Make another program get executed (unless the system command itself runs commands based on arguments; e.g.: bash itself,git filter-branch, etc).
    • Mess with the pipe setup of the commands.
  • Interactive commands such as vim, bash -i, or ssh, which require a TTY, work just fine.
  • With the syntax above, just like in Bash, only stdout streams are piped into stdin streams. Each child process' stderr stream is still connected to the Node script's stderr stream, so if any of them fails, error details will typically be printed before the script stops.
    • Of course, if you want to pipe stderr streams to stdout, to other child processes, or to JavaScript functions and variables, you can.
  • We can very easily pipe command outputs to files in the file system.
  • If any program exits uncleanly, an exception is thrown, which, if unhandled, causes the script to stop, much like Bash with errexit set.
  • All code is completely asynchronous, which means it's reasonable to embed it in an application with other async operations going on, such as a web server.
Clone this wiki locally