Skip to content

Commit 67b2bb1

Browse files
committed
✨ Feature: add current uploader && transformer log
1 parent f901505 commit 67b2bb1

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

src/core/Lifecycle.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ class Lifecycle extends EventEmitter {
4949

5050
private async doTransform (): Promise<IPicGo> {
5151
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 30)
52-
this.ctx.log.info('Transforming...')
5352
const type = this.ctx.getConfig<Undefinable<string>>('picBed.transformer') || 'path'
53+
let currentTransformer = type
5454
let transformer = this.ctx.helper.transformer.get(type)
5555
if (!transformer) {
5656
transformer = this.ctx.helper.transformer.get('path')
57+
currentTransformer = 'path'
5758
this.ctx.log.warn(`Can't find transformer - ${type}, switch to default transformer - path`)
5859
}
60+
this.ctx.log.info(`Transforming... Current transformer is [${currentTransformer}]`)
5961
await transformer?.handle(this.ctx)
6062
return this.ctx
6163
}
@@ -69,14 +71,16 @@ class Lifecycle extends EventEmitter {
6971
}
7072

7173
private async doUpload (): Promise<IPicGo> {
72-
this.ctx.log.info('Uploading...')
7374
let type = this.ctx.getConfig<Undefinable<string>>('picBed.uploader') || this.ctx.getConfig<Undefinable<string>>('picBed.current') || 'smms'
7475
let uploader = this.ctx.helper.uploader.get(type)
76+
let currentTransformer = type
7577
if (!uploader) {
7678
type = 'smms'
79+
currentTransformer = 'smms'
7780
uploader = this.ctx.helper.uploader.get('smms')
7881
this.ctx.log.warn(`Can't find uploader - ${type}, switch to default uploader - smms`)
7982
}
83+
this.ctx.log.info(`Uploading... Current uploader is [${currentTransformer}]`)
8084
await uploader?.handle(this.ctx)
8185
for (const outputImg of this.ctx.output) {
8286
outputImg.type = type

src/lib/PluginHandler.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
IPluginHandler,
77
IPluginHandlerOptions,
88
Undefinable,
9-
IPicGo
9+
IPicGo,
10+
IPluginHandlerResult
1011
} from '../types'
1112
import { IBuildInEvent } from '../utils/enum'
1213
import { getProcessPluginName, getNormalPluginName } from '../utils/common'
@@ -18,7 +19,7 @@ class PluginHandler implements IPluginHandler {
1819
this.ctx = ctx
1920
}
2021

21-
async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
22+
async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<IPluginHandlerResult<boolean>> {
2223
const installedPlugins: string[] = []
2324
const processPlugins = plugins
2425
.map((item: string) => handlePluginNameProcess(this.ctx, item))
@@ -52,13 +53,23 @@ class PluginHandler implements IPluginHandler {
5253
title: '插件安装成功',
5354
body: [...pkgNameList, ...installedPlugins]
5455
})
56+
const res: IPluginHandlerResult<true> = {
57+
success: true,
58+
body: [...pkgNameList, ...installedPlugins]
59+
}
60+
return res
5561
} else {
5662
const err = `插件安装失败,失败码为${result.code},错误日志为${result.data}`
5763
this.ctx.log.error(err)
5864
this.ctx.emit('installFailed', {
5965
title: '插件安装失败',
6066
body: err
6167
})
68+
const res: IPluginHandlerResult<false> = {
69+
success: false,
70+
body: err
71+
}
72+
return res
6273
}
6374
} else if (installedPlugins.length === 0) {
6475
const err = '插件安装失败,请输入合法插件名或合法安装路径'
@@ -67,16 +78,26 @@ class PluginHandler implements IPluginHandler {
6778
title: '插件安装失败',
6879
body: err
6980
})
81+
const res: IPluginHandlerResult<false> = {
82+
success: false,
83+
body: err
84+
}
85+
return res
7086
} else {
7187
this.ctx.log.success('插件安装成功')
7288
this.ctx.emit('installSuccess', {
7389
title: '插件安装成功',
7490
body: [...pkgNameList, ...installedPlugins]
7591
})
92+
const res: IPluginHandlerResult<true> = {
93+
success: true,
94+
body: [...pkgNameList, ...installedPlugins]
95+
}
96+
return res
7697
}
7798
}
7899

