|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.root = exports.map = exports.section = exports.option = void 0; |
| 7 | +const lodash_1 = __importDefault(require("lodash")); |
| 8 | +const errors_1 = require("./errors"); |
| 9 | +const lazy_1 = require("./lazy"); |
| 10 | +const locator_1 = __importDefault(require("./locator")); |
| 11 | +/** |
| 12 | + * Single option |
| 13 | + */ |
| 14 | +function option({ defaultValue, parseCli = lodash_1.default.identity, parseEnv = lodash_1.default.identity, validate = lodash_1.default.noop, map: mapFunc = lodash_1.default.identity }) { |
| 15 | + const validateFunc = validate; |
| 16 | + return (locator, parsed) => { |
| 17 | + const config = parsed.root; |
| 18 | + const currNode = locator.parent ? lodash_1.default.get(parsed, locator.parent) : config; |
| 19 | + let value; |
| 20 | + if (locator.cliOption !== undefined) { |
| 21 | + value = parseCli(locator.cliOption); |
| 22 | + } |
| 23 | + else if (locator.envVar !== undefined) { |
| 24 | + value = parseEnv(locator.envVar); |
| 25 | + } |
| 26 | + else if (locator.option !== undefined) { |
| 27 | + value = locator.option; |
| 28 | + } |
| 29 | + else if (defaultValue !== undefined) { |
| 30 | + value = lodash_1.default.isFunction(defaultValue) |
| 31 | + ? defaultValue(config, currNode) |
| 32 | + : defaultValue; |
| 33 | + } |
| 34 | + else { |
| 35 | + throw new errors_1.MissingOptionError(locator.name); |
| 36 | + } |
| 37 | + validateFunc(value, config, currNode); |
| 38 | + return mapFunc(value, config, currNode); |
| 39 | + }; |
| 40 | +} |
| 41 | +exports.option = option; |
| 42 | +/** |
| 43 | + * Object with fixed properties. |
| 44 | + * Any unknown property will be reported as error. |
| 45 | + */ |
| 46 | +function section(properties) { |
| 47 | + const expectedKeys = lodash_1.default.keys(properties); |
| 48 | + return (locator, config) => { |
| 49 | + const unknownKeys = lodash_1.default.difference(lodash_1.default.keys(locator.option), expectedKeys); |
| 50 | + if (unknownKeys.length > 0) { |
| 51 | + throw new errors_1.UnknownKeysError(unknownKeys.map((key) => `${locator.name}.${key}`)); |
| 52 | + } |
| 53 | + const lazyResult = (0, lazy_1.buildLazyObject)(expectedKeys, (key) => { |
| 54 | + const parser = properties[key]; |
| 55 | + return () => parser(locator.nested(key), config); |
| 56 | + }); |
| 57 | + lodash_1.default.set(config, locator.name, lazyResult); |
| 58 | + return lazyResult; |
| 59 | + }; |
| 60 | +} |
| 61 | +exports.section = section; |
| 62 | +/** |
| 63 | + * Object with user-specified keys and values, |
| 64 | + * parsed by valueParser. |
| 65 | + */ |
| 66 | +function map(valueParser, defaultValue) { |
| 67 | + return (locator, config) => { |
| 68 | + if (locator.option === undefined) { |
| 69 | + if (!defaultValue) { |
| 70 | + return {}; |
| 71 | + } |
| 72 | + locator = locator.resetOption(defaultValue); |
| 73 | + } |
| 74 | + const optionsToParse = Object.keys(locator.option); |
| 75 | + const lazyResult = (0, lazy_1.buildLazyObject)(optionsToParse, (key) => { |
| 76 | + return () => valueParser(locator.nested(key), config); |
| 77 | + }); |
| 78 | + lodash_1.default.set(config, locator.name, lazyResult); |
| 79 | + return lazyResult; |
| 80 | + }; |
| 81 | +} |
| 82 | +exports.map = map; |
| 83 | +function root(rootParser, { envPrefix, cliPrefix }) { |
| 84 | + return ({ options, env, argv }) => { |
| 85 | + const rootLocator = (0, locator_1.default)({ options, env, argv, envPrefix, cliPrefix }); |
| 86 | + const parsed = rootParser(rootLocator, {}); |
| 87 | + return (0, lazy_1.forceParsing)(parsed); |
| 88 | + }; |
| 89 | +} |
| 90 | +exports.root = root; |
0 commit comments