migrate config files to user home so makes the file can be read both in cli and gui

This commit is contained in:
bossgeekgo
2024-02-12 02:00:03 +08:00
parent 9eb6d4c3f2
commit 960c637372
6 changed files with 80 additions and 46 deletions
@@ -0,0 +1,3 @@
{
"cookies": []
}
@@ -0,0 +1,3 @@
{
"groupRobotAccessToken": ""
}
@@ -0,0 +1,84 @@
[
"抖音",
"字节",
"字跳",
"有竹居",
"脸萌",
"头条",
"滴滴",
"网易",
"腾讯",
"搜狗",
"京东",
"百度",
"度小满",
"爱奇艺",
"携程",
"趣拿",
"去哪儿",
"集度",
"理想",
"顺丰",
"讯飞",
"同程",
"艺龙",
"贝壳",
"链家",
"我爱我家",
"多点",
"金山",
"小米",
"猎豹",
"新浪",
"微博",
"阿里",
"蚂蚁",
"飞猪",
"高德",
"乌鸫",
"美团",
"三快",
"快手",
"映客",
"小红书",
"行吟",
"奇虎",
"360",
"鸿盈",
"奇富",
"亚信",
"启明星辰",
"奇安信",
"汽车之家",
"车好多",
"瓜子",
"易车",
"昆仑万维",
"闲徕",
"趣加",
"完美",
"马上消费",
"轻松",
"水滴",
"白龙马",
"58",
"车欢欢",
"五八",
"红布林",
"致美",
"美餐",
"知乎",
"易点云",
"搜狐",
"用友",
"畅捷通",
"猿辅导",
"小猿",
"猿力",
"好未来",
"学而思",
"希望学",
"新东方",
"东方甄选",
"作业帮"
]
@@ -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.')
@@ -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
}
@@ -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)