Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

2.6 KB
Raw Blame
#!/usr/bin/env node

// Detects the best available Chromium-based browser on the system.
// Returns a Playwright channel name ('msedge', 'chrome', 'chromium').
// Used by the Playwright MCP launcher.

const { execFileSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');

function exists(filePath) {
  try { return fs.existsSync(filePath); } catch { return false; }
}

function whichExists(cmd) {
  try {
    // execFileSync (no shell) avoids interpolating `cmd` into a shell command
    // string. Call sites pass fixed literals today, but this removes the
    // injection-prone `which ${cmd}` pattern outright.
    execFileSync('which', [cmd], { stdio: 'ignore' });
    return true;
  } catch {
    return false;
  }
}

function detectBrowser() {
  const platform = os.platform();

  if (platform === 'win32') {
    const localAppData = process.env.LOCALAPPDATA || '';
    const programFiles = process.env.PROGRAMFILES || '';
    const programFilesX86 = process.env['PROGRAMFILES(X86)'] || '';

    // Edge — pre-installed on all Windows 10/11 machines
    const edgePaths = [
      path.join(programFilesX86, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
      path.join(programFiles, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
      path.join(localAppData, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
    ];
    for (const p of edgePaths) {
      if (exists(p)) return 'msedge';
    }

    // Chrome
    const chromePaths = [
      path.join(localAppData, 'Google', 'Chrome', 'Application', 'chrome.exe'),
      path.join(programFiles, 'Google', 'Chrome', 'Application', 'chrome.exe'),
      path.join(programFilesX86, 'Google', 'Chrome', 'Application', 'chrome.exe'),
    ];
    for (const p of chromePaths) {
      if (exists(p)) return 'chrome';
    }
  } else if (platform === 'darwin') {
    // macOS
    if (exists('/Applications/Google Chrome.app')) return 'chrome';
    if (exists('/Applications/Microsoft Edge.app')) return 'msedge';
  } else {
    // Linux
    if (whichExists('google-chrome')) return 'chrome';
    if (whichExists('google-chrome-stable')) return 'chrome';
    if (whichExists('microsoft-edge')) return 'msedge';
    if (whichExists('microsoft-edge-stable')) return 'msedge';
    if (whichExists('chromium-browser')) return 'chromium';
    if (whichExists('chromium')) return 'chromium';
  }

  // Fallback: Playwright's bundled Chromium (requires `npx playwright install chromium`)
  return 'chromium';
}

module.exports = { detectBrowser };