microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d40a8b0f5b413d55cf039040ad28b27b697dd162

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

100lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as child_process from "child_process";
5import Q = require("q");
6
7export interface IExecResult {
8 process: child_process.ChildProcess;
9 outcome: Q.Promise<Buffer>;
10}
11
12export interface ISpawnResult {
13 spawnedProcess: child_process.ChildProcess;
14 stdin: NodeJS.WritableStream;
15 stdout: NodeJS.ReadableStream;
16 stderr: NodeJS.ReadableStream;
17 outcome: Q.Promise<void>;
18}
19
20interface IExecOptions {
21 cwd?: string;
22 stdio?: any;
23 env?: any;
24 encoding?: string;
25 timeout?: number;
26 maxBuffer?: number;
27 killSignal?: string;
28}
29
30interface ISpawnOptions {
31 cwd?: string;
32 stdio?: any;
33 env?: any;
34 detached?: boolean;
35}
36
37export interface IExecRejection {
38 error: Error;
39 stderr: Buffer;
40}
41
42export class ChildProcess {
43 public exec(command: string, options: IExecOptions = {}): IExecResult {
44 let outcome = Q.defer<Buffer>();
45
46 let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
47 if (error) {
48 const rejection: IExecRejection = { error: error, stderr: stderr};
49 outcome.reject(rejection);
50 } else {
51 outcome.resolve(stdout);
52 }
53 });
54
55 return { process: execProcess, outcome: outcome.promise };
56 }
57
58 public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
59 return this.exec(command).outcome.then(stdout => stdout.toString());
60 }
61
62 public spawnWithExitHandler(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
63 let outcome = Q.defer<void>();
64
65 let spawnedProcess = child_process.spawn(command, args, options);
66 spawnedProcess.once("error", (error: any) => {
67 outcome.reject(error);
68 });
69 spawnedProcess.once("exit", (code: number) => {
70 if (code === 0) {
71 outcome.resolve(void 0);
72 } else {
73 outcome.reject(new Error(`Command ${command} failed with error code ${code}`));
74 }
75 });
76
77 return {
78 spawnedProcess: spawnedProcess,
79 stdin: spawnedProcess.stdin,
80 stdout: spawnedProcess.stdout,
81 stderr: spawnedProcess.stderr,
82 outcome: outcome.promise };
83 }
84
85 public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
86 let outcome = Q.defer<void>();
87 let spawnedProcess = child_process.spawn(command, args, options);
88 spawnedProcess.once("error", (error: any) => {
89 outcome.reject({ error: error });
90 });
91
92 return {
93 spawnedProcess: spawnedProcess,
94 stdin: spawnedProcess.stdin,
95 stdout: spawnedProcess.stdout,
96 stderr: spawnedProcess.stderr,
97 outcome: outcome.promise
98 };
99 }
100}
101