{"version":3,"file":"chrome-paths.mjs","sources":["webpack://@agent-infra/browser-finder/./src/chrome-paths.ts"],"sourcesContent":["/**\n * The following code is modified based on\n * https://github.com/shirshak55/edge-paths/blob/master/index.ts\n *\n * MIT Licensed\n * Copyright (c) 2020 Shirshak\n * https://github.com/shirshak55/edge-paths/blob/master/LICENSE\n */\n\n/**\n * Q: Why not use [find-chrome-bin](https://github.com/mbalabash/find-chrome-bin) or [chrome-finder](https://github.com/gwuhaolin/chrome-finder)?\n *\n * A: The `find-chrome-bin` or `chrome-finder` libraries execute `lsregister -dump` under Darwin (macOS),\n *    which is a time-consuming operation (taking up to 6 seconds on my computer!).\n *    Since this process is performed during the app's startup, such a delay is unacceptable.\n */\nimport { existsSync } from 'fs';\nimport { sep, join } from 'path';\nimport which from 'which';\n\nconst platform = process.platform;\n\nfunction getChromeOnLinux(\n  list: (\n    | 'google-chrome'\n    | 'google-chrome-stable'\n    | 'google-chrome-beta'\n    | 'google-chrome-dev'\n    | 'chromium-browser'\n    | 'chromium'\n  )[],\n): string | null {\n  // TODO: scan desktop installation folders, the `grep` operation can be somewhat time-consuming.\n  // https://github.com/mbalabash/find-chrome-bin/blob/main/src/linux/index.js\n  try {\n    for (const name of list) {\n      const path = which.sync(name);\n      return path;\n    }\n  } catch (e) {}\n\n  return null;\n}\n\nfunction getChromeOnWindows(\n  name: 'Chrome' | 'Chrome Beta' | 'Chrome Dev' | 'Chrome SxS',\n): string | null {\n  const suffix = `${sep}Google${sep}${name}${sep}Application${sep}chrome.exe`;\n\n  const prefixes = [\n    process.env.LOCALAPPDATA,\n    process.env.PROGRAMFILES,\n    process.env['PROGRAMFILES(X86)'],\n  ].filter(Boolean);\n\n  for (const prefix of prefixes) {\n    const chrome = join(prefix!, suffix);\n    if (existsSync(chrome)) {\n      return chrome;\n    }\n  }\n\n  return null;\n}\n\nfunction getChromeOnDarwin(\n  name:\n    | 'Google Chrome'\n    | 'Google Chrome Beta'\n    | 'Google Chrome Dev'\n    | 'Google Chrome Canary',\n): string | null {\n  const suffix = `/Applications/${name}.app/Contents/MacOS/${name}`;\n  const prefixes = ['', process.env.HOME].filter((item) => item !== undefined);\n\n  for (const prefix of prefixes) {\n    const chromePath = join(prefix, suffix);\n    if (existsSync(chromePath)) {\n      return chromePath;\n    }\n  }\n\n  return null;\n}\n\nconst chromePaths = {\n  chrome: {\n    linux: () => getChromeOnLinux(['google-chrome-stable', 'google-chrome']),\n    darwin: () => getChromeOnDarwin('Google Chrome'),\n    win32: () => getChromeOnWindows('Chrome'),\n  },\n  beta: {\n    linux: () => getChromeOnLinux(['google-chrome-beta']),\n    darwin: () => getChromeOnDarwin('Google Chrome Beta'),\n    win32: () => getChromeOnWindows('Chrome Beta'),\n  },\n  dev: {\n    linux: () => getChromeOnLinux(['google-chrome-dev']),\n    darwin: () => getChromeOnDarwin('Google Chrome Dev'),\n    win32: () => getChromeOnWindows('Chrome Dev'),\n  },\n  canary: {\n    linux: () => getChromeOnLinux(['chromium-browser', 'chromium']),\n    darwin: () => getChromeOnDarwin('Google Chrome Canary'),\n    win32: () => getChromeOnWindows('Chrome SxS'),\n  },\n};\n\nfunction getChromePath() {\n  const chrome = chromePaths.chrome;\n\n  if (platform && Object.keys(chrome).includes(platform)) {\n    const pth = chrome[platform as keyof typeof chrome]();\n    if (pth) {\n      return pth;\n    }\n  }\n}\n\nfunction getChromeBetaPath() {\n  const beta = chromePaths.beta;\n\n  if (platform && Object.keys(beta).includes(platform)) {\n    const pth = beta[platform as keyof typeof beta]();\n    if (pth) {\n      return pth;\n    }\n  }\n}\n\nfunction getChromeDevPath() {\n  const dev = chromePaths.dev;\n\n  if (platform && Object.keys(dev).includes(platform)) {\n    const pth = dev[platform as keyof typeof dev]();\n    if (pth) {\n      return pth;\n    }\n  }\n}\n\nfunction getChromeCanaryPath() {\n  const canary = chromePaths.canary;\n\n  if (platform && Object.keys(canary).includes(platform)) {\n    const pth = canary[platform as keyof typeof canary]();\n    if (pth) {\n      return pth;\n    }\n  }\n}\n\nexport function getAnyChromeStable(): string {\n  const chrome = getChromePath();\n  if (chrome) {\n    return chrome;\n  }\n\n  const beta = getChromeBetaPath();\n  if (beta) {\n    return beta;\n  }\n\n  const dev = getChromeDevPath();\n  if (dev) {\n    return dev;\n  }\n\n  const canary = getChromeCanaryPath();\n  if (canary) {\n    return canary;\n  }\n\n  const error = new Error('Unable to find any chrome browser.');\n  error.name = 'ChromePathsError';\n  throw error;\n}\n"],"names":["platform","process","getChromeOnLinux","list","name","path","which","e","getChromeOnWindows","suffix","sep","prefixes","Boolean","prefix","chrome","join","existsSync","getChromeOnDarwin","item","undefined","chromePath","chromePaths","getChromePath","Object","pth","getChromeBetaPath","beta","getChromeDevPath","dev","getChromeCanaryPath","canary","getAnyChromeStable","error","Error"],"mappings":";;;;;;;AAoBA,MAAMA,WAAWC,QAAQ,QAAQ;AAEjC,SAASC,iBACPC,IAOG;IAIH,IAAI;QACF,KAAK,MAAMC,QAAQD,KAAM;YACvB,MAAME,OAAOC,MAAM,IAAI,CAACF;YACxB,OAAOC;QACT;IACF,EAAE,OAAOE,GAAG,CAAC;IAEb,OAAO;AACT;AAEA,SAASC,mBACPJ,IAA4D;IAE5D,MAAMK,SAAS,GAAGC,IAAI,MAAM,EAAEA,MAAMN,OAAOM,IAAI,WAAW,EAAEA,IAAI,UAAU,CAAC;IAE3E,MAAMC,WAAW;QACfV,QAAQ,GAAG,CAAC,YAAY;QACxBA,QAAQ,GAAG,CAAC,YAAY;QACxBA,QAAQ,GAAG,CAAC,oBAAoB;KACjC,CAAC,MAAM,CAACW;IAET,KAAK,MAAMC,UAAUF,SAAU;QAC7B,MAAMG,SAASC,KAAKF,QAASJ;QAC7B,IAAIO,WAAWF,SACb,OAAOA;IAEX;IAEA,OAAO;AACT;AAEA,SAASG,kBACPb,IAI0B;IAE1B,MAAMK,SAAS,CAAC,cAAc,EAAEL,KAAK,oBAAoB,EAAEA,MAAM;IACjE,MAAMO,WAAW;QAAC;QAAIV,QAAQ,GAAG,CAAC,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