Skip to content

Commit

Permalink
🐛 Fix: enum type error
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Dec 20, 2019
1 parent 87638a2 commit 4e3fa28
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 18 deletions.
76 changes: 76 additions & 0 deletions src/main/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 4 additions & 3 deletions src/main/utils/picgoCoreIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]}`),
Expand Down
22 changes: 22 additions & 0 deletions src/universal/types/enum.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 14 deletions src/universal/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/universal/utils/pasteTemplate.ts
Original file line number Diff line number Diff line change
@@ -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}$`), '')
Expand All @@ -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 = {
Expand Down

0 comments on commit 4e3fa28

Please sign in to comment.