@tehshrike/shell-escape-tag

shell-escape-tag, but with a much smaller install size


Keywords
es6, escape, interpolate, interpolation, quote, shell, tag, tags, tagged, tagged-template, tagged-templates, template, templates
License
Artistic-2.0
Install
npm install @tehshrike/shell-escape-tag@1.2.2

Documentation

shell-escape-tag

npm status build status

An ES6+ template tag which escapes parameters for interpolation into shell commands

INSTALL

npm install shell-escape-tag

SYNOPSIS

import shell from 'shell-escape-tag'

let filenames = glob('Holiday Snaps/*.jpg')
let title     = 'Holiday Snaps'
let command   = shell`compress --title ${title} ${filenames}`

console.log(command) // compress --title 'Holiday Snaps' 'Holiday Snaps/Picture 1.jpg' 'Holiday Snaps/Picture 2.jpg'

DESCRIPTION

This module exports an ES6+ tagged-template function which escapes (i.e. quotes) its parameters for safe inclusion in shell commands. Parameters can be strings, arrays of strings, or nested arrays of strings, arrays and already-processed parameters.

The exported function also provides two helper methods which respectively escape and preserve their parameters and protect them from further escaping.

EXPORTS

shell (default)

Signature: (strings: Array[string], ...params: Any) -> command: string

let filenames = [ 'foo bar', 'baz quux' ]
let title     = 'My Title'
let command   = shell`command --title ${title} ${filenames}`

console.log(command) // command --title 'My Title' 'foo bar' 'baz quux'

Takes a template string and escapes any interpolated parameters. null and undefined values are ignored (i.e. mapped to an empty string). Arrays are flattened and their members are escaped and joined with a space. All other values are stringified i.e. false is mapped to "false" &c.. Parameters that have been escaped with shell.escape or preserved with shell.preserve are passed through verbatim.

Methods

escape

Signature: ...Any -> Object

let param = shell.escape(shell.preserve('foo bar'), [ "baz's quux" ])
let command = shell`command ${param}`

console.log(command) // command foo bar 'baz'"'"'s quux'

Flattens and escapes any parameters which haven't already been escaped or preserved, and returns an object which is passed through verbatim when passed as a direct or nested parameter to the shell function.

preserve

Aliases: protect, verbatim

Signature: ...Any -> Object

let param = shell.preserve(shell.escape('foo bar'), [ "baz's quux" ])
let command = shell`command ${param}`

console.log(command) // command 'foo bar' baz's quux

Flattens and preserves any parameters which haven't already been escaped or preserved, and returns an object which is passed through verbatim when passed as a direct or nested parameter to the shell function.

SEE ALSO

VERSION

1.0.0

AUTHOR

chocolateboy

COPYRIGHT AND LICENSE

Copyright © 2015 by chocolateboy

shell-escape-tag is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.