UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

92 lines (91 loc) 4.16 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const apis_1 = require("../../../util/apis"); const interaction_1 = require("../../../util/interaction"); const os = require("os"); const process = require("process"); const exit_codes_1 = require("./exit-codes"); class StateChecker { constructor(client, testRunId, ownerName, appName, streamingOutput) { this.client = client; this.testRunId = testRunId; this.ownerName = ownerName; this.appName = appName; if (!streamingOutput) { this.streamingOutput = new interaction_1.StreamingArrayOutput(); this.isInternalStreamingOutput = true; } else { this.streamingOutput = streamingOutput; this.isInternalStreamingOutput = false; } } checkUntilCompleted(timeoutSec = null) { return __awaiter(this, void 0, void 0, function* () { let exitCode = 0; const startTime = process.hrtime(); if (this.isInternalStreamingOutput) { this.streamingOutput.start(); } while (true) { const state = yield interaction_1.out.progress("Checking status...", this.getTestRunState(this.client, this.testRunId)); if (state && state.message) { this.streamingOutput.text((state) => `Current test status: ${state.message.join(os.EOL)}`, state); } if (typeof state.exitCode === "number") { exitCode = state.exitCode; break; } if (timeoutSec) { const elapsedSeconds = process.hrtime(startTime)[0]; if (elapsedSeconds + state.waitTime > timeoutSec) { exitCode = exit_codes_1.ExitCodes.Timeout; this.streamingOutput.text((timeoutSec) => `After ${timeoutSec} seconds, command timed out waiting for tests to finish.`, timeoutSec); break; } } yield interaction_1.out.progress(`Waiting ${state.waitTime} seconds...`, this.delay(1000 * state.waitTime)); } if (this.isInternalStreamingOutput) { this.streamingOutput.finish(); } return exitCode; }); } checkOnce() { return __awaiter(this, void 0, void 0, function* () { const state = yield interaction_1.out.progress("Checking status...", this.getTestRunState(this.client, this.testRunId)); if (this.isInternalStreamingOutput) { this.streamingOutput.start(); } this.streamingOutput.text((state) => `Current test status: ${state.message.join(os.EOL)}`, state); if (this.isInternalStreamingOutput) { this.streamingOutput.finish(); } return state.exitCode; }); } getTestRunState(client, testRunId) { return apis_1.clientCall((cb) => { client.test.getTestRunState(testRunId, this.ownerName, this.appName, cb); }); } delay(milliseconds) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { // Turn off tslint false error /* tslint:disable-next-line:no-string-based-set-timeout */ setTimeout(resolve, milliseconds); }); }); } } exports.StateChecker = StateChecker;