import * as Path from 'path' ; import * as Types from './sysTypes' ; import { IStringDictionary , ISystemVariables } from './types' ; export abstract class AbstractSystemVariables implements ISystemVariables { public resolve ( value ) ; public resolve ( value ) ; public resolve ( value ) ; public resolve ( value ) ; public resolve ( value ) ; public resolve ( value ) { if ( Types . isString ( value ) ) { return this . __resolveString ( value ) ; } else if ( Types . isArray ( value ) ) { return this . __resolveArray ( value ) ; } else if ( Types . isObject ( value ) ) { return this . __resolveLiteral ( value ) ; } return value ; } public resolveAny < T > ( value ) ; public resolveAny ( value ) { if ( Types . isString ( value ) ) { return this . __resolveString ( value ) ; } else if ( Types . isArray ( value ) ) { return this . __resolveAnyArray ( value ) ; } else if ( Types . isObject ( value ) ) { return this . __resolveAnyLiteral ( value ) ; } return value ; } private __resolveString ( value ) { const regexp = /\$\{(.*?)\}/g ; return value . replace ( regexp , ( match , name ) => { const newValue = ( < any > this ) [ name ] ; if ( Types . isString ( newValue ) ) { return newValue ; } else { return match && ( match . indexOf ( 'env.' ) > 0 || match . indexOf ( 'env:' ) > 0 ) ? '' : match ; } } ) ; } private __resolveLiteral ( values ) { const result = Object . create ( null ) ; Object . keys ( values ) . forEach ( key => { const value = values [ key ] ; result [ key ] = < any > this . resolve ( < any > value ) ; } ) ; return result ; } private __resolveAnyLiteral < T > ( values ) ; private __resolveAnyLiteral ( values ) { const result = Object . create ( null ) ; Object . keys ( values ) . forEach ( key => { const value = values [ key ] ; result [ key ] = < any > this . resolveAny ( < any > value ) ; } ) ; return result ; } private __resolveArray ( value ) { return value . map ( s => this . __resolveString ( s ) ) ; } private __resolveAnyArray < T > ( value ) ; private __resolveAnyArray ( value ) { return value . map ( s => this . resolveAny ( s ) ) ; } } export class SystemVariables extends AbstractSystemVariables { private _workspaceFolder : string ; private _workspaceFolderName : string ; constructor ( workspaceFolder ? ) { super ( ) ; this . _workspaceFolder = typeof workspaceFolder === 'string' ? workspaceFolder : __dirname ; this . _workspaceFolderName = Path . basename ( this . _workspaceFolder ) ; Object . keys ( process . env ) . forEach ( key => { ( this as any as { [ key ] : string | undefined ; } ) [ `env:${ key }` ] = ( this as any as { [ key ] : string | undefined ; } ) [ `env.${ key }` ] = process . env [ key ] ; } ) ; } public get cwd ( ) : string { return this . workspaceFolder ; } public get workspaceRoot ( ) : string { return this . _workspaceFolder ; } public get workspaceFolder ( ) : string { return this . _workspaceFolder ; } public get workspaceRootFolderName ( ) : string { return this . _workspaceFolderName ; } public get workspaceFolderBasename ( ) : string { return this . _workspaceFolderName ; } }