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
89 lines (88 loc) • 4.59 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.platform = platform;
exports.getBinaryFilePath = getBinaryFilePath;
exports.downloadServerBinaries = downloadServerBinaries;
const fs_1 = __importDefault(require("fs"));
const got_1 = __importDefault(require("got"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const stream_1 = __importDefault(require("stream"));
const path_2 = require("../utils/path");
const logger_1 = require("./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);
}
function platform() {
const pf = os_1.default.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_1.default.arch}" is not available.
If this problem still occurs, please contact us at info@askui.com for more information`);
}
function getBinaryFilePath(version) {
return path_1.default.join((0, path_2.getPathToNodeModulesRoot)(), 'release', version, ...binarySubPathsByPlatform[platform()]);
}
function getBinaryDownloadUrl(binaryVersion) {
const baseUrl = `https://files.askui.com/releases/askui-ui-controller/${binaryVersion}`;
const arch = os_1.default.arch();
return `${baseUrl}/${platform()}/${arch}/${binarySubPathsByPlatform[platform()][1]}`;
}
function downloadServerBinaries(binaryVersion, proxyAgent) {
return __awaiter(this, void 0, void 0, function* () {
logger_1.logger.info(`Start downloading UI Controller version "${binaryVersion}"`);
const url = getBinaryDownloadUrl(binaryVersion);
const binaryFilePath = getBinaryFilePath(binaryVersion);
const binaryFolderPath = path_1.default.dirname(binaryFilePath);
const tempFilePath = path_1.default.join(binaryFolderPath, 'askui-ui-controller.temp');
const pipeline = (0, util_1.promisify)(stream_1.default.pipeline);
const downloadStream = got_1.default.stream(url, proxyAgent ? { agent: proxyAgent } : {});
const fileWriterStream = fs_1.default.createWriteStream(tempFilePath);
if (!(fs_1.default.existsSync(binaryFolderPath))) {
fs_1.default.mkdirSync(binaryFolderPath, { recursive: true });
}
try {
yield pipeline(downloadStream, fileWriterStream);
fs_1.default.rename(tempFilePath, binaryFilePath, () => {
logger_1.logger.info(`UI Controller version ${binaryVersion} for your system "${platform()} ${os_1.default.arch}" was downloaded`);
logger_1.logger.debug(`Binary of UI Controller is located at "${binaryFilePath}".`);
});
}
catch (error) {
logger_1.logger.error(error);
fs_1.default.unlink(tempFilePath, (err) => { logger_1.logger.error(err); });
throw buildBinaryNotAvailableError(binaryVersion);
}
return Promise.resolve();
});
}