Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

spown

nextools167.3kMIT0.1.2TypeScript support: included

Spawn child process

spawn, child_process

readme

spown npm

Spawn child process.

Install

$ yarn add spown

Usage

type TSpawnChildProcessOptions = {
  argv0?: string,
  cwd?: string,
  env?: NodeJS.ProcessEnv,
  stdin?: Stream | null,
  stdout?: Stream | null,
  stderr?: Stream | null,
  uid?: number,
  gid?: number,
  serialization?: 'json' | 'advanced'
}

const spawnChildProcess: (command: string, options?: TSpawnChildProcessOptions) => Promise<{
  stdout: string | null,
  stderr: string | null
}>
type TSpawnChildProcessStreamOptions = {
  argv0?: string,
  cwd?: string,
  env?: NodeJS.ProcessEnv,
  stdin?: Stream | null,
  stdout?: Stream | null,
  stderr?: Stream | null,
  shouldCreateIpcChannel?: boolean,
  uid?: number,
  gid?: number,
  serialization?: 'json' | 'advanced'
}

const spawnChildProcessStream: (command: string, options?: TSpawnChildProcessStreamOptions) => ChildProcess
import { spawnChildProcess, spawnChildProcessStream } from 'spown'
import { unchunkString } from 'unchunk'

console.log(
  await spawnChildProcess('ls /')
)
// {
//   stdout: 'Applications  Library  System  …',
//   stderr: ''
// }

try {
  await spawnChildProcess('ls /foo')
} catch (e) {
  console.error(e.message)
  // ls: cannot access '/foo': No such file or directory
  console.error(e.exitCode)
  // 2
}

const childProcess = spawnChildProcessStream('ls /foo')
const stderr = await unchunkString(childProcess.stderr)

console.log(stderr)
// ls: cannot access '/foo': No such file or directory