|
| 1 | +///<reference path=".d.ts"/> |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +export class PluginVariablesHelper implements IPluginVariablesHelper { |
| 5 | + constructor(private $options: ICommonOptions) { } |
| 6 | + |
| 7 | + /** |
| 8 | + * Checks if the specified pluginVariable exists in the --var option specified by user. |
| 9 | + * The variable can be added to --var option for configuration or globally, for ex.: |
| 10 | + * `--var.APP_ID myAppIdentifier` or `--var.debug.APP_ID myAppIdentifier`. |
| 11 | + * NOTE: If the variable is added for specific configuration and globally, |
| 12 | + * the value for the specified configuration will be used as it has higher priority. For ex.: |
| 13 | + * `--var.APP_ID myAppIdentifier1 --var.debug.APP_ID myAppIdentifier2` will return myAppIdentifier2 for debug configuration |
| 14 | + * and myAppIdentifier for release configuration. |
| 15 | + * @param {string} variableName The name of the plugin variable. |
| 16 | + * @param {string} configuration The configuration for which the variable will be used. |
| 17 | + * @returns {any} The value of the plugin variable specified in --var or undefined. |
| 18 | + */ |
| 19 | + public getPluginVariableFromVarOption(variableName: string, configuration?: string): any { |
| 20 | + let varOption = this.$options.var; |
| 21 | + configuration = configuration ? configuration.toLowerCase() : undefined; |
| 22 | + let lowerCasedVariableName = variableName.toLowerCase(); |
| 23 | + if(varOption) { |
| 24 | + let configVariableValue: string; |
| 25 | + let generalVariableValue: string; |
| 26 | + if(variableName.indexOf(".") !== -1) { |
| 27 | + varOption = this.simplifyYargsObject(varOption, configuration); |
| 28 | + } |
| 29 | + _.each(varOption, (propValue: any, propKey: string) => { |
| 30 | + if(propKey.toLowerCase() === configuration) { |
| 31 | + _.each(propValue, (configPropValue: string, configPropKey: string) => { |
| 32 | + if(configPropKey.toLowerCase() === lowerCasedVariableName) { |
| 33 | + configVariableValue = configPropValue; |
| 34 | + return false; |
| 35 | + } |
| 36 | + }); |
| 37 | + } else if(propKey.toLowerCase() === lowerCasedVariableName) { |
| 38 | + generalVariableValue = propValue; |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + let value = configVariableValue || generalVariableValue; |
| 43 | + if(value) { |
| 44 | + let obj = Object.create(null); |
| 45 | + obj[variableName] = value.toString(); |
| 46 | + return obj; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return undefined; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Converts complicated yargs object with many subobjects, to simplified one. |
| 55 | + * Use it when the plugin variable contains dots ("."). In this case yargs treats them as inner object instead of propery name. |
| 56 | + * For ex. '--var.debug.DATA.APP.ID testId' will be converted to {debug: {DATA: {APP: {ID: testId}}}}, while we need {debug: {DATA.APP.ID: testId}} |
| 57 | + * '--var.DATA.APP.ID testId' will be converted to DATA: {APP: {ID: testId}}}, while we need {DATA.APP.ID: testId} |
| 58 | + * @param {any} obj varObject created by yargs |
| 59 | + * @param {string} configuration The configuration for which the plugin variable will be used. |
| 60 | + * @return {any} Converted object if the obj paramater is of type object, otherwise - the object itself. |
| 61 | + */ |
| 62 | + public simplifyYargsObject(obj: any, configuration?: string): any { |
| 63 | + if(obj && typeof(obj) === "object") { |
| 64 | + let convertedObject:any = Object.create({}); |
| 65 | + |
| 66 | + _.each(obj, (propValue: any, propKey: string) => { |
| 67 | + if(typeof(propValue) !== "object") { |
| 68 | + convertedObject[propKey] = propValue; |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + configuration = configuration ? configuration.toLowerCase() : undefined; |
| 73 | + let innerObj = this.simplifyYargsObject(propValue, configuration); |
| 74 | + |
| 75 | + if(propKey.toLowerCase() === configuration) { |
| 76 | + // for --var.debug.DATA.APP.ID testId |
| 77 | + convertedObject[propKey] = innerObj; |
| 78 | + } else { |
| 79 | + // for --var.DATA.APP.ID testId |
| 80 | + _.each(innerObj, (innerPropValue: any, innerPropKey: string) => { |
| 81 | + convertedObject[`${propKey}.${innerPropKey}`] = innerPropValue; |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | + return convertedObject; |
| 88 | + } |
| 89 | + |
| 90 | + return obj; |
| 91 | + } |
| 92 | +} |
| 93 | +$injector.register("pluginVariablesHelper", PluginVariablesHelper); |
0 commit comments