Skip to content

Commit 40c709d

Browse files
Reworked patching code
1 parent a0ed904 commit 40c709d

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Diff for: scripts/postinstall.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,46 @@ 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+
// Check if the file exists
2829
if (!fs.existsSync(assistedInstallerPath)) {
2930
console.error(`File not found: ${assistedInstallerPath}`);
3031
process.exit(1);
3132
}
3233

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) => {
3743
if (line.includes(lineToComment) && !line.trim().startsWith(';')) {
38-
return `${commentString}${line}`;
44+
return `${commentString}${line}`; // Add commentString to the target line
3945
}
4046
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
4349

50+
fs.ftruncateSync(fd); // Truncate the file to ensure old content is removed
51+
fs.writeSync(fd, modifiedContent);
4452

45-
if (fs.existsSync(assistedInstallerPath)) {
46-
fs.writeFileSync(assistedInstallerPath, modifiedContent, 'utf-8');
4753
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+
}
4860
}
4961
};
5062

0 commit comments

Comments
 (0)