diff --git a/config/boss.json b/packages/geek-auto-start-chat-with-boss/default-config-file/boss.json similarity index 100% rename from config/boss.json rename to packages/geek-auto-start-chat-with-boss/default-config-file/boss.json diff --git a/config/dingtalk.json b/packages/geek-auto-start-chat-with-boss/default-config-file/dingtalk.json similarity index 100% rename from config/dingtalk.json rename to packages/geek-auto-start-chat-with-boss/default-config-file/dingtalk.json diff --git a/config/target-company-list.json b/packages/geek-auto-start-chat-with-boss/default-config-file/target-company-list.json similarity index 77% rename from config/target-company-list.json rename to packages/geek-auto-start-chat-with-boss/default-config-file/target-company-list.json index 0ea231d..40da787 100644 --- a/config/target-company-list.json +++ b/packages/geek-auto-start-chat-with-boss/default-config-file/target-company-list.json @@ -1,26 +1,4 @@ [ - "青钱", - "软通动力", - "南天", - "睿服", - "中电金信", - "佰钧成", - "云链", - "博彦", - "汉克时代", - "柯莱特", - "拓保", - "亿达信息", - "纬创", - "微创", - "微澜", - "诚迈科技", - "法本", - "兆尹", - "诚迈", - "联合永道", - "新致软件", - "宇信科技", "抖音", "字节", "字跳", diff --git a/packages/geek-auto-start-chat-with-boss/index.mjs b/packages/geek-auto-start-chat-with-boss/index.mjs index 060e9e7..5be9cb7 100644 --- a/packages/geek-auto-start-chat-with-boss/index.mjs +++ b/packages/geek-auto-start-chat-with-boss/index.mjs @@ -11,25 +11,14 @@ import { get__dirname } from '@bossgeekgo/utils/legacy-path.mjs'; import path from 'node:path'; import JSON5 from 'json5' +import { readConfigFile, ensureConfigFileExist } from './runtime-file-utils.mjs' +ensureConfigFileExist() + puppeteer.use(StealthPlugin()) -const { cookies: bossCookies } = JSON5.parse( - fs.readFileSync( - path.join( - get__dirname(), - '../../config/boss.json' - ) - ) -) +const { cookies: bossCookies } = readConfigFile('boss.json') -const targetCompanyList = JSON5.parse( - fs.readFileSync( - path.join( - get__dirname(), - '../../config/target-company-list.json' - ) - ) -) +const targetCompanyList = readConfigFile('target-company-list.json') if (!bossCookies?.length) { console.error('There is no cookies. you can save a copy with EditThisCookie extension.') 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 new file mode 100644 index 0000000..4ce373f --- /dev/null +++ b/packages/geek-auto-start-chat-with-boss/runtime-file-utils.mjs @@ -0,0 +1,73 @@ +import fs from 'node:fs' +import path from 'node:path' +import os from 'node:os' + +import defaultDingtalkConf from './default-config-file/dingtalk.json' assert {type: 'json'} +import defaultBossConf from './default-config-file/boss.json' assert {type: 'json'} +import defaultTargetCompanyListConf from './default-config-file/target-company-list.json' assert {type: 'json'} + +const defaultConfigFileContentMap = { + 'boss.json': JSON.stringify(defaultBossConf), + 'dingtalk.json': JSON.stringify(defaultDingtalkConf), + 'target-company-list.json': JSON.stringify(defaultTargetCompanyListConf) +} + +const runtimeFolderPath = path.join(os.homedir(), '.bossgeekgo') +const ensureRuntimeFolderPathExist = () => { + if (!fs.existsSync(runtimeFolderPath)) { + fs.mkdirSync(runtimeFolderPath) + } + ;['config'].forEach(dirPath => { + if (!fs.existsSync( + path.join(runtimeFolderPath, dirPath) + )) { + fs.mkdirSync( + path.join(runtimeFolderPath, dirPath) + ) + } + }) +} + +const configFolderPath = path.join( + runtimeFolderPath, + 'config' +) +export const ensureConfigFileExist = () => { + ensureRuntimeFolderPathExist() + ;[ + 'boss.json', 'dingtalk.json', 'target-company-list.json' + ].forEach( + fileName => { + if (!fs.existsSync( + path.join(configFolderPath, fileName) + )) { + fs.writeFileSync( + path.join(configFolderPath, fileName), + defaultConfigFileContentMap[fileName] + ) + } + } + ) +} + +export const readConfigFile = (fileName) => { + if (!fs.existsSync( + path.join(configFolderPath, fileName) + )) { + ensureConfigFileExist() + } + + let o + try { + o = JSON.parse( + fs.readFileSync(path.join(configFolderPath, fileName)) + ) + } catch { + fs.unlinkSync(fs.readFileSync(path.join(configFolderPath, fileName))) + ensureConfigFileExist() + o = JSON.parse(defaultConfigFileContentMap[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 4a9e9ac..d7a3cf7 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,15 +8,9 @@ import fs from 'node:fs' import path from 'node:path' import { get__dirname } from '@bossgeekgo/utils/legacy-path.mjs'; import JSON5 from 'json5' +import { readConfigFile } from '@bossgeekgo/geek-auto-start-chat-with-boss/runtime-file-utils.mjs' -const { groupRobotAccessToken: dingTalkAccessToken } = JSON5.parse( - fs.readFileSync( - path.join( - get__dirname(), - '../../config/dingtalk.json' - ) - ) -) +const { groupRobotAccessToken: dingTalkAccessToken } = readConfigFile('dingtalk.json') const initPlugins = (hooks) => { new DingtalkPlugin(dingTalkAccessToken).apply(hooks)