From f901505fa439f6b5e91746669f3368adb19039e2 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sat, 13 Mar 2021 23:04:13 +0800 Subject: [PATCH] :sparkles: Feature: limit some of config's capabilities --- src/core/PicGo.ts | 35 ++++++++++++++++++++++++++------- src/utils/common.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/core/PicGo.ts b/src/core/PicGo.ts index dc6f880..9880a06 100644 --- a/src/core/PicGo.ts +++ b/src/core/PicGo.ts @@ -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 @@ -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 { @@ -114,26 +115,43 @@ class PicGo extends EventEmitter implements IPicGo { getConfig (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): 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): 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] @@ -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) } @@ -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 } diff --git a/src/utils/common.ts b/src/utils/common.ts index 5c57c99..1fc55df 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -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)) +// }