From cd0bbb6cfb5dc8574ec5f6ad34b62da929ff62d8 Mon Sep 17 00:00:00 2001 From: alex8088 <244096523@qq.com> Date: Sun, 18 Sep 2022 01:46:02 +0800 Subject: [PATCH] perf: cache electron executable path --- src/electron.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/electron.ts b/src/electron.ts index be01d46..78e225d 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -20,18 +20,23 @@ const ensureElectronEntryFile = (root = process.cwd()): void => { } } -const getElectronPath = (): string => { - const electronModulePath = path.resolve(process.cwd(), 'node_modules', 'electron') - const pathFile = path.join(electronModulePath, 'path.txt') - let executablePath - if (fs.existsSync(pathFile)) { - executablePath = fs.readFileSync(pathFile, 'utf-8') - } - if (executablePath) { - return path.join(electronModulePath, 'dist', executablePath) - } else { - throw new Error('Electron uninstall') +export const getElectronPath = (): string => { + let electronExecPath = process.env.ELECTRON_EXEC_PATH || '' + if (!electronExecPath) { + const electronModulePath = path.resolve(process.cwd(), 'node_modules', 'electron') + const pathFile = path.join(electronModulePath, 'path.txt') + let executablePath + if (fs.existsSync(pathFile)) { + executablePath = fs.readFileSync(pathFile, 'utf-8') + } + if (executablePath) { + electronExecPath = path.join(electronModulePath, 'dist', executablePath) + process.env.ELECTRON_EXEC_PATH = electronExecPath + } else { + throw new Error('Electron uninstall') + } } + return electronExecPath } export function startElectron(root: string | undefined, logger: Logger): ChildProcessWithoutNullStreams {