@@ -17,34 +17,40 @@ fs.writeFileSync(antdLibSettingsPath, JSON.stringify(antdlibSettings, null, 2));
17
17
18
18
19
19
// The code below is needed to provide a better uninstall experience.
20
- // Here we patch assistedInstaller.nsh to avoid redudant uninstaller welcome page.
20
+ // Here we patch assistedInstaller.nsh to avoid redundant uninstaller welcome page.
21
21
// When we upgrade electron-builder package to at least 23.0.6 we need to use the removeDefaultUninstallWelcomePage
22
22
// option and the code below can be removed.
23
23
// @see https://stackoverflow.com/questions/73454796/default-unwelcome-page-with-electron-builder-nsis-cant-be-changed-or-removed
24
24
const assistedInstallerPath = path . resolve ( './node_modules/app-builder-lib/templates/nsis/assistedInstaller.nsh' ) ;
25
25
26
- // Read the file , modify it , and write back
26
+ // Function to read , modify, and write without closing the file
27
27
const commentLineInFile = ( assistedInstallerPath , lineToComment , commentString = ';' ) => {
28
- if ( ! fs . existsSync ( assistedInstallerPath ) ) {
29
- console . error ( `File not found: ${ assistedInstallerPath } ` ) ;
30
- process . exit ( 1 ) ;
31
- }
28
+ let fd ;
29
+
30
+ try {
31
+ fd = fs . openSync ( assistedInstallerPath , 'r+' ) ;
32
32
33
- let fileContent = fs . readFileSync ( assistedInstallerPath , 'utf-8' ) ;
34
- const modifiedContent = fileContent
35
- . split ( '\n' )
36
- . map ( ( line ) => {
33
+ const fileContent = fs . readFileSync ( fd , 'utf-8' ) ;
34
+ const modifiedContent = fileContent
35
+ . split ( '\n' )
36
+ . map ( ( line ) => {
37
37
if ( line . includes ( lineToComment ) && ! line . trim ( ) . startsWith ( ';' ) ) {
38
- return `${ commentString } ${ line } ` ;
38
+ return `${ commentString } ${ line } ` ; // Add commentString to the target line
39
39
}
40
40
return line ; // Leave all other lines unchanged
41
- } )
42
- . join ( '\n' ) ; // Rejoin the lines back together into a single string
41
+ } )
42
+ . join ( '\n' ) ; // Rejoin the lines back together into a single string
43
43
44
+ fs . ftruncateSync ( fd ) ; // Truncate the file to ensure old content is removed
45
+ fs . writeSync ( fd , modifiedContent ) ;
44
46
45
- if ( fs . existsSync ( assistedInstallerPath ) ) {
46
- fs . writeFileSync ( assistedInstallerPath , modifiedContent , 'utf-8' ) ;
47
47
console . log ( `Post install. Successfully commented "${ lineToComment } " in file: ${ assistedInstallerPath } ` ) ;
48
+ } catch ( error ) {
49
+ console . error ( `Post install. An error occurred while processing the file: ${ error . message } ` ) ;
50
+ } finally {
51
+ if ( fd !== undefined ) {
52
+ fs . closeSync ( fd ) ;
53
+ }
48
54
}
49
55
} ;
50
56
0 commit comments