Skip to content

Commit 6adc070

Browse files
committed
✨ Feature: add plugin running && error logs
1 parent daa7508 commit 6adc070

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

src/core/Lifecycle.ts

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
22
import PicGo from './PicGo'
33
import { Plugin } from '../utils/interfaces'
44
import { handleUrlEncode } from '../utils/common'
5+
import LifecyclePlugins from '../lib/LifecyclePlugins'
56

67
class Lifecycle extends EventEmitter {
78
configPath: string
@@ -22,11 +23,11 @@ class Lifecycle extends EventEmitter {
2223
this.ctx.output = []
2324

2425
// lifecycle main
25-
await this.beforeTransform(this.ctx)
26-
await this.doTransform(this.ctx)
27-
await this.beforeUpload(this.ctx)
28-
await this.doUpload(this.ctx)
29-
await this.afterUpload(this.ctx)
26+
await this.beforeTransform()
27+
await this.doTransform()
28+
await this.beforeUpload()
29+
await this.doUpload()
30+
await this.afterUpload()
3031
return this.ctx
3132
} catch (e) {
3233
this.ctx.log.warn('failed')
@@ -38,71 +39,80 @@ class Lifecycle extends EventEmitter {
3839
}
3940
}
4041
}
41-
private async beforeTransform (ctx: PicGo): Promise<PicGo> {
42+
private async beforeTransform (): Promise<PicGo> {
4243
this.ctx.emit('uploadProgress', 0)
43-
this.ctx.emit('beforeTransform', ctx)
44+
this.ctx.emit('beforeTransform', this.ctx)
4445
this.ctx.log.info('Before transform')
45-
await this.handlePlugins(ctx.helper.beforeTransformPlugins.getList(), ctx)
46-
return ctx
46+
await this.handlePlugins(this.ctx.helper.beforeTransformPlugins)
47+
return this.ctx
4748
}
48-
private async doTransform (ctx: PicGo): Promise<PicGo> {
49+
private async doTransform (): Promise<PicGo> {
4950
this.ctx.emit('uploadProgress', 30)
5051
this.ctx.log.info('Transforming...')
51-
let type = ctx.getConfig('picBed.transformer') || 'path'
52+
let type = this.ctx.getConfig('picBed.transformer') || 'path'
5253
let transformer = this.ctx.helper.transformer.get(type)
5354
if (!transformer) {
5455
transformer = this.ctx.helper.transformer.get('path')
55-
ctx.log.warn(`Can't find transformer - ${type}, swtich to default transformer - path`)
56+
this.ctx.log.warn(`Can't find transformer - ${type}, swtich to default transformer - path`)
5657
}
57-
await transformer.handle(ctx)
58-
return ctx
58+
await transformer.handle(this.ctx)
59+
return this.ctx
5960
}
60-
private async beforeUpload (ctx: PicGo): Promise<PicGo> {
61+
private async beforeUpload (): Promise<PicGo> {
6162
this.ctx.emit('uploadProgress', 60)
6263
this.ctx.log.info('Before upload')
63-
this.ctx.emit('beforeUpload', ctx)
64-
await this.handlePlugins(ctx.helper.beforeUploadPlugins.getList(), ctx)
65-
return ctx
64+
this.ctx.emit('beforeUpload', this.ctx)
65+
await this.handlePlugins(this.ctx.helper.beforeUploadPlugins)
66+
return this.ctx
6667
}
67-
private async doUpload (ctx: PicGo): Promise<PicGo> {
68+
private async doUpload (): Promise<PicGo> {
6869
this.ctx.log.info('Uploading...')
69-
let type = ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current') || 'smms'
70+
let type = this.ctx.getConfig('picBed.uploader') || this.ctx.getConfig('picBed.current') || 'smms'
7071
let uploader = this.ctx.helper.uploader.get(type)
7172
if (!uploader) {
7273
type = 'smms'
7374
uploader = this.ctx.helper.uploader.get('smms')
74-
ctx.log.warn(`Can't find uploader - ${type}, swtich to default uploader - smms`)
75+
this.ctx.log.warn(`Can't find uploader - ${type}, swtich to default uploader - smms`)
7576
}
76-
await uploader.handle(ctx)
77-
for (let i in ctx.output) {
78-
ctx.output[i].type = type
77+
await uploader.handle(this.ctx)
78+
for (let i in this.ctx.output) {
79+
this.ctx.output[i].type = type
7980
}
80-
return ctx
81+
return this.ctx
8182
}
82-
private async afterUpload (ctx: PicGo): Promise<PicGo> {
83-
this.ctx.emit('afterUpload', ctx)
83+
private async afterUpload (): Promise<PicGo> {
84+
this.ctx.emit('afterUpload', this.ctx)
8485
this.ctx.emit('uploadProgress', 100)
85-
await this.handlePlugins(ctx.helper.afterUploadPlugins.getList(), ctx)
86+
await this.handlePlugins(this.ctx.helper.afterUploadPlugins)
8687
let msg = ''
87-
let length = ctx.output.length
88+
let length = this.ctx.output.length
8889
for (let i = 0; i < length; i++) {
89-
msg += handleUrlEncode(ctx.output[i].imgUrl)
90+
msg += handleUrlEncode(this.ctx.output[i].imgUrl)
9091
if (i !== length - 1) {
9192
msg += '\n'
9293
}
93-
delete ctx.output[i].base64Image
94-
delete ctx.output[i].buffer
94+
delete this.ctx.output[i].base64Image
95+
delete this.ctx.output[i].buffer
9596
}
96-
this.ctx.emit('finished', ctx)
97+
this.ctx.emit('finished', this.ctx)
9798
this.ctx.log.success(`\n${msg}`)
98-
return ctx
99+
return this.ctx
99100
}
100101

101-
private async handlePlugins (plugins: Plugin[], ctx: PicGo): Promise<PicGo> {
102-
await Promise.all(plugins.map(async (plugin: Plugin) => {
103-
await plugin.handle(ctx)
102+
private async handlePlugins (lifeCyclePlugins: LifecyclePlugins): Promise<PicGo> {
103+
const plugins = lifeCyclePlugins.getList()
104+
const pluginNames = lifeCyclePlugins.getIdList()
105+
const lifeCycleName = lifeCyclePlugins.getName()
106+
await Promise.all(plugins.map(async (plugin: Plugin, index: number) => {
107+
try {
108+
this.ctx.log.info(`${lifeCycleName}: ${pluginNames[index]} running`)
109+
await plugin.handle(this.ctx)
110+
} catch (e) {
111+
this.ctx.log.error(`${lifeCycleName}: ${pluginNames[index]} error`)
112+
throw e
113+
}
104114
}))
105-
return ctx
115+
return this.ctx
106116
}
107117
}
108118

src/lib/LifecyclePlugins.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Plugin } from '../utils/interfaces'
22

33
class LifecyclePlugins {
44
static currentPlugin: string | null
5-
list: Map<string, Plugin>
6-
pluginIdMap: Map<string, string[]>
7-
name: string
5+
private list: Map<string, Plugin>
6+
private pluginIdMap: Map<string, string[]>
7+
private name: string
88

99
constructor (name: string) {
1010
this.name = name
@@ -35,6 +35,10 @@ class LifecyclePlugins {
3535
}
3636
}
3737

38+
getName (): string {
39+
return this.name
40+
}
41+
3842
get (id: string): Plugin {
3943
return this.list.get(id)
4044
}

src/lib/Logger.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class Logger {
1818
[ILogType.error]: 'red'
1919
}
2020
private ctx: PicGo
21-
// private logger: Console
2221
private logLevel: string
2322
private logPath: string
2423
constructor (ctx: PicGo) {
@@ -28,13 +27,13 @@ class Logger {
2827
// if configPath is invalid then this.ctx.config === undefined
2928
// if not then check config.silent
3029
if (this.ctx.getConfig() === undefined || !this.ctx.getConfig('silent')) {
31-
let log = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]: `)
32-
console.log(log, ...msg)
30+
const logHeader = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`)
31+
console.log(logHeader, ...msg)
3332
this.logLevel = this.ctx.getConfig('settings.logLevel')
3433
const logPath = this.checkLogPathChange()
34+
// The incoming logPath is a value
35+
// lock the path with a closure
3536
setTimeout(() => {
36-
// The incoming logPath is a value
37-
// lock the path with a closure
3837
this.handleWriteLog(logPath, type, ...msg)
3938
}, 0)
4039
} else {
@@ -50,7 +49,7 @@ class Logger {
5049
return logPath
5150
}
5251

53-
protected handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void {
52+
private handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void {
5453
try {
5554
if (this.checkLogLevel(type, this.logLevel)) {
5655
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
@@ -62,16 +61,15 @@ class Logger {
6261
}
6362
})
6463
log += '\n'
65-
fs.appendFile(logPath, log, (err: Error) => {
66-
if (err) console.log(err)
67-
})
64+
// A synchronized approach to avoid log msg sequence errors
65+
fs.appendFileSync(logPath, log)
6866
}
6967
} catch (e) {
7068
console.log(e)
7169
}
7270
}
7371

74-
protected checkLogLevel (type: string, level: undefined | string | string[]): boolean {
72+
private checkLogLevel (type: string, level: undefined | string | string[]): boolean {
7573
if (level === undefined || level === 'all') {
7674
return true
7775
}

0 commit comments

Comments
 (0)