Skip to content

Commit 4001014

Browse files
Reworked patching code
1 parent a0ed904 commit 4001014

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

scripts/postinstall.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,40 @@ fs.writeFileSync(antdLibSettingsPath, JSON.stringify(antdlibSettings, null, 2));
1717

1818

1919
// 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.
2121
// When we upgrade electron-builder package to at least 23.0.6 we need to use the removeDefaultUninstallWelcomePage
2222
// option and the code below can be removed.
2323
// @see https://stackoverflow.com/questions/73454796/default-unwelcome-page-with-electron-builder-nsis-cant-be-changed-or-removed
2424
const assistedInstallerPath = path.resolve('./node_modules/app-builder-lib/templates/nsis/assistedInstaller.nsh');
2525

26-
// Read the file, modify it, and write back
26+
// Function to read, modify, and write without closing the file
2727
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+');
3232

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) => {
3737
if (line.includes(lineToComment) && !line.trim().startsWith(';')) {
38-
return `${commentString}${line}`;
38+
return `${commentString}${line}`; // Add commentString to the target line
3939
}
4040
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
4343

44+
fs.ftruncateSync(fd); // Truncate the file to ensure old content is removed
45+
fs.writeSync(fd, modifiedContent);
4446

45-
if (fs.existsSync(assistedInstallerPath)) {
46-
fs.writeFileSync(assistedInstallerPath, modifiedContent, 'utf-8');
4747
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+
}
4854
}
4955
};
5056

0 commit comments

Comments
 (0)