microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
eb4cb70c8f0f4fcc4d72b116ca1b0cff5e89c5a0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

98lines · 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 exec(command: string, options: IExecOptions = {}): IExecResult {
41 let outcome = Q.defer<Buffer>();
42
43 let execProcess = child_process.exec(command, options, (error: Error, stdout: Buffer, stderr: Buffer) => {
44 if (error) {
45 outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
46 } else {
47 outcome.resolve(stdout);
48 }
49 });
50
51 return { process: execProcess, outcome: outcome.promise };
52 }
53
54 public execToString(command: string, options: IExecOptions = {}): Q.Promise<string> {
55 return this.exec(command, options).outcome.then(stdout => stdout.toString());
56 }
57
58 public spawnWithExitHandler(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
59 let outcome = Q.defer<void>();
60 let commandWithArgs = command + " " + args.join(" ");
61
62 let spawnedProcess = child_process.spawn(command, args, options);
63 spawnedProcess.once("error", (error: any) => {
64 outcome.reject(error);
65 });
66 spawnedProcess.once("exit", (code: number) => {
67 if (code === 0) {
68 outcome.resolve(void 0);
69 } else {
70 outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailedWithErrorCode, commandWithArgs, code));
71 }
72 });
73
74 return {
75 spawnedProcess: spawnedProcess,
76 stdin: spawnedProcess.stdin,
77 stdout: spawnedProcess.stdout,
78 stderr: spawnedProcess.stderr,
79 outcome: outcome.promise };
80 }
81
82 public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
83 let outcome = Q.defer<void>();
84 let commandWithArgs = command + " " + args.join(" ");
85 let spawnedProcess = child_process.spawn(command, args, options);
86 spawnedProcess.once("error", (error: any) => {
87 outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, commandWithArgs));
88 });
89
90 return {
91 spawnedProcess: spawnedProcess,
92 stdin: spawnedProcess.stdin,
93 stdout: spawnedProcess.stdout,
94 stderr: spawnedProcess.stderr,
95 outcome: outcome.promise
96 };
97 }
98}
99