import Immutable from "immutable" ; import File from "./file" ; import PluginDependency from "./pluginDependency" ; import configDefault from "../constants/configDefault" ; import reducedObject from "../utils/reducedObject" ; class Config extends Immutable . Record ( { file : new File ( ) , values : configDefault , } , "Config" ) { getFile ( ) { return this . get ( "file" ) ; } getValues ( ) { return this . get ( "values" ) ; } return { Map } toReducedVersion ( ) { return reducedObject ( configDefault , this . getValues ( ) ) ; } return { Promise < String > } toText ( ) { return JSON . stringify ( this . toReducedVersion ( ) . toJS ( ) , null , 4 ) ; } param { File } file return { Config } setFile ( file ) { return this . set ( "file" , file ) ; } /**
     * Return a configuration value by its key path
     */ getValue ( keyPath , def ? ) { const values = this . getValues ( ) ; keyPath = Config . keyToKeyPath ( keyPath ) ; if ( ! values . hasIn ( keyPath ) ) { return Immutable . fromJS ( def ) ; } return values . getIn ( keyPath ) ; } return { Config } setValue ( keyPath , value ) { keyPath = Config . keyToKeyPath ( keyPath ) ; value = Immutable . fromJS ( value ) ; let values = this . getValues ( ) ; values = values . setIn ( keyPath , value ) ; return this . set ( "values" , values ) as Config ; } return { List < PluginDependency > } getPluginDependencies ( ) { const plugins = this . getValue ( "plugins" ) ; if ( typeof plugins === "string" ) { return PluginDependency . listFromString ( plugins ) ; } else { return PluginDependency . listFromArray ( plugins ) ; } } param { string } name return { PluginDependency } getPluginDependency ( name ) { const plugins = this . getPluginDependencies ( ) ; return plugins . find ( ( dep ) => { return dep . getName ( ) === name ; } ) ; } param { List < PluginDependency > } return { Config } setPluginDependencies ( deps ) { const plugins = PluginDependency . listToArray ( deps ) ; return this . setValue ( "plugins" , plugins ) ; } param { Object } values returns { Config } updateValues ( values ) { values = Immutable . fromJS ( values ) ; return this . set ( "values" , values ) ; } param { Config } config param { Object } values returns { Config } mergeValues ( values ) { let currentValues = this . getValues ( ) ; values = Immutable . fromJS ( values ) ; currentValues = currentValues . mergeDeep ( values ) ; return this . set ( "values" , currentValues ) ; } param { File } file param { Object } values returns { Config } static create ( file , values ) { return new Config ( { file : file , values : Immutable . fromJS ( values ) , } ) ; } param { Object } values returns { Config } static createWithValues ( values ) { return new Config ( { values : Immutable . fromJS ( values ) , } ) ; } /**
     * Convert a keyPath to an array of keys
     */ static keyToKeyPath ( keyPath ) { if ( typeof keyPath === "string" ) { return keyPath . split ( "." ) ; } return keyPath ; } } export default Config ;