@@ -19,23 +19,47 @@ fileLogService.logData({ message: "Initializing Cleanup process." });
19
19
20
20
const commandsInfos : ISpawnCommandInfo [ ] = [ ] ;
21
21
const filesToDelete : string [ ] = [ ] ;
22
+ const jsCommands : IJSCommand [ ] = [ ] ;
23
+
24
+ const executeJSCleanup = async ( jsCommand : IJSCommand ) => {
25
+ const $childProcess = $injector . resolve < IChildProcess > ( "childProcess" ) ;
26
+
27
+ try {
28
+ fileLogService . logData ( { message : `Start executing action for file: ${ jsCommand . filePath } and data ${ JSON . stringify ( jsCommand . data ) } ` } ) ;
29
+
30
+ await $childProcess . trySpawnFromCloseEvent ( process . execPath , [ path . join ( __dirname , "cleanup-js-subprocess.js" ) , pathToBootstrap , logFile , jsCommand . filePath , JSON . stringify ( jsCommand . data ) ] , { } , { throwError : true , timeout : jsCommand . timeout || 3000 } ) ;
31
+ fileLogService . logData ( { message : `Finished xecuting action for file: ${ jsCommand . filePath } and data ${ JSON . stringify ( jsCommand . data ) } ` } ) ;
32
+
33
+ } catch ( err ) {
34
+ fileLogService . logData ( { message : `Unable to execute action for file ${ jsCommand . filePath } with data ${ JSON . stringify ( jsCommand . data ) } . Error is: ${ err } .` , type : FileLogMessageType . Error } ) ;
35
+ }
36
+ } ;
22
37
23
38
const executeCleanup = async ( ) => {
24
39
const $childProcess = $injector . resolve < IChildProcess > ( "childProcess" ) ;
40
+
41
+ for ( const jsCommand of jsCommands ) {
42
+ await executeJSCleanup ( jsCommand ) ;
43
+ }
44
+
25
45
for ( const commandInfo of commandsInfos ) {
26
46
try {
27
47
fileLogService . logData ( { message : `Start executing command: ${ JSON . stringify ( commandInfo ) } ` } ) ;
28
48
29
49
await $childProcess . trySpawnFromCloseEvent ( commandInfo . command , commandInfo . args , { } , { throwError : true , timeout : commandInfo . timeout || 3000 } ) ;
30
50
fileLogService . logData ( { message : `Successfully executed command: ${ JSON . stringify ( commandInfo ) } ` } ) ;
31
51
} catch ( err ) {
32
- fileLogService . logData ( { message : `Unable to execute command: ${ JSON . stringify ( commandInfo ) } ` , type : FileLogMessageType . Error } ) ;
52
+ fileLogService . logData ( { message : `Unable to execute command: ${ JSON . stringify ( commandInfo ) } . Error is: ${ err } . ` , type : FileLogMessageType . Error } ) ;
33
53
}
34
54
}
35
55
36
56
if ( filesToDelete . length ) {
37
- fileLogService . logData ( { message : `Deleting files ${ filesToDelete . join ( " " ) } ` } ) ;
38
- shelljs . rm ( "-Rf" , filesToDelete ) ;
57
+ try {
58
+ fileLogService . logData ( { message : `Deleting files ${ filesToDelete . join ( " " ) } ` } ) ;
59
+ shelljs . rm ( "-Rf" , filesToDelete ) ;
60
+ } catch ( err ) {
61
+ fileLogService . logData ( { message : `Unable to delete files: ${ JSON . stringify ( filesToDelete ) } . Error is: ${ err } .` , type : FileLogMessageType . Error } ) ;
62
+ }
39
63
}
40
64
41
65
fileLogService . logData ( { message : `cleanup-process finished` } ) ;
@@ -56,7 +80,7 @@ const removeCleanupAction = (commandInfo: ISpawnCommandInfo): void => {
56
80
_ . remove ( commandsInfos , currentCommandInfo => _ . isEqual ( currentCommandInfo , commandInfo ) ) ;
57
81
fileLogService . logData ( { message : `cleanup-process removed command for execution: ${ JSON . stringify ( commandInfo ) } ` } ) ;
58
82
} else {
59
- fileLogService . logData ( { message : `cleanup-process cannot remove command for execution as it has note been added before: ${ JSON . stringify ( commandInfo ) } ` } ) ;
83
+ fileLogService . logData ( { message : `cleanup-process cannot remove command for execution as it has not been added before: ${ JSON . stringify ( commandInfo ) } ` } ) ;
60
84
}
61
85
} ;
62
86
@@ -82,6 +106,32 @@ const removeDeleteAction = (filePath: string): void => {
82
106
}
83
107
} ;
84
108
109
+ const addJSFile = ( jsCommand : IJSCommand ) : void => {
110
+ const fullPath = path . resolve ( jsCommand . filePath ) ;
111
+
112
+ jsCommand . filePath = fullPath ;
113
+
114
+ if ( _ . some ( jsCommands , currentJSCommand => _ . isEqual ( currentJSCommand , jsCommand ) ) ) {
115
+ fileLogService . logData ( { message : `cleanup-process will not add JS file for execution as it has been added already: ${ JSON . stringify ( jsCommand ) } ` } ) ;
116
+ } else {
117
+ fileLogService . logData ( { message : `cleanup-process added JS file for execution: ${ JSON . stringify ( jsCommand ) } ` } ) ;
118
+ jsCommands . push ( jsCommand ) ;
119
+ }
120
+ } ;
121
+
122
+ const removeJSFile = ( jsCommand : IJSCommand ) : void => {
123
+ const fullPath = path . resolve ( jsCommand . filePath ) ;
124
+
125
+ jsCommand . filePath = fullPath ;
126
+
127
+ if ( _ . some ( jsCommands , currentJSCommand => _ . isEqual ( currentJSCommand , jsCommand ) ) ) {
128
+ _ . remove ( jsCommands , currentJSCommand => _ . isEqual ( currentJSCommand , jsCommand ) ) ;
129
+ fileLogService . logData ( { message : `cleanup-process removed JS action for execution: ${ JSON . stringify ( jsCommand ) } ` } ) ;
130
+ } else {
131
+ fileLogService . logData ( { message : `cleanup-process cannot remove JS action for execution as it has not been added before: ${ JSON . stringify ( jsCommand ) } ` } ) ;
132
+ }
133
+ } ;
134
+
85
135
process . on ( "message" , async ( cleanupProcessMessage : ICleanupMessageBase ) => {
86
136
fileLogService . logData ( { message : `cleanup-process received message of type: ${ JSON . stringify ( cleanupProcessMessage ) } ` } ) ;
87
137
@@ -93,10 +143,18 @@ process.on("message", async (cleanupProcessMessage: ICleanupMessageBase) => {
93
143
removeCleanupAction ( ( < ISpawnCommandCleanupMessage > cleanupProcessMessage ) . commandInfo ) ;
94
144
break ;
95
145
case CleanupProcessMessage . AddDeleteFileAction :
96
- addDeleteAction ( ( < IDeleteFileCleanupMessage > cleanupProcessMessage ) . filePath ) ;
146
+ addDeleteAction ( ( < IFileCleanupMessage > cleanupProcessMessage ) . filePath ) ;
97
147
break ;
98
148
case CleanupProcessMessage . RemoveDeleteFileAction :
99
- removeDeleteAction ( ( < IDeleteFileCleanupMessage > cleanupProcessMessage ) . filePath ) ;
149
+ removeDeleteAction ( ( < IFileCleanupMessage > cleanupProcessMessage ) . filePath ) ;
150
+ break ;
151
+ case CleanupProcessMessage . AddJSFileToRequire :
152
+ const jsCleanupMessage = < IJSCleanupMessage > cleanupProcessMessage ;
153
+ addJSFile ( jsCleanupMessage . jsCommand ) ;
154
+ break ;
155
+ case CleanupProcessMessage . RemoveJSFileToRequire :
156
+ const msgToRemove = < IJSCleanupMessage > cleanupProcessMessage ;
157
+ removeJSFile ( msgToRemove . jsCommand ) ;
100
158
break ;
101
159
default :
102
160
fileLogService . logData ( { message : `Unable to handle message of type ${ cleanupProcessMessage . messageType } . Full message is ${ JSON . stringify ( cleanupProcessMessage ) } ` , type : FileLogMessageType . Error } ) ;
0 commit comments