@@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
2
2
import PicGo from './PicGo'
3
3
import { Plugin } from '../utils/interfaces'
4
4
import { handleUrlEncode } from '../utils/common'
5
+ import LifecyclePlugins from '../lib/LifecyclePlugins'
5
6
6
7
class Lifecycle extends EventEmitter {
7
8
configPath : string
@@ -22,11 +23,11 @@ class Lifecycle extends EventEmitter {
22
23
this . ctx . output = [ ]
23
24
24
25
// 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 ( )
30
31
return this . ctx
31
32
} catch ( e ) {
32
33
this . ctx . log . warn ( 'failed' )
@@ -38,71 +39,80 @@ class Lifecycle extends EventEmitter {
38
39
}
39
40
}
40
41
}
41
- private async beforeTransform ( ctx : PicGo ) : Promise < PicGo > {
42
+ private async beforeTransform ( ) : Promise < PicGo > {
42
43
this . ctx . emit ( 'uploadProgress' , 0 )
43
- this . ctx . emit ( 'beforeTransform' , ctx )
44
+ this . ctx . emit ( 'beforeTransform' , this . ctx )
44
45
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
47
48
}
48
- private async doTransform ( ctx : PicGo ) : Promise < PicGo > {
49
+ private async doTransform ( ) : Promise < PicGo > {
49
50
this . ctx . emit ( 'uploadProgress' , 30 )
50
51
this . ctx . log . info ( 'Transforming...' )
51
- let type = ctx . getConfig ( 'picBed.transformer' ) || 'path'
52
+ let type = this . ctx . getConfig ( 'picBed.transformer' ) || 'path'
52
53
let transformer = this . ctx . helper . transformer . get ( type )
53
54
if ( ! transformer ) {
54
55
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` )
56
57
}
57
- await transformer . handle ( ctx )
58
- return ctx
58
+ await transformer . handle ( this . ctx )
59
+ return this . ctx
59
60
}
60
- private async beforeUpload ( ctx : PicGo ) : Promise < PicGo > {
61
+ private async beforeUpload ( ) : Promise < PicGo > {
61
62
this . ctx . emit ( 'uploadProgress' , 60 )
62
63
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
66
67
}
67
- private async doUpload ( ctx : PicGo ) : Promise < PicGo > {
68
+ private async doUpload ( ) : Promise < PicGo > {
68
69
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'
70
71
let uploader = this . ctx . helper . uploader . get ( type )
71
72
if ( ! uploader ) {
72
73
type = 'smms'
73
74
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` )
75
76
}
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
79
80
}
80
- return ctx
81
+ return this . ctx
81
82
}
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 )
84
85
this . ctx . emit ( 'uploadProgress' , 100 )
85
- await this . handlePlugins ( ctx . helper . afterUploadPlugins . getList ( ) , ctx )
86
+ await this . handlePlugins ( this . ctx . helper . afterUploadPlugins )
86
87
let msg = ''
87
- let length = ctx . output . length
88
+ let length = this . ctx . output . length
88
89
for ( let i = 0 ; i < length ; i ++ ) {
89
- msg += handleUrlEncode ( ctx . output [ i ] . imgUrl )
90
+ msg += handleUrlEncode ( this . ctx . output [ i ] . imgUrl )
90
91
if ( i !== length - 1 ) {
91
92
msg += '\n'
92
93
}
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
95
96
}
96
- this . ctx . emit ( 'finished' , ctx )
97
+ this . ctx . emit ( 'finished' , this . ctx )
97
98
this . ctx . log . success ( `\n${ msg } ` )
98
- return ctx
99
+ return this . ctx
99
100
}
100
101
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
+ }
104
114
} ) )
105
- return ctx
115
+ return this . ctx
106
116
}
107
117
}
108
118
0 commit comments