Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
lib/browserPaths: move more into lib file
Browse files Browse the repository at this point in the history
  • Loading branch information
CanadaHonk committed Apr 8, 2023
1 parent 996a57b commit 720e73d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 67 deletions.
68 changes: 3 additions & 65 deletions src/index.js
@@ -1,5 +1,5 @@
import { join, dirname, extname, delimiter, sep, isAbsolute } from 'path';
import { access, readdir } from 'fs/promises';
import { join, dirname, extname, isAbsolute } from 'path';

import { fileURLToPath } from 'url';
import { log, dangerousAPI } from './lib/logger.js';

Expand All @@ -8,82 +8,20 @@ import Firefox from './browser/firefox.js';

import * as ExtensionsAPI from './extensions.js';
import LocalHTTP from './lib/local/http.js';
import browserPaths from './lib/browserPaths.js';
import { findBrowserPath, getBrowserType } from './lib/browserPaths.js';

process.versions.gluon = '0.14.0-alpha.0';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

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()).includes(path);
};

const getBrowserPath = async browser => {
for (const path of Array.isArray(browserPaths[browser]) ? browserPaths[browser] : [ browserPaths[browser] ]) {
// log('checking if ' + browser + ' exists:', path, await exists(path));

if (await exists(path)) return path;
}

return null;
};

const findBrowserPath = async (forceBrowser, forceEngine) => {
if (forceBrowser) return [ await getBrowserPath(forceBrowser), forceBrowser ];

for (const x in browserPaths) {
if (process.argv.includes('--' + x) || process.argv.includes('--' + x.split('_')[0])) return [ await getBrowserPath(x), x ];
}

if (process.argv.some(x => x.startsWith('--browser='))) {
const given = process.argv.find(x => x.startsWith('--browser='));
const split = given.slice(given.indexOf('=') + 1).split(',');
const name = split[0];
const path = split.slice(1).join(',');

return [ path || await getBrowserPath(name), name ];
}

for (const name in browserPaths) {
const path = await getBrowserPath(name);

if (path) {
if (forceEngine && getBrowserType(name) !== forceEngine) continue; // if forceEngine is set, ignore path if it isn't

return [ path, name ];
}
}

return null;
};

const getFriendlyName = whichBrowser => whichBrowser[0].toUpperCase() + whichBrowser.slice(1).replace(/[a-z]_[a-z]/g, _ => _[0] + ' ' + _[2].toUpperCase());

const ranJsDir = !process.argv[1] ? __dirname : (extname(process.argv[1]) ? dirname(process.argv[1]) : process.argv[1]);
const getDataPath = browser => join(ranJsDir, 'gluon_data', browser);

const getBrowserType = name => { // todo: not need this
if (name.startsWith('firefox') ||
[ 'librewolf', 'waterfox' ].includes(name)) return 'firefox';

return 'chromium';
};

const portRange = [ 10000, 60000 ];
const generatePort = () => (Math.floor(Math.random() * (portRange[1] - portRange[0] + 1)) + portRange[0]);

Expand Down
65 changes: 63 additions & 2 deletions src/lib/browserPaths.js
@@ -1,4 +1,5 @@
import { join } from 'path';
import { join, delimiter, sep } from 'path';
import { access, readdir } from 'fs/promises';

const browserPaths = ({
win32: process.platform === 'win32' && { // windows paths are automatically prepended with program files, program files (x86), and local appdata if a string, see below
Expand Down Expand Up @@ -99,4 +100,64 @@ if (process.platform === 'win32') { // windows: automatically generate env-based
}
}

export default browserPaths;
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()).includes(path);
};

const getBrowserPath = async browser => {
for (const path of Array.isArray(browserPaths[browser]) ? browserPaths[browser] : [ browserPaths[browser] ]) {
if (await exists(path)) return path;
}

return null;
};

export const getBrowserType = name => { // todo: not need this
if (name.startsWith('firefox') ||
[ 'librewolf', 'waterfox' ].includes(name)) return 'firefox';

return 'chromium';
};

export const findBrowserPath = async (forceBrowser, forceEngine) => {
if (forceBrowser) return [ await getBrowserPath(forceBrowser), forceBrowser ];

for (const x in browserPaths) {
if (process.argv.includes('--' + x) || process.argv.includes('--' + x.split('_')[0])) return [ await getBrowserPath(x), x ];
}

if (process.argv.some(x => x.startsWith('--browser='))) {
const given = process.argv.find(x => x.startsWith('--browser='));
const split = given.slice(given.indexOf('=') + 1).split(',');
const name = split[0];
const path = split.slice(1).join(',');

return [ path || await getBrowserPath(name), name ];
}

for (const name in browserPaths) {
const path = await getBrowserPath(name);

if (path) {
if (forceEngine && getBrowserType(name) !== forceEngine) continue; // if forceEngine is set, ignore path if it isn't

return [ path, name ];
}
}

return null;
};

0 comments on commit 720e73d

Please sign in to comment.