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

View File

@@ -1,26 +1,4 @@
[
"青钱",
"软通动力",
"南天",
"睿服",
"中电金信",
"佰钧成",
"云链",
"博彦",
"汉克时代",
"柯莱特",
"拓保",
"亿达信息",
"纬创",
"微创",
"微澜",
"诚迈科技",
"法本",
"兆尹",
"诚迈",
"联合永道",
"新致软件",
"宇信科技",
"抖音",
"字节",
"字跳",

View File

@@ -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.')

View File

@@ -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
}

View File

@@ -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)