@@ -65,6 +65,7 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
6565 /** @deprecated */
6666 arguments ! : string [ ] ;
6767 _destinationRoot ! : string ;
68+ _contextMap ?: Map < string , any > ;
6869 _sourceRoot ! : string ;
6970
7071 generatorConfig ?: Storage ;
@@ -744,6 +745,9 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
744745 destinationRoot ( rootPath ?: string ) {
745746 if ( typeof rootPath === 'string' ) {
746747 this . _destinationRoot = pathResolve ( rootPath ) ;
748+ if ( 'getContextMap' in this . env ) {
749+ this . _contextMap = ( this . env as any ) . getContextMap ( this . _destinationRoot ) ;
750+ }
747751
748752 if ( ! fs . existsSync ( this . _destinationRoot ) ) {
749753 fs . mkdirSync ( this . _destinationRoot , { recursive : true } ) ;
@@ -814,6 +818,24 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
814818 const appName : string = this . packageJson . get ( 'name' ) ?? path . basename ( this . destinationRoot ( ) ) ;
815819 return appName . replaceAll ( / [ ^ \s \w ] + ?/ g, ' ' ) ;
816820 }
821+
822+ getContextData < const T = any > ( key : string , factory ?: ( ) => T ) : T {
823+ if ( ! this . _contextMap ) {
824+ throw new Error ( 'getContextMap is not implemented in the environment' ) ;
825+ }
826+
827+ if ( this . _contextMap . has ( key ) ) {
828+ return this . _contextMap . get ( key ) ;
829+ }
830+
831+ if ( factory ) {
832+ const value = factory ( ) ;
833+ this . _contextMap . set ( key , value ) ;
834+ return value ;
835+ }
836+
837+ throw new Error ( `Context data ${ key } not found and no factory provided` ) ;
838+ }
817839}
818840
819841// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
0 commit comments