enhance logic to check if puppeteer is exist in user home runtime folder - use stdio pipe

This commit is contained in:
bossgeekgo
2024-02-14 18:24:38 +08:00
parent 1854059761
commit bd572bc6c1
3 changed files with 84 additions and 26 deletions

View File

@@ -1,10 +1,9 @@
import DingtalkPlugin from '@bossgeekgo/dingtalk-plugin/index.mjs'
import { app } from 'electron'
import {
SyncHook,
AsyncSeriesHook
} from 'tapable'
import { SyncHook, AsyncSeriesHook } from 'tapable'
import { readConfigFile } from '@bossgeekgo/geek-auto-start-chat-with-boss/runtime-file-utils.mjs'
import * as net from 'net'
const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json')
const initPlugins = (hooks) => {
@@ -12,10 +11,31 @@ const initPlugins = (hooks) => {
}
export const runAutoChat = async () => {
let pipe: null | net.Socket = null
try {
pipe = new net.Socket({ fd: 3 })
} catch {
console.warn('pipe is not available')
}
pipe?.write(
JSON.stringify({
type: 'INITIALIZE_PUPPETEER'
})
)
try {
await (await import('@bossgeekgo/geek-auto-start-chat-with-boss/index.mjs')).initPuppeteer()
pipe?.write(
JSON.stringify({
type: 'PUPPETEER_INITIALIZE_SUCCESSFULLY'
})
)
} catch {
console.error(new Error('PUPPETEER_MAY_NOT_INSTALLED'))
pipe?.write(
JSON.stringify({
type: 'PUPPETEER_MAY_NOT_INSTALLED'
})
)
app.exit(1)
return
}
@@ -32,14 +52,21 @@ export const runAutoChat = async () => {
errorEncounter: new SyncHook(['errorInfo'])
}
initPlugins(hooks)
pipe?.write(
JSON.stringify({
type: 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED' //geek-auto-start-chat-with-boss-started
})
)
while (true) {
try {
await mainLoop(hooks)
} catch (err) {
console.log(err)
if(err instanceof Error && err.message.includes('ERR_MODULE_NOT_FOUND')) {
throw err
}
// if(err instanceof Error && err.message.includes('ERR_MODULE_NOT_FOUND')) {
// throw err
// } else {
void err
// }
}
}
}

View File

@@ -85,30 +85,41 @@ export function createMainWindow(): void {
if (subProcessOfPuppeteer) {
return
}
try {
await import('@bossgeekgo/geek-auto-start-chat-with-boss/index.mjs')
} catch (err) {
console.log(err) // TODO: what's the error?
throw new Error('PUPPETEER_MAY_NOT_INSTALLED')
}
console.log(process)
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {
env: {
...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss'
// PUPPETEER_EXECUTABLE_PATH: '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'
}
},
stdio: [null, null, null, 'pipe']
})
ipcMain.emit('geek-auto-start-chat-with-boss-started')
subProcessOfPuppeteer.once('exit', () => {
mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopped')
subProcessOfPuppeteer = null
})
console.log(subProcessOfPuppeteer)
return new Promise((resolve) => {
subProcessOfPuppeteer!.stdio[3]!.on('data', (raw) => {
const data = JSON.parse(raw.toString())
if (data.type === 'PUPPETEER_MAY_NOT_INSTALLED') {
resolve(data)
}
if (data.type === 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED') {
resolve(data)
}
})
})
// TODO:
})
ipcMain.handle('stop-geek-auto-start-chat-with-boss', async () => {
mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopping')
subProcessOfPuppeteer?.kill('SIGINT')
})
ipcMain.on('open-project-homepage-on-github', () => {
shell.openExternal(`https://github.com/bossgeekgo`, {
activate: true
})
})
}

View File

@@ -33,8 +33,8 @@
<script setup lang="ts">
import { ref } from 'vue'
import JSON5 from 'json5'
import { ElForm, ElMessage } from 'element-plus'
import router from '../../router/index';
import { ElForm, ElMessage, ElMessageBox } from 'element-plus'
import router from '../../router/index'
const formContent = ref({
bossZhipinCookies: '',
@@ -52,11 +52,11 @@ electron.ipcRenderer.invoke('fetch-config-file-content').then((res) => {
const formRules = {
bossZhipinCookies: [
{
required: true,
required: true
},
{
trigger: 'blur',
validator (rule, val, cb) {
validator(rule, val, cb) {
let arr
try {
arr = JSON5.parse(val)
@@ -78,13 +78,29 @@ const formRef = ref<InstanceType<typeof ElForm>>()
const handleSubmit = async () => {
await formRef.value!.validate()
await electron.ipcRenderer.invoke('save-config-file-from-ui', JSON.stringify(formContent.value))
try {
await electron.ipcRenderer.invoke('run-geek-auto-start-chat-with-boss', JSON.stringify(formContent.value))
} catch (err) {
console.log(err)
return
const res = await electron.ipcRenderer.invoke(
'run-geek-auto-start-chat-with-boss',
JSON.stringify(formContent.value)
)
if (res.type === 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED') {
router.replace('/geekAutoStartChatWithBoss/runningStatus')
} else if (res.type === 'PUPPETEER_MAY_NOT_INSTALLED') {
ElMessageBox.confirm(
'Some core components is broken, please reinstall this program. Will you go to the download page?',
'Error',
{
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'error'
}
)
.then(() => {
electron.ipcRenderer.emit('open-project-homepage-on-github')
})
.catch(() => {})
return
}
router.replace('/geekAutoStartChatWithBoss/runningStatus')
}
const handleSave = async () => {
await formRef.value!.validate()
@@ -93,7 +109,11 @@ const handleSave = async () => {
}
const handleExpectCompaniesInputBlur = (event) => {
event.target.value = (event.target?.value ?? '').split(/,|/).map(it => it.trim()).filter(Boolean).join(',')
event.target.value = (event.target?.value ?? '')
.split(/,|/)
.map((it) => it.trim())
.filter(Boolean)
.join(',')
}
</script>