import * as path from 'path' ; import * as fs from 'fs-extra' ; interface ConfigFile { userId : string ; apiToken : string ; hideBootCampProjects : boolean ; projectIndex : { [ name ] : number ; } ; } class Config { private cache : ConfigFile = null ; private defaults : Partial < ConfigFile > = { projectIndex : { } , } ; public constructor ( private configFilePath ) { } public get < T extends keyof ConfigFile > ( key ) { this . loadCacheIfNecessary ( ) ; if ( key in this . cache ) { return this . cache [ key ] ; } if ( key in this . defaults ) { return this . defaults [ key ] as any ; } throw new Error ( `There is no default value for config item with key '${ key }'` ) ; } public set < T extends keyof ConfigFile > ( key , value ) { this . loadCacheIfNecessary ( ) ; this . cache [ key ] = value ; const fileContent = JSON . stringify ( this . cache , null , 2 ) + '\n' ; fs . writeFileSync ( this . configFilePath , fileContent ) ; } public fileExists ( ) { return fs . existsSync ( this . configFilePath ) ; } public createFile ( ) { fs . writeFileSync ( this . configFilePath , '{}\n' ) ; } private loadCacheIfNecessary ( ) { if ( this . cache === null ) { this . cache = JSON . parse ( fs . readFileSync ( this . configFilePath ) . toString ( ) ) ; } } } export const config = new Config ( path . resolve ( process . cwd ( ) , 'quantconnect-cli.json' ) ) ;