@@ -8,25 +8,28 @@ import Lifecycle from './Lifecycle'
8
8
import LifecyclePlugins from '../lib/LifecyclePlugins'
9
9
import uploaders from '../plugins/uploader'
10
10
import transformers from '../plugins/transformer'
11
- import { saveConfig , initConfig } from '../utils/config'
12
11
import PluginLoader from '../lib/PluginLoader'
13
- import { get , set } from 'lodash'
12
+ import { get , set , unset } from 'lodash'
14
13
import { Helper , ImgInfo , Config } from '../utils/interfaces'
15
14
import getClipboardImage from '../utils/getClipboardImage'
16
15
import Request from '../lib/Request'
16
+ import DB from '../utils/db'
17
+ import PluginHandler from '../lib/PluginHandler'
17
18
18
19
class PicGo extends EventEmitter {
20
+ private config : Config
21
+ private lifecycle : Lifecycle
22
+ private db : DB
19
23
configPath : string
20
24
baseDir : string
21
25
helper : Helper
22
26
log : Logger
23
27
cmd : Commander
24
- config : Config
25
28
output : ImgInfo [ ]
26
29
input : any [ ]
27
30
pluginLoader : PluginLoader
31
+ pluginHandler : PluginHandler
28
32
Request : Request
29
- private lifecycle : Lifecycle
30
33
31
34
constructor ( configPath : string = '' ) {
32
35
super ( )
@@ -42,38 +45,49 @@ class PicGo extends EventEmitter {
42
45
}
43
46
this . log = new Logger ( this )
44
47
this . cmd = new Commander ( this )
48
+ this . pluginHandler = new PluginHandler ( this )
49
+ this . initConfig ( )
45
50
this . init ( )
46
51
}
47
52
48
- init ( ) : any {
53
+ setCurrentPluginName ( name : string ) : void {
54
+ LifecyclePlugins . currentPlugin = name
55
+ }
56
+
57
+ initConfig ( ) : void {
49
58
if ( this . configPath === '' ) {
50
59
this . configPath = homedir ( ) + '/.picgo/config.json'
51
60
}
52
61
if ( path . extname ( this . configPath ) . toUpperCase ( ) !== '.JSON' ) {
53
62
this . configPath = ''
54
- return this . log . error ( 'The configuration file only supports JSON format.' )
63
+ this . log . error ( 'The configuration file only supports JSON format.' )
64
+ return
55
65
}
56
66
this . baseDir = path . dirname ( this . configPath )
57
67
const exist = fs . pathExistsSync ( this . configPath )
58
68
if ( ! exist ) {
59
69
fs . ensureFileSync ( `${ this . configPath } ` )
60
70
}
71
+ this . db = new DB ( this )
72
+ this . config = this . db . read ( ) . value ( )
73
+ }
74
+
75
+ init ( ) : any {
61
76
try {
62
- // init config
63
- const config = initConfig ( this . configPath ) . read ( ) . value ( )
64
- this . config = config
65
77
// load self plugins
66
78
this . Request = new Request ( this )
67
79
this . pluginLoader = new PluginLoader ( this )
80
+ this . setCurrentPluginName ( 'picgo' )
68
81
uploaders ( this )
69
82
transformers ( this )
83
+ this . setCurrentPluginName ( null )
70
84
// load third-party plugins
71
85
this . pluginLoader . load ( )
72
86
this . lifecycle = new Lifecycle ( this )
73
87
} catch ( e ) {
74
88
this . emit ( 'uploadProgress' , - 1 )
75
89
this . log . error ( e )
76
- Promise . reject ( e )
90
+ throw e
77
91
}
78
92
}
79
93
@@ -88,6 +102,7 @@ class PicGo extends EventEmitter {
88
102
89
103
// get config
90
104
getConfig ( name : string = '' ) : any {
105
+ if ( ! this . config ) return
91
106
if ( name ) {
92
107
return get ( this . config , name )
93
108
} else {
@@ -96,19 +111,32 @@ class PicGo extends EventEmitter {
96
111
}
97
112
98
113
// save to db
99
- saveConfig ( config : any ) : void {
100
- saveConfig ( this . configPath , config )
114
+ saveConfig ( config : Config ) : void {
101
115
this . setConfig ( config )
116
+ this . db . saveConfig ( config )
117
+ }
118
+
119
+ // remove from db
120
+ removeConfig ( key : string , propName : string ) : void {
121
+ if ( ! key || ! propName ) return
122
+ this . unsetConfig ( key , propName )
123
+ this . db . unset ( key , propName )
102
124
}
103
125
104
126
// set config for ctx but will not be saved to db
105
127
// it's more lightweight
106
- setConfig ( config : any ) : void {
128
+ setConfig ( config : Config ) : void {
107
129
Object . keys ( config ) . forEach ( ( name : string ) => {
108
130
set ( this . config , name , config [ name ] )
109
131
} )
110
132
}
111
133
134
+ // unset config for ctx but won't be saved to db
135
+ unsetConfig ( key : string , propName : string ) : void {
136
+ if ( ! key || ! propName ) return
137
+ unset ( this . getConfig ( key ) , propName )
138
+ }
139
+
112
140
async upload ( input ?: any [ ] ) : Promise < void | string | Error > {
113
141
if ( this . configPath === '' ) return this . log . error ( 'The configuration file only supports JSON format.' )
114
142
// upload from clipboard
0 commit comments