microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bcb44c4d4e94b00d16fc644eee9a1d64a2ef86b7

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/childProcess.ts

160lines · 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;
34472878RedMickey5 years ago53return new Promise<IExecResult>(resolveRes => {
ce5e88eeYuri Skorokhodov5 years ago54outcome = new Promise<string>((resolve, reject) => {
34472878RedMickey5 years ago55process = this.childProcess.exec(
56command,
57options,
58// eslint-disable-next-line @typescript-eslint/no-unused-vars
59(error: Error, stdout: string, stderr: string) => {
ce5e88eeYuri Skorokhodov5 years ago60if (error) {
34472878RedMickey5 years ago61reject(
62ErrorHelper.getNestedError(
63error,
64InternalErrorCode.CommandFailed,
65command,
66),
67);
ce5e88eeYuri Skorokhodov5 years ago68} else {
69resolve(stdout);
70}
34472878RedMickey5 years ago71},
72);
73});
74resolveRes({ process: process, outcome: outcome });
3fb37ad5unknown10 years ago75});
76}
77
ce5e88eeYuri Skorokhodov5 years ago78public execToString(command: string, options: IExecOptions = {}): Promise<string> {
34472878RedMickey5 years ago79return this.exec(command, options).then(result =>
80result.outcome.then(stdout => stdout.toString()),
81);
3fb37ad5unknown10 years ago82}
83
34472878RedMickey5 years ago84public execFileSync(
85command: string,
86args: string[] = [],
87options: IExecOptions = {},
88): Buffer | string {
b3753d4eRedMickey6 years ago89return this.childProcess.execFileSync(command, args, options);
90}
91
34472878RedMickey5 years ago92public spawn(
93command: string,
94args: string[] = [],
95options: ISpawnOptions = {},
96showStdOutputsOnError: boolean = false,
97): ISpawnResult {
9596aa53digeff10 years ago98const spawnedProcess = this.childProcess.spawn(command, args, options);
ce5e88eeYuri Skorokhodov5 years ago99let outcome: Promise<void> = new Promise((resolve, reject) => {
100spawnedProcess.once("error", (error: any) => {
101reject(error);
102});
103
dd8375caJiglioNero5 years ago104const stderrChunks: string[] = [];
105const stdoutChunks: string[] = [];
106
34472878RedMickey5 years ago107spawnedProcess.stderr.on("data", data => {
dd8375caJiglioNero5 years ago108stderrChunks.push(data.toString());
109});
110
34472878RedMickey5 years ago111spawnedProcess.stdout.on("data", data => {
dd8375caJiglioNero5 years ago112stdoutChunks.push(data.toString());
113});
114
ce5e88eeYuri Skorokhodov5 years ago115spawnedProcess.once("exit", (code: number) => {
116if (code === 0) {
117resolve();
118} else {
119const commandWithArgs = command + " " + args.join(" ");
dd8375caJiglioNero5 years ago120if (showStdOutputsOnError) {
121let details = "";
122if (stdoutChunks.length > 0) {
34472878RedMickey5 years ago123details = details.concat(
124`\n\tSTDOUT: ${stdoutChunks[stdoutChunks.length - 1]}`,
125);
dd8375caJiglioNero5 years ago126}
127if (stderrChunks.length > 0) {
128details = details.concat(`\n\tSTDERR: ${stderrChunks.join("\n\t")}`);
129}
130if (details === "") {
131details = "STDOUT and STDERR are empty!";
132}
34472878RedMickey5 years ago133reject(
134ErrorHelper.getInternalError(
135InternalErrorCode.CommandFailedWithDetails,
136commandWithArgs,
137details,
138),
139);
140} else {
141reject(
142ErrorHelper.getInternalError(
143InternalErrorCode.CommandFailed,
144commandWithArgs,
145code,
146),
147);
dd8375caJiglioNero5 years ago148}
ce5e88eeYuri Skorokhodov5 years ago149}
150});
3fb37ad5unknown10 years ago151});
c3a987a7Meena Kunnathur Balakrishnan10 years ago152return {
ce5e88eeYuri Skorokhodov5 years ago153spawnedProcess: spawnedProcess,
154stdin: spawnedProcess.stdin,
155stdout: spawnedProcess.stdout,
156stderr: spawnedProcess.stderr,
157outcome: outcome,
34472878RedMickey5 years ago158};
c3a987a7Meena Kunnathur Balakrishnan10 years ago159}
3fb37ad5unknown10 years ago160}