Files
git.stella-ops.org/src/Web/StellaOps.Web/scripts/chrome-path.js
StellaOps Bot 564df71bfb
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
up
2025-12-13 00:20:26 +02:00

172 lines
4.5 KiB
JavaScript

const { existsSync, readdirSync, statSync } = require('fs');
const { join } = require('path');
const linuxArchivePath = ['.cache', 'chromium', 'chrome-linux64', 'chrome'];
const windowsArchivePath = ['.cache', 'chromium', 'chrome-win64', 'chrome.exe'];
const macArchivePath = [
'.cache',
'chromium',
'chrome-mac',
'Chromium.app',
'Contents',
'MacOS',
'Chromium'
];
function expandVersionedArchives(rootDir = join(__dirname, '..')) {
const base = join(rootDir, '.cache', 'chromium');
if (!existsSync(base)) {
return [];
}
const nestedCandidates = [];
for (const entry of readdirSync(base)) {
const nestedRoot = join(base, entry);
try {
if (!statSync(nestedRoot).isDirectory()) {
continue;
}
} catch {
continue;
}
nestedCandidates.push(
join(nestedRoot, 'chrome-linux64', 'chrome'),
join(nestedRoot, 'chrome-win64', 'chrome.exe'),
join(nestedRoot, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium')
);
}
return nestedCandidates;
}
function expandNestedArchives(rootDir = join(__dirname, '..')) {
const base = join(rootDir, '.cache', 'chromium');
if (!existsSync(base)) {
return [];
}
const maxDepth = 4;
const queue = [{ dir: base, depth: 0 }];
const candidates = [];
while (queue.length) {
const { dir, depth } = queue.shift();
let entries;
try {
entries = readdirSync(dir);
} catch {
continue;
}
for (const entry of entries) {
const nested = join(dir, entry);
let stats;
try {
stats = statSync(nested);
} catch {
continue;
}
if (!stats.isDirectory()) {
continue;
}
candidates.push(
join(nested, 'chrome-linux64', 'chrome'),
join(nested, 'chrome-win64', 'chrome.exe'),
join(nested, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium')
);
if (depth + 1 <= maxDepth) {
queue.push({ dir: nested, depth: depth + 1 });
}
}
}
return candidates;
}
function candidatePaths(rootDir = join(__dirname, '..')) {
const { env } = process;
const playwrightBase = join(rootDir, 'node_modules', 'playwright-core', '.local-browsers');
const homePlaywrightBase = env.HOME ? join(env.HOME, '.cache', 'ms-playwright') : null;
let playwrightChromium = [];
try {
if (existsSync(playwrightBase)) {
playwrightChromium = readdirSync(playwrightBase)
.filter((d) => d.startsWith('chromium-'))
.map((d) => join(playwrightBase, d, 'chrome-linux', 'chrome'))
.concat(
readdirSync(playwrightBase)
.filter((d) => d.startsWith('chromium-'))
.map((d) => join(playwrightBase, d, 'chrome-win', 'chrome.exe')),
readdirSync(playwrightBase)
.filter((d) => d.startsWith('chromium-'))
.map((d) => join(playwrightBase, d, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'))
);
}
} catch {
playwrightChromium = [];
}
let homeChromium = [];
try {
if (homePlaywrightBase && existsSync(homePlaywrightBase)) {
homeChromium = readdirSync(homePlaywrightBase)
.filter((d) => d.startsWith('chromium'))
.flatMap((d) => [
join(homePlaywrightBase, d, 'chrome-linux', 'chrome'),
join(homePlaywrightBase, d, 'chrome-win', 'chrome.exe'),
join(homePlaywrightBase, d, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium'),
]);
}
} catch {
homeChromium = [];
}
const baseCandidates = [
env.STELLAOPS_CHROMIUM_BIN,
env.CHROME_BIN,
env.PUPPETEER_EXECUTABLE_PATH,
...playwrightChromium,
...homeChromium,
'/usr/bin/chromium-browser',
'/usr/bin/chromium',
'/usr/bin/google-chrome',
'/usr/bin/google-chrome-stable',
join(rootDir, ...linuxArchivePath),
join(rootDir, ...windowsArchivePath),
join(rootDir, ...macArchivePath),
...expandVersionedArchives(rootDir),
...expandNestedArchives(rootDir)
];
const seen = new Set();
return baseCandidates
.filter(Boolean)
.filter((candidate) => {
if (seen.has(candidate)) {
return false;
}
seen.add(candidate);
return true;
});
}
function resolveChromeBinary(rootDir = join(__dirname, '..')) {
for (const candidate of candidatePaths(rootDir)) {
if (existsSync(candidate)) {
return candidate;
}
}
return null;
}
module.exports = {
candidatePaths,
resolveChromeBinary
};