Skip to content

Commit

Permalink
✨ Feature: limit some of config's capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Mar 13, 2021
1 parent df22d8e commit f901505
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import { IBuildInEvent, IBusEvent } from '../utils/enum'
import { version } from '../../package.json'
import { eventBus } from '../utils/eventBus'
import { RequestPromiseAPI } from 'request-promise-native'
import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'

class PicGo extends EventEmitter implements IPicGo {
private config!: IConfig
private _config!: IConfig
private lifecycle!: Lifecycle
private db!: DB
private _pluginLoader!: PluginLoader
Expand Down Expand Up @@ -83,7 +84,7 @@ class PicGo extends EventEmitter implements IPicGo {

private initConfig (): void {
this.db = new DB(this)
this.config = this.db.read().value()
this._config = this.db.read().value()
}

private init (): void {
Expand Down Expand Up @@ -114,26 +115,43 @@ class PicGo extends EventEmitter implements IPicGo {

getConfig<T> (name?: string): T {
if (!name) {
return this.config as unknown as T
return this._config as unknown as T
} else {
return get(this.config, name)
return get(this._config, name)
}
}

saveConfig (config: object): void {
saveConfig (config: IStringKeyMap<any>): void {
if (!isInputConfigValid(config)) {
this.log.warn('the format of config is invalid, please provide object')
return
}
this.setConfig(config)
this.db.saveConfig(config)
}

removeConfig (key: string, propName: string): void {
if (!key || !propName) return
if (isConfigKeyInBlackList(key)) {
this.log.warn(`the config.${key} can't be removed`)
return
}
this.unsetConfig(key, propName)
this.db.unset(key, propName)
}

setConfig (config: IStringKeyMap<any>): void {
if (!isInputConfigValid(config)) {
this.log.warn('the format of config is invalid, please provide object')
return
}
Object.keys(config).forEach((name: string) => {
set(this.config, name, config[name])
if (isConfigKeyInBlackList(name)) {
this.log.warn(`the config.${name} can't be modified`)
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete config[name]
}
set(this._config, name, config[name])
eventBus.emit(IBusEvent.CONFIG_CHANGE, {
configName: name,
value: config[name]
Expand All @@ -143,6 +161,10 @@ class PicGo extends EventEmitter implements IPicGo {

unsetConfig (key: string, propName: string): void {
if (!key || !propName) return
if (isConfigKeyInBlackList(key)) {
this.log.warn(`the config.${key} can't be unset`)
return
}
unset(this.getConfig(key), propName)
}

Expand Down Expand Up @@ -178,7 +200,6 @@ class PicGo extends EventEmitter implements IPicGo {
return this.output
}
} catch (e) {
this.log.error(e)
this.emit(IBuildInEvent.FAILED, e)
throw e
}
Expand Down
48 changes: 48 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,51 @@ export const removePluginVersion = (nameOrPath: string): string => {
}
}
}

/**
* the config black item list which won't be setted
* only can be got
*/
export const configBlackList = ['uploaded']

/**
* check some config key is in blackList
* @param key
*/
export const isConfigKeyInBlackList = (key: string): boolean => {
return configBlackList.some(blackItem => key.startsWith(blackItem))
}

/**
* check the input config is valid
* config must be object such as { xxx: 'xxx' }
* && can't be array
* @param config
* @returns
*/
export const isInputConfigValid = (config: any): boolean => {
if (
typeof config === 'object' &&
!Array.isArray(config) &&
Object.keys(config).length > 0
) {
return true
}
return false
}

// hold...
// export const configWhiteList: RegExp[] = [
// /^picBed/,
// /^picgoPlugins/,
// /^@[^/]+\/picgo-plugin-/,
// /debug/,
// /silent/,
// /configPath/,
// /^settings/,
// /PICGO_ENV/
// ]

// export const isConfigKeyInWhiteList = (key: string): boolean => {
// return configWhiteList.some(whiteItem => whiteItem.test(key))
// }

0 comments on commit f901505

Please sign in to comment.