microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a9925ee8b659b3f9dba8a521555c8ee58fd8f31

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

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