UNPKG

@testplane/storybook

Version:

Testplane plugin that enables runtime screenshot storybook tests autogeneration

74 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseConfig = void 0; const lodash_1 = require("lodash"); const gemini_configparser_1 = require("gemini-configparser"); const assertType = (name, validationFn, type) => { return (v) => { if (!validationFn(v)) { throw new Error(`"${name}" option must be a ${type}, but got ${typeof v}`); } }; }; const assertRecordOfRecords = (value, name) => { if (!(0, lodash_1.isNull)(value) && !(0, lodash_1.isPlainObject)(value)) { throw new Error(`"${name}" must be an object`); } const record = value; for (const key of Object.keys(record)) { if (!(0, lodash_1.isPlainObject)(record[key])) { throw new Error(`"${name}.${key}" must be an object`); } } }; const booleanOption = (name, defaultValue = false) => (0, gemini_configparser_1.option)({ parseEnv: (val) => Boolean(JSON.parse(val)), parseCli: (val) => Boolean(JSON.parse(val)), defaultValue, validate: assertType(name, lodash_1.isBoolean, "boolean"), }); const numberOption = (name, defaultValue) => (0, gemini_configparser_1.option)({ defaultValue, validate: assertType(name, lodash_1.isNumber, "number"), }); const stringOption = (name, defaultValue = "") => (0, gemini_configparser_1.option)({ defaultValue, validate: assertType(name, lodash_1.isString, "string"), }); const stringAndRegExpArrayOption = (name, defaultValue) => (0, gemini_configparser_1.option)({ defaultValue, validate: value => { const errorMessage = `"${name}" option must be an array of strings and regular expressions, but got`; if (!(0, lodash_1.isArray)(value)) { throw new Error(`${errorMessage} ${typeof value}`); } const nonStringIndex = value.findIndex(v => !(0, lodash_1.isString)(v) && !(0, lodash_1.isRegExp)(v)); if (nonStringIndex !== -1) { throw new Error(`${errorMessage} ${typeof value[nonStringIndex]} at index ${nonStringIndex}`); } }, }); const optionalRecordOfRecordsOption = (name, defaultValue = {}) => (0, gemini_configparser_1.option)({ parseEnv: JSON.parse, parseCli: JSON.parse, defaultValue, validate: value => assertRecordOfRecords(value, name), }); function parseConfig(options) { const { env, argv } = process; const parseOptions = (0, gemini_configparser_1.root)((0, gemini_configparser_1.section)({ enabled: booleanOption("enabled", true), storybookConfigDir: stringOption("storybookConfigDir", ".storybook"), autoScreenshots: booleanOption("autoScreenshots", true), autoscreenshotSelector: stringOption("autoscreenshotSelector", ""), autoScreenshotStorybookGlobals: optionalRecordOfRecordsOption("autoScreenshotStorybookGlobals", {}), localport: numberOption("localport", 6006), remoteStorybookUrl: stringOption("remoteStorybookUrl", ""), waitStorybookJsonTimeout: numberOption("waitStorybookJsonTimeout", 30000), browserIds: stringAndRegExpArrayOption("browserIds", []), unsafeAllowOtherTests: booleanOption("unsafeAllowOtherTests", false), }), { envPrefix: "testplane_storybook_", cliPrefix: "--storybook-" }); return parseOptions({ options, env, argv }); } exports.parseConfig = parseConfig; //# sourceMappingURL=config.js.map