79-
async uninstall (plugins: string[]): Promise<void> {
100+
async uninstall (plugins: string[]): Promise<IPluginHandlerResult<boolean>> {
80101
const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success)
81102
const pkgNameList = processPlugins.map(item => item.pkgName)
82103
if (pkgNameList.length > 0) {
@@ -92,13 +113,23 @@ class PluginHandler implements IPluginHandler {
92113
title: '插件卸载成功',
93114
body: pkgNameList
94115
})
116+
const res: IPluginHandlerResult<true> = {
117+
success: true,
118+
body: pkgNameList
119+
}
120+
return res
95121
} else {
96122
const err = `插件卸载失败,失败码为${result.code},错误日志为${result.data}`
97123
this.ctx.log.error(err)
98124
this.ctx.emit('uninstallFailed', {
99125
title: '插件卸载失败',
100126
body: err
101127
})
128+
const res: IPluginHandlerResult<false> = {
129+
success: false,
130+
body: err
131+
}
132+
return res
102133
}
103134
} else {
104135
const err = '插件卸载失败,请输入合法插件名'
@@ -107,10 +138,15 @@ class PluginHandler implements IPluginHandler {
107138
title: '插件卸载失败',
108139
body: err
109140
})
141+
const res: IPluginHandlerResult<false> = {
142+
success: false,
143+
body: err
144+
}
145+
return res
110146
}
111147
}
112148

113-
async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
149+
async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<IPluginHandlerResult<boolean>> {
114150
const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success)
115151
const pkgNameList = processPlugins.map(item => item.pkgName)
116152
if (pkgNameList.length > 0) {
@@ -123,13 +159,23 @@ class PluginHandler implements IPluginHandler {
123159
title: '插件更新成功',
124160
body: pkgNameList
125161
})
162+
const res: IPluginHandlerResult<true> = {
163+
success: true,
164+
body: pkgNameList
165+
}
166+
return res
126167
} else {
127168
const err = `插件更新失败,失败码为${result.code},错误日志为 \n ${result.data}`
128169
this.ctx.log.error(err)
129170
this.ctx.emit('updateFailed', {
130171
title: '插件更新失败',
131172
body: err
132173
})
174+
const res: IPluginHandlerResult<false> = {
175+
success: false,
176+
body: err
177+
}
178+
return res
133179
}
134180
} else {
135181
const err = '插件更新失败,请输入合法插件名'
@@ -138,6 +184,11 @@ class PluginHandler implements IPluginHandler {
138184
title: '插件更新失败',
139185
body: err
140186
})
187+
const res: IPluginHandlerResult<false> = {
188+
success: false,
189+
body: err
190+
}
191+
return res
141192
}
142193
}
143194

src/types/index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,14 @@ interface IPluginProcessResult {
330330
}
331331

332332
interface IPluginHandler {
333-
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
334-
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
335-
uninstall: (plugins: string[]) => Promise<void>
333+
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<IPluginHandlerResult<boolean>>
334+
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<IPluginHandlerResult<boolean>>
335+
uninstall: (plugins: string[]) => Promise<IPluginHandlerResult<boolean>>
336+
}
337+
338+
interface IPluginHandlerResult<T> {
339+
success: T
340+
body: T extends true ? string[] : string
336341
}
337342

338343
interface IPluginHandlerOptions {

src/utils/common.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ export const getNormalPluginName = (nameOrPath: string, logger: ILogger | Consol
244244
const pluginNameType = getPluginNameType(nameOrPath)
245245
switch (pluginNameType) {
246246
case 'normal':
247-
case 'scope':
248247
return removePluginVersion(nameOrPath)
248+
case 'scope':
249+
return removePluginVersion(nameOrPath, true)
249250
case 'simple':
250251
return removePluginVersion(handleCompletePluginName(nameOrPath))
251252
default: {
@@ -287,13 +288,20 @@ export const handleUnixStylePath = (pathStr: string): string => {
287288
/**
288289
* remove plugin version when register plugin name
289290
* 1. picgo-plugin-xxx@1.0.0 -> picgo-plugin-xxx
291+
* 2. @xxx/picgo-plugin-xxx@1.0.0 -> @xxx/picgo-plugin-xxx
290292
* @param nameOrPath
293+
* @param scope
291294
*/
292-
export const removePluginVersion = (nameOrPath: string): string => {
295+
export const removePluginVersion = (nameOrPath: string, scope: boolean = false): string => {
293296
if (!nameOrPath.includes('@')) {
294297
return nameOrPath
295298
} else {
296-
const matchArr = nameOrPath.match(/(.+\/)?(picgo-plugin-\w+)(@.+)*/)
299+
let reg = /(.+\/)?(picgo-plugin-\w+)(@.+)*/
300+
// if is a scope pkg
301+
if (scope) {
302+
reg = /(.+\/)?(^@[^/]+\/picgo-plugin-\w+)(@.+)*/
303+
}
304+
const matchArr = nameOrPath.match(reg)
297305
if (!matchArr) {
298306
console.warn('can not remove plugin version')
299307
return nameOrPath

0 commit comments

Comments
 (0)