Skip to content

Commit f9bb9fb

Browse files
committed
✨ Feature: add config methods && pluginHandler to ctx
1. removeConfig 2. unsetConfig 3. ctx.pluginHandler
1 parent 5e68426 commit f9bb9fb

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/core/PicGo.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ import Lifecycle from './Lifecycle'
88
import LifecyclePlugins from '../lib/LifecyclePlugins'
99
import uploaders from '../plugins/uploader'
1010
import transformers from '../plugins/transformer'
11-
import { saveConfig, initConfig } from '../utils/config'
1211
import PluginLoader from '../lib/PluginLoader'
13-
import { get, set } from 'lodash'
12+
import { get, set, unset } from 'lodash'
1413
import { Helper, ImgInfo, Config } from '../utils/interfaces'
1514
import getClipboardImage from '../utils/getClipboardImage'
1615
import Request from '../lib/Request'
16+
import DB from '../utils/db'
17+
import PluginHandler from '../lib/PluginHandler'
1718

1819
class PicGo extends EventEmitter {
20+
private config: Config
21+
private lifecycle: Lifecycle
22+
private db: DB
1923
configPath: string
2024
baseDir: string
2125
helper: Helper
2226
log: Logger
2327
cmd: Commander
24-
config: Config
2528
output: ImgInfo[]
2629
input: any[]
2730
pluginLoader: PluginLoader
31+
pluginHandler: PluginHandler
2832
Request: Request
29-
private lifecycle: Lifecycle
3033

3134
constructor (configPath: string = '') {
3235
super()
@@ -42,38 +45,49 @@ class PicGo extends EventEmitter {
4245
}
4346
this.log = new Logger(this)
4447
this.cmd = new Commander(this)
48+
this.pluginHandler = new PluginHandler(this)
49+
this.initConfig()
4550
this.init()
4651
}
4752

48-
init (): any {
53+
setCurrentPluginName (name: string): void {
54+
LifecyclePlugins.currentPlugin = name
55+
}
56+
57+
initConfig (): void {
4958
if (this.configPath === '') {
5059
this.configPath = homedir() + '/.picgo/config.json'
5160
}
5261
if (path.extname(this.configPath).toUpperCase() !== '.JSON') {
5362
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
5565
}
5666
this.baseDir = path.dirname(this.configPath)
5767
const exist = fs.pathExistsSync(this.configPath)
5868
if (!exist) {
5969
fs.ensureFileSync(`${this.configPath}`)
6070
}
71+
this.db = new DB(this)
72+
this.config = this.db.read().value()
73+
}
74+
75+
init (): any {
6176
try {
62-
// init config
63-
const config = initConfig(this.configPath).read().value()
64-
this.config = config
6577
// load self plugins
6678
this.Request = new Request(this)
6779
this.pluginLoader = new PluginLoader(this)
80+
this.setCurrentPluginName('picgo')
6881
uploaders(this)
6982
transformers(this)
83+
this.setCurrentPluginName(null)
7084
// load third-party plugins
7185
this.pluginLoader.load()
7286
this.lifecycle = new Lifecycle(this)
7387
} catch (e) {
7488
this.emit('uploadProgress', -1)
7589
this.log.error(e)
76-
Promise.reject(e)
90+
throw e
7791
}
7892
}
7993

@@ -88,6 +102,7 @@ class PicGo extends EventEmitter {
88102

89103
// get config
90104
getConfig (name: string = ''): any {
105+
if (!this.config) return
91106
if (name) {
92107
return get(this.config, name)
93108
} else {
@@ -96,19 +111,32 @@ class PicGo extends EventEmitter {
96111
}
97112

98113
// save to db
99-
saveConfig (config: any): void {
100-
saveConfig(this.configPath, config)
114+
saveConfig (config: Config): void {
101115
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)
102124
}
103125

104126
// set config for ctx but will not be saved to db
105127
// it's more lightweight
106-
setConfig (config: any): void {
128+
setConfig (config: Config): void {
107129
Object.keys(config).forEach((name: string) => {
108130
set(this.config, name, config[name])
109131
})
110132
}
111133

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+
112140
async upload (input?: any[]): Promise<void | string | Error> {
113141
if (this.configPath === '') return this.log.error('The configuration file only supports JSON format.')
114142
// upload from clipboard

0 commit comments

Comments
 (0)