@@ -2,125 +2,128 @@ import { EventEmitter } from 'events'
2
2
import { ILifecyclePlugins , IPicGo , IPlugin , Undefinable } from '../types'
3
3
import { handleUrlEncode } from '../utils/common'
4
4
import { IBuildInEvent } from '../utils/enum'
5
+ import { createContext } from '../utils/createContext'
5
6
6
7
class Lifecycle extends EventEmitter {
7
- private ctx : IPicGo
8
+ private readonly ctx : IPicGo
8
9
9
10
constructor ( ctx : IPicGo ) {
10
11
super ( )
11
12
this . ctx = ctx
12
13
}
13
14
14
15
async start ( input : any [ ] ) : Promise < IPicGo > {
16
+ // ensure every upload process has an unique context
17
+ const ctx = createContext ( this . ctx )
15
18
try {
16
19
// images input
17
20
if ( ! Array . isArray ( input ) ) {
18
21
throw new Error ( 'Input must be an array.' )
19
22
}
20
- this . ctx . input = input
21
- this . ctx . output = [ ]
23
+ ctx . input = input
24
+ ctx . output = [ ]
22
25
23
26
// lifecycle main
24
- await this . beforeTransform ( )
25
- await this . doTransform ( )
26
- await this . beforeUpload ( )
27
- await this . doUpload ( )
28
- await this . afterUpload ( )
29
- return this . ctx
27
+ await this . beforeTransform ( ctx )
28
+ await this . doTransform ( ctx )
29
+ await this . beforeUpload ( ctx )
30
+ await this . doUpload ( ctx )
31
+ await this . afterUpload ( ctx )
32
+ return ctx
30
33
} catch ( e ) {
31
- this . ctx . log . warn ( 'failed' )
32
- this . ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , - 1 )
33
- this . ctx . emit ( IBuildInEvent . FAILED , e )
34
- this . ctx . log . error ( e )
35
- if ( this . ctx . getConfig < Undefinable < string > > ( 'debug' ) ) {
34
+ ctx . log . warn ( 'failed' )
35
+ ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , - 1 )
36
+ ctx . emit ( IBuildInEvent . FAILED , e )
37
+ ctx . log . error ( e )
38
+ if ( ctx . getConfig < Undefinable < string > > ( 'debug' ) ) {
36
39
throw e
37
40
}
38
- return this . ctx
41
+ return ctx
39
42
}
40
43
}
41
44
42
- private async beforeTransform ( ) : Promise < IPicGo > {
43
- this . ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 0 )
44
- this . ctx . emit ( IBuildInEvent . BEFORE_TRANSFORM , this . ctx )
45
- this . ctx . log . info ( 'Before transform' )
46
- await this . handlePlugins ( this . ctx . helper . beforeTransformPlugins )
47
- return this . ctx
45
+ private async beforeTransform ( ctx : IPicGo ) : Promise < IPicGo > {
46
+ ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 0 )
47
+ ctx . emit ( IBuildInEvent . BEFORE_TRANSFORM , ctx )
48
+ ctx . log . info ( 'Before transform' )
49
+ await this . handlePlugins ( ctx . helper . beforeTransformPlugins , ctx )
50
+ return ctx
48
51
}
49
52
50
- private async doTransform ( ) : Promise < IPicGo > {
51
- this . ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 30 )
52
- const type = this . ctx . getConfig < Undefinable < string > > ( 'picBed.transformer' ) || 'path'
53
+ private async doTransform ( ctx : IPicGo ) : Promise < IPicGo > {
54
+ ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 30 )
55
+ const type = ctx . getConfig < Undefinable < string > > ( 'picBed.transformer' ) || 'path'
53
56
let currentTransformer = type
54
- let transformer = this . ctx . helper . transformer . get ( type )
57
+ let transformer = ctx . helper . transformer . get ( type )
55
58
if ( ! transformer ) {
56
- transformer = this . ctx . helper . transformer . get ( 'path' )
59
+ transformer = ctx . helper . transformer . get ( 'path' )
57
60
currentTransformer = 'path'
58
- this . ctx . log . warn ( `Can't find transformer - ${ type } , switch to default transformer - path` )
61
+ ctx . log . warn ( `Can't find transformer - ${ type } , switch to default transformer - path` )
59
62
}
60
- this . ctx . log . info ( `Transforming... Current transformer is [${ currentTransformer } ]` )
61
- await transformer ?. handle ( this . ctx )
62
- return this . ctx
63
+ ctx . log . info ( `Transforming... Current transformer is [${ currentTransformer } ]` )
64
+ await transformer ?. handle ( ctx )
65
+ return ctx
63
66
}
64
67
65
- private async beforeUpload ( ) : Promise < IPicGo > {
66
- this . ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 60 )
67
- this . ctx . log . info ( 'Before upload' )
68
- this . ctx . emit ( IBuildInEvent . BEFORE_UPLOAD , this . ctx )
69
- await this . handlePlugins ( this . ctx . helper . beforeUploadPlugins )
70
- return this . ctx
68
+ private async beforeUpload ( ctx : IPicGo ) : Promise < IPicGo > {
69
+ ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 60 )
70
+ ctx . log . info ( 'Before upload' )
71
+ ctx . emit ( IBuildInEvent . BEFORE_UPLOAD , ctx )
72
+ await this . handlePlugins ( ctx . helper . beforeUploadPlugins , ctx )
73
+ return ctx
71
74
}
72
75
73
- private async doUpload ( ) : Promise < IPicGo > {
74
- let type = this . ctx . getConfig < Undefinable < string > > ( 'picBed.uploader' ) || this . ctx . getConfig < Undefinable < string > > ( 'picBed.current' ) || 'smms'
75
- let uploader = this . ctx . helper . uploader . get ( type )
76
+ private async doUpload ( ctx : IPicGo ) : Promise < IPicGo > {
77
+ let type = ctx . getConfig < Undefinable < string > > ( 'picBed.uploader' ) || ctx . getConfig < Undefinable < string > > ( 'picBed.current' ) || 'smms'
78
+ let uploader = ctx . helper . uploader . get ( type )
76
79
let currentTransformer = type
77
80
if ( ! uploader ) {
78
81
type = 'smms'
79
82
currentTransformer = 'smms'
80
- uploader = this . ctx . helper . uploader . get ( 'smms' )
81
- this . ctx . log . warn ( `Can't find uploader - ${ type } , switch to default uploader - smms` )
83
+ uploader = ctx . helper . uploader . get ( 'smms' )
84
+ ctx . log . warn ( `Can't find uploader - ${ type } , switch to default uploader - smms` )
82
85
}
83
- this . ctx . log . info ( `Uploading... Current uploader is [${ currentTransformer } ]` )
84
- await uploader ?. handle ( this . ctx )
85
- for ( const outputImg of this . ctx . output ) {
86
+ ctx . log . info ( `Uploading... Current uploader is [${ currentTransformer } ]` )
87
+ await uploader ?. handle ( ctx )
88
+ for ( const outputImg of ctx . output ) {
86
89
outputImg . type = type
87
90
}
88
- return this . ctx
91
+ return ctx
89
92
}
90
93
91
- private async afterUpload ( ) : Promise < IPicGo > {
92
- this . ctx . emit ( IBuildInEvent . AFTER_UPLOAD , this . ctx )
93
- this . ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 100 )
94
- await this . handlePlugins ( this . ctx . helper . afterUploadPlugins )
94
+ private async afterUpload ( ctx : IPicGo ) : Promise < IPicGo > {
95
+ ctx . emit ( IBuildInEvent . AFTER_UPLOAD , ctx )
96
+ ctx . emit ( IBuildInEvent . UPLOAD_PROGRESS , 100 )
97
+ await this . handlePlugins ( ctx . helper . afterUploadPlugins , ctx )
95
98
let msg = ''
96
- const length = this . ctx . output . length
99
+ const length = ctx . output . length
97
100
for ( let i = 0 ; i < length ; i ++ ) {
98
- msg += handleUrlEncode ( this . ctx . output [ i ] . imgUrl )
101
+ msg += handleUrlEncode ( ctx . output [ i ] . imgUrl )
99
102
if ( i !== length - 1 ) {
100
103
msg += '\n'
101
104
}
102
- delete this . ctx . output [ i ] . base64Image
103
- delete this . ctx . output [ i ] . buffer
105
+ delete ctx . output [ i ] . base64Image
106
+ delete ctx . output [ i ] . buffer
104
107
}
105
- this . ctx . emit ( IBuildInEvent . FINISHED , this . ctx )
106
- this . ctx . log . success ( `\n${ msg } ` )
107
- return this . ctx
108
+ ctx . emit ( IBuildInEvent . FINISHED , ctx )
109
+ ctx . log . success ( `\n${ msg } ` )
110
+ return ctx
108
111
}
109
112
110
- private async handlePlugins ( lifeCyclePlugins : ILifecyclePlugins ) : Promise < IPicGo > {
113
+ private async handlePlugins ( lifeCyclePlugins : ILifecyclePlugins , ctx : IPicGo ) : Promise < IPicGo > {
111
114
const plugins = lifeCyclePlugins . getList ( )
112
115
const pluginNames = lifeCyclePlugins . getIdList ( )
113
116
const lifeCycleName = lifeCyclePlugins . getName ( )
114
117
await Promise . all ( plugins . map ( async ( plugin : IPlugin , index : number ) => {
115
118
try {
116
- this . ctx . log . info ( `${ lifeCycleName } : ${ pluginNames [ index ] } running` )
117
- await plugin . handle ( this . ctx )
119
+ ctx . log . info ( `${ lifeCycleName } : ${ pluginNames [ index ] } running` )
120
+ await plugin . handle ( ctx )
118
121
} catch ( e ) {
119
- this . ctx . log . error ( `${ lifeCycleName } : ${ pluginNames [ index ] } error` )
122
+ ctx . log . error ( `${ lifeCycleName } : ${ pluginNames [ index ] } error` )
120
123
throw e
121
124
}
122
125
} ) )
123
- return this . ctx
126
+ return ctx
124
127
}
125
128
}
126
129
0 commit comments