microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
54fa47fb3e78bced5b45cc6f803794d9eaa076da

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/utils/node/childProcess.ts

78lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
3fb37ad5unknown10 years ago4import * as child_process from "child_process";
5import Q = require("q");
6
7export interface IExecResult {
8process: child_process.ChildProcess;
9outcome: Q.Promise<Buffer>;
10}
11
45944d15Meena Kunnathur Balakrishnan10 years ago12export interface ISpawnResult {
13spawnedProcess: child_process.ChildProcess;
14stdin: any;
15stdout: any;
16stderr: any;
17outcome: Q.Promise<number>;
18}
19
3fb37ad5unknown10 years ago20interface IExecOptions {
21cwd?: string;
22stdio?: any;
23env?: any;
24encoding?: string;
25timeout?: number;
26maxBuffer?: number;
27killSignal?: string;
28}
29
30interface ISpawnOptions {
31cwd?: string;
32stdio?: any;
33env?: any;
34detached?: boolean;
35}
36
37export class ChildProcess {
38public exec(command: string, options: IExecOptions = {}): IExecResult {
39let outcome = Q.defer<Buffer>();
40
41let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
42if (error) {
43outcome.reject({ error: error, stderr: stderr});
44} else {
45outcome.resolve(stdout);
46}
47});
48
49return { process: execProcess, outcome: outcome.promise };
50}
51
52public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
53return this.exec(command).outcome.then(stdout => stdout.toString());
54}
55
56public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
57let outcome = Q.defer<number>();
58
59let spawnedProcess = child_process.spawn(command, args, options);
60spawnedProcess.once("error", (error: any) => {
61outcome.reject({ error: error });
62});
6c330a04dlebu10 years ago63spawnedProcess.once("exit", (code: number) => {
3fb37ad5unknown10 years ago64if (code === 0) {
65outcome.resolve(code);
6c330a04dlebu10 years ago66} else {
67outcome.reject({error: code});
3fb37ad5unknown10 years ago68}
69});
70
b70ce8b3Meena10 years ago71return {
72spawnedProcess: spawnedProcess,
73stdin: spawnedProcess.stdin,
74stdout: spawnedProcess.stdout,
3fb37ad5unknown10 years ago75stderr: spawnedProcess.stderr,
b70ce8b3Meena10 years ago76outcome: outcome.promise };
3fb37ad5unknown10 years ago77}
78}