diff --git a/packages/geek-auto-start-chat-with-boss/default-config-file/boss.json b/packages/geek-auto-start-chat-with-boss/default-config-file/boss.json index d2b80d7..7a73a41 100644 --- a/packages/geek-auto-start-chat-with-boss/default-config-file/boss.json +++ b/packages/geek-auto-start-chat-with-boss/default-config-file/boss.json @@ -1,3 +1,2 @@ { - "cookies": [] } \ No newline at end of file diff --git a/packages/geek-auto-start-chat-with-boss/default-storage-file/boss-cookies.json b/packages/geek-auto-start-chat-with-boss/default-storage-file/boss-cookies.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/packages/geek-auto-start-chat-with-boss/default-storage-file/boss-cookies.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/geek-auto-start-chat-with-boss/index.mjs b/packages/geek-auto-start-chat-with-boss/index.mjs index 95eec78..bb550f9 100644 --- a/packages/geek-auto-start-chat-with-boss/index.mjs +++ b/packages/geek-auto-start-chat-with-boss/index.mjs @@ -9,8 +9,9 @@ import { get__dirname } from '@geekgeekrun/utils/legacy-path.mjs'; import path from 'node:path'; import JSON5 from 'json5' -import { readConfigFile, ensureConfigFileExist } from './runtime-file-utils.mjs' +import { readConfigFile, ensureConfigFileExist, readStorageFile, ensureStorageFileExist } from './runtime-file-utils.mjs' ensureConfigFileExist() +ensureStorageFileExist() const isRunFromUi = Boolean(process.env.MAIN_BOSSGEEKGO_UI_RUN_MODE) const isUiDev = process.env.NODE_ENV === 'development' @@ -45,7 +46,7 @@ export async function initPuppeteer () { puppeteer.use(StealthPlugin()) } -const { cookies: bossCookies } = readConfigFile('boss.json') +const bossCookies = readStorageFile('boss-cookie.json') const targetCompanyList = readConfigFile('target-company-list.json') 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 6336294..01c9db6 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 @@ -7,6 +7,7 @@ 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'} +import defaultBossCookieStorage from './default-storage-file/boss-cookies.json' assert { type: 'json' } export const configFileNameList = ['boss.json', 'dingtalk.json', 'target-company-list.json'] const defaultConfigFileContentMap = { @@ -20,7 +21,7 @@ const ensureRuntimeFolderPathExist = () => { if (!fs.existsSync(runtimeFolderPath)) { fs.mkdirSync(runtimeFolderPath) } - ;['config'].forEach(dirPath => { + ;['config', 'storage'].forEach(dirPath => { if (!fs.existsSync( path.join(runtimeFolderPath, dirPath) )) { @@ -52,8 +53,9 @@ export const ensureConfigFileExist = () => { } export const readConfigFile = (fileName) => { + const joinedPath = path.join(configFolderPath, fileName) if (!fs.existsSync( - path.join(configFolderPath, fileName) + joinedPath )) { ensureConfigFileExist() } @@ -61,10 +63,10 @@ export const readConfigFile = (fileName) => { let o try { o = JSON.parse( - fs.readFileSync(path.join(configFolderPath, fileName)) + fs.readFileSync(joinedPath) ) } catch { - fs.unlinkSync(fs.readFileSync(path.join(configFolderPath, fileName))) + fs.existsSync(joinedPath) && fs.unlinkSync(joinedPath) ensureConfigFileExist() o = JSON.parse(defaultConfigFileContentMap[fileName]) } @@ -81,3 +83,50 @@ export const writeConfigFile = async (fileName, content) => { ) } +export const storageFilePath = path.join( + runtimeFolderPath, + 'storage' +) +export const storageFileNameList = ['boss-cookies.json'] + +const defaultStorageFileContentMap = { + 'boss-cookies.json': JSON.stringify(defaultBossCookieStorage) +} +export const ensureStorageFileExist = () => { + ensureRuntimeFolderPathExist() + ;storageFileNameList.forEach( + fileName => { + if (!fs.existsSync( + path.join(storageFilePath, fileName) + )) { + fs.writeFileSync( + path.join(storageFilePath, fileName), + defaultStorageFileContentMap[fileName] + ) + } + } + ) +} + +export const readStorageFile = (fileName) => { + const joinedPath = path.join(storageFilePath, fileName) + + if (!fs.existsSync( + joinedPath + )) { + ensureStorageFileExist() + } + + let o + try { + o = JSON.parse( + fs.readFileSync(joinedPath) + ) + } catch { + fs.existsSync(joinedPath) && fs.unlinkSync(joinedPath) + ensureStorageFileExist() + o = JSON.parse(defaultStorageFileContentMap[fileName]) + } + + return o +} diff --git a/packages/run-core-of-geek-auto-start-chat-with-boss/index.mjs b/packages/run-core-of-geek-auto-start-chat-with-boss/index.mjs index 464524d..0a21dba 100644 --- a/packages/run-core-of-geek-auto-start-chat-with-boss/index.mjs +++ b/packages/run-core-of-geek-auto-start-chat-with-boss/index.mjs @@ -8,8 +8,8 @@ import fs from 'node:fs' import path from 'node:path' import { get__dirname } from '@geekgeekrun/utils/legacy-path.mjs'; import JSON5 from 'json5' -import { readConfigFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' -const { cookies: bossCookies } = readConfigFile('boss.json') +import { readConfigFile, readStorageFile } from '@geekgeekrun/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' +const bossCookies = readStorageFile('boss-cookies.json') const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json') const initPlugins = (hooks) => {