UNPKG

@auttam/easycli

Version:

A quick and easy way of creating cli for your npm package.

86 lines (85 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProgramConfiguration = void 0; const command_config_1 = require("./command-config"); const option_config_1 = require("./option-config"); const param_config_1 = require("./param-config"); const string_1 = require("../utility/string"); const reflection_1 = require("../utility/reflection"); const settings_1 = require("../settings"); const config_error_1 = require("../errors/config-error"); class ProgramConfiguration { constructor() { this.name = ''; this.help = ''; this.version = '1.0.0'; this.binaryName = ''; this.commands = new command_config_1.CommandCollection(); this.options = new option_config_1.OptionCollection(); this.params = new param_config_1.ParamCollection(); this.decoratorsEnabled = false; } static injectConfiguration(target, propertyName = 'config') { if (!target[propertyName]) { var progConfig = new ProgramConfiguration(); progConfig.readFromObject(target); target[propertyName] = progConfig; } return target[propertyName]; } readFromObject(source) { if (source.constructor && source.constructor.name) { if (source.constructor.name == 'Object') { throw new config_error_1.ConfigurationError('Source must have a named constructor', source); } this.name = string_1.separateWords(source.constructor.name); this.binaryName = string_1.hyphenate(source.constructor.name); } if (source.constructor.name != 'Program') { if (settings_1.SettingStore.enableCommands) { var nonCmdMethods = settings_1.SettingStore.nonCmdMethods || []; for (var prop of reflection_1.getOwnFunctions(source)) { if (prop && prop == settings_1.SettingStore.defaultCommandMethod) { return; } if (prop.endsWith('Command') && nonCmdMethods.indexOf(prop) == -1) { this.commands.addMethod(prop, source[prop]); } } } else { if (settings_1.SettingStore.mainMethod && source[settings_1.SettingStore.mainMethod]) { this.params.initByMethod(source[settings_1.SettingStore.mainMethod]); } } } } merge(config) { if (!config || typeof config != 'object') return; this.name = config.name || this.name; this.help = config.help || this.help; this.version = config.version || this.version; this.binaryName = config.binaryName || this.binaryName; this.options.addList(config.options); this.params.mergeByConfigs(config.params || []); this.commands.addList(config.commands); } toConfig() { return { name: this.name, binaryName: this.binaryName, help: this.help, version: this.version }; } verify() { if (settings_1.SettingStore.enableCommands) { this.commands.verify(); } else { this.params.verify(); } } } exports.ProgramConfiguration = ProgramConfiguration;