flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
214 lines • 7.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const flagpoleconfig_1 = require("./flagpoleconfig");
const fs_extra_1 = require("fs-extra");
const fs_1 = require("fs");
const util_1 = require("./util");
var FlagpoleOutput;
(function (FlagpoleOutput) {
FlagpoleOutput["console"] = "console";
FlagpoleOutput["text"] = "text";
FlagpoleOutput["json"] = "json";
FlagpoleOutput["html"] = "html";
FlagpoleOutput["csv"] = "csv";
FlagpoleOutput["tsv"] = "tsv";
FlagpoleOutput["psv"] = "psv";
FlagpoleOutput["browser"] = "browser";
})(FlagpoleOutput = exports.FlagpoleOutput || (exports.FlagpoleOutput = {}));
function loadOptsFromConfigFile(configFilePath) {
const defaultConfig = flagpoleconfig_1.getDefaultConfig(configFilePath);
if (!fs_1.existsSync(configFilePath)) {
return defaultConfig;
}
const configContent = fs_extra_1.readFileSync(configFilePath, "utf8");
const configData = util_1.jsonParse(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) {
var _a, _b, _c, _d;
const configOptions = loadOptsFromConfigFile(opts.configFilePath || "./flagpole.json");
const defaultEnv = ((_a = configOptions) === null || _a === void 0 ? void 0 : _a.environments) ? Object.values((_b = configOptions) === null || _b === void 0 ? void 0 : _b.environments)[0]
: undefined;
opts.environmentName = opts.environmentName || ((_c = defaultEnv) === null || _c === void 0 ? void 0 : _c.name);
opts.baseDomain = opts.baseDomain || ((_d = defaultEnv) === null || _d === void 0 ? void 0 : _d.defaultDomain);
return new FlagpoleExecution(opts, new flagpoleconfig_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;
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.volume > 0;
}
get shouldWriteHtml() {
return this.isHtmlOutput || this.isBrowserOutput;
}
get isConsoleOutput() {
return this.outputFormat === FlagpoleOutput.console;
}
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;
}
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=flagpoleexecution.js.map