import { TaskPhases , NgPackagerHooks , NormalizedNgPackagerHooks } from './hooks' ; import { normalizeHooks , normalizeTaskPhases , HOOK_PHASES , TRANSFORM_PROVIDER_MAP } from './utils' ; import { JobMetadata , findJobMetadata , Type } from './job' ; export class HookRegistry { private _jobs : JobMetadata [ ] = [ ] ; private _hooks : NormalizedNgPackagerHooks = { } ; get hasSelfWatchJob ( ) : boolean { return this . _jobs . some ( job => job . internalWatch ) ; } constructor ( initialHooks ? ) { if ( initialHooks ) { this . _hooks = normalizeHooks ( initialHooks ) ; } } register < T > ( job ) ; register < T extends keyof NgPackagerHooks > ( hook , handlers ) ; register < T extends keyof NgPackagerHooks > ( hookOrJob , handlers ? ) { if ( typeof hookOrJob === 'string' ) { const taskPhases = normalizeTaskPhases ( handlers ) ; for ( const phase of HOOK_PHASES ) { const handlers = taskPhases [ phase ] ; if ( handlers ) { this . getHookPhase ( hookOrJob , phase ) . push ( ... handlers ) ; } } } else { const jobMeta = findJobMetadata ( hookOrJob ) ; if ( ! jobMeta ) { throw new Error ( `Unknown Job: ${ hookOrJob }` ) ; } this . _jobs . push ( jobMeta ) ; const { hooks } = jobMeta ; const hookNames = Object . keys ( TRANSFORM_PROVIDER_MAP ) as any ; for ( const key of hookNames ) { if ( hooks [ key ] ) { this . register ( key , hooks [ key ] ) ; } } } return this ; } getHooks ( ) { return this . _hooks ; } getJobs ( ) { return this . _jobs ; } private getHookPhase < T extends keyof NgPackagerHooks , P extends keyof TaskPhases > ( hook , phase ) { const hookMap = this . _hooks [ hook ] || ( this . _hooks [ hook ] = { } as NormalizedNgPackagerHooks [ T ] ) ; if ( ! hookMap [ phase ] ) { hookMap [ phase ] = [ ] as NormalizedNgPackagerHooks [ T ] [ P ] ; } return hookMap [ phase ] ; } }