From b10b963e42d04e224feb483dd5936b95c1dce735 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Mon, 8 Feb 2021 15:12:45 +0800 Subject: [PATCH] :sparkles: Feature: add proxy & registry options for pluginHandler --- src/core/PicGo.ts | 3 +++ src/lib/Commander.ts | 4 +-- src/lib/Logger.ts | 20 +++------------ src/lib/PluginHandler.ts | 27 +++++++++++++------- src/plugins/commander/pluginHandler.ts | 19 +++++++++++--- src/types/index.d.ts | 34 +++++++++++++++++--------- 6 files changed, 66 insertions(+), 41 deletions(-) diff --git a/src/core/PicGo.ts b/src/core/PicGo.ts index 6919df1..923ad69 100644 --- a/src/core/PicGo.ts +++ b/src/core/PicGo.ts @@ -16,6 +16,7 @@ import Request from '../lib/Request' import DB from '../utils/db' import PluginHandler from '../lib/PluginHandler' import { IBuildInEvent } from '../utils/enum' +import { version } from '../../package.json' class PicGo extends EventEmitter implements IPicGo { private config!: IConfig @@ -31,6 +32,8 @@ class PicGo extends EventEmitter implements IPicGo { pluginLoader!: PluginLoader pluginHandler: PluginHandler Request!: Request + VERSION: string = version + GUI_VERSION?: string constructor (configPath: string = '') { super() diff --git a/src/lib/Commander.ts b/src/lib/Commander.ts index b1e8d15..9075ed1 100644 --- a/src/lib/Commander.ts +++ b/src/lib/Commander.ts @@ -3,7 +3,7 @@ import program, { CommanderStatic } from 'commander' import inquirer, { Inquirer } from 'inquirer' import { IPlugin } from '../types' import commanders from '../plugins/commander' -import pkg from '../../package.json' +import { version } from '../../package.json' class Commander { list: { @@ -23,7 +23,7 @@ class Commander { init (): void { this.program - .version(pkg.version, '-v, --version') + .version(version, '-v, --version') .option('-d, --debug', 'debug mode', () => { this.ctx.setConfig({ debug: true diff --git a/src/lib/Logger.ts b/src/lib/Logger.ts index bcccadc..bfb3dec 100644 --- a/src/lib/Logger.ts +++ b/src/lib/Logger.ts @@ -8,7 +8,6 @@ import { ILogType } from '../utils/enum' import { ILogArgvType, ILogArgvTypeWithError, - IConfig, Undefinable, ILogColor, ILogger @@ -30,29 +29,18 @@ class Logger implements ILogger { } private handleLog (type: ILogType, ...msg: ILogArgvTypeWithError[]): void { - // if configPath is invalid then this.ctx.config === undefined - // if not then check config.silent - if (this.ctx.getConfig() === undefined || !this.ctx.getConfig>('silent')) { + // check config.silent + if (!this.ctx.getConfig>('silent')) { const logHeader = chalk[this.level[type] as ILogColor](`[PicGo ${type.toUpperCase()}]:`) console.log(logHeader, ...msg) this.logLevel = this.ctx.getConfig('settings.logLevel') - const logPath = this.checkLogPathChange() - // The incoming logPath is a value - // lock the path with a closure + this.logPath = this.ctx.getConfig>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log') setTimeout(() => { - this.handleWriteLog(logPath, type, ...msg) + this.handleWriteLog(this.logPath, type, ...msg) }, 0) } } - private checkLogPathChange (): string { - const logPath = this.ctx.getConfig>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log') - if (logPath !== this.logPath) { - this.logPath = logPath - } - return logPath - } - private handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void { try { if (this.checkLogLevel(type, this.logLevel)) { diff --git a/src/lib/PluginHandler.ts b/src/lib/PluginHandler.ts index 314b454..62e261a 100644 --- a/src/lib/PluginHandler.ts +++ b/src/lib/PluginHandler.ts @@ -1,17 +1,24 @@ import PicGo from '../core/PicGo' import spawn from 'cross-spawn' -import { IResult, IProcessEnv, Undefinable, IPluginProcessResult } from '../types' +import { + IResult, + IProcessEnv, + IPluginProcessResult, + IPluginHandler, + IPluginHandlerOptions, + Undefinable +} from '../types' import { IBuildInEvent } from '../utils/enum' import { getProcessPluginName, getNormalPluginName } from '../utils/common' -class PluginHandler { +class PluginHandler implements IPluginHandler { // Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js - ctx: PicGo + private readonly ctx: PicGo constructor (ctx: PicGo) { this.ctx = ctx } - async install (plugins: string[], proxy: string = '', env?: IProcessEnv): Promise { + async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise { const installedPlugins: string[] = [] const processPlugins = plugins .map((item: string) => handlePluginNameProcess(this.ctx, item)) @@ -35,7 +42,7 @@ class PluginHandler { // install plugins must use fullNameList: // 1. install remote pacage // 2. install local pacage - const result = await this.execCommand('install', fullNameList, this.ctx.baseDir, proxy, env) + const result = await this.execCommand('install', fullNameList, this.ctx.baseDir, options, env) if (!result.code) { pkgNameList.forEach((pluginName: string) => { this.ctx.pluginLoader.registerPlugin(pluginName) @@ -103,13 +110,13 @@ class PluginHandler { } } - async update (plugins: string[], proxy: string = '', env?: IProcessEnv): Promise { + async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise { const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success) const pkgNameList = processPlugins.map(item => item.pkgName) if (pkgNameList.length > 0) { // update plugins must use pkgNameList: // npm update will use the package.json's name - const result = await this.execCommand('update', pkgNameList, this.ctx.baseDir, proxy, env) + const result = await this.execCommand('update', pkgNameList, this.ctx.baseDir, options, env) if (!result.code) { this.ctx.log.success('插件更新成功') this.ctx.emit('updateSuccess', { @@ -134,8 +141,10 @@ class PluginHandler { } } - async execCommand (cmd: string, modules: string[], where: string, proxy: string = '', env: IProcessEnv = {}): Promise { - const registry = this.ctx.getConfig>('registry') + private async execCommand (cmd: string, modules: string[], where: string, options: IPluginHandlerOptions = {}, env: IProcessEnv = {}): Promise { + // options first + const registry = options.registry || this.ctx.getConfig>('settings.registry') + const proxy = options.proxy || this.ctx.getConfig>('settings.proxy') return await new Promise((resolve: any): void => { let args = [cmd].concat(modules).concat('--color=always').concat('--save') if (registry) { diff --git a/src/plugins/commander/pluginHandler.ts b/src/plugins/commander/pluginHandler.ts index b2f0bb5..5805669 100644 --- a/src/plugins/commander/pluginHandler.ts +++ b/src/plugins/commander/pluginHandler.ts @@ -10,8 +10,14 @@ const pluginHandler: IPlugin = { .description('install picgo plugin') .alias('add') .option('-p, --proxy ', 'Add proxy for installing') + .option('-r, --registry ', 'Choose a registry for installing') .action((plugins: string[], program: any) => { - ctx.pluginHandler.install(plugins, program.proxy).catch((e) => { ctx.log.error(e) }) + const { proxy, registry } = program + const options = { + proxy, + registry + } + ctx.pluginHandler.install(plugins, options).catch((e) => { ctx.log.error(e) }) }) cmd.program .command('uninstall ') @@ -23,8 +29,15 @@ const pluginHandler: IPlugin = { cmd.program .command('update ') .description('update picgo plugin') - .action((plugins: string[]) => { - ctx.pluginHandler.update(plugins).catch((e) => { ctx.log.error(e) }) + .option('-p, --proxy ', 'Add proxy for installing') + .option('-r, --registry ', 'Choose a registry for installing') + .action((plugins: string[], program: any) => { + const { proxy, registry } = program + const options = { + proxy, + registry + } + ctx.pluginHandler.update(plugins, options).catch((e) => { ctx.log.error(e) }) }) } } diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 3040d48..e2deceb 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,7 +1,5 @@ -import PicGo from '../core/PicGo' import LifecyclePlugins from '../lib/LifecyclePlugins' import Commander from '../lib/Commander' -import PluginHandler from '../lib/PluginHandler' import PluginLoader from '../lib/PluginLoader' import Request from '../lib/Request' @@ -13,9 +11,11 @@ interface IPicGo extends NodeJS.EventEmitter { output: IImgInfo[] input: any[] pluginLoader: PluginLoader - pluginHandler: PluginHandler + pluginHandler: IPluginHandler Request: Request helper: IHelper + VERSION: string + GUI_VERSION?: string registerCommands: () => void getConfig: (name?: string) => T @@ -173,7 +173,7 @@ interface IConfig { aliyun?: IAliyunConfig imgur?: IImgurConfig transformer?: string - proxy: string + proxy?: string } picgoPlugins: { [propName: string]: boolean @@ -181,11 +181,13 @@ interface IConfig { debug?: boolean silent?: boolean settings?: { - logLevel: string - logPath: string + logLevel?: string + logPath?: string + /** for npm */ + registry?: string + /** for npm */ + proxy?: string } - /** 下载插件时 npm 命令自定义的 registry */ - registry: string [configOptions: string]: any } @@ -211,10 +213,20 @@ interface IPluginProcessResult { fullName: string } +interface IPluginHandler { + install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise + update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise + uninstall: (plugins: string[]) => Promise +} + +interface IPluginHandlerOptions { + proxy?: string + registry?: string +} + /** * for picgo npm plugins */ - type IPicGoPlugin = (ctx: IPicGo) => IPicGoPluginInterface /** @@ -224,11 +236,11 @@ interface IPicGoPluginInterface { /** * since PicGo-Core v1.5, register will inject ctx */ - register: (ctx?: PicGo) => void + register: (ctx?: IPicGo) => void /** * this plugin's config */ - config?: (ctx?: PicGo) => IPluginConfig[] + config?: (ctx?: IPicGo) => IPluginConfig[] /** * register uploader name */