Skip to content

Commit b10b963

Browse files
committed
✨ Feature: add proxy & registry options for pluginHandler
1 parent 35e15b0 commit b10b963

File tree

6 files changed

+66
-41
lines changed

6 files changed

+66
-41
lines changed

src/core/PicGo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Request from '../lib/Request'
1616
import DB from '../utils/db'
1717
import PluginHandler from '../lib/PluginHandler'
1818
import { IBuildInEvent } from '../utils/enum'
19+
import { version } from '../../package.json'
1920

2021
class PicGo extends EventEmitter implements IPicGo {
2122
private config!: IConfig
@@ -31,6 +32,8 @@ class PicGo extends EventEmitter implements IPicGo {
3132
pluginLoader!: PluginLoader
3233
pluginHandler: PluginHandler
3334
Request!: Request
35+
VERSION: string = version
36+
GUI_VERSION?: string
3437

3538
constructor (configPath: string = '') {
3639
super()

src/lib/Commander.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import program, { CommanderStatic } from 'commander'
33
import inquirer, { Inquirer } from 'inquirer'
44
import { IPlugin } from '../types'
55
import commanders from '../plugins/commander'
6-
import pkg from '../../package.json'
6+
import { version } from '../../package.json'
77

88
class Commander {
99
list: {
@@ -23,7 +23,7 @@ class Commander {
2323

2424
init (): void {
2525
this.program
26-
.version(pkg.version, '-v, --version')
26+
.version(version, '-v, --version')
2727
.option('-d, --debug', 'debug mode', () => {
2828
this.ctx.setConfig({
2929
debug: true

src/lib/Logger.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ILogType } from '../utils/enum'
88
import {
99
ILogArgvType,
1010
ILogArgvTypeWithError,
11-
IConfig,
1211
Undefinable,
1312
ILogColor,
1413
ILogger
@@ -30,29 +29,18 @@ class Logger implements ILogger {
3029
}
3130

3231
private handleLog (type: ILogType, ...msg: ILogArgvTypeWithError[]): void {
33-
// if configPath is invalid then this.ctx.config === undefined
34-
// if not then check config.silent
35-
if (this.ctx.getConfig<IConfig>() === undefined || !this.ctx.getConfig<Undefinable<string>>('silent')) {
32+
// check config.silent
33+
if (!this.ctx.getConfig<Undefinable<string>>('silent')) {
3634
const logHeader = chalk[this.level[type] as ILogColor](`[PicGo ${type.toUpperCase()}]:`)
3735
console.log(logHeader, ...msg)
3836
this.logLevel = this.ctx.getConfig('settings.logLevel')
39-
const logPath = this.checkLogPathChange()
40-
// The incoming logPath is a value
41-
// lock the path with a closure
37+
this.logPath = this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
4238
setTimeout(() => {
43-
this.handleWriteLog(logPath, type, ...msg)
39+
this.handleWriteLog(this.logPath, type, ...msg)
4440
}, 0)
4541
}
4642
}
4743

48-
private checkLogPathChange (): string {
49-
const logPath = this.ctx.getConfig<Undefinable<string>>('settings.logPath') || path.join(this.ctx.baseDir, './picgo.log')
50-
if (logPath !== this.logPath) {
51-
this.logPath = logPath
52-
}
53-
return logPath
54-
}
55-
5644
private handleWriteLog (logPath: string, type: string, ...msg: ILogArgvTypeWithError[]): void {
5745
try {
5846
if (this.checkLogLevel(type, this.logLevel)) {

src/lib/PluginHandler.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import PicGo from '../core/PicGo'
22
import spawn from 'cross-spawn'
3-
import { IResult, IProcessEnv, Undefinable, IPluginProcessResult } from '../types'
3+
import {
4+
IResult,
5+
IProcessEnv,
6+
IPluginProcessResult,
7+
IPluginHandler,
8+
IPluginHandlerOptions,
9+
Undefinable
10+
} from '../types'
411
import { IBuildInEvent } from '../utils/enum'
512
import { getProcessPluginName, getNormalPluginName } from '../utils/common'
613

7-
class PluginHandler {
14+
class PluginHandler implements IPluginHandler {
815
// Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js
9-
ctx: PicGo
16+
private readonly ctx: PicGo
1017
constructor (ctx: PicGo) {
1118
this.ctx = ctx
1219
}
1320

14-
async install (plugins: string[], proxy: string = '', env?: IProcessEnv): Promise<void> {
21+
async install (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
1522
const installedPlugins: string[] = []
1623
const processPlugins = plugins
1724
.map((item: string) => handlePluginNameProcess(this.ctx, item))
@@ -35,7 +42,7 @@ class PluginHandler {
3542
// install plugins must use fullNameList:
3643
// 1. install remote pacage
3744
// 2. install local pacage
38-
const result = await this.execCommand('install', fullNameList, this.ctx.baseDir, proxy, env)
45+
const result = await this.execCommand('install', fullNameList, this.ctx.baseDir, options, env)
3946
if (!result.code) {
4047
pkgNameList.forEach((pluginName: string) => {
4148
this.ctx.pluginLoader.registerPlugin(pluginName)
@@ -103,13 +110,13 @@ class PluginHandler {
103110
}
104111
}
105112

106-
async update (plugins: string[], proxy: string = '', env?: IProcessEnv): Promise<void> {
113+
async update (plugins: string[], options: IPluginHandlerOptions = {}, env?: IProcessEnv): Promise<void> {
107114
const processPlugins = plugins.map((item: string) => handlePluginNameProcess(this.ctx, item)).filter(item => item.success)
108115
const pkgNameList = processPlugins.map(item => item.pkgName)
109116
if (pkgNameList.length > 0) {
110117
// update plugins must use pkgNameList:
111118
// npm update will use the package.json's name
112-
const result = await this.execCommand('update', pkgNameList, this.ctx.baseDir, proxy, env)
119+
const result = await this.execCommand('update', pkgNameList, this.ctx.baseDir, options, env)
113120
if (!result.code) {
114121
this.ctx.log.success('插件更新成功')
115122
this.ctx.emit('updateSuccess', {
@@ -134,8 +141,10 @@ class PluginHandler {
134141
}
135142
}
136143

137-
async execCommand (cmd: string, modules: string[], where: string, proxy: string = '', env: IProcessEnv = {}): Promise<IResult> {
138-
const registry = this.ctx.getConfig<Undefinable<string>>('registry')
144+
private async execCommand (cmd: string, modules: string[], where: string, options: IPluginHandlerOptions = {}, env: IProcessEnv = {}): Promise<IResult> {
145+
// options first
146+
const registry = options.registry || this.ctx.getConfig<Undefinable<string>>('settings.registry')
147+
const proxy = options.proxy || this.ctx.getConfig<Undefinable<string>>('settings.proxy')
139148
return await new Promise((resolve: any): void => {
140149
let args = [cmd].concat(modules).concat('--color=always').concat('--save')
141150
if (registry) {

src/plugins/commander/pluginHandler.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ const pluginHandler: IPlugin = {
1010
.description('install picgo plugin')
1111
.alias('add')
1212
.option('-p, --proxy <proxy>', 'Add proxy for installing')
13+
.option('-r, --registry <registry>', 'Choose a registry for installing')
1314
.action((plugins: string[], program: any) => {
14-
ctx.pluginHandler.install(plugins, program.proxy).catch((e) => { ctx.log.error(e) })
15+
const { proxy, registry } = program
16+
const options = {
17+
proxy,
18+
registry
19+
}
20+
ctx.pluginHandler.install(plugins, options).catch((e) => { ctx.log.error(e) })
1521
})
1622
cmd.program
1723
.command('uninstall <plugins...>')
@@ -23,8 +29,15 @@ const pluginHandler: IPlugin = {
2329
cmd.program
2430
.command('update <plugins...>')
2531
.description('update picgo plugin')
26-
.action((plugins: string[]) => {
27-
ctx.pluginHandler.update(plugins).catch((e) => { ctx.log.error(e) })
32+
.option('-p, --proxy <proxy>', 'Add proxy for installing')
33+
.option('-r, --registry <registry>', 'Choose a registry for installing')
34+
.action((plugins: string[], program: any) => {
35+
const { proxy, registry } = program
36+
const options = {
37+
proxy,
38+
registry
39+
}
40+
ctx.pluginHandler.update(plugins, options).catch((e) => { ctx.log.error(e) })
2841
})
2942
}
3043
}

src/types/index.d.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import PicGo from '../core/PicGo'
21
import LifecyclePlugins from '../lib/LifecyclePlugins'
32
import Commander from '../lib/Commander'
4-
import PluginHandler from '../lib/PluginHandler'
53
import PluginLoader from '../lib/PluginLoader'
64
import Request from '../lib/Request'
75

@@ -13,9 +11,11 @@ interface IPicGo extends NodeJS.EventEmitter {
1311
output: IImgInfo[]
1412
input: any[]
1513
pluginLoader: PluginLoader
16-
pluginHandler: PluginHandler
14+
pluginHandler: IPluginHandler
1715
Request: Request
1816
helper: IHelper
17+
VERSION: string
18+
GUI_VERSION?: string
1919

2020
registerCommands: () => void
2121
getConfig: <T>(name?: string) => T
@@ -173,19 +173,21 @@ interface IConfig {
173173
aliyun?: IAliyunConfig
174174
imgur?: IImgurConfig
175175
transformer?: string
176-
proxy: string
176+
proxy?: string
177177
}
178178
picgoPlugins: {
179179
[propName: string]: boolean
180180
}
181181
debug?: boolean
182182
silent?: boolean
183183
settings?: {
184-
logLevel: string
185-
logPath: string
184+
logLevel?: string
185+
logPath?: string
186+
/** for npm */
187+
registry?: string
188+
/** for npm */
189+
proxy?: string
186190
}
187-
/** 下载插件时 npm 命令自定义的 registry */
188-
registry: string
189191
[configOptions: string]: any
190192
}
191193

@@ -211,10 +213,20 @@ interface IPluginProcessResult {
211213
fullName: string
212214
}
213215

216+
interface IPluginHandler {
217+
install: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
218+
update: (plugins: string[], options: IPluginHandlerOptions, env?: IProcessEnv) => Promise<void>
219+
uninstall: (plugins: string[]) => Promise<void>
220+
}
221+
222+
interface IPluginHandlerOptions {
223+
proxy?: string
224+
registry?: string
225+
}
226+
214227
/**
215228
* for picgo npm plugins
216229
*/
217-
218230
type IPicGoPlugin = (ctx: IPicGo) => IPicGoPluginInterface
219231

220232
/**
@@ -224,11 +236,11 @@ interface IPicGoPluginInterface {
224236
/**
225237
* since PicGo-Core v1.5, register will inject ctx
226238
*/
227-
register: (ctx?: PicGo) => void
239+
register: (ctx?: IPicGo) => void
228240
/**
229241
* this plugin's config
230242
*/
231-
config?: (ctx?: PicGo) => IPluginConfig[]
243+
config?: (ctx?: IPicGo) => IPluginConfig[]
232244
/**
233245
* register uploader name
234246
*/

0 commit comments

Comments
 (0)