UNPKG

askui

Version:

Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on

127 lines (126 loc) 6.05 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { spawn } from 'child_process'; import fs from 'fs-extra'; import waitPort from 'wait-port'; import fkill from 'fkill'; import os from 'os'; import path from 'path'; import { createArgsWithDefaults, createCliFlagsFromArgs, } from './ui-controller-args'; import { downloadServerBinaries, getBinaryFilePath } from './download-binaries'; import { logger } from './logger'; import { TimeoutError } from './timeout-error'; import { UnkownError } from './unkown-error'; import { envProxyAgents } from '../utils/proxy/proxy-builder'; export class UiControllerFacade { constructor() { this.binaryFilePath = getBinaryFilePath('latest'); this.DefaultmaxWaitingForStartingInMs = 30 * 1000; } start(args, maxWaitingForStartingInSeconds) { return __awaiter(this, void 0, void 0, function* () { yield this.runPreStartChecks(); const argsWithDefaults = createArgsWithDefaults(args); const argsWithLogPath = this.serverLogFilePath(argsWithDefaults); this.binaryFilePath = getBinaryFilePath(argsWithLogPath.binaryVersion); yield this.getBinary(argsWithLogPath.binaryVersion, argsWithLogPath.overWriteBinary, argsWithLogPath.proxyAgents || (yield envProxyAgents())); this.makeBinaryExecutable(); logger.debug(`UI Controller log path "${this.serverLogFile}"`); yield this.startWithDefaults(argsWithLogPath, maxWaitingForStartingInSeconds); }); } stop(args, forceStop) { return __awaiter(this, void 0, void 0, function* () { try { const argsWithDefaults = createArgsWithDefaults(args); yield this.killPort(argsWithDefaults.port, forceStop); } catch (err) { throw new Error(`An unknown error occured while closing of the UI Controller. Log file: "${this.serverLogFile}". ErrorReason: ${err}`); } }); } serverLogFilePath(args) { if (args === null || args === void 0 ? void 0 : args.logFilePath) { this.serverLogFile = args.logFilePath; return args; } const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'askui')); this.serverLogFile = path.join(tmpDir, 'askui-ui-controller.log'); const argPath = { logFilePath: this.serverLogFile }; return Object.assign(argPath, args); } // eslint-disable-next-line class-methods-use-this killPort(port, forceStop) { return fkill(`:${port}`, { force: forceStop || false, silent: true, }); } getStartingCommand() { return `"${this.binaryFilePath}"`; } // eslint-disable-next-line class-methods-use-this makeBinaryExecutable() { /* Executable out of the box */ } // eslint-disable-next-line class-methods-use-this runPreStartChecks() { return Promise.resolve(); } // eslint-disable-next-line class-methods-use-this waitUntilStarted(args, maxWaitingForStartingInSeconds) { return new Promise((resolve, reject) => { try { const timeoutInMs = maxWaitingForStartingInSeconds ? maxWaitingForStartingInSeconds * 1000 : this.DefaultmaxWaitingForStartingInMs; waitPort({ host: args.host, output: (process === null || process === void 0 ? void 0 : process.env['LOG_LEVEL']) === 'verbose' ? 'dots' : 'silent', port: args.port, timeout: timeoutInMs, }).then((returnObject) => { if (returnObject.open) { logger.info('The Control UI Server has been started.'); return resolve(); } return reject(new TimeoutError('Starting time limit has been reached')); }); } catch (err) { reject(new UnkownError(`An unknown error occured while waiting for the UI Controller: ${err}`)); } }); } getBinary(binaryVersion_1) { return __awaiter(this, arguments, void 0, function* (binaryVersion, overWriteBinary = false, proxyAgent) { if (!fs.existsSync(this.binaryFilePath) || overWriteBinary) { logger.debug(`Currently, no binary of the UI Controller is available at "${this.binaryFilePath}"`); yield downloadServerBinaries(binaryVersion, proxyAgent); } else { logger.debug(`Binary of UI Controller is already present at "${this.binaryFilePath}".`); } }); } startWithDefaults(args, maxWaitingForStartingInSeconds) { return __awaiter(this, void 0, void 0, function* () { try { logger.debug('Starting the UI Controller...'); spawn(this.getStartingCommand(), createCliFlagsFromArgs(args), { shell: true }); yield this.waitUntilStarted(args, maxWaitingForStartingInSeconds); } catch (err) { throw new Error(`The UI Controller could not be started. Log file : ${this.serverLogFile}. ErrorReason: ${err} Check this website for more information: https://docs.askui.com/docs/general/Troubleshooting`); } }); } }