UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

256 lines 8.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlagpoleExecution = exports.FlagpoleOutput = void 0; const flagpole_config_1 = require("./flagpole-config"); const fs_extra_1 = require("fs-extra"); const path = require("path"); const util_1 = require("./util"); var FlagpoleOutput; (function (FlagpoleOutput) { FlagpoleOutput["console"] = "console"; FlagpoleOutput["ci"] = "ci"; FlagpoleOutput["text"] = "text"; FlagpoleOutput["json"] = "json"; FlagpoleOutput["html"] = "html"; FlagpoleOutput["csv"] = "csv"; FlagpoleOutput["tsv"] = "tsv"; FlagpoleOutput["psv"] = "psv"; FlagpoleOutput["browser"] = "browser"; FlagpoleOutput["xml"] = "xml"; })(FlagpoleOutput = exports.FlagpoleOutput || (exports.FlagpoleOutput = {})); function loadOptsFromConfigFile(configFilePath) { const defaultConfig = flagpole_config_1.getDefaultConfig(configFilePath); if (!fs_extra_1.existsSync(configFilePath)) { return defaultConfig; } const configContent = fs_extra_1.readFileSync(configFilePath, "utf8"); const configData = util_1.toJson(configContent); return { project: Object.assign(Object.assign({}, defaultConfig.project), configData.project), environments: Object.assign(Object.assign({}, defaultConfig.environments), configData.environments), suites: configData.suites ? configData.suites : defaultConfig.suites, }; } class FlagpoleExecution { constructor(opts, config) { this._opts = opts; this._config = config; } static get global() { if (!FlagpoleExecution._globalSingleton) { FlagpoleExecution.global = FlagpoleExecution.createWithArgs(process.argv); } return FlagpoleExecution._globalSingleton; } static set global(value) { FlagpoleExecution._globalSingleton = value; } static createWithArgs(args) { const opts = { configFilePath: "./flagpole.json", }; let lastArg = null; args.forEach(function (arg) { if (lastArg == "-e") { opts.environmentName = arg; } else if (lastArg == "-o") { opts.outputFormat = arg; opts.automaticallyPrintToConsole = true; } else if (lastArg == "--base") { opts.baseDomain = arg; } else if (lastArg == "--config") { opts.configFilePath = arg; } else if (lastArg == "--volume") { opts.volume = parseInt(arg); } else if (arg == "-x") { opts.exitOnDone = true; lastArg = null; return; } else if (arg == "-z") { opts.isChildProcess = true; lastArg = null; return; } else if (arg == "--headless") { opts.headless = true; lastArg = null; return; } else if (arg == "--headed") { opts.headless = false; lastArg = null; return; } lastArg = arg; }); return FlagpoleExecution.create(opts); } static create(opts) { const configOptions = loadOptsFromConfigFile(opts.configFilePath || "./flagpole.json"); const defaultEnv = (configOptions === null || configOptions === void 0 ? void 0 : configOptions.environments) ? Object.values(configOptions === null || configOptions === void 0 ? void 0 : configOptions.environments)[0] : undefined; opts.environmentName = opts.environmentName || (defaultEnv === null || defaultEnv === void 0 ? void 0 : defaultEnv.name); return new FlagpoleExecution(opts, new flagpole_config_1.FlagpoleConfig(configOptions, opts.configFilePath)); } get config() { return this._config; } get automaticallyPrintToConsole() { return !!this._opts.automaticallyPrintToConsole; } get exitOnDone() { return !!this._opts.exitOnDone; } get isChildProcess() { return !!this._opts.isChildProcess; } get volume() { return this._opts.volume === undefined ? 50 : this._opts.volume; } set outputFormat(value) { this._opts.outputFormat = value; } get outputFormat() { return (this._opts.outputFormat = this._opts.outputFormat === undefined ? FlagpoleOutput.console : this._opts.outputFormat); } get environment() { return this._opts.environmentName ? this.config.environments[this._opts.environmentName] : this.config.defaultEnvironment; } get baseDomain() { var _a; if (this._opts.baseDomain !== "undefined" && this._opts.baseDomain) { return this._opts.baseDomain; } return (_a = this.environment) === null || _a === void 0 ? void 0 : _a.defaultDomain; } get headless() { return this._opts.headless; } set headless(value) { this._opts.headless = value; } get isQuietMode() { return this.volume === 0; } get shouldOutputToConsole() { return (this.isConsoleOutput || this.isCiOutput) && this.volume > 0; } get shouldWriteHtml() { return this.isHtmlOutput || this.isBrowserOutput; } get isConsoleOutput() { return this.outputFormat === FlagpoleOutput.console; } get isCiOutput() { return this.outputFormat === FlagpoleOutput.ci; } get isTextOutput() { return this.outputFormat === FlagpoleOutput.text; } get isCsvOutput() { return this.outputFormat === FlagpoleOutput.csv; } get isPsvOutput() { return this.outputFormat === FlagpoleOutput.psv; } get isDelimitedOutput() { return this.isTsvOutput || this.isPsvOutput || this.isCsvOutput; } get isTsvOutput() { return this.outputFormat === FlagpoleOutput.tsv; } get isJsonOutput() { return this.outputFormat === FlagpoleOutput.json; } get isBrowserOutput() { return this.outputFormat === FlagpoleOutput.browser; } get isHtmlOutput() { return this.outputFormat === FlagpoleOutput.html; } get isXmlOutput() { return this.outputFormat === FlagpoleOutput.xml; } getCachePath(cacheFileName) { const cacheFolder = FlagpoleExecution.global.config.getCacheFolder(); if (!cacheFolder) { throw "Flagpole cache folder path not found."; } fs_extra_1.ensureDirSync(cacheFolder); return cacheFileName ? path.join(cacheFolder, cacheFileName) : cacheFolder; } setCache(key, data) { fs_extra_1.writeFileSync(this.getCachePath(key), typeof data == "string" ? data : JSON.stringify(data, null, 2)); return this; } getCache(key) { const path = this.getCachePath(key); const data = fs_extra_1.existsSync(path) ? fs_extra_1.readFileSync(path, "utf8") : null; if (data !== null) { try { return JSON.parse(data); } catch (_a) { } } return data; } removeCache(key) { const path = this.getCachePath(key); fs_extra_1.existsSync(path) && fs_extra_1.unlinkSync(path); return this; } clearCache() { return fs_extra_1.emptyDirSync(this.getCachePath()); } getOptionsArray() { return this.getOptionsString().split(" "); } getOptionsString() { let opts = `--volume ${this.volume}`; if (this.baseDomain) { opts += ` --base ${this._opts.baseDomain}`; } if (this.config) { opts += ` --config ${this.config.getConfigPath()}`; } if (this.environment !== undefined) { opts += " -e " + this.environment.name; } if (this.exitOnDone === true) { opts += " -x"; } if (this.isChildProcess === true) { opts += " -z"; } if (this.outputFormat !== undefined) { opts += " -o " + this.outputFormat; } if (this.headless !== undefined) { opts += ` --${this.headless ? "headless" : "headed"}`; } return opts; } clone(opts) { const clonedExecution = new FlagpoleExecution(this._opts, this._config); if (opts) { Object.keys(opts).forEach((key) => { clonedExecution._opts[key] = opts[key]; }); } return clonedExecution; } } exports.FlagpoleExecution = FlagpoleExecution; //# sourceMappingURL=flagpole-execution.js.map