node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »

@ygor/shell

@ygor/shell

NPM version Downloads Build Status Coverage Status

A no-frills shell template tag. Built on promises to work wonderfully with async and await in Node.js 8 and above. Part of the Ygor toolkit.

Install

$ npm install --save @ygor/file

Usage

const shell = require('@ygor/shell');
 
const foo = 'hello world';
const bar = 'goodnight moon';
 
await shell`
    echo ${foo}
    >&2 echo ${bar}
`;
 
const result = await shell({ stdio: 'pipe' })`
    echo ${foo}
    >&2 echo ${bar}
`;
 
console.log(result.stdout, result.stderr);

API

shell`cmd`: Promise

  • cmd {String}

Interpolates a string, escapes values to make them safe to run in the shell, then executes the command. Supports multi-line commands. Directs stdout and stderr to the parent process.

await shell`
    echo ${foo}
    >&2 echo ${bar}
`;
 
try {
  await shell`exit 123`;
} catch (e) {
  console.error(`Exited with code: ${e.code}`);
}

shell([options]): Function(strings, ...values): Promise

Creates a template-tag function with the given options. Useful for overriding things like the current working directory or how stdio is handled.

const shellPipe = shell({ stdio: 'pipe' });
const process = shellPipe`echo hello world`;
 
process.stdout.pipe(process.stdout);
 
console.log(await process);

© 2017 Shannon Moeller me@shannonmoeller.com (shannonmoeller.com)

Licensed under MIT