import * as fs from 'fs' ; import * as nls from 'vscode-nls' ; nls . config ( { messageFormat : nls . MessageFormat . bundle , bundleFormat : nls . BundleFormat . standalone } ) ( ) ; const localize = nls . loadMessageBundle ( ) ; export interface Environment { name : string ; value : string ; } export class ParsedEnvironmentFile { public Env : Environment [ ] ; public Warning ? : string ; private constructor ( env , warning ? ) { this . Env = env ; this . Warning = warning ; } public static CreateFromFile ( envFile , initialEnv ? ) { const content = fs . readFileSync ( envFile , "utf8" ) ; return this . CreateFromContent ( content , envFile , initialEnv ) ; } public static CreateFromContent ( content , envFile , initialEnv ? ) { if ( content . charAt ( 0 ) === '\uFEFF' ) { content = content . substr ( 1 ) ; } const parseErrors = [ ] ; const env = new Map ( ) ; if ( initialEnv ) { initialEnv . forEach ( ( e ) => { env . set ( e . name , e . value ) ; } ) ; } content . split ( "\n" ) . forEach ( line => { const r = line . match ( /^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/ ) ; if ( r ) { const key = r [ 1 ] ; let value = r [ 2 ] ?? "" ; if ( ( value . length > 0 ) && ( value . charAt ( 0 ) === '"' ) && ( value . charAt ( value . length - 1 ) === '"' ) ) { value = value . replace ( /\\n/gm , "\n" ) ; } value = value . replace ( /(^['"]|['"]$)/g , "" ) ; env . set ( key , value ) ; } else { const comments = new RegExp ( /^\s*(#|$)/ ) ; if ( ! comments . test ( line ) ) { parseErrors . push ( line ) ; } } } ) ; let warning ; if ( parseErrors . length !== 0 ) { warning = localize ( "ignoring.lines.in.envfile" , "Ignoring non-parseable lines in {0} {1}: " , "envFile" , envFile ) ; parseErrors . forEach ( function ( value , idx , array ) : void { warning += "\"" + value + "\"" + ( ( idx !== array . length - 1 ) ? ", " : "." ) ; } ) ; } const arrayEnv = [ ] ; for ( const key of env . keys ( ) ) { arrayEnv . push ( { name : key , value : env . get ( key ) } ) ; } return new ParsedEnvironmentFile ( arrayEnv , warning ) ; } }