Fix script deletion during execution on desktop
This commit fixes an issue seen on certain Windows environments (Windows 10 22H2 and 11 23H2 Pro Azure VMs) where scripts were being deleted during execution due to temporary directory usage. To resolve this, scripts are now stored in a persistent directory, enhancing reliability for long-running scripts and improving auditability along with troubleshooting. Key changes: - Move script execution logic to the `main` process from `preloader` to utilize Electron's `app.getPath`. - Improve runtime environment detection for non-browser environments to allow its usage in Electron main process. - Introduce a secure module to expose IPC channels from the main process to the renderer via the preloader process. Supporting refactorings include: - Simplify `CodeRunner` interface by removing the `tempScriptFolderName` parameter. - Rename `NodeSystemOperations` to `NodeElectronSystemOperations` as it now wraps electron APIs too, and convert it to class for simplicity. - Rename `TemporaryFileCodeRunner` to `ScriptFileCodeRunner` to reflect its new functinoality. - Rename `SystemOperations` folder to `System` for simplicity. - Rename `HostRuntimeEnvironment` to `BrowserRuntimeEnvironment` for clarity. - Refactor main Electron process configuration to align with latest Electron documentation/recommendations. - Refactor unit tests `BrowserRuntimeEnvironment` to simplify singleton workaround. - Use alias imports like `electron/main` and `electron/common` for better clarity.
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
// Initializes Electron's main process, always runs in the background, and manages the main window.
|
||||
|
||||
import {
|
||||
app, protocol, BrowserWindow, shell, screen,
|
||||
} from 'electron';
|
||||
app, protocol, BrowserWindow, screen,
|
||||
} from 'electron/main';
|
||||
import { shell } from 'electron/common';
|
||||
import log from 'electron-log/main';
|
||||
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
|
||||
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||
import { ElectronLogger } from '@/infrastructure/Log/ElectronLogger';
|
||||
import { name } from '@/../package.json' assert { type: 'json' };
|
||||
import { setupAutoUpdater } from './Update/UpdateInitializer';
|
||||
import {
|
||||
APP_ICON_PATH, PRELOADER_SCRIPT_PATH, RENDERER_HTML_PATH, RENDERER_URL,
|
||||
} from './ElectronConfig';
|
||||
import { registerAllIpcChannels } from './IpcRegistration';
|
||||
|
||||
const isDevelopment = !app.isPackaged;
|
||||
|
||||
@@ -57,36 +60,11 @@ function createWindow() {
|
||||
});
|
||||
}
|
||||
|
||||
let macOsQuit = false;
|
||||
// Quit when all windows are closed.
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform === 'darwin'
|
||||
&& !macOsQuit) {
|
||||
// On macOS it is common for applications and their menu bar
|
||||
// to stay active until the user quits explicitly with Cmd + Q
|
||||
return;
|
||||
}
|
||||
app.quit();
|
||||
});
|
||||
configureAppQuitBehavior();
|
||||
registerAllIpcChannels();
|
||||
setAppName();
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
// On macOS we application quit is stopped if user does not Cmd + Q
|
||||
// But we still want to be able to use app.quit() and quit the application
|
||||
// on menu bar, after updates etc.
|
||||
app.on('before-quit', () => {
|
||||
macOsQuit = true;
|
||||
});
|
||||
}
|
||||
|
||||
app.on('activate', () => {
|
||||
// On macOS it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
if (win === null) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
||||
app.on('ready', async () => {
|
||||
app.whenReady().then(async () => {
|
||||
if (isDevelopment) {
|
||||
try {
|
||||
await installExtension(VUEJS_DEVTOOLS);
|
||||
@@ -95,6 +73,13 @@ app.on('ready', async () => {
|
||||
}
|
||||
}
|
||||
createWindow();
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
// On macOS it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Exit cleanly on request from parent process in development mode.
|
||||
@@ -156,3 +141,39 @@ function setupLogger(): void {
|
||||
log.transports.file.level = 'silly';
|
||||
log.eventLogger.startLogging();
|
||||
}
|
||||
|
||||
function configureAppQuitBehavior() {
|
||||
let macOsQuit = false;
|
||||
// Quit when all windows are closed.
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform === 'darwin'
|
||||
&& !macOsQuit) {
|
||||
/*
|
||||
On macOS it is common for applications and their menu bar
|
||||
to stay active until the user quits explicitly with Cmd + Q
|
||||
*/
|
||||
return;
|
||||
}
|
||||
app.quit();
|
||||
});
|
||||
if (process.platform === 'darwin') {
|
||||
/*
|
||||
On macOS we application quit is stopped if user does not Cmd + Q
|
||||
But we still want to be able to use app.quit() and quit the application
|
||||
on menu bar, after updates etc.
|
||||
*/
|
||||
app.on('before-quit', () => {
|
||||
macOsQuit = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setAppName() {
|
||||
/*
|
||||
Set the app name in development mode to ensure correct userData path.
|
||||
Without this, `app.getPath('userData')` defaults to 'Electron'.
|
||||
*/
|
||||
if (isDevelopment) {
|
||||
app.setName(name); // Works only for Windows, unsolved for macOS.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user