@@ -17,34 +17,46 @@ 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
+ // Check if the file exists
28
29
if ( ! fs . existsSync ( assistedInstallerPath ) ) {
29
30
console . error ( `File not found: ${ assistedInstallerPath } ` ) ;
30
31
process . exit ( 1 ) ;
31
32
}
32
33
33
- let fileContent = fs . readFileSync ( assistedInstallerPath , 'utf-8' ) ;
34
- const modifiedContent = fileContent
35
- . split ( '\n' )
36
- . map ( ( line ) => {
34
+ let fd ;
35
+
36
+ try {
37
+ fd = fs . openSync ( assistedInstallerPath , 'r+' ) ;
38
+
39
+ const fileContent = fs . readFileSync ( fd , 'utf-8' ) ;
40
+ const modifiedContent = fileContent
41
+ . split ( '\n' )
42
+ . map ( ( line ) => {
37
43
if ( line . includes ( lineToComment ) && ! line . trim ( ) . startsWith ( ';' ) ) {
38
- return `${ commentString } ${ line } ` ;
44
+ return `${ commentString } ${ line } ` ; // Add commentString to the target line
39
45
}
40
46
return line ; // Leave all other lines unchanged
41
- } )
42
- . join ( '\n' ) ; // Rejoin the lines back together into a single string
47
+ } )
48
+ . join ( '\n' ) ; // Rejoin the lines back together into a single string
43
49
50
+ fs . ftruncateSync ( fd ) ; // Truncate the file to ensure old content is removed
51
+ fs . writeSync ( fd , modifiedContent ) ;
44
52
45
- if ( fs . existsSync ( assistedInstallerPath ) ) {
46
- fs . writeFileSync ( assistedInstallerPath , modifiedContent , 'utf-8' ) ;
47
53
console . log ( `Post install. Successfully commented "${ lineToComment } " in file: ${ assistedInstallerPath } ` ) ;
54
+ } catch ( error ) {
55
+ console . error ( `Post install. An error occurred while processing the file: ${ error . message } ` ) ;
56
+ } finally {
57
+ if ( fd !== undefined ) {
58
+ fs . closeSync ( fd ) ;
59
+ }
48
60
}
49
61
} ;
50
62
0 commit comments