/*
 * Copyright (c) 2017 NumberFour AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   NumberFour AG - Initial API and implementation
 */


import {IEventEmitter} from "events";
import {IReadableStream} from "stream";
import {IWritableStream} from "stream";
export external public interface ~ChildProcess extends IEventEmitter {
    public pid: int;
    public connected: boolean;
    public disconnect(): void;
    public kill(signal: string=): boolean;
    @Promisifiable
    public send(message: Object, sendHandle: any=, callback: {function(res: any)}=): boolean;
    public stderr: IReadableStream;
    public stdout: IReadableStream;
    public stdin: IWritableStream;
    public stdio: ?[];
}

@Promisifiable
export external public function exec(
    command: string,
    options: Object=,
    callback: {function(err: Error, stdout: Buffer, stderr: Buffer)}=): ChildProcess;

@Promisifiable
export external public function execFile(
    file: string,
    args: string[] =,
    options: Object=,
    callback: {function(err: any, stdout: Buffer, stderr: Buffer)}=): ChildProcess;

export external public function execSync(command: string, options: Object=): union{Buffer, string};
export external public function execFileSync(file: string, args: string[] =, options: Object=): union{Buffer, string};

export external public function fork(modulePath: string, args: string[] =, options: Object=): ChildProcess;
export external public function spawn(command: string, args: string[] =, options: SpawnOptions=): ChildProcess;
export external public function spawnSync(command: string, args: string[] =, options: Object=): ~Object with {
    pid: int;
    output: ?[];
    stdout: union{Buffer, string};
    stderr: union{Buffer, string};
    status: int;
    signal: string;
    error: Error;
};

export external public interface ~SpawnOptions {
	public cwd?: string;
	public env?: Object;
	public argv0?: string;
	public stdio?: Array<any>|string;
	public detached?: boolean;
	public uid?: number;
	public gid?: number;
	public serialization?: string;
	public shell?: boolean|string;
	public windowsVerbatimArguments?: boolean;
	public windowsHide?: boolean;
}

