Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions desktop-app/e2e/ensure-electron.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');

const projectRoot = path.resolve(__dirname, '..');
const electronRoot = path.join(projectRoot, 'node_modules', 'electron');
const electronInstallScript = path.join(electronRoot, 'install.js');
const electronPathFile = path.join(electronRoot, 'path.txt');

const getPlatformPath = () => {
const platform = process.env.npm_config_platform || process.platform;

switch (platform) {
case 'mas':
case 'darwin':
return 'Electron.app/Contents/MacOS/Electron';
case 'freebsd':
case 'openbsd':
case 'linux':
return 'electron';
case 'win32':
return 'electron.exe';
default:
throw new Error(`Electron builds are not available on platform: ${platform}`);
}
};

const expectedPlatformPath = getPlatformPath();
const expectedElectronPath = path.join(electronRoot, 'dist', expectedPlatformPath);
const expectedFrameworkPath = path.join(
electronRoot,
'dist',
'Electron.app/Contents/Frameworks/Electron Framework.framework/Electron Framework'
);

const hasUsableElectronInstall = () => {
if (!fs.existsSync(expectedElectronPath)) {
return false;
}

if ((process.env.npm_config_platform || process.platform) === 'darwin') {
return fs.existsSync(expectedFrameworkPath);
}

return true;
};

const writeElectronPathFile = () => {
fs.writeFileSync(electronPathFile, expectedPlatformPath);
};

const resolveElectronPath = () => {
const electronIndex = require.resolve('electron/index.js');
delete require.cache[electronIndex];
return require('electron/index.js');
};

const installElectron = () => {
const env = {...process.env};
Object.keys(env).forEach((key) => {
const normalizedKey = key.toLowerCase();
if (
normalizedKey === 'electron_skip_binary_download' ||
normalizedKey === 'npm_config_electron_skip_binary_download'
) {
delete env[key];
}
});

fs.rmSync(path.join(electronRoot, 'dist'), {recursive: true, force: true});

console.log('Repairing Electron install...');
childProcess.execFileSync(process.execPath, [electronInstallScript], {
cwd: projectRoot,
env,
stdio: 'inherit',
});
};

const repairElectron = () => {
if (!hasUsableElectronInstall()) {
installElectron();
}

if (hasUsableElectronInstall()) {
writeElectronPathFile();
}
};

let electronPath;

try {
electronPath = resolveElectronPath();
} catch {
repairElectron();
electronPath = resolveElectronPath();
}

if (!fs.existsSync(electronPath) || !hasUsableElectronInstall()) {
repairElectron();
electronPath = resolveElectronPath();
}

if (!fs.existsSync(electronPath)) {
throw new Error(`Electron executable was not found at ${electronPath}`);
}

console.log(`Electron executable: ${electronPath}`);
2 changes: 1 addition & 1 deletion desktop-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test": "vitest run",
"test:watch": "vitest",
"test:coverage": "vitest run --coverage",
"test:e2e": "playwright test --config=e2e/playwright.config.ts",
"test:e2e": "node e2e/ensure-electron.cjs && playwright test --config=e2e/playwright.config.ts",
"test:e2e:headless": "cross-env E2E_HEADLESS=true playwright test --config=e2e/playwright.config.ts",
"test:e2e:headed": "playwright test --config=e2e/playwright.config.ts --headed",
"test:e2e:debug": "playwright test --config=e2e/playwright.config.ts --debug",
Expand Down
Loading
Loading