Skip to content

Latest commit

 

History

History
75 lines (62 loc) · 1.51 KB

readme.md

File metadata and controls

75 lines (62 loc) · 1.51 KB

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