@@ -41,10 +41,12 @@ function execShellCommand(cmd: string): Promise<string> {
4141
4242async function executeHooks (
4343 hookName : string ,
44- _scripts : Types . LifeCycleHookValue = [ ] ,
45- args : string [ ] = [ ]
46- ) : Promise < void > {
44+ _scripts : Types . LifeCycleHookValue | Types . LifeCycleAlterHookValue = [ ] ,
45+ args : string [ ] = [ ] ,
46+ initialState ?: string
47+ ) : Promise < void | string > {
4748 debugLog ( `Running lifecycle hook "${ hookName } " scripts...` ) ;
49+ let state = initialState ;
4850 const scripts = Array . isArray ( _scripts ) ? _scripts : [ _scripts ] ;
4951
5052 const quotedArgs = quote ( args ) ;
@@ -54,9 +56,16 @@ async function executeHooks(
5456 await execShellCommand ( `${ script } ${ quotedArgs } ` ) ;
5557 } else {
5658 debugLog ( `Running lifecycle hook "${ hookName } " script: ${ script . name } with args: ${ args . join ( ' ' ) } ...` ) ;
57- await script ( ...args ) ;
59+ const hookArgs = state === undefined ? args : [ ...args , state ] ;
60+ const hookResult = await script ( ...hookArgs ) ;
61+ if ( typeof hookResult === 'string' && typeof state === 'string' ) {
62+ debugLog ( `Received new content from lifecycle hook "${ hookName } " script: ${ script . name } ` ) ;
63+ state = hookResult ;
64+ }
5865 }
5966 }
67+
68+ return state ;
6069}
6170
6271export const lifecycleHooks = ( _hooks : Partial < Types . LifecycleHooksDefinition > = { } ) => {
@@ -66,18 +75,30 @@ export const lifecycleHooks = (_hooks: Partial<Types.LifecycleHooksDefinition> =
6675 } ;
6776
6877 return {
69- afterStart : async ( ) : Promise < void > => executeHooks ( 'afterStart' , hooks . afterStart ) ,
70- onWatchTriggered : async ( event : string , path : string ) : Promise < void > =>
71- executeHooks ( 'onWatchTriggered' , hooks . onWatchTriggered , [ event , path ] ) ,
72- onError : async ( error : string ) : Promise < void > => executeHooks ( 'onError' , hooks . onError , [ error ] ) ,
73- afterOneFileWrite : async ( path : string ) : Promise < void > =>
74- executeHooks ( 'afterOneFileWrite' , hooks . afterOneFileWrite , [ path ] ) ,
75- afterAllFileWrite : async ( paths : string [ ] ) : Promise < void > =>
76- executeHooks ( 'afterAllFileWrite' , hooks . afterAllFileWrite , paths ) ,
77- beforeOneFileWrite : async ( path : string ) : Promise < void > =>
78- executeHooks ( 'beforeOneFileWrite' , hooks . beforeOneFileWrite , [ path ] ) ,
79- beforeAllFileWrite : async ( paths : string [ ] ) : Promise < void > =>
80- executeHooks ( 'beforeAllFileWrite' , hooks . beforeAllFileWrite , paths ) ,
81- beforeDone : async ( ) : Promise < void > => executeHooks ( 'beforeDone' , hooks . beforeDone ) ,
78+ afterStart : async ( ) : Promise < void > => {
79+ await executeHooks ( 'afterStart' , hooks . afterStart ) ;
80+ } ,
81+ onWatchTriggered : async ( event : string , path : string ) : Promise < void > => {
82+ await executeHooks ( 'onWatchTriggered' , hooks . onWatchTriggered , [ event , path ] ) ;
83+ } ,
84+ onError : async ( error : string ) : Promise < void > => {
85+ await executeHooks ( 'onError' , hooks . onError , [ error ] ) ;
86+ } ,
87+ afterOneFileWrite : async ( path : string ) : Promise < void > => {
88+ await executeHooks ( 'afterOneFileWrite' , hooks . afterOneFileWrite , [ path ] ) ;
89+ } ,
90+ afterAllFileWrite : async ( paths : string [ ] ) : Promise < void > => {
91+ await executeHooks ( 'afterAllFileWrite' , hooks . afterAllFileWrite , paths ) ;
92+ } ,
93+ beforeOneFileWrite : async ( path : string , content : string ) : Promise < string > => {
94+ const result = await executeHooks ( 'beforeOneFileWrite' , hooks . beforeOneFileWrite , [ path ] , content ) ;
95+ return typeof result === 'string' ? result : content ;
96+ } ,
97+ beforeAllFileWrite : async ( paths : string [ ] ) : Promise < void > => {
98+ await executeHooks ( 'beforeAllFileWrite' , hooks . beforeAllFileWrite , paths ) ;
99+ } ,
100+ beforeDone : async ( ) : Promise < void > => {
101+ await executeHooks ( 'beforeDone' , hooks . beforeDone ) ;
102+ } ,
82103 } ;
83104} ;
0 commit comments