Skip to content

Commit

Permalink
🔨 Refactor: pluginLoader && pluginHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Dec 26, 2019
1 parent 7ea1923 commit 5e68426
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 38 deletions.
34 changes: 25 additions & 9 deletions src/lib/LifecyclePlugins.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
import { Plugin } from '../utils/interfaces'

class LifecyclePlugins {
list: {
[propName: string]: Plugin
}
static currentPlugin: string | null
list: Map<string, Plugin>
pluginIdMap: Map<string, string[]>
name: string

constructor (name: string) {
this.name = name
this.list = {}
this.list = new Map()
this.pluginIdMap = new Map()
}

register (id: string, plugin: Plugin): void {
if (!id) throw new TypeError('id is required!')
if (typeof plugin.handle !== 'function') throw new TypeError('plugin.handle must be a function!')
if (this.list[id]) throw new TypeError(`${this.name} duplicate id: ${id}!`)
if (this.list.has(id)) throw new TypeError(`${this.name} duplicate id: ${id}!`)
this.list.set(id, plugin)
if (LifecyclePlugins.currentPlugin) { // maybe null
if (this.pluginIdMap.has(LifecyclePlugins.currentPlugin)) {
this.pluginIdMap.get(LifecyclePlugins.currentPlugin).push(id)
} else {
this.pluginIdMap.set(LifecyclePlugins.currentPlugin, [id])
}
}
}

this.list[id] = plugin
unregister (pluginName: string): void {
if (this.pluginIdMap.has(pluginName)) {
const pluginList = this.pluginIdMap.get(pluginName)
pluginList.forEach((plugin: string) => {
this.list.delete(plugin)
})
}
}

get (id: string): Plugin {
return this.list[id]
return this.list.get(id)
}

getList (): Plugin[] {
return Object.keys(this.list).map((item: string) => this.list[item])
return [...this.list.values()]
}

getIdList (): string[] {
return Object.keys(this.list)
return [...this.list.keys()]
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ class PluginHandler {
this.ctx = ctx
}

async install (plugins: string[], proxy: string): Promise<void> {
async install (plugins: string[], proxy: string = ''): Promise<void> {
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
const result = await this.execCommand('install', plugins, this.ctx.baseDir, proxy)
if (!result.code) {
plugins.forEach((plugin: string) => {
this.ctx.pluginLoader.registerPlugin(plugin)
})
this.ctx.log.success('插件安装成功')
this.ctx.emit('installSuccess', {
title: '插件安装成功',
Expand All @@ -21,7 +24,7 @@ class PluginHandler {
} else {
const err = `插件安装失败,失败码为${result.code},错误日志为${result.data}`
this.ctx.log.error(err)
this.ctx.emit('failed', {
this.ctx.emit('installFailed', {
title: '插件安装失败',
body: err
})
Expand All @@ -31,6 +34,9 @@ class PluginHandler {
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
const result = await this.execCommand('uninstall', plugins, this.ctx.baseDir)
if (!result.code) {
plugins.forEach((plugin: string) => {
this.ctx.pluginLoader.unregisterPlugin(plugin)
})
this.ctx.log.success('插件卸载成功')
this.ctx.emit('uninstallSuccess', {
title: '插件卸载成功',
Expand All @@ -39,15 +45,15 @@ class PluginHandler {
} else {
const err = `插件卸载失败,失败码为${result.code},错误日志为${result.data}`
this.ctx.log.error(err)
this.ctx.emit('failed', {
this.ctx.emit('uninstallFailed', {
title: '插件卸载失败',
body: err
})
}
}
async update (plugins: string[]): Promise<void> {
async update (plugins: string[], proxy: string = ''): Promise<void> {
plugins = plugins.map((item: string) => 'picgo-plugin-' + item)
const result = await this.execCommand('update', plugins, this.ctx.baseDir)
const result = await this.execCommand('update', plugins, this.ctx.baseDir, proxy)
if (!result.code) {
this.ctx.log.success('插件更新成功')
this.ctx.emit('updateSuccess', {
Expand All @@ -57,14 +63,14 @@ class PluginHandler {
} else {
const err = `插件更新失败,失败码为${result.code},错误日志为${result.data}`
this.ctx.log.error(err)
this.ctx.emit('failed', {
this.ctx.emit('updateFailed', {
title: '插件更新失败',
body: err
})
}
}
execCommand (cmd: string, modules: string[], where: string, proxy: string = ''): Promise<Result> {
const registry = this.ctx.config.registry
const registry = this.ctx.getConfig('registry')
return new Promise((resolve: any, reject: any): void => {
let args = [cmd].concat(modules).concat('--color=always').concat('--save')
if (registry) {
Expand Down
51 changes: 34 additions & 17 deletions src/lib/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,44 @@ class PluginLoader {
return fs.existsSync(path)
})
for (let i in modules) {
this.list.push(modules[i])
if (this.ctx.config.picgoPlugins[modules[i]] || this.ctx.config.picgoPlugins[modules[i]] === undefined) {
try {
this.getPlugin(modules[i]).register()
const plugin = `picgoPlugins[${modules[i]}]`
this.ctx.saveConfig(
{
[plugin]: true
}
)
} catch (e) {
this.ctx.log.error(e)
this.ctx.emit('notification', {
title: `Plugin ${modules[i]} Load Error`,
body: e
})
}
this.registerPlugin(modules[i])
}
}

registerPlugin (name: string): void {
if (this.ctx.getConfig(`picgoPlugins.${name}`) === true || (this.ctx.getConfig(`picgoPlugins.${name}`) === undefined)) {
try {
this.list.push(name)
this.ctx.setCurrentPluginName(name)
this.getPlugin(name).register()
const plugin = `picgoPlugins[${name}]`
this.ctx.saveConfig(
{
[plugin]: true
}
)
} catch (e) {
this.list = this.list.filter((item: string) => item !== name)
this.ctx.log.error(e)
this.ctx.emit('notification', {
title: `Plugin ${name} Load Error`,
body: e
})
}
}
}

unregisterPlugin (name: string): void {
this.list = this.list.filter((item: string) => item !== name)
this.ctx.setCurrentPluginName(name)
this.ctx.helper.uploader.unregister(name)
this.ctx.helper.transformer.unregister(name)
this.ctx.helper.beforeTransformPlugins.unregister(name)
this.ctx.helper.beforeUploadPlugins.unregister(name)
this.ctx.helper.afterUploadPlugins.unregister(name)
this.ctx.removeConfig('picgoPlugin', name)
}

// get plugin by name
getPlugin (name: string): any {
const pluginDir = path.join(this.ctx.baseDir, 'node_modules/')
Expand Down
8 changes: 3 additions & 5 deletions src/plugins/commander/pluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import PicGo from '../../core/PicGo'
import PluginHandler from '../../lib/PluginHandler'

export default {
handle: (ctx: PicGo): void => {
const pluginHandler = new PluginHandler(ctx)
// const pluginHandler = new PluginHandler(ctx)
const cmd = ctx.cmd
cmd.program
Expand All @@ -12,20 +10,20 @@ export default {
.alias('add')
.option('-p, --proxy <proxy>', 'Add proxy for installing')
.action((plugins: string[], program: any) => {
pluginHandler.install(plugins, program.proxy)
return ctx.pluginHandler.install(plugins, program.proxy)
})
cmd.program
.command('uninstall <plugins...>')
.alias('rm')
.description('uninstall picgo plugin')
.action((plugins: string[]) => {
pluginHandler.uninstall(plugins)
return ctx.pluginHandler.uninstall(plugins)
})
cmd.program
.command('update <plugins...>')
.description('update picgo plugin')
.action((plugins: string[]) => {
pluginHandler.update(plugins)
return ctx.pluginHandler.update(plugins)
})
}
}

0 comments on commit 5e68426

Please sign in to comment.