@ygor/shell

A transformable shell object.

Usage no npm install needed!

<script type="module">
  import ygorShell from 'https://cdn.skypack.dev/@ygor/shell';
</script>

README

@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/shell

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);

ygor


MIT © Shannon Moeller