GitXplorerGitXplorer
j

spawn-tag

public
1 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
17d55b401081b8e848aa175aeacd7ecf9634e328

Badges!

jj-f1 committed 6 years ago
Unverified
eaa6b0a9c03b3bc56033843ea4549280f098d4b2

More test improvements

jj-f1 committed 6 years ago
Unverified
25847c912abff3e387b63a94f87cc64f7f363360

Words

jj-f1 committed 6 years ago
Unverified
9aaad030d2ed7aeb95c4f0d16b716d30c96b54e0

Create a test helper

jj-f1 committed 6 years ago
Unverified
4ac85d1ed4c0a4ede9a319a09df9632d7750cb47

Use regular snapshots

jj-f1 committed 6 years ago
Unverified
d8c7ff2cc94fd79946c6fd2f352980533619126d

Add/improve tests

jj-f1 committed 6 years ago

README

The README file for this repository.

spawn-tag

Build status MIT licensed view on npm download count node npm type definitions standard-readme compliant

A template tag that safely runs commands for you

spawn-tag allows you to safely run terminal commands while including user input. Instead of using escaping techniques, it uses Node.js’s spawn API to pass arguments directly to the target command without using a shell to handle parsing.

Table of Contents

Security

Although the method used in this module should be secure, please email me (address is in my profile) or contact me via Keybase (I’m j_f) if you find a security issue. Please do not open an issue as this would reveal the security issue before I can release a fix.

Install

npm install spawn-tag
# or:
yarn add spawn-tag

Usage

Import the library:

import spawn from 'spawn-tag'
// or:
import { spawn } from 'spawn-tag'
// or, if you’re using CommonJS still:
const { spawn } = require('spawn-tag')
const message = 'Hello, world!'
await spawn`echo ${message}`
// => { stdout: 'Hello, world!\n', stderr: '', code: 0, signal: null }

If you don’t need to keep the output, use .silently to avoid capturing it and save memory.

await spawn.silently`rm -r node_modules`
// => { stdout: null, stderr: null, code: 0, signal: null }

If you want to customize encodings or other options passed to spawn, pass an object:

await spawn({
  env: { ...process.env, MESSAGE: message },
})`node -e 'console.log(process.env.MESSAGE)'`
// => { stdout: 'Hello, world!\n', stderr: '', code: 0, signal: null }

Important: Since spawn-tag does not use a shell, things like $VAR or ~ won’t resolve themselves.

spawn can also give you buffers for stdout/stderr:

await spawn({ capture: { stdout: true } })`echo ${message}`
// => { stdout: <Buffer 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a>, stderr: null, code: 0, signal: null }

The Promise-like object returned from spawn has a childProcess property that contains the actual ChildProcess object if you need to interact with it.

API

// Either of these will work:
import spawn, { spawn } from 'spawn-tag'

// Same with these:
import { spawnSilently, silently } from 'spawn-tag'
const { silently } = spawn

// if you use TypeScript
import { Options } from 'spawn-tag'
interface Result {
  // string by default or if you pass an encoding
  // Buffer if you pass `true`
  // null if you pass `false` or use `.silently`
  stdout: string | Buffer | null
  stderr: string | Buffer | null
  code: number
  signal: string | null
}

interface PromiseLike extends Promise<Result> {
  childProcess: ChildProcess
}

declare const silently:
  | TemplateTag<PromiseLike>
  | ((options: Options) => TemplateTag<PromiseLike>)

declare const spawn: { silently: typeof silently } & (
  | TemplateTag<PromiseLike>
  | ((options: Options) => TemplateTag<PromiseLike>))

Maintainers

@j-f1

Contribute

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2018 Jed Fox