From 5d80d3a80dda1e1c8d175d7934f7a29af1eab3bf Mon Sep 17 00:00:00 2001 From: bossgeekgo Date: Mon, 12 Feb 2024 10:05:02 +0800 Subject: [PATCH] add ops of save and read config files. migrate mainWindow to single file. --- .../runtime-file-utils.mjs | 18 ++++- packages/ui/src/main/index.ts | 41 ++-------- packages/ui/src/main/window/mainWindow.ts | 77 +++++++++++++++++++ .../GeekAutoStartChatWithBoss.vue | 55 ++++++++++++- packages/ui/src/renderer/src/router/index.ts | 4 +- 5 files changed, 151 insertions(+), 44 deletions(-) create mode 100644 packages/ui/src/main/window/mainWindow.ts diff --git a/packages/geek-auto-start-chat-with-boss/runtime-file-utils.mjs b/packages/geek-auto-start-chat-with-boss/runtime-file-utils.mjs index 4ce373f..2ab6170 100644 --- a/packages/geek-auto-start-chat-with-boss/runtime-file-utils.mjs +++ b/packages/geek-auto-start-chat-with-boss/runtime-file-utils.mjs @@ -1,4 +1,5 @@ import fs from 'node:fs' +import fsPromise from 'node:fs/promises' import path from 'node:path' import os from 'node:os' @@ -6,6 +7,8 @@ import defaultDingtalkConf from './default-config-file/dingtalk.json' assert {ty import defaultBossConf from './default-config-file/boss.json' assert {type: 'json'} import defaultTargetCompanyListConf from './default-config-file/target-company-list.json' assert {type: 'json'} +export const configFileNameList = ['boss.json', 'dingtalk.json', 'target-company-list.json'] + const defaultConfigFileContentMap = { 'boss.json': JSON.stringify(defaultBossConf), 'dingtalk.json': JSON.stringify(defaultDingtalkConf), @@ -28,15 +31,13 @@ const ensureRuntimeFolderPathExist = () => { }) } -const configFolderPath = path.join( +export const configFolderPath = path.join( runtimeFolderPath, 'config' ) export const ensureConfigFileExist = () => { ensureRuntimeFolderPathExist() - ;[ - 'boss.json', 'dingtalk.json', 'target-company-list.json' - ].forEach( + ;configFileNameList.forEach( fileName => { if (!fs.existsSync( path.join(configFolderPath, fileName) @@ -71,3 +72,12 @@ export const readConfigFile = (fileName) => { return o } +export const writeConfigFile = async (fileName, content) => { + const filePath = path.join(configFolderPath, fileName) + const fileContent = JSON.stringify(content) + return fsPromise.writeFile( + filePath, + fileContent + ) +} + diff --git a/packages/ui/src/main/index.ts b/packages/ui/src/main/index.ts index dc74ae3..5e30666 100644 --- a/packages/ui/src/main/index.ts +++ b/packages/ui/src/main/index.ts @@ -1,39 +1,8 @@ -import { app, shell, BrowserWindow, ipcMain } from 'electron' -import { join } from 'path' -import { electronApp, optimizer, is } from '@electron-toolkit/utils' +import { app, BrowserWindow, ipcMain } from 'electron' +import { electronApp, optimizer } from '@electron-toolkit/utils' import { mainLoop } from '@bossgeekgo/geek-auto-start-chat-with-boss/index.mjs' +import { createMainWindow } from './window/mainWindow' console.log(mainLoop) -function createWindow(): void { - // Create the browser window. - const mainWindow = new BrowserWindow({ - width: 900, - height: 670, - show: false, - autoHideMenuBar: true, - ...(process.platform === 'linux' ? { icon } : {}), - webPreferences: { - preload: join(__dirname, '../preload/index.js'), - sandbox: false - } - }) - - mainWindow.on('ready-to-show', () => { - mainWindow.show() - }) - - mainWindow.webContents.setWindowOpenHandler((details) => { - shell.openExternal(details.url) - return { action: 'deny' } - }) - - // HMR for renderer base on electron-vite cli. - // Load the remote URL for development or the local html file for production. - if (is.dev && process.env['ELECTRON_RENDERER_URL']) { - mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) - } else { - mainWindow.loadFile(join(__dirname, '../renderer/index.html')) - } -} // This method will be called when Electron has finished // initialization and is ready to create browser windows. @@ -52,12 +21,12 @@ app.whenReady().then(() => { // IPC test ipcMain.on('ping', () => console.log('pong')) - createWindow() + createMainWindow() app.on('activate', function () { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. - if (BrowserWindow.getAllWindows().length === 0) createWindow() + if (BrowserWindow.getAllWindows().length === 0) createMainWindow() }) }) diff --git a/packages/ui/src/main/window/mainWindow.ts b/packages/ui/src/main/window/mainWindow.ts new file mode 100644 index 0000000..0aa661d --- /dev/null +++ b/packages/ui/src/main/window/mainWindow.ts @@ -0,0 +1,77 @@ +import { BrowserWindow, ipcMain, shell } from 'electron' +import path from 'path' +import { is } from '@electron-toolkit/utils' +import { + readConfigFile, + configFileNameList, + ensureConfigFileExist, + writeConfigFile +} from '@bossgeekgo/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' +let mainWindow: BrowserWindow + +export function createMainWindow(): void { + // Create the browser window. + mainWindow = new BrowserWindow({ + width: 900, + height: 670, + show: false, + autoHideMenuBar: true, + ...(process.platform === 'linux' + ? { + /* icon */ + } + : {}), + webPreferences: { + preload: path.join(__dirname, '../preload/index.js'), + sandbox: false + } + }) + + is.dev && mainWindow.webContents.openDevTools() + + mainWindow.on('ready-to-show', () => { + mainWindow.show() + }) + + mainWindow.webContents.setWindowOpenHandler((details) => { + shell.openExternal(details.url) + return { action: 'deny' } + }) + + // HMR for renderer base on electron-vite cli. + // Load the remote URL for development or the local html file for production. + if (is.dev && process.env['ELECTRON_RENDERER_URL']) { + mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL']) + } else { + mainWindow.loadFile(path.join(__dirname, '../renderer/index.html')) + } + + ipcMain.handle('fetch-config-file-content', async () => { + const fileContentList = configFileNameList.map((fileName) => { + return readConfigFile(fileName) + }) + const result = {} + + configFileNameList.forEach((fileName, index) => { + result[fileName] = fileContentList[index] + }) + return result + }) + + ipcMain.handle('save-config-file-from-ui', async (ev, payload) => { + payload = JSON.parse(payload) + ensureConfigFileExist() + + const dingtalkConfig = readConfigFile('dingtalk.json') + dingtalkConfig.groupRobotAccessToken = payload.dingtalkRobotAccessToken + + const bossZhipinConfig = readConfigFile('boss.json') + bossZhipinConfig.cookies = JSON.parse(payload.bossZhipinCookies) + + return await Promise.all([ + writeConfigFile('boss.json', bossZhipinConfig), + writeConfigFile('dingtalk.json', dingtalkConfig), + writeConfigFile('target-company-list.json', payload.expectCompanies.split(',')) + ]) + }) +} diff --git a/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue b/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue index 1516931..d4ee1c0 100644 --- a/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue +++ b/packages/ui/src/renderer/src/page/Configuration/GeekAutoStartChatWithBoss.vue @@ -1,6 +1,6 @@