microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

128lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
6f8b71fedigeff10 years ago4import * as nodeChildProcess from "child_process";
ce5e88eeYuri Skorokhodov5 years ago5import { ErrorHelper } from "../error/errorHelper";
6import { InternalErrorCode } from "../error/internalErrorCode";
3fb37ad5unknown10 years ago7
c7f1165cdigeff10 years ago8// Uncomment the following lines to record all spawned processes executions
3c172a05Artem Egorov8 years ago9// import {Recorder} from "../../../test/resources/processExecution/recorder";
c7f1165cdigeff10 years ago10// Recorder.installGlobalRecorder();
11
3fb37ad5unknown10 years ago12export interface IExecResult {
6f8b71fedigeff10 years ago13process: nodeChildProcess.ChildProcess;
ce5e88eeYuri Skorokhodov5 years ago14outcome: Promise<string>;
3fb37ad5unknown10 years ago15}
16
45944d15Meena Kunnathur Balakrishnan10 years ago17export interface ISpawnResult {
6f8b71fedigeff10 years ago18spawnedProcess: nodeChildProcess.ChildProcess;
5b0582f3digeff10 years ago19stdin: NodeJS.WritableStream;
20stdout: NodeJS.ReadableStream;
21stderr: NodeJS.ReadableStream;
ce5e88eeYuri Skorokhodov5 years ago22outcome: Promise<void>;
45944d15Meena Kunnathur Balakrishnan10 years ago23}
24
3fb37ad5unknown10 years ago25interface IExecOptions {
26cwd?: string;
27stdio?: any;
28env?: any;
29encoding?: string;
30timeout?: number;
31maxBuffer?: number;
32killSignal?: string;
33}
34
35interface ISpawnOptions {
36cwd?: string;
37stdio?: any;
38env?: any;
39detached?: boolean;
40}
41
42export class ChildProcess {
88246fe9Meena Kunnathur Balakrishnan10 years ago43public static ERROR_TIMEOUT_MILLISECONDS = 300;
6f8b71fedigeff10 years ago44private childProcess: typeof nodeChildProcess;
aab2095edigeff10 years ago45
ce5e88eeYuri Skorokhodov5 years ago46constructor({ childProcess = nodeChildProcess } = {}) {
aab2095edigeff10 years ago47this.childProcess = childProcess;
48}
49
ce5e88eeYuri Skorokhodov5 years ago50public exec(command: string, options: IExecOptions = {}): Promise<IExecResult> {
51let outcome: Promise<string>;
52let process: nodeChildProcess.ChildProcess;
53return new Promise<IExecResult>((resolveRes) => {
54outcome = new Promise<string>((resolve, reject) => {
55process = this.childProcess.exec(command, options, (error: Error, stdout: string, stderr: string) => {
56if (error) {
57reject(ErrorHelper.getNestedError(error, InternalErrorCode.CommandFailed, command));
58} else {
59resolve(stdout);
60}
61});
62});
63resolveRes({process: process, outcome: outcome});
3fb37ad5unknown10 years ago64});
65
66}
67
ce5e88eeYuri Skorokhodov5 years ago68public execToString(command: string, options: IExecOptions = {}): Promise<string> {
69return this.exec(command, options).then(result => result.outcome.then(stdout => stdout.toString()));
3fb37ad5unknown10 years ago70}
71
b3753d4eRedMickey6 years ago72public execFileSync(command: string, args: string[] = [], options: IExecOptions = {}): Buffer | string {
73return this.childProcess.execFileSync(command, args, options);
74}
75
dd8375caJiglioNero5 years ago76public spawn(command: string, args: string[] = [], options: ISpawnOptions = {}, showStdOutputsOnError: boolean = false): ISpawnResult {
9596aa53digeff10 years ago77const spawnedProcess = this.childProcess.spawn(command, args, options);
ce5e88eeYuri Skorokhodov5 years ago78let outcome: Promise<void> = new Promise((resolve, reject) => {
dd8375caJiglioNero5 years ago79
ce5e88eeYuri Skorokhodov5 years ago80spawnedProcess.once("error", (error: any) => {
81reject(error);
82});
83
dd8375caJiglioNero5 years ago84const stderrChunks: string[] = [];
85const stdoutChunks: string[] = [];
86
87spawnedProcess.stderr.on("data", (data) => {
88stderrChunks.push(data.toString());
89});
90
91spawnedProcess.stdout.on("data", (data) => {
92stdoutChunks.push(data.toString());
93});
94
ce5e88eeYuri Skorokhodov5 years ago95spawnedProcess.once("exit", (code: number) => {
96if (code === 0) {
97resolve();
98} else {
99const commandWithArgs = command + " " + args.join(" ");
dd8375caJiglioNero5 years ago100if (showStdOutputsOnError) {
101let details = "";
102if (stdoutChunks.length > 0) {
103details = details.concat(`\n\tSTDOUT: ${stdoutChunks[stdoutChunks.length-1]}`);
104}
105if (stderrChunks.length > 0) {
106details = details.concat(`\n\tSTDERR: ${stderrChunks.join("\n\t")}`);
107}
108if (details === "") {
109details = "STDOUT and STDERR are empty!";
110}
111reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailedWithDetails, commandWithArgs, details));
112}
113else {
114reject(ErrorHelper.getInternalError(InternalErrorCode.CommandFailed, commandWithArgs, code));
115}
ce5e88eeYuri Skorokhodov5 years ago116}
117});
3fb37ad5unknown10 years ago118});
c3a987a7Meena Kunnathur Balakrishnan10 years ago119return {
ce5e88eeYuri Skorokhodov5 years ago120spawnedProcess: spawnedProcess,
121stdin: spawnedProcess.stdin,
122stdout: spawnedProcess.stdout,
123stderr: spawnedProcess.stderr,
124outcome: outcome,
125};
126
c3a987a7Meena Kunnathur Balakrishnan10 years ago127}
3fb37ad5unknown10 years ago128}