mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 11:08:13 -05:00
🐛 Fix: enum type error
This commit is contained in:
parent
87638a2882
commit
4e3fa28be6
76
src/main/utils/logger.ts
Normal file
76
src/main/utils/logger.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import chalk from 'chalk'
|
||||
import dayjs from 'dayjs'
|
||||
import fs from 'fs-extra'
|
||||
import path from 'path'
|
||||
import util from 'util'
|
||||
import db from '#/datastore'
|
||||
import { app } from 'electron'
|
||||
import { IChalkType } from '#/types/enum'
|
||||
const baseDir = app.getPath('userData')
|
||||
|
||||
class Logger {
|
||||
private level = {
|
||||
success: IChalkType.success,
|
||||
info: IChalkType.info,
|
||||
warn: IChalkType.warn,
|
||||
error: IChalkType.error
|
||||
}
|
||||
protected handleLog (type: ILogType, msg: string | Error): string | Error | undefined {
|
||||
// if configPath is invalid then this.ctx.config === undefined
|
||||
// if not then check config.silent
|
||||
const log = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`)
|
||||
console.log(log, msg)
|
||||
process.nextTick(() => {
|
||||
this.handleWriteLog(type, msg)
|
||||
})
|
||||
return msg
|
||||
}
|
||||
|
||||
protected handleWriteLog (type: string, msg: string | Error): void {
|
||||
try {
|
||||
const logLevel = db.get('settings.logLevel')
|
||||
const logPath = db.get('settings.logPath') || path.join(baseDir, './picgo.log')
|
||||
if (this.checkLogLevel(type, logLevel)) {
|
||||
const picgoLog = fs.createWriteStream(logPath, { flags: 'a', encoding: 'utf8' })
|
||||
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] ${msg}`
|
||||
const logger = new console.Console(picgoLog)
|
||||
if (typeof msg === 'object' && type === 'error') {
|
||||
log += `\n------Error Stack Begin------\n${util.format(msg.stack)}\n-------Error Stack End-------`
|
||||
}
|
||||
logger.log(log)
|
||||
picgoLog.destroy()
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
protected checkLogLevel (type: string, level: undefined | string | string[]): boolean {
|
||||
if (level === undefined || level === 'all') {
|
||||
return true
|
||||
}
|
||||
if (Array.isArray(level)) {
|
||||
return level.some((item: string) => (item === type || item === 'all'))
|
||||
} else {
|
||||
return type === level
|
||||
}
|
||||
}
|
||||
|
||||
success (msg: string | Error): string | Error | undefined {
|
||||
return this.handleLog('success', msg)
|
||||
}
|
||||
|
||||
info (msg: string | Error): string | Error | undefined {
|
||||
return this.handleLog('info', msg)
|
||||
}
|
||||
|
||||
error (msg: string | Error): string | Error | undefined {
|
||||
return this.handleLog('error', msg)
|
||||
}
|
||||
|
||||
warn (msg: string | Error): string | Error | undefined {
|
||||
return this.handleLog('warn', msg)
|
||||
}
|
||||
}
|
||||
|
||||
export default new Logger()
|
@ -3,6 +3,7 @@ import GuiApi from './guiApi'
|
||||
import { dialog, shell, IpcMain, IpcMainEvent, App } from 'electron'
|
||||
import db from '#/datastore'
|
||||
import PicGoCore from '~/universal/types/picgo'
|
||||
import { IPicGoHelperType } from '#/types/enum'
|
||||
|
||||
// eslint-disable-next-line
|
||||
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
|
||||
@ -20,7 +21,7 @@ interface GuiMenuItem {
|
||||
}
|
||||
|
||||
// get uploader or transformer config
|
||||
const getConfig = (name: string, type: PicGoHelperType, ctx: PicGoCore) => {
|
||||
const getConfig = (name: string, type: IPicGoHelperType, ctx: PicGoCore) => {
|
||||
let config: any[] = []
|
||||
if (name === '') {
|
||||
return config
|
||||
@ -82,11 +83,11 @@ const handleGetPluginList = (ipcMain: IpcMain, STORE_PATH: string, CONFIG_PATH:
|
||||
},
|
||||
uploader: {
|
||||
name: uploaderName,
|
||||
config: handleConfigWithFunction(getConfig(uploaderName, PicGoHelperType.uploader, picgo))
|
||||
config: handleConfigWithFunction(getConfig(uploaderName, IPicGoHelperType.uploader, picgo))
|
||||
},
|
||||
transformer: {
|
||||
name: transformerName,
|
||||
config: handleConfigWithFunction(getConfig(uploaderName, PicGoHelperType.transformer, picgo))
|
||||
config: handleConfigWithFunction(getConfig(uploaderName, IPicGoHelperType.transformer, picgo))
|
||||
}
|
||||
},
|
||||
enabled: picgo.getConfig(`picgoPlugins.${pluginList[i]}`),
|
||||
|
22
src/universal/types/enum.ts
Normal file
22
src/universal/types/enum.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export enum IChalkType {
|
||||
success = 'green',
|
||||
info = 'blue',
|
||||
warn = 'yellow',
|
||||
error = 'red'
|
||||
}
|
||||
|
||||
export enum IPicGoHelperType {
|
||||
afterUploadPlugins = 'afterUploadPlugins',
|
||||
beforeTransformPlugins = 'beforeTransformPlugins',
|
||||
beforeUploadPlugins = 'beforeUploadPlugins',
|
||||
uploader = 'uploader',
|
||||
transformer = 'transformer'
|
||||
}
|
||||
|
||||
export enum IPasteStyle {
|
||||
MARKDOWN = 'markdown',
|
||||
HTML = 'HTML',
|
||||
URL = 'URL',
|
||||
UBB = 'UBB',
|
||||
CUSTOM = 'Custom'
|
||||
}
|
15
src/universal/types/types.d.ts
vendored
15
src/universal/types/types.d.ts
vendored
@ -77,25 +77,12 @@ interface Bounds {
|
||||
y: number
|
||||
}
|
||||
|
||||
declare enum PasteStyle {
|
||||
MARKDOWN = 'markdown',
|
||||
HTML = 'HTML',
|
||||
URL = 'URL',
|
||||
UBB = 'UBB',
|
||||
CUSTOM = 'Custom'
|
||||
}
|
||||
declare type ILogType = 'success' | 'info' | 'warn' | 'error'
|
||||
|
||||
// global value
|
||||
declare var __static: string
|
||||
|
||||
// PicGo Types
|
||||
declare enum PicGoHelperType {
|
||||
afterUploadPlugins = 'afterUploadPlugins',
|
||||
beforeTransformPlugins = 'beforeTransformPlugins',
|
||||
beforeUploadPlugins = 'beforeUploadPlugins',
|
||||
uploader = 'uploader',
|
||||
transformer = 'transformer'
|
||||
}
|
||||
|
||||
interface IPicGoPlugin {
|
||||
name: string
|
||||
|
@ -1,4 +1,5 @@
|
||||
import db from '#/datastore'
|
||||
import { IPasteStyle } from '#/types/enum'
|
||||
|
||||
const formatCustomLink = (customLink: string, item: ImgInfo) => {
|
||||
let fileName = item.fileName!.replace(new RegExp(`\\${item.extname}$`), '')
|
||||
@ -17,7 +18,7 @@ const formatCustomLink = (customLink: string, item: ImgInfo) => {
|
||||
return customLink
|
||||
}
|
||||
|
||||
export default (style: PasteStyle, item: ImgInfo) => {
|
||||
export default (style: IPasteStyle, item: ImgInfo) => {
|
||||
let url = item.url || item.imgUrl
|
||||
const customLink = db.get('settings.customLink') || '$url'
|
||||
const tpl = {
|
||||
|
Loading…
Reference in New Issue
Block a user