make edge on windows can be used as puppeteer executable

This commit is contained in:
geekgeekrun
2024-02-25 19:35:49 +08:00
parent 3d58571c35
commit d85240b94e
3 changed files with 68 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ import { is } from '@electron-toolkit/utils'
import electron from 'electron'
import { saveLastUsedAndAvailableBrowserPath } from './history-utils'
const expectBuildId = process.env.EXPECT_CHROME_FOR_PUPPETEER_BUILD_ID || '113.0.5672.63'
export const EXPECT_CHROMIUM_BUILD_ID = '113.0.5672.63'
const cacheDir = path.join(
os.homedir(),
'.geekgeekrun',
@@ -39,7 +39,7 @@ export const getExpectCachedPuppeteerExecutablePath = async () => {
return puppeteerManager.computeExecutablePath({
browser: puppeteerManager.Browser.CHROME,
cacheDir,
buildId: expectBuildId
buildId: EXPECT_CHROMIUM_BUILD_ID
})
}
@@ -70,13 +70,13 @@ const checkAndDownloadPuppeteerExecutable = async (
// maybe the exist installation is broken.
await puppeteerManager.uninstall({
cacheDir,
buildId: expectBuildId,
buildId: EXPECT_CHROMIUM_BUILD_ID,
browser: puppeteerManager.Browser.CHROME
})
installedBrowser = await puppeteerManager.install({
browser: puppeteerManager.Browser.CHROME,
cacheDir,
buildId: expectBuildId,
buildId: EXPECT_CHROMIUM_BUILD_ID,
downloadProgressCallback: options.downloadProgressCallback
})
} else {
@@ -84,7 +84,7 @@ const checkAndDownloadPuppeteerExecutable = async (
await puppeteerManager.getInstalledBrowsers({
cacheDir
})
).find((it) => it.buildId === expectBuildId)!
).find((it) => it.buildId === EXPECT_CHROMIUM_BUILD_ID)!
}
await saveLastUsedAndAvailableBrowserPath(await getExpectCachedPuppeteerExecutablePath())

View File

@@ -1,9 +1,42 @@
import { is } from '@electron-toolkit/utils'
import electron from 'electron'
import * as os from 'node:os'
import * as fs from 'node:fs'
import path from 'node:path'
import { EXPECT_CHROMIUM_BUILD_ID } from './check-and-download-puppeteer-executable'
import {
getExecutableFileVersion
} from '@geekgeekrun/utils/windows-only/file.mjs'
export default async function findAndLocateExistedChromiumExecutable() {
const exceptChromiumMainVersion = Number(EXPECT_CHROMIUM_BUILD_ID.split('.')[0]) + 1
// For windows, try to find Edge(chromium)
if (os.platform() === 'win32') {
// TODO: handle windows
const edgeExecutableLocation = path.join(
process.env['ProgramFiles(x86)'],
'Microsoft/Edge/Application',
'msedge.exe'
)
if (
fs.existsSync(edgeExecutableLocation)
) {
try {
const version = await getExecutableFileVersion(edgeExecutableLocation)
const mainVersion = Number(version.split('.')[0])
if ( mainVersion >= exceptChromiumMainVersion) {
return {
path: edgeExecutableLocation,
browser: `Edge ${version}`
}
}
} catch(err) {
console.log(err)
}
}
}
// For other, use findChrome
let findChrome: typeof import('find-chrome-bin').findChrome
if (is.dev) {
findChrome = (await import('find-chrome-bin')).findChrome
@@ -19,16 +52,14 @@ export default async function findAndLocateExistedChromiumExecutable() {
)
).findChromeBin.findChrome
}
// For windows, try to find Edge(chromium)
if (os.platform() === 'win32') {
// TODO: handle windows
}
// For other, use findChrome
const targetBrowser = await findChrome({})
const targetBrowser = await findChrome({
min: exceptChromiumMainVersion
})
if (!targetBrowser?.executablePath) {
throw new Error('NO_EXPECT_CHROMIUM_FOUND')
}
return {
path: targetBrowser.executablePath
path: targetBrowser.executablePath,
browser: targetBrowser.browser
}
}

View File

@@ -0,0 +1,25 @@
import * as childProcess from 'node:child_process'
export const getExecutableFileVersion = async (p) => {
const path = await import('node:path/win32')
p = path.resolve(p).replaceAll('\\', '\\\\')
return new Promise(
(resolve, reject) => {
childProcess.exec(
`wmic datafile where Name="${
p
}" get Version /format:list`,
(err, stdout) => {
if (err) {
console.log(err)
reject(err)
} else {
const lines = stdout.trim().split('\n').map(it => it.split('='))
const versionLine = lines.find(ln => ln[0] === 'Version')
resolve(versionLine?.[1])
}
}
)
}
)
}