From d6c4d2769ab575c17b0f731072a850f1cad80e73 Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Tue, 13 Dec 2022 18:49:58 +0000 Subject: [PATCH] gluon: add support for checking if binaries exist in path --- gluon/index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/gluon/index.js b/gluon/index.js index 899f29d..bdd3adc 100644 --- a/gluon/index.js +++ b/gluon/index.js @@ -3,8 +3,8 @@ global.log = (...args) => console.log(`[${rgb(88, 101, 242, 'Gluon')}]`, ...args process.versions.gluon = '5.1-dev'; -import { join, dirname } from 'path'; -import { access } from 'fs/promises'; +import { join, dirname, delimiter, sep } from 'path'; +import { access, readdir } from 'fs/promises'; import { fileURLToPath } from 'url'; import Chromium from './browser/chromium.js'; @@ -29,7 +29,23 @@ const browserPaths = ({ } })[process.platform]; -const exists = path => access(path).then(() => true).catch(() => false); +let _binariesInPath; // cache as to avoid excessive reads +const getBinariesInPath = async () => { + if (_binariesInPath) return _binariesInPath; + + return _binariesInPath = (await Promise.all(process.env.PATH + .replaceAll('"', '') + .split(delimiter) + .filter(Boolean) + .map(x => readdir(x.replace(/"+/g, '')).catch(() => [])))).flat(); +}; + +const exists = async path => { + if (path.includes(sep)) return await access(path).then(() => true).catch(() => false); + + // just binary name, so check path + return (await getBinariesInPath()).some(x => x.includes(path)); +}; const findBrowserPath = async (forceBrowser) => { if (forceBrowser) return [ browserPaths[forceBrowser], forceBrowser ];