microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.17

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/outputVerifier.ts

82lines · modeblame

7cc67271digeff10 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
4import * as Q from "q";
5import {ISpawnResult} from "./node/childProcess";
6
ef902673Vladimir Kotikov9 years ago7export type PatternToFailure = {
8pattern: string | RegExp,
9message: string
10};
7cc67271digeff10 years ago11
e4c8f7d4digeff10 years ago12/* This class transforms a spawn process to only succeed if all defined success patterns
7cc67271digeff10 years ago13are found on stdout, and none of the failure patterns were found on stderr */
f8b7022ddigeff10 years ago14export class OutputVerifier {
7cc67271digeff10 years ago15private generatePatternsForSuccess: () => Q.Promise<string[]>;
ef902673Vladimir Kotikov9 years ago16private generatePatternToFailure: () => Q.Promise<PatternToFailure[]>;
77a9922aRuslan Bikkinin8 years ago17private platformName: string;
7cc67271digeff10 years ago18
19private output = "";
20private errors = "";
21
77a9922aRuslan Bikkinin8 years ago22constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure[]>, platformName: string) {
7cc67271digeff10 years ago23this.generatePatternsForSuccess = generatePatternsForSuccess;
24this.generatePatternToFailure = generatePatternToFailure;
77a9922aRuslan Bikkinin8 years ago25this.platformName = platformName;
7cc67271digeff10 years ago26}
27
28public process(spawnResult: ISpawnResult): Q.Promise<void> {
29// Store all output
aab2095edigeff10 years ago30this.store(spawnResult.stdout, new_content =>
31this.output += new_content);
32this.store(spawnResult.stderr, new_content =>
33this.errors += new_content);
7cc67271digeff10 years ago34
35return spawnResult.outcome // Wait for the process to finish
36.then(this.generatePatternToFailure) // Generate the failure patterns to check
ef902673Vladimir Kotikov9 years ago37.then(patterns => {
38const failureMessage = this.findAnyFailurePattern(patterns);
7cc67271digeff10 years ago39if (failureMessage) {
40return Q.reject<string[]>(new Error(failureMessage)); // If at least one failure happened, we fail
41} else {
42return this.generatePatternsForSuccess(); // If not we generate the success patterns
43}
44}).then(successPatterns => {
45if (!this.areAllSuccessPatternsPresent(successPatterns)) { // If we don't find all the success patterns, we also fail
77a9922aRuslan Bikkinin8 years ago46const message =
47`Unknown error: not all success patterns were matched.
48It means that "react-native run-${this.platformName}" command failed. \
14ebf4e6Ruslan Bikkinin8 years ago49Please, check the View -> Toggle Output -> React Native, \
50View -> Toggle Output -> React Native: Run ${this.platformName} output windows.`;
77a9922aRuslan Bikkinin8 years ago51return Q.reject<void>(new Error(message));
7cc67271digeff10 years ago52} // else we found all the success patterns, so we succeed
5c8365a6Artem Egorov8 years ago53return Q.resolve(void 0);
7cc67271digeff10 years ago54});
55}
56
57private store(stream: NodeJS.ReadableStream, append: (new_content: string) => void) {
58stream.on("data", (data: Buffer) => {
59append(data.toString());
60});
61}
62
63// 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
5c8365a6Artem Egorov8 years ago64private findAnyFailurePattern(patterns: PatternToFailure[]): string | null {
9ce5a776digeff10 years ago65const errorsAndOutput = this.errors + this.output;
ef902673Vladimir Kotikov9 years ago66const patternThatAppeared = patterns.find(pattern => {
67return pattern.pattern instanceof RegExp ?
68(pattern.pattern as RegExp).test(errorsAndOutput) :
69errorsAndOutput.indexOf(pattern.pattern as string) !== -1;
70});
71
72return patternThatAppeared ? patternThatAppeared.message : null;
7cc67271digeff10 years ago73}
74
75// We check that all the patterns appeared on the output
76private areAllSuccessPatternsPresent(successPatterns: string[]): boolean {
e03193abArtem Egorov8 years ago77return successPatterns.every(pattern => {
78let patternRe = new RegExp(pattern, "i");
79return patternRe.test(this.output);
80});
7cc67271digeff10 years ago81}
82}