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
@@ -1,10 +1,9 @@
import DingtalkPlugin from '@bossgeekgo/dingtalk-plugin/index.mjs' import DingtalkPlugin from '@bossgeekgo/dingtalk-plugin/index.mjs'
import { app } from 'electron' import { app } from 'electron'
import { import { SyncHook, AsyncSeriesHook } from 'tapable'
SyncHook,
AsyncSeriesHook
} from 'tapable'
import { readConfigFile } from '@bossgeekgo/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' 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 { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json')
const initPlugins = (hooks) => { const initPlugins = (hooks) => {
@@ -12,10 +11,31 @@ const initPlugins = (hooks) => {
} }
export const runAutoChat = async () => { 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 { try {
await (await import('@bossgeekgo/geek-auto-start-chat-with-boss/index.mjs')).initPuppeteer() await (await import('@bossgeekgo/geek-auto-start-chat-with-boss/index.mjs')).initPuppeteer()
pipe?.write(
JSON.stringify({
type: 'PUPPETEER_INITIALIZE_SUCCESSFULLY'
})
)
} catch { } catch {
console.error(new Error('PUPPETEER_MAY_NOT_INSTALLED')) console.error(new Error('PUPPETEER_MAY_NOT_INSTALLED'))
pipe?.write(
JSON.stringify({
type: 'PUPPETEER_MAY_NOT_INSTALLED'
})
)
app.exit(1) app.exit(1)
return return
} }
@@ -32,14 +52,21 @@ export const runAutoChat = async () => {
errorEncounter: new SyncHook(['errorInfo']) errorEncounter: new SyncHook(['errorInfo'])
} }
initPlugins(hooks) initPlugins(hooks)
pipe?.write(
JSON.stringify({
type: 'GEEK_AUTO_START_CHAT_WITH_BOSS_STARTED' //geek-auto-start-chat-with-boss-started
})
)
while (true) { while (true) {
try { try {
await mainLoop(hooks) await mainLoop(hooks)
} catch (err) { } catch (err) {
console.log(err) console.log(err)
if(err instanceof Error && err.message.includes('ERR_MODULE_NOT_FOUND')) { // if(err instanceof Error && err.message.includes('ERR_MODULE_NOT_FOUND')) {
throw err // throw err
} // } else {
void err
// }
} }
} }
} }
+19 -8
View File
@@ -85,30 +85,41 @@ export function createMainWindow(): void {
if (subProcessOfPuppeteer) { if (subProcessOfPuppeteer) {
return 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) console.log(process)
subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), { subProcessOfPuppeteer = childProcess.spawn(process.argv[0], process.argv.slice(1), {
env: { env: {
...process.env, ...process.env,
MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss' MAIN_BOSSGEEKGO_UI_RUN_MODE: 'geekAutoStartWithBoss'
// PUPPETEER_EXECUTABLE_PATH: '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge' // 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', () => { subProcessOfPuppeteer.once('exit', () => {
mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopped') mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopped')
subProcessOfPuppeteer = null subProcessOfPuppeteer = null
}) })
console.log(subProcessOfPuppeteer) 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 () => { ipcMain.handle('stop-geek-auto-start-chat-with-boss', async () => {
mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopping') mainWindow.webContents.send('geek-auto-start-chat-with-boss-stopping')
subProcessOfPuppeteer?.kill('SIGINT') subProcessOfPuppeteer?.kill('SIGINT')
}) })
ipcMain.on('open-project-homepage-on-github', () => {
shell.openExternal(`https://github.com/bossgeekgo`, {
activate: true
})
})
} }
@@ -33,8 +33,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { ref } from 'vue'
import JSON5 from 'json5' import JSON5 from 'json5'
import { ElForm, ElMessage } from 'element-plus' import { ElForm, ElMessage, ElMessageBox } from 'element-plus'
import router from '../../router/index'; import router from '../../router/index'
const formContent = ref({ const formContent = ref({
bossZhipinCookies: '', bossZhipinCookies: '',
@@ -52,11 +52,11 @@ electron.ipcRenderer.invoke('fetch-config-file-content').then((res) => {
const formRules = { const formRules = {
bossZhipinCookies: [ bossZhipinCookies: [
{ {
required: true, required: true
}, },
{ {
trigger: 'blur', trigger: 'blur',
validator (rule, val, cb) { validator(rule, val, cb) {
let arr let arr
try { try {
arr = JSON5.parse(val) arr = JSON5.parse(val)
@@ -78,13 +78,29 @@ const formRef = ref<InstanceType<typeof ElForm>>()
const handleSubmit = async () => { const handleSubmit = async () => {
await formRef.value!.validate() await formRef.value!.validate()
await electron.ipcRenderer.invoke('save-config-file-from-ui', JSON.stringify(formContent.value)) await electron.ipcRenderer.invoke('save-config-file-from-ui', JSON.stringify(formContent.value))
try { const res = await electron.ipcRenderer.invoke(
await electron.ipcRenderer.invoke('run-geek-auto-start-chat-with-boss', JSON.stringify(formContent.value)) 'run-geek-auto-start-chat-with-boss',
} catch (err) { JSON.stringify(formContent.value)
console.log(err) )
return
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 () => { const handleSave = async () => {
await formRef.value!.validate() await formRef.value!.validate()
@@ -93,7 +109,11 @@ const handleSave = async () => {
} }
const handleExpectCompaniesInputBlur = (event) => { 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> </script>