UNPKG

@nu-art/commando

Version:
96 lines (95 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CLIParamsResolver = void 0; const ts_common_1 = require("@nu-art/ts-common"); const consts_1 = require("./consts"); class CLIParamsResolver { static create(...params) { return new CLIParamsResolver(params); } constructor(params) { this.params = this.translate(params); } /** * Format current input params and return it structured by the app params type. * @param inputParams current console input arguments * @returns CliParamsObject */ resolveParamValue(inputParams = process.argv.slice(2, process.argv.length)) { const runtimeParams = inputParams.reduce((output, inputParam) => { var _a; const cliParamToResolve = this.findMatchingParamToResolve(inputParam); if (!cliParamToResolve) return output; const value = inputParam.split('=')[1]; const finalValue = cliParamToResolve.process(value, cliParamToResolve.defaultValue); // validate options if exits if (cliParamToResolve.options && !cliParamToResolve.options.includes(value)) throw new Error('value not supported for this param'); const key = cliParamToResolve.keyName; if ((0, ts_common_1.exists)(cliParamToResolve.dependencies)) (_a = cliParamToResolve.dependencies) === null || _a === void 0 ? void 0 : _a.forEach(dependency => { output[dependency.param.keyName] = dependency.value; }); if (cliParamToResolve.isArray) { let currentValues = output[key]; currentValues = (0, ts_common_1.filterDuplicates)([...(currentValues !== null && currentValues !== void 0 ? currentValues : []), ...(0, ts_common_1.asArray)(finalValue)]); output[key] = currentValues; return output; } //if already exists and the value ain't an array warn that the value will be overridden if (output[key]) ts_common_1.StaticLogger.logWarning(`this param does not accept multiple values, overriding prev value: ${output[key]}`); //Apply single value to the object output[key] = finalValue; return output; }, {}); this.params.filter(param => (0, ts_common_1.exists)(param.defaultValue) && !(0, ts_common_1.exists)(runtimeParams[param.keyName])).forEach(param => { runtimeParams[param.keyName] = param.defaultValue; }); return runtimeParams; } /** * Find the matching param by finding it's key in the current argument * Go over the app params and find the CLIParam object representing it * @param inputParam The current console argument being parsed * @returns The CliParam or undefined if not found * @private */ findMatchingParamToResolve(inputParam) { let maxKeyLength = 0; let matchingParam; // look for the longest fitting param in order to make sure we find the perfect match this.params.forEach((param) => { param.keys.forEach((key) => { if (inputParam.startsWith(key) && key.length > maxKeyLength) { maxKeyLength = key.length; matchingParam = param; } }); }); return matchingParam; } /** * Translate BaseCLIParams passed to the constructor to CLIParams * @param params User input of CLI params being passed to the constructor * @private * @returns CLI Params filled with all mandatory data */ translate(params) { return params.map(param => { //If name is not defined apply key name as the param name if (!param.name) param.name = param.keyName; //If no processor is passed apply default by type if (!param.process) { param.process = consts_1.DefaultProcessorsMapper[param.type.split('[')[0].trim()]; } //Determine if value is array by the param type TODO: improve this chunk of code, this is not strict enough if (!(0, ts_common_1.exists)(param.isArray) && param.type.includes('[]')) param.isArray = true; return param; }); } } exports.CLIParamsResolver = CLIParamsResolver;