import { EventEmitter } from "events" ; import { rawEffectivePom } from "../utils/mavenUtils" ; import { Utils } from "../utils/Utils" ; import { IEffectivePom } from "./model/IEffectivePom" ; export class EffectivePomProvider { private pomPath : string ; private emitter : EventEmitter = new EventEmitter ( ) ; private isCalculating : boolean = false ; constructor ( pomPath ) { this . pomPath = pomPath ; this . emitter . on ( "complete" , ( _resp ) => { this . isCalculating = false ; } ) ; this . emitter . on ( "error" , ( _error ) => { this . isCalculating = false ; } ) ; } public async calculateEffectivePom ( options ? ) { if ( this . isCalculating ) { return new Promise < void > ( ( resolve , reject ) => { this . emitter . once ( "complete" , resolve ) ; this . emitter . once ( "error" , reject ) ; } ) ; } const pomPath = this . pomPath ; try { this . isCalculating = true ; const ePomString = await rawEffectivePom ( pomPath , options ) ; if ( ePomString === undefined ) { this . emitter . emit ( "complete" , undefined ) ; } else { const ePom = await Utils . parseXmlContent ( ePomString ) ; this . emitter . emit ( "complete" , { pomPath , ePomString , ePom } ) ; } } catch ( error ) { this . emitter . emit ( "error" , error ) ; } } public async getEffectivePom ( options ? ) { const promise = new Promise < IEffectivePom > ( ( resolve , reject ) => { this . emitter . once ( "complete" , ( resp ) => { resolve ( resp ) ; } ) ; this . emitter . once ( "error" , ( error ) => { reject ( error ) ; } ) ; } ) ; if ( this . isCalculating ) { return promise ; } this . calculateEffectivePom ( options ) . catch ( console . error ) ; return promise ; } }