Skip to content

Commit

Permalink
✨ Feature: add config methods && pluginHandler to ctx
Browse files Browse the repository at this point in the history
1. removeConfig
2. unsetConfig
3. ctx.pluginHandler
  • Loading branch information
Molunerfinn committed Dec 26, 2019
1 parent 5e68426 commit f9bb9fb
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ import Lifecycle from './Lifecycle'
import LifecyclePlugins from '../lib/LifecyclePlugins'
import uploaders from '../plugins/uploader'
import transformers from '../plugins/transformer'
import { saveConfig, initConfig } from '../utils/config'
import PluginLoader from '../lib/PluginLoader'
import { get, set } from 'lodash'
import { get, set, unset } from 'lodash'
import { Helper, ImgInfo, Config } from '../utils/interfaces'
import getClipboardImage from '../utils/getClipboardImage'
import Request from '../lib/Request'
import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'

class PicGo extends EventEmitter {
private config: Config
private lifecycle: Lifecycle
private db: DB
configPath: string
baseDir: string
helper: Helper
log: Logger
cmd: Commander
config: Config
output: ImgInfo[]
input: any[]
pluginLoader: PluginLoader
pluginHandler: PluginHandler
Request: Request
private lifecycle: Lifecycle

constructor (configPath: string = '') {
super()
Expand All @@ -42,38 +45,49 @@ class PicGo extends EventEmitter {
}
this.log = new Logger(this)
this.cmd = new Commander(this)
this.pluginHandler = new PluginHandler(this)
this.initConfig()
this.init()
}

init (): any {
setCurrentPluginName (name: string): void {
LifecyclePlugins.currentPlugin = name
}

initConfig (): void {
if (this.configPath === '') {
this.configPath = homedir() + '/.picgo/config.json'
}
if (path.extname(this.configPath).toUpperCase() !== '.JSON') {
this.configPath = ''
return this.log.error('The configuration file only supports JSON format.')
this.log.error('The configuration file only supports JSON format.')
return
}
this.baseDir = path.dirname(this.configPath)
const exist = fs.pathExistsSync(this.configPath)
if (!exist) {
fs.ensureFileSync(`${this.configPath}`)
}
this.db = new DB(this)
this.config = this.db.read().value()
}

init (): any {
try {
// init config
const config = initConfig(this.configPath).read().value()
this.config = config
// load self plugins
this.Request = new Request(this)
this.pluginLoader = new PluginLoader(this)
this.setCurrentPluginName('picgo')
uploaders(this)
transformers(this)
this.setCurrentPluginName(null)
// load third-party plugins
this.pluginLoader.load()
this.lifecycle = new Lifecycle(this)
} catch (e) {
this.emit('uploadProgress', -1)
this.log.error(e)
Promise.reject(e)
throw e
}
}

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

// get config
getConfig (name: string = ''): any {
if (!this.config) return
if (name) {
return get(this.config, name)
} else {
Expand All @@ -96,19 +111,32 @@ class PicGo extends EventEmitter {
}

// save to db
saveConfig (config: any): void {
saveConfig(this.configPath, config)
saveConfig (config: Config): void {
this.setConfig(config)
this.db.saveConfig(config)
}

// remove from db
removeConfig (key: string, propName: string): void {
if (!key || !propName) return
this.unsetConfig(key, propName)
this.db.unset(key, propName)
}

// set config for ctx but will not be saved to db
// it's more lightweight
setConfig (config: any): void {
setConfig (config: Config): void {
Object.keys(config).forEach((name: string) => {
set(this.config, name, config[name])
})
}

// unset config for ctx but won't be saved to db
unsetConfig (key: string, propName: string): void {
if (!key || !propName) return
unset(this.getConfig(key), propName)
}

async upload (input?: any[]): Promise<void | string | Error> {
if (this.configPath === '') return this.log.error('The configuration file only supports JSON format.')
// upload from clipboard
Expand Down

0 comments on commit f9bb9fb

Please sign in to comment.