Skip to content

Commit

Permalink
✨ Feature: add proxy & registry options for pluginHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Feb 8, 2021
1 parent 35e15b0 commit b10b963
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
3 changes: 3 additions & 0 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
Expand Down
20 changes: 4 additions & 16 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ILogType } from '../utils/enum'
import {
ILogArgvType,
ILogArgvTypeWithError,
IConfig,
Undefinable,
ILogColor,
ILogger
Expand All @@ -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<IConfig>() === undefined || !this.ctx.getConfig<Undefinable<string>>('silent')) {
// check config.silent
if (!this.ctx.getConfig<Undefinable<string>>('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<Undefinable<string>>('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<Undefinable<string>>('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)) {
Expand Down
27 changes: 18 additions & 9 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
const installedPlugins: string[] = []
const processPlugins = plugins
.map((item: string) => handlePluginNameProcess(this.ctx, item))
Expand All @@ -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)
Expand Down Expand Up @@ -103,13 +110,13 @@ class PluginHandler {
}
}

async update (plugins: string[], proxy: string = '', env?: IProcessEnv): Promise<void> {
async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
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', {
Expand All @@ -134,8 +141,10 @@ class PluginHandler {
}
}

async execCommand (cmd: string, modules: string[], where: string, proxy: string = '', env: IProcessEnv = {}): Promise<IResult> {
const registry = this.ctx.getConfig<Undefinable<string>>('registry')
private async execCommand (cmd: string, modules: string[], where: string, options: IPluginHandlerOptions = {}, env: IProcessEnv = {}): Promise<IResult> {
// options first
const registry = options.registry || this.ctx.getConfig<Undefinable<string>>('settings.registry')
const proxy = options.proxy || this.ctx.getConfig<Undefinable<string>>('settings.proxy')
return await new Promise((resolve: any): void => {
let args = [cmd].concat(modules).concat('--color=always').concat('--save')
if (registry) {
Expand Down
19 changes: 16 additions & 3 deletions src/plugins/commander/pluginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ const pluginHandler: IPlugin = {
.description('install picgo plugin')
.alias('add')
.option('-p, --proxy <proxy>', 'Add proxy for installing')
.option('-r, --registry <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 <plugins...>')
Expand All @@ -23,8 +29,15 @@ const pluginHandler: IPlugin = {
cmd.program
.command('update <plugins...>')
.description('update picgo plugin')
.action((plugins: string[]) => {
ctx.pluginHandler.update(plugins).catch((e) => { ctx.log.error(e) })
.option('-p, --proxy <proxy>', 'Add proxy for installing')
.option('-r, --registry <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) })
})
}
}
Expand Down
34 changes: 23 additions & 11 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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: <T>(name?: string) => T
Expand Down Expand Up @@ -173,19 +173,21 @@ interface IConfig {
aliyun?: IAliyunConfig
imgur?: IImgurConfig
transformer?: string
proxy: string
proxy?: string
}
picgoPlugins: {
[propName: string]: boolean
}
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
}

Expand All @@ -211,10 +213,20 @@ interface IPluginProcessResult {
fullName: string
}

interface IPluginHandler {
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
uninstall: (plugins: string[]) => Promise<void>
}

interface IPluginHandlerOptions {
proxy?: string
registry?: string
}

/**
* for picgo npm plugins
*/

type IPicGoPlugin = (ctx: IPicGo) => IPicGoPluginInterface

/**
Expand All @@ -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
*/
Expand Down

0 comments on commit b10b963

Please sign in to comment.