Skip to content

Commit

Permalink
✨ Feature: add current uploader && transformer log
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Apr 4, 2021
1 parent f901505 commit 67b2bb1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ class Lifecycle extends EventEmitter {

private async doTransform (): Promise<IPicGo> {
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 30)
this.ctx.log.info('Transforming...')
const type = this.ctx.getConfig<Undefinable<string>>('picBed.transformer') || 'path'
let currentTransformer = type
let transformer = this.ctx.helper.transformer.get(type)
if (!transformer) {
transformer = this.ctx.helper.transformer.get('path')
currentTransformer = 'path'
this.ctx.log.warn(`Can't find transformer - ${type}, switch to default transformer - path`)
}
this.ctx.log.info(`Transforming... Current transformer is [${currentTransformer}]`)
await transformer?.handle(this.ctx)
return this.ctx
}
Expand All @@ -69,14 +71,16 @@ class Lifecycle extends EventEmitter {
}

private async doUpload (): Promise<IPicGo> {
this.ctx.log.info('Uploading...')
let type = this.ctx.getConfig<Undefinable<string>>('picBed.uploader') || this.ctx.getConfig<Undefinable<string>>('picBed.current') || 'smms'
let uploader = this.ctx.helper.uploader.get(type)
let currentTransformer = type
if (!uploader) {
type = 'smms'
currentTransformer = 'smms'
uploader = this.ctx.helper.uploader.get('smms')
this.ctx.log.warn(`Can't find uploader - ${type}, switch to default uploader - smms`)
}
this.ctx.log.info(`Uploading... Current uploader is [${currentTransformer}]`)
await uploader?.handle(this.ctx)
for (const outputImg of this.ctx.output) {
outputImg.type = type
Expand Down
59 changes: 55 additions & 4 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
IPluginHandler,
IPluginHandlerOptions,
Undefinable,
IPicGo
IPicGo,
IPluginHandlerResult
} from '../types'
import { IBuildInEvent } from '../utils/enum'
import { getProcessPluginName, getNormalPluginName } from '../utils/common'
Expand All @@ -18,7 +19,7 @@ class PluginHandler implements IPluginHandler {
this.ctx = ctx
}

async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<IPluginHandlerResult<boolean>> {
const installedPlugins: string[] = []
const processPlugins = plugins
.map((item: string) => handlePluginNameProcess(this.ctx, item))
Expand Down Expand Up @@ -52,13 +53,23 @@ class PluginHandler implements IPluginHandler {
title: '插件安装成功',
body: [...pkgNameList, ...installedPlugins]
})
const res: IPluginHandlerResult<true> = {
success: true,
body: [...pkgNameList, ...installedPlugins]
}
return res
} else {
const err = `插件安装失败,失败码为${result.code},错误日志为${result.data}`
this.ctx.log.error(err)
this.ctx.emit('installFailed', {
title: '插件安装失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
}
} else if (installedPlugins.length === 0) {
const err = '插件安装失败,请输入合法插件名或合法安装路径'
Expand All @@ -67,16 +78,26 @@ class PluginHandler implements IPluginHandler {
title: '插件安装失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
} else {
this.ctx.log.success('插件安装成功')
this.ctx.emit('installSuccess', {
title: '插件安装成功',
body: [...pkgNameList, ...installedPlugins]
})
const res: IPluginHandlerResult<true> = {
success: true,
body: [...pkgNameList, ...installedPlugins]
}
return res
}
}

async uninstall (plugins: string[]): Promise<void> {
async uninstall (plugins: string[]): Promise<IPluginHandlerResult<boolean>> {
const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success)
const pkgNameList = processPlugins.map(item => item.pkgName)
if (pkgNameList.length > 0) {
Expand All @@ -92,13 +113,23 @@ class PluginHandler implements IPluginHandler {
title: '插件卸载成功',
body: pkgNameList
})
const res: IPluginHandlerResult<true> = {
success: true,
body: pkgNameList
}
return res
} else {
const err = `插件卸载失败,失败码为${result.code},错误日志为${result.data}`
this.ctx.log.error(err)
this.ctx.emit('uninstallFailed', {
title: '插件卸载失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
}
} else {
const err = '插件卸载失败,请输入合法插件名'
Expand All @@ -107,10 +138,15 @@ class PluginHandler implements IPluginHandler {
title: '插件卸载失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
}
}

async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<IPluginHandlerResult<boolean>> {
const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success)
const pkgNameList = processPlugins.map(item => item.pkgName)
if (pkgNameList.length > 0) {
Expand All @@ -123,13 +159,23 @@ class PluginHandler implements IPluginHandler {
title: '插件更新成功',
body: pkgNameList
})
const res: IPluginHandlerResult<true> = {
success: true,
body: pkgNameList
}
return res
} else {
const err = `插件更新失败,失败码为${result.code},错误日志为 \n ${result.data}`
this.ctx.log.error(err)
this.ctx.emit('updateFailed', {
title: '插件更新失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
}
} else {
const err = '插件更新失败,请输入合法插件名'
Expand All @@ -138,6 +184,11 @@ class PluginHandler implements IPluginHandler {
title: '插件更新失败',
body: err
})
const res: IPluginHandlerResult<false> = {
success: false,
body: err
}
return res
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,14 @@ interface IPluginProcessResult {
}

interface IPluginHandler {
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
uninstall: (plugins: string[]) => Promise<void>
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<IPluginHandlerResult<boolean>>
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<IPluginHandlerResult<boolean>>
uninstall: (plugins: string[]) => Promise<IPluginHandlerResult<boolean>>
}

interface IPluginHandlerResult<T> {
success: T
body: T extends true ? string[] : string
}

interface IPluginHandlerOptions {
Expand Down
14 changes: 11 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ export const getNormalPluginName = (nameOrPath: string, logger: ILogger | Consol
const pluginNameType = getPluginNameType(nameOrPath)
switch (pluginNameType) {
case 'normal':
case 'scope':
return removePluginVersion(nameOrPath)
case 'scope':
return removePluginVersion(nameOrPath, true)
case 'simple':
return removePluginVersion(handleCompletePluginName(nameOrPath))
default: {
Expand Down Expand Up @@ -287,13 +288,20 @@ export const handleUnixStylePath = (pathStr: string): string => {
/**
* remove plugin version when register plugin name
* 1. picgo-plugin-xxx@1.0.0 -> picgo-plugin-xxx
* 2. @xxx/picgo-plugin-xxx@1.0.0 -> @xxx/picgo-plugin-xxx
* @param nameOrPath
* @param scope
*/
export const removePluginVersion = (nameOrPath: string): string => {
export const removePluginVersion = (nameOrPath: string, scope: boolean = false): string => {
if (!nameOrPath.includes('@')) {
return nameOrPath
} else {
const matchArr = nameOrPath.match(/(.+\/)?(picgo-plugin-\w+)(@.+)*/)
let reg = /(.+\/)?(picgo-plugin-\w+)(@.+)*/
// if is a scope pkg
if (scope) {
reg = /(.+\/)?(^@[^/]+\/picgo-plugin-\w+)(@.+)*/
}
const matchArr = nameOrPath.match(reg)
if (!matchArr) {
console.warn('can not remove plugin version')
return nameOrPath
Expand Down

0 comments on commit 67b2bb1

Please sign in to comment.