feat: save data in the current folder

This commit is contained in:
mio 2023-05-03 21:22:37 +08:00
parent b88b57ae62
commit 3dbb746a38
4 changed files with 47 additions and 25 deletions

@ -1,6 +1,6 @@
{
"name": "star-rail-warp-export",
"version": "0.0.7",
"version": "0.0.8",
"main": "./dist/electron/main/main.js",
"author": "biuuu <https://github.com/biuuu>",
"license": "MIT",

@ -1,4 +1,4 @@
const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale } = require('./utils')
const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale, userDataPath, globalUserDataPath } = require('./utils')
const config = {
urls: [],
@ -13,7 +13,10 @@ const config = {
}
const getLocalConfig = async () => {
const localConfig = await readJSON('config.json')
let localConfig = await readJSON(userDataPath, 'config.json')
if (!localConfig) {
localConfig = await readJSON(globalUserDataPath, 'config.json')
}
if (!localConfig) return
const configTemp = {}
for (let key in localConfig) {

@ -3,7 +3,7 @@ const util = require('util')
const path = require('path')
const { URL } = require('url')
const { app, ipcMain, shell } = require('electron')
const { sleep, request, sendMsg, readJSON, saveJSON, detectLocale, userDataPath, userPath, localIp, langMap } = require('./utils')
const { sleep, request, sendMsg, readJSON, saveJSON, detectLocale, userDataPath, userPath, localIp, langMap, globalUserDataPath } = require('./utils')
const config = require('./config')
const i18n = require('./i18n')
const { enableProxy, disableProxy } = require('./module/system-proxy')
@ -29,25 +29,42 @@ const defaultTypeMap = new Map([
['2', '始发跃迁']
])
const findDataFiles = async (dataPath, fileMap) => {
const files = await readdir(dataPath)
if (files?.length) {
for (let name of files) {
if (/^gacha-list-\d+\.json$/.test(name) && !fileMap.has(name)) {
fileMap.set(name, dataPath)
}
}
}
}
const collectDataFiles = async () => {
await fs.ensureDir(userDataPath)
await fs.ensureDir(globalUserDataPath)
const fileMap = new Map()
await findDataFiles(userDataPath, fileMap)
await findDataFiles(globalUserDataPath, fileMap)
return fileMap
}
let localDataReaded = false
const readdir = util.promisify(fs.readdir)
const readData = async () => {
if (localDataReaded) return
localDataReaded = true
await fs.ensureDir(userDataPath)
const files = await readdir(userDataPath)
for (let name of files) {
if (/^gacha-list-\d+\.json$/.test(name)) {
try {
const data = await readJSON(name)
data.typeMap = new Map(data.typeMap) || defaultTypeMap
data.result = new Map(data.result)
if (data.uid) {
dataMap.set(data.uid, data)
}
} catch (e) {
sendMsg(e, 'ERROR')
const fileMap = await collectDataFiles()
for (let [name, dataPath] of fileMap) {
try {
const data = await readJSON(dataPath, name)
data.typeMap = new Map(data.typeMap) || defaultTypeMap
data.result = new Map(data.result)
if (data.uid) {
dataMap.set(data.uid, data)
}
} catch (e) {
sendMsg(e, 'ERROR')
}
}
if ((!config.current && dataMap.size) || (config.current && dataMap.size && !dataMap.has(config.current))) {

@ -11,8 +11,9 @@ const Registry = require('winreg')
const isDev = !app.isPackaged
const userPath = app.getPath('userData')
const appRoot = isDev ? path.resolve(__dirname, '..', '..') : userPath
const appRoot = isDev ? path.resolve(__dirname, '..', '..') : path.resolve(app.getAppPath(), '..', '..')
const userDataPath = path.resolve(appRoot, 'userData')
const globalUserDataPath = path.resolve(userPath, 'userData')
let win = null
const initWindow = () => {
@ -56,7 +57,7 @@ const saveLog = () => {
const text = item[2]
return `[${type}][${time}]${text}`
}).join('\r\n')
fs.outputFileSync(path.join(userDataPath, 'log.txt'), text)
fs.outputFile(path.join(userDataPath, 'log.txt'), text)
}
const authkeyMask = (text = '') => {
@ -144,19 +145,20 @@ const detectLocale = (value) => {
const saveJSON = async (name, data) => {
try {
await fs.outputJSON(path.join(userDataPath, name), data, {
spaces: 2
})
await fs.outputJSON(path.join(userDataPath, name), data)
if (!isDev) {
await fs.outputJSON(path.join(globalUserDataPath, name), data)
}
} catch (e) {
sendMsg(e, 'ERROR')
await sleep(3)
}
}
const readJSON = async (name) => {
const readJSON = async (dataPath, name) => {
let data = null
try {
data = await fs.readJSON(path.join(userDataPath, name))
data = await fs.readJSON(path.join(dataPath, name))
} catch (e) {}
return data
}
@ -203,5 +205,5 @@ const localIp = () => {
module.exports = {
sleep, request, hash, cipherAes, decipherAes, saveLog,
sendMsg, readJSON, saveJSON, initWindow, getWin, localIp, userPath, detectLocale, langMap,
appRoot, userDataPath
appRoot, userDataPath, globalUserDataPath
}