mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-05 23:47:54 +08:00
try to write browser info(executablePath, browser) to last-used-browser-record for store user profile use
This commit is contained in:
+14
-5
@@ -4,7 +4,7 @@ import * as fs from 'node:fs'
|
|||||||
import type { InstalledBrowser } from '@puppeteer/browsers'
|
import type { InstalledBrowser } from '@puppeteer/browsers'
|
||||||
import { is } from '@electron-toolkit/utils'
|
import { is } from '@electron-toolkit/utils'
|
||||||
import electron from 'electron'
|
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'
|
export const EXPECT_CHROMIUM_BUILD_ID = '113.0.5672.63'
|
||||||
const cacheDir = path.join(
|
const cacheDir = path.join(
|
||||||
@@ -33,19 +33,25 @@ const getPuppeteerManagerModule = async () => {
|
|||||||
return puppeteerManager
|
return puppeteerManager
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getExpectCachedPuppeteerExecutablePath = async () => {
|
export const getExpectCachedPuppeteerExecutable = async (): Promise<BrowserInfo> => {
|
||||||
const puppeteerManager = await getPuppeteerManagerModule()
|
const puppeteerManager = await getPuppeteerManagerModule()
|
||||||
|
|
||||||
return puppeteerManager.computeExecutablePath({
|
const executablePath = puppeteerManager.computeExecutablePath({
|
||||||
browser: puppeteerManager.Browser.CHROME,
|
browser: puppeteerManager.Browser.CHROME,
|
||||||
cacheDir,
|
cacheDir,
|
||||||
buildId: EXPECT_CHROMIUM_BUILD_ID
|
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 () => {
|
export const checkCachedPuppeteerExecutable = async () => {
|
||||||
try {
|
try {
|
||||||
const executablePath = await getExpectCachedPuppeteerExecutablePath()
|
const executablePath = (await getExpectCachedPuppeteerExecutable()).executablePath
|
||||||
return fs.existsSync(executablePath)
|
return fs.existsSync(executablePath)
|
||||||
} catch {
|
} catch {
|
||||||
// should limit [ERR_MODULE_NOT_FOUND]
|
// should limit [ERR_MODULE_NOT_FOUND]
|
||||||
@@ -86,7 +92,10 @@ const checkAndDownloadPuppeteerExecutable = async (
|
|||||||
})
|
})
|
||||||
).find((it) => it.buildId === EXPECT_CHROMIUM_BUILD_ID)!
|
).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
|
return installedBrowser
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -7,14 +7,15 @@ import { EXPECT_CHROMIUM_BUILD_ID } from './check-and-download-puppeteer-executa
|
|||||||
import {
|
import {
|
||||||
getExecutableFileVersion
|
getExecutableFileVersion
|
||||||
} from '@geekgeekrun/utils/windows-only/file.mjs'
|
} from '@geekgeekrun/utils/windows-only/file.mjs'
|
||||||
|
import { BrowserInfo } from './history-utils'
|
||||||
|
|
||||||
export default async function findAndLocateExistedChromiumExecutable() {
|
export default async function findAndLocateExistedChromiumExecutable(): Promise<BrowserInfo> {
|
||||||
const exceptChromiumMainVersion = Number(EXPECT_CHROMIUM_BUILD_ID.split('.')[0]) + 1
|
const exceptChromiumMainVersion = Number(EXPECT_CHROMIUM_BUILD_ID.split('.')[0])
|
||||||
// For windows, try to find Edge(chromium)
|
// For windows, try to find Edge(chromium)
|
||||||
if (os.platform() === 'win32') {
|
if (os.platform() === 'win32') {
|
||||||
// TODO: handle windows
|
// TODO: handle windows
|
||||||
const edgeExecutableLocation = path.join(
|
const edgeExecutableLocation = path.join(
|
||||||
process.env['ProgramFiles(x86)'],
|
process.env['ProgramFiles(x86)']!,
|
||||||
'Microsoft/Edge/Application',
|
'Microsoft/Edge/Application',
|
||||||
'msedge.exe'
|
'msedge.exe'
|
||||||
)
|
)
|
||||||
@@ -26,7 +27,7 @@ export default async function findAndLocateExistedChromiumExecutable() {
|
|||||||
const mainVersion = Number(version.split('.')[0])
|
const mainVersion = Number(version.split('.')[0])
|
||||||
if ( mainVersion >= exceptChromiumMainVersion) {
|
if ( mainVersion >= exceptChromiumMainVersion) {
|
||||||
return {
|
return {
|
||||||
path: edgeExecutableLocation,
|
executablePath: edgeExecutableLocation,
|
||||||
browser: `Edge ${version}`
|
browser: `Edge ${version}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,7 +60,7 @@ export default async function findAndLocateExistedChromiumExecutable() {
|
|||||||
throw new Error('NO_EXPECT_CHROMIUM_FOUND')
|
throw new Error('NO_EXPECT_CHROMIUM_FOUND')
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
path: targetBrowser.executablePath,
|
executablePath: targetBrowser.executablePath,
|
||||||
browser: targetBrowser.browser
|
browser: targetBrowser.browser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ import * as os from 'os'
|
|||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as fsPromise from 'fs/promises'
|
import * as fsPromise from 'fs/promises'
|
||||||
|
|
||||||
|
export interface BrowserInfo {
|
||||||
|
browser: string;
|
||||||
|
executablePath: string;
|
||||||
|
}
|
||||||
|
|
||||||
const runtimeFolderPath = path.join(os.homedir(), '.geekgeekrun')
|
const runtimeFolderPath = path.join(os.homedir(), '.geekgeekrun')
|
||||||
export const lastUsedBrowserRecordFilePath = path.join(
|
export const lastUsedBrowserRecordFilePath = path.join(
|
||||||
runtimeFolderPath,
|
runtimeFolderPath,
|
||||||
@@ -16,29 +21,38 @@ export const lastUsedBrowserRecordFilePath = path.join(
|
|||||||
* else remove its history
|
* else remove its history
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const getLastUsedAndAvailableBrowserPath = async (): Promise<string | null> => {
|
export const getLastUsedAndAvailableBrowser = async (): Promise<BrowserInfo | null> => {
|
||||||
if (!fs.existsSync(lastUsedBrowserRecordFilePath)) {
|
if (!fs.existsSync(lastUsedBrowserRecordFilePath)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const fileContent = (await fsPromise.readFile(lastUsedBrowserRecordFilePath)).toString()
|
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()
|
await removeLastUsedAndAvailableBrowserPath()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return fileContent
|
return {
|
||||||
|
executablePath: path,
|
||||||
|
browser
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
await removeLastUsedAndAvailableBrowserPath()
|
await removeLastUsedAndAvailableBrowserPath()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const saveLastUsedAndAvailableBrowserPath = async (pathToBrowser: string) => {
|
export const saveLastUsedAndAvailableBrowserInfo = async (browserInfo: BrowserInfo) => {
|
||||||
try {
|
try {
|
||||||
if (!fs.existsSync(runtimeFolderPath)) {
|
if (!fs.existsSync(runtimeFolderPath)) {
|
||||||
await fsPromise.mkdir(runtimeFolderPath)
|
await fsPromise.mkdir(runtimeFolderPath)
|
||||||
}
|
}
|
||||||
await fsPromise.writeFile(lastUsedBrowserRecordFilePath, pathToBrowser)
|
await fsPromise.writeFile(
|
||||||
|
lastUsedBrowserRecordFilePath, [
|
||||||
|
browserInfo.executablePath,
|
||||||
|
browserInfo.browser
|
||||||
|
].join('\n')
|
||||||
|
)
|
||||||
} catch {
|
} catch {
|
||||||
console.warn('lastUsedBrowserRecordFile write error')
|
console.warn('lastUsedBrowserRecordFile write error')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
import checkAndDownloadPuppeteerExecutable, {
|
import checkAndDownloadPuppeteerExecutable, {
|
||||||
checkCachedPuppeteerExecutable,
|
checkCachedPuppeteerExecutable,
|
||||||
getExpectCachedPuppeteerExecutablePath
|
getExpectCachedPuppeteerExecutable
|
||||||
} from './check-and-download-puppeteer-executable'
|
} from './check-and-download-puppeteer-executable'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import { pipeWriteRegardlessError } from '../utils/pipe'
|
import { pipeWriteRegardlessError } from '../utils/pipe'
|
||||||
import {
|
import {
|
||||||
removeLastUsedAndAvailableBrowserPath,
|
removeLastUsedAndAvailableBrowserPath,
|
||||||
getLastUsedAndAvailableBrowserPath,
|
getLastUsedAndAvailableBrowser,
|
||||||
saveLastUsedAndAvailableBrowserPath
|
saveLastUsedAndAvailableBrowserInfo,
|
||||||
|
BrowserInfo
|
||||||
} from './history-utils'
|
} from './history-utils'
|
||||||
import findAndLocateExistedChromiumExecutable from './check-and-locate-existed-chromium-executable'
|
import findAndLocateExistedChromiumExecutable from './check-and-locate-existed-chromium-executable'
|
||||||
import {
|
import {
|
||||||
@@ -19,23 +20,27 @@ export enum DOWNLOAD_ERROR_EXIT_CODE {
|
|||||||
NO_ERROR = 0,
|
NO_ERROR = 0,
|
||||||
DOWNLOAD_ERROR = 1
|
DOWNLOAD_ERROR = 1
|
||||||
}
|
}
|
||||||
export const getAnyAvailablePuppeteerExecutablePath = async (): Promise<string | null> => {
|
|
||||||
const lastUsedOnePath = await getLastUsedAndAvailableBrowserPath()
|
export const getAnyAvailablePuppeteerExecutable = async (): Promise<BrowserInfo | null> => {
|
||||||
if (lastUsedOnePath) {
|
const lastUsedOne = await getLastUsedAndAvailableBrowser()
|
||||||
return lastUsedOnePath
|
if (lastUsedOne) {
|
||||||
|
return lastUsedOne
|
||||||
}
|
}
|
||||||
// find existed browser - the one maybe actively installed by user or ship with os like Edge on windows
|
// find existed browser - the one maybe actively installed by user or ship with os like Edge on windows
|
||||||
try {
|
try {
|
||||||
const existedOnePath = (await findAndLocateExistedChromiumExecutable()).path
|
const existedOne = (await findAndLocateExistedChromiumExecutable())
|
||||||
await saveLastUsedAndAvailableBrowserPath(existedOnePath)
|
await saveLastUsedAndAvailableBrowserInfo(existedOne)
|
||||||
// save its path
|
// save its path
|
||||||
return existedOnePath
|
return existedOne
|
||||||
} catch {
|
} catch {
|
||||||
console.log('no existed browser path found')
|
console.log('no existed browser path found')
|
||||||
}
|
}
|
||||||
// find existed browser - the fallback one
|
// find existed browser - the fallback one
|
||||||
if (await checkCachedPuppeteerExecutable()) {
|
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
|
// if no one available, then return null and remove last used browser
|
||||||
|
|||||||
@@ -3,11 +3,8 @@ import { app } from 'electron'
|
|||||||
import { SyncHook, AsyncSeriesHook } from 'tapable'
|
import { SyncHook, AsyncSeriesHook } from 'tapable'
|
||||||
import { readConfigFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
|
import { readConfigFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import {
|
|
||||||
checkPuppeteerExecutable,
|
|
||||||
} from './CHECK_AND_DOWNLOAD_DEPENDENCIES/check-and-download-puppeteer-executable'
|
|
||||||
import { pipeWriteRegardlessError } from './utils/pipe'
|
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')
|
const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json')
|
||||||
|
|
||||||
@@ -53,7 +50,7 @@ export const runAutoChat = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPuppeteerExecutable = !!(await getAnyAvailablePuppeteerExecutablePath())
|
const isPuppeteerExecutable = !!(await getAnyAvailablePuppeteerExecutable())
|
||||||
if (!isPuppeteerExecutable) {
|
if (!isPuppeteerExecutable) {
|
||||||
app.exit(1)
|
app.exit(1)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { ChildProcess } from 'child_process'
|
|||||||
import * as JSONStream from 'JSONStream'
|
import * as JSONStream from 'JSONStream'
|
||||||
import {
|
import {
|
||||||
DOWNLOAD_ERROR_EXIT_CODE,
|
DOWNLOAD_ERROR_EXIT_CODE,
|
||||||
getAnyAvailablePuppeteerExecutablePath
|
getAnyAvailablePuppeteerExecutable
|
||||||
} from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES'
|
} from '../flow/CHECK_AND_DOWNLOAD_DEPENDENCIES'
|
||||||
let mainWindow: BrowserWindow | null = null
|
let mainWindow: BrowserWindow | null = null
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ export function createMainWindow(): void {
|
|||||||
const subProcessEnv = {
|
const subProcessEnv = {
|
||||||
...process.env,
|
...process.env,
|
||||||
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss',
|
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), {
|
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {
|
||||||
env: subProcessEnv,
|
env: subProcessEnv,
|
||||||
@@ -129,11 +129,11 @@ export function createMainWindow(): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.handle('check-dependencies', async () => {
|
ipcMain.handle('check-dependencies', async () => {
|
||||||
const [anyAvailablePuppeteerExecutablePath] = await Promise.all([
|
const [anyAvailablePuppeteerExecutable] = await Promise.all([
|
||||||
getAnyAvailablePuppeteerExecutablePath()
|
getAnyAvailablePuppeteerExecutable()
|
||||||
])
|
])
|
||||||
return {
|
return {
|
||||||
puppeteerExecutableAvailable: !!anyAvailablePuppeteerExecutablePath
|
puppeteerExecutableAvailable: !!anyAvailablePuppeteerExecutable
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user