Skip to content

Commit

Permalink
feat: export Flow types
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmclaurin committed Nov 27, 2018
1 parent ce24cf8 commit e260ed9
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
import type { ChildProcess } from 'child_process'
import child_process from 'child_process'

type Output = {
export type ChildProcessOutput = {
stdout: ?(string | Buffer),
stderr: ?(string | Buffer),
}

type ErrorWithOutput = Error & {
export type ErrorWithOutput = Error & {
stdout?: ?(string | Buffer),
stderr?: ?(string | Buffer),
code?: ?number,
signal?: ?string,
}

type ChildProcessPromise = ChildProcess & Promise<Output>
export type ChildProcessPromise = ChildProcess & Promise<ChildProcessOutput>

type PromisifyChildProcessBaseOpts = {
encoding?: $PropertyType<child_process$spawnSyncOpts, 'encoding'>,
killSignal?: $PropertyType<child_process$spawnSyncOpts, 'killSignal'>,
maxBuffer?: $PropertyType<child_process$spawnSyncOpts, 'maxBuffer'>,
}

export type SpawnOpts = child_process$spawnOpts & PromisifyChildProcessBaseOpts
export type ForkOpts = child_process$forkOpts & PromisifyChildProcessBaseOpts

function joinChunks(
chunks: $ReadOnlyArray<string | Buffer>,
Expand All @@ -31,15 +40,11 @@ function joinChunks(

export function promisifyChildProcess(
child: ChildProcess,
options: {
encoding?: $PropertyType<child_process$spawnSyncOpts, 'encoding'>,
killSignal?: $PropertyType<child_process$spawnSyncOpts, 'killSignal'>,
maxBuffer?: $PropertyType<child_process$spawnSyncOpts, 'maxBuffer'>,
} = {}
options: PromisifyChildProcessBaseOpts = {}
): ChildProcessPromise {
const _promise = new Promise(
(
resolve: (result: Output) => void,
resolve: (result: ChildProcessOutput) => void,
reject: (error: ErrorWithOutput) => void
) => {
const { encoding, killSignal } = options
Expand Down Expand Up @@ -115,7 +120,7 @@ export function promisifyChildProcess(
/* eslint-enable no-console */
}
}
const output: Output = ({}: any)
const output: ChildProcessOutput = ({}: any)
defineOutputs(output)
const finalError: ?ErrorWithOutput = error
if (finalError) {
Expand All @@ -140,11 +145,7 @@ export function promisifyChildProcess(
export function spawn(
command: string,
args?: Array<string> | child_process$spawnOpts,
options?: child_process$spawnOpts & {
encoding?: $PropertyType<child_process$spawnSyncOpts, 'encoding'>,
killSignal?: $PropertyType<child_process$spawnSyncOpts, 'killSignal'>,
maxBuffer?: $PropertyType<child_process$spawnSyncOpts, 'maxBuffer'>,
}
options?: SpawnOpts
): ChildProcessPromise {
return promisifyChildProcess(
child_process.spawn(command, args, options),
Expand All @@ -155,11 +156,7 @@ export function spawn(
export function fork(
module: string,
args?: Array<string> | child_process$forkOpts,
options?: child_process$forkOpts & {
encoding?: $PropertyType<child_process$spawnSyncOpts, 'encoding'>,
killSignal?: $PropertyType<child_process$spawnSyncOpts, 'killSignal'>,
maxBuffer?: $PropertyType<child_process$spawnSyncOpts, 'maxBuffer'>,
}
options?: ForkOpts
): ChildProcessPromise {
return promisifyChildProcess(
child_process.fork(module, args, options),
Expand All @@ -172,7 +169,7 @@ function promisifyExecMethod(method: any): any {
let child: ?ChildProcess
const _promise = new Promise(
(
resolve: (output: Output) => void,
resolve: (output: ChildProcessOutput) => void,
reject: (error: ErrorWithOutput) => void
) => {
child = method(
Expand Down

0 comments on commit e260ed9

Please sign in to comment.