try to write browser info(executablePath, browser) to last-used-browser-record for store user profile use

This commit is contained in:
geekgeekrun
2024-02-26 01:44:52 +08:00
parent d85240b94e
commit fbfae5f683
6 changed files with 62 additions and 36 deletions

View File

@@ -4,7 +4,7 @@ import * as fs from 'node:fs'
import type { InstalledBrowser } from '@puppeteer/browsers'
import { is } from '@electron-toolkit/utils'
import electron from 'electron'
import { saveLastUsedAndAvailableBrowserPath } from './history-utils'
import { saveLastUsedAndAvailableBrowserInfo, BrowserInfo } from './history-utils'
export const EXPECT_CHROMIUM_BUILD_ID = '113.0.5672.63'
const cacheDir = path.join(
@@ -33,19 +33,25 @@ const getPuppeteerManagerModule = async () => {
return puppeteerManager
}
export const getExpectCachedPuppeteerExecutablePath = async () => {
export const getExpectCachedPuppeteerExecutable = async (): Promise<BrowserInfo> => {
const puppeteerManager = await getPuppeteerManagerModule()
return puppeteerManager.computeExecutablePath({
const executablePath = puppeteerManager.computeExecutablePath({
browser: puppeteerManager.Browser.CHROME,
cacheDir,
buildId: EXPECT_CHROMIUM_BUILD_ID
})
const browser = puppeteerManager.Browser.CHROME[0].toUpperCase() + puppeteerManager.Browser.CHROME.slice(1) + ' ' + EXPECT_CHROMIUM_BUILD_ID
return {
executablePath,
browser
}
}
export const checkCachedPuppeteerExecutable = async () => {
try {
const executablePath = await getExpectCachedPuppeteerExecutablePath()
const executablePath = (await getExpectCachedPuppeteerExecutable()).executablePath
return fs.existsSync(executablePath)
} catch {
// should limit [ERR_MODULE_NOT_FOUND]
@@ -86,7 +92,10 @@ const checkAndDownloadPuppeteerExecutable = async (
})
).find((it) => it.buildId === EXPECT_CHROMIUM_BUILD_ID)!
}
await saveLastUsedAndAvailableBrowserPath(await getExpectCachedPuppeteerExecutablePath())
await saveLastUsedAndAvailableBrowserInfo({
executablePath: installedBrowser.executablePath,
browser: installedBrowser.browser[0].toUpperCase() + installedBrowser.browser.slice(1) + ' ' + EXPECT_CHROMIUM_BUILD_ID
})
return installedBrowser
}

View File

@@ -7,14 +7,15 @@ import { EXPECT_CHROMIUM_BUILD_ID } from './check-and-download-puppeteer-executa
import {
getExecutableFileVersion
} from '@geekgeekrun/utils/windows-only/file.mjs'
import { BrowserInfo } from './history-utils'
export default async function findAndLocateExistedChromiumExecutable() {
const exceptChromiumMainVersion = Number(EXPECT_CHROMIUM_BUILD_ID.split('.')[0]) + 1
export default async function findAndLocateExistedChromiumExecutable(): Promise<BrowserInfo> {
const exceptChromiumMainVersion = Number(EXPECT_CHROMIUM_BUILD_ID.split('.')[0])
// For windows, try to find Edge(chromium)
if (os.platform() === 'win32') {
// TODO: handle windows
const edgeExecutableLocation = path.join(
process.env['ProgramFiles(x86)'],
process.env['ProgramFiles(x86)']!,
'Microsoft/Edge/Application',
'msedge.exe'
)
@@ -26,7 +27,7 @@ export default async function findAndLocateExistedChromiumExecutable() {
const mainVersion = Number(version.split('.')[0])
if ( mainVersion >= exceptChromiumMainVersion) {
return {
path: edgeExecutableLocation,
executablePath: edgeExecutableLocation,
browser: `Edge ${version}`
}
}
@@ -59,7 +60,7 @@ export default async function findAndLocateExistedChromiumExecutable() {
throw new Error('NO_EXPECT_CHROMIUM_FOUND')
}
return {
path: targetBrowser.executablePath,
executablePath: targetBrowser.executablePath,
browser: targetBrowser.browser
}
}

View File

@@ -3,6 +3,11 @@ import * as os from 'os'
import * as fs from 'fs'
import * as fsPromise from 'fs/promises'
export interface BrowserInfo {
browser: string;
executablePath: string;
}
const runtimeFolderPath = path.join(os.homedir(), '.geekgeekrun')
export const lastUsedBrowserRecordFilePath = path.join(
runtimeFolderPath,
@@ -16,29 +21,38 @@ export const lastUsedBrowserRecordFilePath = path.join(
* else remove its history
* @returns
*/
export const getLastUsedAndAvailableBrowserPath = async (): Promise<string | null> => {
export const getLastUsedAndAvailableBrowser = async (): Promise<BrowserInfo | null> => {
if (!fs.existsSync(lastUsedBrowserRecordFilePath)) {
return null
}
try {
const fileContent = (await fsPromise.readFile(lastUsedBrowserRecordFilePath)).toString()
if (!fileContent || !fs.existsSync(fileContent)) {
const [path, browser] = fileContent.split('\n').map(it => it.trim())
if (!path || !fs.existsSync(path)) {
await removeLastUsedAndAvailableBrowserPath()
return null
}
return fileContent
return {
executablePath: path,
browser
}
} catch {
await removeLastUsedAndAvailableBrowserPath()
return null
}
}
export const saveLastUsedAndAvailableBrowserPath = async (pathToBrowser: string) => {
export const saveLastUsedAndAvailableBrowserInfo = async (browserInfo: BrowserInfo) => {
try {
if (!fs.existsSync(runtimeFolderPath)) {
await fsPromise.mkdir(runtimeFolderPath)
}
await fsPromise.writeFile(lastUsedBrowserRecordFilePath, pathToBrowser)
await fsPromise.writeFile(
lastUsedBrowserRecordFilePath, [
browserInfo.executablePath,
browserInfo.browser
].join('\n')
)
} catch {
console.warn('lastUsedBrowserRecordFile write error')
}

View File

@@ -1,14 +1,15 @@
import { app } from 'electron'
import checkAndDownloadPuppeteerExecutable, {
checkCachedPuppeteerExecutable,
getExpectCachedPuppeteerExecutablePath
getExpectCachedPuppeteerExecutable
} from './check-and-download-puppeteer-executable'
import * as fs from 'fs'
import { pipeWriteRegardlessError } from '../utils/pipe'
import {
removeLastUsedAndAvailableBrowserPath,
getLastUsedAndAvailableBrowserPath,
saveLastUsedAndAvailableBrowserPath
getLastUsedAndAvailableBrowser,
saveLastUsedAndAvailableBrowserInfo,
BrowserInfo
} from './history-utils'
import findAndLocateExistedChromiumExecutable from './check-and-locate-existed-chromium-executable'
import {
@@ -19,23 +20,27 @@ export enum DOWNLOAD_ERROR_EXIT_CODE {
NO_ERROR = 0,
DOWNLOAD_ERROR = 1
}
export const getAnyAvailablePuppeteerExecutablePath = async (): Promise<string | null> => {
const lastUsedOnePath = await getLastUsedAndAvailableBrowserPath()
if (lastUsedOnePath) {
return lastUsedOnePath
export const getAnyAvailablePuppeteerExecutable = async (): Promise<BrowserInfo | null> => {
const lastUsedOne = await getLastUsedAndAvailableBrowser()
if (lastUsedOne) {
return lastUsedOne
}
// find existed browser - the one maybe actively installed by user or ship with os like Edge on windows
try {
const existedOnePath = (await findAndLocateExistedChromiumExecutable()).path
await saveLastUsedAndAvailableBrowserPath(existedOnePath)
const existedOne = (await findAndLocateExistedChromiumExecutable())
await saveLastUsedAndAvailableBrowserInfo(existedOne)
// save its path
return existedOnePath
return existedOne
} catch {
console.log('no existed browser path found')
}
// find existed browser - the fallback one
if (await checkCachedPuppeteerExecutable()) {
return await getExpectCachedPuppeteerExecutablePath()
const cachedOne = await getExpectCachedPuppeteerExecutable()
await saveLastUsedAndAvailableBrowserInfo(cachedOne)
return cachedOne
}
// if no one available, then return null and remove last used browser

View File

@@ -3,11 +3,8 @@ import { app } from 'electron'
import { SyncHook, AsyncSeriesHook } from 'tapable'
import { readConfigFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
import * as fs from 'fs'
import {
checkPuppeteerExecutable,
} from './CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer-executable'
import { pipeWriteRegardlessError } from './utils/pipe'
import { getAnyAvailablePuppeteerExecutablePath } from './CHECK_AND_DOWNLOAD_DEPENDENCIES'
import { getAnyAvailablePuppeteerExecutable } from './CHECK_AND_DOWNLOAD_DEPENDENCIES'
const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json')
@@ -53,7 +50,7 @@ export const runAutoChat = async () => {
return
}
const isPuppeteerExecutable = !!(await getAnyAvailablePuppeteerExecutablePath())
const isPuppeteerExecutable = !!(await getAnyAvailablePuppeteerExecutable())
if (!isPuppeteerExecutable) {
app.exit(1)
return

View File

@@ -12,7 +12,7 @@ import { ChildProcess } from 'child_process'
import * as JSONStream from 'JSONStream'
import {
DOWNLOAD_ERROR_EXIT_CODE,
getAnyAvailablePuppeteerExecutablePath
getAnyAvailablePuppeteerExecutable
} from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES'
let mainWindow: BrowserWindow | null = null
@@ -94,7 +94,7 @@ export function createMainWindow(): void {
const subProcessEnv = {
...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss',
PUPPETEER_EXECUTABLE_PATH: (await getAnyAvailablePuppeteerExecutablePath())!
PUPPETEER_EXECUTABLE_PATH: (await getAnyAvailablePuppeteerExecutable())!.executablePath
}
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {
env: subProcessEnv,
@@ -129,11 +129,11 @@ export function createMainWindow(): void {
})
ipcMain.handle('check-dependencies', async () => {
const [anyAvailablePuppeteerExecutablePath] = await Promise.all([
getAnyAvailablePuppeteerExecutablePath()
const [anyAvailablePuppeteerExecutable] = await Promise.all([
getAnyAvailablePuppeteerExecutable()
])
return {
puppeteerExecutableAvailable: !!anyAvailablePuppeteerExecutablePath
puppeteerExecutableAvailable: !!anyAvailablePuppeteerExecutable
}
})