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
81 lines (80 loc) • 3.95 kB
JavaScript
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 fs from 'fs';
import got from 'got';
import os from 'os';
import path from 'path';
import { promisify } from 'util';
import stream from 'stream';
import { getPathToNodeModulesRoot } from '../utils/path';
import { logger } from './logger';
var SupportedPlatform;
(function (SupportedPlatform) {
SupportedPlatform["DARWIN"] = "darwin";
SupportedPlatform["LINUX"] = "linux";
SupportedPlatform["WIN32"] = "win32";
})(SupportedPlatform || (SupportedPlatform = {}));
const binarySubPathsByPlatform = {
darwin: ['darwin', 'askui-ui-controller.dmg'],
linux: ['linux', 'askui-ui-controller.AppImage'],
win32: ['windows', 'askui-ui-controller.exe'],
};
function isSupportedPlatform(value) {
return Object.values(SupportedPlatform).includes(value);
}
export function platform() {
const pf = os.platform();
if (isSupportedPlatform(pf)) {
return pf;
}
throw new Error(`Platform "${pf}" is not supported.`);
}
function buildBinaryNotAvailableError(binaryVersion) {
return new Error(`There was an error during downloading and creating the askui UI Controller. You can try a fresh install.
If you specified a binary version it is possible that the askui UI Controller version "${binaryVersion}"
for your system "${platform()} ${os.arch}" is not available.
If this problem still occurs, please contact us at info@askui.com for more information`);
}
export function getBinaryFilePath(version) {
return path.join(getPathToNodeModulesRoot(), 'release', version, ...binarySubPathsByPlatform[platform()]);
}
function getBinaryDownloadUrl(binaryVersion) {
const baseUrl = `https://files.askui.com/releases/askui-ui-controller/${binaryVersion}`;
const arch = os.arch();
return `${baseUrl}/${platform()}/${arch}/${binarySubPathsByPlatform[platform()][1]}`;
}
export function downloadServerBinaries(binaryVersion, proxyAgent) {
return __awaiter(this, void 0, void 0, function* () {
logger.info(`Start downloading UI Controller version "${binaryVersion}"`);
const url = getBinaryDownloadUrl(binaryVersion);
const binaryFilePath = getBinaryFilePath(binaryVersion);
const binaryFolderPath = path.dirname(binaryFilePath);
const tempFilePath = path.join(binaryFolderPath, 'askui-ui-controller.temp');
const pipeline = promisify(stream.pipeline);
const downloadStream = got.stream(url, proxyAgent ? { agent: proxyAgent } : {});
const fileWriterStream = fs.createWriteStream(tempFilePath);
if (!(fs.existsSync(binaryFolderPath))) {
fs.mkdirSync(binaryFolderPath, { recursive: true });
}
try {
yield pipeline(downloadStream, fileWriterStream);
fs.rename(tempFilePath, binaryFilePath, () => {
logger.info(`UI Controller version ${binaryVersion} for your system "${platform()} ${os.arch}" was downloaded`);
logger.debug(`Binary of UI Controller is located at "${binaryFilePath}".`);
});
}
catch (error) {
logger.error(error);
fs.unlink(tempFilePath, (err) => { logger.error(err); });
throw buildBinaryNotAvailableError(binaryVersion);
}
return Promise.resolve();
});
}