microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3b6023b2c4497b5b8cf54373639977efc9e6e612

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

96lines · 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
61 let spawnedProcess = child_process.spawn(command, args, options);
62 spawnedProcess.once("error", (error: any) => {
63 outcome.reject(error);
64 });
65 spawnedProcess.once("exit", (code: number) => {
66 if (code === 0) {
67 outcome.resolve(void 0);
68 } else {
69 outcome.reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed));
70 }
71 });
72
73 return {
74 spawnedProcess: spawnedProcess,
75 stdin: spawnedProcess.stdin,
76 stdout: spawnedProcess.stdout,
77 stderr: spawnedProcess.stderr,
78 outcome: outcome.promise };
79 }
80
81 public spawn(command: string, args?: string[], options: ISpawnOptions = {}): ISpawnResult {
82 let outcome = Q.defer<void>();
83 let spawnedProcess = child_process.spawn(command, args, options);
84 spawnedProcess.once("error", (error: any) => {
85 outcome.reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
86 });
87
88 return {
89 spawnedProcess: spawnedProcess,
90 stdin: spawnedProcess.stdin,
91 stdout: spawnedProcess.stdout,
92 stderr: spawnedProcess.stderr,
93 outcome: outcome.promise
94 };
95 }
96}
97