From a676c083fe597ac14bbf2aef0c9c13d769d390df Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sat, 21 Aug 2021 10:42:52 +0800 Subject: [PATCH] :bug: Fix: some bugs ISSUES CLOSED: #722 --- src/main/apis/core/datastore/dbChecker.ts | 17 +++++++----- src/main/apis/core/utils/localLogger.ts | 32 +++++++++++++++++++++++ src/main/lifeCycle/index.ts | 4 +-- src/main/migrate/index.ts | 2 +- src/renderer/components/ConfigForm.vue | 2 +- src/renderer/layouts/Main.vue | 7 ++++- src/universal/types/electron.d.ts | 2 +- src/universal/types/types.d.ts | 4 +++ 8 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 src/main/apis/core/utils/localLogger.ts diff --git a/src/main/apis/core/datastore/dbChecker.ts b/src/main/apis/core/datastore/dbChecker.ts index 598f3416..9855e586 100644 --- a/src/main/apis/core/datastore/dbChecker.ts +++ b/src/main/apis/core/datastore/dbChecker.ts @@ -2,6 +2,7 @@ import fs from 'fs-extra' import path from 'path' import { remote, app } from 'electron' import dayjs from 'dayjs' +import { getLogger } from '@core/utils/localLogger' const APP = process.type === 'renderer' ? remote.app : app const STORE_PATH = APP.getPath('userData') const configFilePath = path.join(STORE_PATH, 'data.json') @@ -15,9 +16,11 @@ const errorMsg = { brokenButBackup: 'PicGo 配置文件损坏,已经恢复为备份配置' } +/** ensure notification list */ +if (!global.notificationList) global.notificationList = [] + function dbChecker () { if (process.type !== 'renderer') { - if (!global.notificationList) global.notificationList = [] // db save bak try { const { dbPath, dbBackupPath } = getGalleryDBPath() @@ -50,16 +53,16 @@ function dbChecker () { fs.writeFileSync(configFilePath, configFile, { encoding: 'utf-8' }) const stats = fs.statSync(configFileBackupPath) optionsTpl.body = `${errorMsg.brokenButBackup}\n备份文件版本:${dayjs(stats.mtime).format('YYYY-MM-DD HH:mm:ss')}` - global.notificationList.push(optionsTpl) + global.notificationList?.push(optionsTpl) return } catch (e) { optionsTpl.body = errorMsg.broken - global.notificationList.push(optionsTpl) + global.notificationList?.push(optionsTpl) return } } optionsTpl.body = errorMsg.broken - global.notificationList.push(optionsTpl) + global.notificationList?.push(optionsTpl) return } fs.writeFileSync(configFileBackupPath, configFile, { encoding: 'utf-8' }) @@ -92,15 +95,17 @@ function dbPathChecker (): string { } return _configFilePath } catch (e) { - // TODO: local logger is needed + const picgoLogPath = path.join(defaultConfigPath, 'picgo.log') + const logger = getLogger(picgoLogPath) if (!hasCheckPath) { let optionsTpl = { title: '注意', body: '自定义文件解析出错,请检查路径内容是否正确' } - global.notificationList.push(optionsTpl) + global.notificationList?.push(optionsTpl) hasCheckPath = true } + logger('error', e) console.error(e) _configFilePath = defaultConfigPath return _configFilePath diff --git a/src/main/apis/core/utils/localLogger.ts b/src/main/apis/core/utils/localLogger.ts new file mode 100644 index 00000000..a29e0b56 --- /dev/null +++ b/src/main/apis/core/utils/localLogger.ts @@ -0,0 +1,32 @@ +import fs from 'fs-extra' +import dayjs from 'dayjs' +import util from 'util' + +/** + * for local log before picgo inited + */ +const getLogger = (logPath: string) => { + if (!fs.existsSync(logPath)) { + fs.ensureFileSync(logPath) + } + return (type: string, ...msg: any[]) => { + let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] ` + msg.forEach((item: ILogArgvTypeWithError) => { + if (typeof item === 'object' && type === 'error') { + log += `\n------Error Stack Begin------\n${util.format(item.stack)}\n-------Error Stack End------- ` + } else { + if (typeof item === 'object') { + item = JSON.stringify(item) + } + log += `${item} ` + } + }) + log += '\n' + // A synchronized approach to avoid log msg sequence errors + fs.appendFileSync(logPath, log) + } +} + +export { + getLogger +} diff --git a/src/main/lifeCycle/index.ts b/src/main/lifeCycle/index.ts index a209ea04..f00de8bf 100644 --- a/src/main/lifeCycle/index.ts +++ b/src/main/lifeCycle/index.ts @@ -96,8 +96,8 @@ class LifeCycle { handleStartUpFiles(process.argv, process.cwd()) } - if (global.notificationList?.length > 0) { - while (global.notificationList.length) { + if (global.notificationList && global.notificationList?.length > 0) { + while (global.notificationList?.length) { const option = global.notificationList.pop() const notice = new Notification(option!) notice.show() diff --git a/src/main/migrate/index.ts b/src/main/migrate/index.ts index ffe0249f..b5e8d581 100644 --- a/src/main/migrate/index.ts +++ b/src/main/migrate/index.ts @@ -37,7 +37,7 @@ const migrateGalleryFromVersion230 = async (configDB: typeof ConfigStore, galler const configPath = configDB.getConfigPath() const configBakPath = path.join(path.dirname(configPath), 'config.bak.json') // migrate gallery from config to gallery db - if (originGallery && originGallery?.length > 0) { + if (originGallery && Array.isArray(originGallery) && originGallery?.length > 0) { if (fse.existsSync(configBakPath)) { fse.copyFileSync(configPath, configBakPath) } diff --git a/src/renderer/components/ConfigForm.vue b/src/renderer/components/ConfigForm.vue index 17a118f8..38fbdac7 100644 --- a/src/renderer/components/ConfigForm.vue +++ b/src/renderer/components/ConfigForm.vue @@ -116,7 +116,7 @@ export default class extends Vue { async handleConfig (val: any) { this.ruleForm = Object.assign({}, {}) const config = await this.getConfig(this.getConfigType()) - if (val.length > 0 && config) { + if (val.length > 0) { this.configList = cloneDeep(val).map((item: any) => { let defaultValue = item.default !== undefined ? item.default : item.type === 'checkbox' diff --git a/src/renderer/layouts/Main.vue b/src/renderer/layouts/Main.vue index e2f7f2e9..a4f2830b 100644 --- a/src/renderer/layouts/Main.vue +++ b/src/renderer/layouts/Main.vue @@ -17,6 +17,7 @@ :default-active="defaultActive" @select="handleSelect" :unique-opened="true" + @open="handleGetPicPeds" > @@ -192,8 +193,8 @@ export default class extends Vue { created () { this.os = process.platform this.buildMenu() - ipcRenderer.send('getPicBeds') ipcRenderer.on('getPicBeds', this.getPicBeds) + this.handleGetPicPeds() } @Watch('choosedPicBedForQRCode') @@ -207,6 +208,10 @@ export default class extends Vue { } } + handleGetPicPeds = () => { + ipcRenderer.send('getPicBeds') + } + handleSelect (index: string) { const type = index.match(/picbeds-/) if (type === null) { diff --git a/src/universal/types/electron.d.ts b/src/universal/types/electron.d.ts index e58312d2..1491cdec 100644 --- a/src/universal/types/electron.d.ts +++ b/src/universal/types/electron.d.ts @@ -27,7 +27,7 @@ declare global { interface Global { PICGO_GUI_VERSION: string PICGO_CORE_VERSION: string - notificationList: IAppNotification[] + notificationList?: IAppNotification[] } } } diff --git a/src/universal/types/types.d.ts b/src/universal/types/types.d.ts index 4ec685e2..de53c652 100644 --- a/src/universal/types/types.d.ts +++ b/src/universal/types/types.d.ts @@ -321,3 +321,7 @@ interface IAnalyticsData { interface IStringKeyMap { [propName: string]: any } + +type ILogArgvType = string | number + +type ILogArgvTypeWithError = ILogArgvType | Error