From 8c01ac658c54ff5d4f8911449aa13f88e89f24c6 Mon Sep 17 00:00:00 2001 From: Kasper Date: Tue, 30 Aug 2022 09:15:51 +0200 Subject: [PATCH] Fix electron binary detection --- src/index.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index da05a3a..3c27b75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,33 @@ import { spawn } from 'child_process' -import { resolve } from 'path' +import { resolve, join } from 'path' import { default as esbuild, type BuildOptions as EsbuildOptions } from 'esbuild' import { BuildOptions as ViteBuildOptions, Plugin, ResolvedConfig } from 'vite' -import { default as electronPath } from 'electron' import fs from 'fs' -// Spawn electron the same way as electron cli -async function spawnElectron(args: string[], env: NodeJS.ProcessEnv) { - if (typeof electronPath !== 'string') { - console.error('Could not get Electron path') - process.exit(1) +const electronDir = resolve(process.cwd(), 'node_modules', 'electron') + +// imitates `electron/index.js` +function getElectronPath() { + const pathFile = join(electronDir, 'path.txt') + console.log(pathFile) + + let executablePath: string | undefined + if (fs.existsSync(pathFile)) { + executablePath = fs.readFileSync(pathFile, 'utf-8') + } + if (process.env.ELECTRON_OVERRIDE_DIST_PATH) { + return join(process.env.ELECTRON_OVERRIDE_DIST_PATH, executablePath || 'electron') } + if (executablePath) { + return join(electronDir, 'dist', executablePath) + } + console.error('Could not get Electron path') + process.exit(1) +} + +// Spawn electron, imitates `electron/cli.js` +async function spawnElectron(args: string[], env: NodeJS.ProcessEnv) { + const electronPath = getElectronPath() const child = spawn(electronPath, args, { stdio: 'ignore', env, windowsHide: false }) child.on('close', (code, signal) => { if (code === null) { -- 2.51.2