microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3a0bfdb2c230b6319fe09bfd70e3bae29d0fe088

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

102lines · 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");
6import {ErrorHelper} from "../error/errorHelper";
7import {InternalErrorCode} from "../error/internalErrorCode";
8
9export interface IExecResult {
10 process: child_process.ChildProcess;
11 outcome: Q.Promise<Buffer>;
12}
13
14export interface ISpawnResult {
15 spawnedProcess: child_process.ChildProcess;
16 stdin: NodeJS.WritableStream;
17 stdout: NodeJS.ReadableStream;
18 stderr: NodeJS.ReadableStream;
19 outcome: Q.Promise<void>;
20}
21
22interface IExecOptions {
23 cwd?: string;
24 stdio?: any;
25 env?: any;
26 encoding?: string;
27 timeout?: number;
28 maxBuffer?: number;
29 killSignal?: string;
30}
31
32interface ISpawnOptions {
33 cwd?: string;
34 stdio?: any;
35 env?: any;
36 detached?: boolean;
37}
38
39export class ChildProcess {
40 public static ERROR_TIMEOUT_MILLISECONDS = 300;
41 public exec(command: string, options: IExecOptions = {}): IExecResult {
42 let outcome = Q.defer<Buffer>();
43
44 let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
45 if (error) {
46 outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
47 } else {
48 outcome.resolve(stdout);
49 }
50 });
51
52 return { process: execProcess, outcome: outcome.promise };
53 }
54
55 public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
56 return this.exec(command, options).outcome.then(stdout => stdout.toString());
57 }
58
59 public spawnWaitUntilStarted(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult {
60 let outcome = Q.defer<void>();
61 let spawnedProcess = child_process.spawn(command, args, options);
62 spawnedProcess.once("error", (error: any) => {
63 outcome.reject(error);
64 });
65
66 Q.delay(ChildProcess.ERROR_TIMEOUT_MILLISECONDS).done(() => outcome.resolve(void 0));
67
68 return {
69 spawnedProcess: spawnedProcess,
70 stdin: spawnedProcess.stdin,
71 stdout: spawnedProcess.stdout,
72 stderr: spawnedProcess.stderr,
73 outcome: outcome.promise
74 };
75 }
76
77 public spawnWaitUntilFinished(command: string, args: string[] = [], options: ISpawnOptions = {}): ISpawnResult {
78 let outcome = Q.defer<void>();
79 let commandWithArgs = command + " " + args.join(" ");
80
81 let spawnedProcess = child_process.spawn(command, args, options);
82 spawnedProcess.once("error", (error: any) => {
83 outcome.reject(error);
84 });
85
86 spawnedProcess.once("exit", (code: number) => {
87 if (code === 0) {
88 outcome.resolve(void 0);
89 } else {
90 outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code));
91 }
92 });
93
94 return {
95 spawnedProcess: spawnedProcess,
96 stdin: spawnedProcess.stdin,
97 stdout: spawnedProcess.stdout,
98 stderr: spawnedProcess.stderr,
99 outcome: outcome.promise
100 };
101 }
102}
103