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.
69 lines
2.7 KiB
TypeScript
69 lines
2.7 KiB
TypeScript
import { resolve, dirname } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { VITE_USER_DEFINED_ENVIRONMENT_KEYS } from './src/infrastructure/EnvironmentVariables/Vite/ViteEnvironmentKeys';
|
|
import tsconfigJson from './tsconfig.json' assert { type: 'json' };
|
|
import packageJson from './package.json' assert { type: 'json' };
|
|
|
|
type ViteAliasDefinitions = Record<string, string>;
|
|
|
|
export function getAliases(): ViteAliasDefinitions {
|
|
return {
|
|
...getPathAliasesFromTsConfig(),
|
|
...getElectronProcessSpecificModuleAliases(),
|
|
};
|
|
}
|
|
|
|
export function getSelfDirectoryAbsolutePath() {
|
|
const filePath = fileURLToPath(import.meta.url);
|
|
const directoryPath = dirname(filePath);
|
|
return directoryPath;
|
|
}
|
|
|
|
type ViteEnvironmentKeyValues = {
|
|
[K in
|
|
typeof VITE_USER_DEFINED_ENVIRONMENT_KEYS[keyof typeof VITE_USER_DEFINED_ENVIRONMENT_KEYS]
|
|
]: string
|
|
};
|
|
|
|
type ViteGlobalVariableReplacementDefinitions = Record<string, string>;
|
|
|
|
export function getClientEnvironmentVariables(): ViteGlobalVariableReplacementDefinitions {
|
|
const environmentVariables: ViteEnvironmentKeyValues = {
|
|
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.NAME]: packageJson.name,
|
|
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.VERSION]: packageJson.version,
|
|
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.REPOSITORY_URL]: packageJson.repository.url,
|
|
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.HOMEPAGE_URL]: packageJson.homepage,
|
|
[VITE_USER_DEFINED_ENVIRONMENT_KEYS.SLOGAN]: packageJson.slogan,
|
|
};
|
|
return Object.entries(environmentVariables).reduce((acc, [key, value]) => {
|
|
const formattedEnvVariableKey = `import.meta.env.${key}`;
|
|
const jsonEncodedEnvVariableValue = JSON.stringify(value);
|
|
return { ...acc, [formattedEnvVariableKey]: jsonEncodedEnvVariableValue };
|
|
}, {});
|
|
}
|
|
|
|
function getPathAliasesFromTsConfig(): ViteAliasDefinitions {
|
|
const { paths } = tsconfigJson.compilerOptions;
|
|
return Object.keys(paths).reduce((aliases, pathName) => {
|
|
const pathFolder = paths[pathName][0];
|
|
const aliasFolder = pathFolder.substring(0, pathFolder.length - 1); // trim * from end
|
|
const aliasName = pathName.substring(0, pathName.length - 2); // trim /* from end
|
|
const aliasPath = resolve(getSelfDirectoryAbsolutePath(), aliasFolder);
|
|
aliases[aliasName] = aliasPath;
|
|
return aliases;
|
|
}, {});
|
|
}
|
|
|
|
function getElectronProcessSpecificModuleAliases(): ViteAliasDefinitions {
|
|
// Workaround for scoped Electron module imports due to https://github.com/alex8088/electron-vite/issues/372
|
|
const electronProcessScopedModuleAliases = [
|
|
'electron/main',
|
|
'electron/renderer',
|
|
'electron/common',
|
|
] as const;
|
|
return electronProcessScopedModuleAliases.reduce((aliases, alias) => {
|
|
aliases[alias] = 'electron';
|
|
return aliases;
|
|
}, {});
|
|
}
|