microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1b936088b9092a9f0b649ec25466475140a89d4d

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/makeOutcomeFailDependingOnOutput.ts

63lines · 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 Q from "q";
5import {ISpawnResult} from "./node/childProcess";
6
7export type PatternToFailure = { [pattern: string]: string };
8
9/* This class transforms a spawn process to only succeed if all defined success patterns
10 are found on stdout, and none of the failure patterns were found on stderr */
11export class MakeOutcomeFailDependingOnOutput {
12 private generatePatternsForSuccess: () => Q.Promise<string[]>;
13 private generatePatternToFailure: () => Q.Promise<PatternToFailure>;
14
15 private output = "";
16 private errors = "";
17
18 constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure>) {
19 this.generatePatternsForSuccess = generatePatternsForSuccess;
20 this.generatePatternToFailure = generatePatternToFailure;
21 }
22
23 public process(spawnResult: ISpawnResult): Q.Promise<void> {
24 // Store all output
25 this.store(spawnResult.stdout, new_content => this.output += new_content);
26 this.store(spawnResult.stderr, new_content => this.errors += new_content);
27
28 return spawnResult.outcome // Wait for the process to finish
29 .then(this.generatePatternToFailure) // Generate the failure patterns to check
30 .then(patternToFailure => {
31 const failureMessage = this.findAnyFailurePattern(patternToFailure);
32 if (failureMessage) {
33 return Q.reject<string[]>(new Error(failureMessage)); // If at least one failure happened, we fail
34 } else {
35 return this.generatePatternsForSuccess(); // If not we generate the success patterns
36 }
37 }).then(successPatterns => {
38 if (!this.areAllSuccessPatternsPresent(successPatterns)) { // If we don't find all the success patterns, we also fail
39 return Q.reject<void>(new Error("Unknown error"));
40 } // else we found all the success patterns, so we succeed
41 });
42 }
43
44 private store(stream: NodeJS.ReadableStream, append: (new_content: string) => void) {
45 stream.on("data", (data: Buffer) => {
46 append(data.toString());
47 });
48 }
49
50 // We check the failure patterns one by one, to see if any of those appeared on the errors. If they did, we return the associated error
51 private findAnyFailurePattern(patternToFailure: PatternToFailure): string {
52 const errorsAndOutput = this.errors + this.output;
53 const patternThatAppeared = Object.keys(patternToFailure).find(pattern =>
54 errorsAndOutput.indexOf(pattern) !== -1);
55 return patternThatAppeared ? patternToFailure[patternThatAppeared] : null;
56 }
57
58 // We check that all the patterns appeared on the output
59 private areAllSuccessPatternsPresent(successPatterns: string[]): boolean {
60 return successPatterns.every(pattern =>
61 this.output.indexOf(pattern) !== -1);
62 }
63}
64