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,MAAM,IAAI,IAAI,KAAK,EAAE;YACtB,IAAI,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,KAAK,WAAW,EAAE;gBACzC,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;aAClC;SACJ;QAED,IAAI,WAAW,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE;YACjC,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;SACxC;IACL,CAAC;IAED,uBAAuB,CAAC,IAAY;QAChC,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;QAC9C,IAAI,WAAW,EAAE;YACb,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;SACvC;IACL,CAAC;CACJ,CAAA;AA3IY,OAAO;IADnB,qBAAS,CAAC,EAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,gBAAgB,EAAC,CAAC;GAC9D,OAAO,CA2InB;AA3IY,0BAAO","sourcesContent":["import * as _ from 'lodash';\nimport * as Util from 'util';\nimport * as ts from 'typescript';\n\nimport { Event } from '../events';\nimport { Component, AbstractComponent, ChildableComponent } from '../component';\nimport { Application } from '../../application';\nimport { OptionDeclaration, DeclarationOption, ParameterScope } from './declaration';\n\n/**\n * Child of the master Options component. Options === configuration\n *\n * Serves one of two roles:\n *\n * - Aggregates and contributes configuration declarations declared by components or plugins\n * - Parses option values from various sources (config file, command-line args, etc)\n */\nexport class OptionsComponent extends AbstractComponent<Options> { }\n\nexport enum OptionsReadMode {\n Prefetch,\n Fetch\n}\n\nexport interface OptionsReadResult {\n hasErrors: boolean;\n inputFiles: string[];\n}\n\nexport class DiscoverEvent extends Event {\n readonly mode: OptionsReadMode;\n data: any;\n\n constructor(name: string, mode: OptionsReadMode) {\n super(name);\n this.mode = mode;\n }\n\n inputFiles: string[] = [];\n\n errors: string[] = [];\n\n static DISCOVER = 'optionsDiscover';\n\n /**\n * Add an input/source file.\n *\n * The input files will be used as source files for the compiler. All command line\n * arguments without parameter will be interpreted as being input files.\n *\n * @param fileName The path and filename of the input file.\n */\n addInputFile(fileName: string) {\n this.inputFiles.push(fileName);\n }\n\n addError(message: string, ...args: string[]) {\n this.errors.push(Util.format.apply(this, arguments));\n }\n}\n\n/**\n * Responsible for loading options via child components that read options\n * from various sources.\n */\n@Component({name: 'options', internal: true, childClass: OptionsComponent})\nexport class Options extends ChildableComponent<Application, OptionsComponent> {\n private declarations!: {[name: string]: OptionDeclaration};\n\n private values!: {[name: string]: any};\n\n private compilerOptions!: ts.CompilerOptions;\n\n initialize() {\n this.declarations = {};\n this.values = {};\n this.compilerOptions = {\n target: ts.ScriptTarget.ES3,\n module: ts.ModuleKind.None\n };\n }\n\n read(data: any = {}, mode: OptionsReadMode = OptionsReadMode.Fetch): OptionsReadResult {\n const event = new DiscoverEvent(DiscoverEvent.DISCOVER, mode);\n event.data = data;\n\n this.trigger(event);\n this.setValues(event.data, '', event.addError.bind(event));\n\n if (mode === OptionsReadMode.Fetch) {\n const logger = this.application.logger;\n for (let error of event.errors) {\n logger.error(error);\n }\n }\n\n return {\n hasErrors: event.errors.length > 0,\n inputFiles: event.inputFiles\n };\n }\n\n getValue(name: string): any {\n const declaration = this.getDeclaration(name);\n if (!declaration) {\n throw new Error(Util.format('Unknown option `%s`.', name));\n }\n\n if (declaration.scope === ParameterScope.TypeScript) {\n throw new Error('TypeScript options cannot be fetched using `getValue`, use `getCompilerOptions` instead.');\n }\n\n if (name in this.values) {\n return this.values[name];\n } else {\n return declaration.defaultValue;\n }\n }\n\n getRawValues(): any {\n return _.clone(this.values);\n }\n\n getDeclaration(name: string): OptionDeclaration | undefined {\n return this.declarations[name.toLowerCase()];\n }\n\n getDeclarationsByScope(scope: ParameterScope): OptionDeclaration[] {\n const result = _.values(this.declarations)\n .filter(declaration => declaration.scope === scope);\n return _.uniq(result);\n }\n\n getCompilerOptions(): ts.CompilerOptions {\n return this.compilerOptions;\n }\n\n setValue(name: string | OptionDeclaration, value: any, errorCallback?: (format: string, ...args: string[]) => void) {\n const declaration = name instanceof OptionDeclaration ? name : this.getDeclaration(name);\n if (!declaration) {\n return;\n }\n\n const key = declaration.name;\n if (declaration.scope === ParameterScope.TypeScript) {\n this.compilerOptions[key] = declaration.convert(value, errorCallback);\n } else {\n this.values[key] = declaration.convert(value, errorCallback);\n }\n }\n\n setValues(obj: Object, prefix: string = '', errorCallback?: (format: string, ...args: string[]) => void) {\n for (let key in obj) {\n const value = obj[key];\n const declaration = this.getDeclaration(key);\n const shouldValueBeAnObject = declaration && declaration['map'] === 'object';\n if (!Array.isArray(value) && typeof value === 'object' && !shouldValueBeAnObject) {\n this.setValues(value, prefix + key + '.', errorCallback);\n } else {\n this.setValue(prefix + key, value, errorCallback);\n }\n }\n }\n\n addDeclaration(declaration: OptionDeclaration|DeclarationOption) {\n const decl = declaration instanceof OptionDeclaration\n ? declaration\n : new OptionDeclaration(declaration);\n\n for (let name of decl.getNames()) {\n if (name in this.declarations) {\n this.application.logger.error('The option \"%s\" has already been registered by the \"%s\" component.',\n name,\n this.declarations[name].component || '__unknown');\n } else {\n this.declarations[name] = decl;\n }\n }\n }\n\n addDeclarations(declarations: (OptionDeclaration|DeclarationOption)[]) {\n for (let declaration of declarations) {\n this.addDeclaration(declaration);\n }\n }\n\n removeDeclaration(declaration: OptionDeclaration) {\n const names = _.keys(this.declarations);\n for (const name of names) {\n if (this.declarations[name] === declaration) {\n delete this.declarations[name];\n }\n }\n\n if (declaration.name in this.values) {\n delete this.values[declaration.name];\n }\n }\n\n removeDeclarationByName(name: string) {\n const declaration = this.getDeclaration(name);\n if (declaration) {\n this.removeDeclaration(declaration);\n }\n }\n}\n"]}
\No newline at end of file