Skip to content

Commit f617658

Browse files
committed
🐛 Fix(type): type error in index.d.ts
1. fix type error in index.d.ts 2. refactor picgo.addPlugin -> picgo.pluginLoader.registerPlugin ISSUES CLOSED: #69
1 parent 7b5a024 commit f617658

33 files changed

+219
-176
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
],
2020
'@typescript-eslint/prefer-nullish-coalescing': 0,
2121
'@typescript-eslint/return-await': 0,
22-
'@typescript-eslint/no-floating-promises': 0
22+
'@typescript-eslint/no-floating-promises': 0,
23+
'@typescript-eslint/no-non-null-assertion': 0
2324
}
2425
}

src/core/Lifecycle.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { EventEmitter } from 'events'
2-
import PicGo from './PicGo'
3-
import { IPlugin, Undefinable } from '../types'
2+
import { ILifecyclePlugins, IPicGo, IPlugin, Undefinable } from '../types'
43
import { handleUrlEncode } from '../utils/common'
5-
import LifecyclePlugins from '../lib/LifecyclePlugins'
64
import { IBuildInEvent } from '../utils/enum'
75

86
class Lifecycle extends EventEmitter {
9-
ctx: PicGo
7+
private ctx: IPicGo
108

11-
constructor (ctx: PicGo) {
9+
constructor (ctx: IPicGo) {
1210
super()
1311
this.ctx = ctx
1412
}
1513

16-
async start (input: any[]): Promise<PicGo> {
14+
async start (input: any[]): Promise<IPicGo> {
1715
try {
1816
// images input
1917
if (!Array.isArray(input)) {
@@ -41,15 +39,15 @@ class Lifecycle extends EventEmitter {
4139
}
4240
}
4341

44-
private async beforeTransform (): Promise<PicGo> {
42+
private async beforeTransform (): Promise<IPicGo> {
4543
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 0)
4644
this.ctx.emit(IBuildInEvent.BEFORE_TRANSFORM, this.ctx)
4745
this.ctx.log.info('Before transform')
4846
await this.handlePlugins(this.ctx.helper.beforeTransformPlugins)
4947
return this.ctx
5048
}
5149

52-
private async doTransform (): Promise<PicGo> {
50+
private async doTransform (): Promise<IPicGo> {
5351
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 30)
5452
this.ctx.log.info('Transforming...')
5553
const type = this.ctx.getConfig<Undefinable<string>>('picBed.transformer') || 'path'
@@ -62,15 +60,15 @@ class Lifecycle extends EventEmitter {
6260
return this.ctx
6361
}
6462

65-
private async beforeUpload (): Promise<PicGo> {
63+
private async beforeUpload (): Promise<IPicGo> {
6664
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 60)
6765
this.ctx.log.info('Before upload')
6866
this.ctx.emit(IBuildInEvent.BEFORE_UPLOAD, this.ctx)
6967
await this.handlePlugins(this.ctx.helper.beforeUploadPlugins)
7068
return this.ctx
7169
}
7270

73-
private async doUpload (): Promise<PicGo> {
71+
private async doUpload (): Promise<IPicGo> {
7472
this.ctx.log.info('Uploading...')
7573
let type = this.ctx.getConfig<Undefinable<string>>('picBed.uploader') || this.ctx.getConfig<Undefinable<string>>('picBed.current') || 'smms'
7674
let uploader = this.ctx.helper.uploader.get(type)
@@ -86,7 +84,7 @@ class Lifecycle extends EventEmitter {
8684
return this.ctx
8785
}
8886

89-
private async afterUpload (): Promise<PicGo> {
87+
private async afterUpload (): Promise<IPicGo> {
9088
this.ctx.emit(IBuildInEvent.AFTER_UPLOAD, this.ctx)
9189
this.ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, 100)
9290
await this.handlePlugins(this.ctx.helper.afterUploadPlugins)
@@ -105,7 +103,7 @@ class Lifecycle extends EventEmitter {
105103
return this.ctx
106104
}
107105

108-
private async handlePlugins (lifeCyclePlugins: LifecyclePlugins): Promise<PicGo> {
106+
private async handlePlugins (lifeCyclePlugins: ILifecyclePlugins): Promise<IPicGo> {
109107
const plugins = lifeCyclePlugins.getList()
110108
const pluginNames = lifeCyclePlugins.getIdList()
111109
const lifeCycleName = lifeCyclePlugins.getName()

src/core/PicGo.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import uploaders from '../plugins/uploader'
1010
import transformers from '../plugins/transformer'
1111
import PluginLoader from '../lib/PluginLoader'
1212
import { get, set, unset } from 'lodash'
13-
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPicGoPlugin } from '../types'
13+
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap } from '../types'
1414
import getClipboardImage from '../utils/getClipboardImage'
1515
import Request from '../lib/Request'
1616
import DB from '../utils/db'
@@ -150,22 +150,6 @@ class PicGo extends EventEmitter implements IPicGo {
150150
unset(this.getConfig(key), propName)
151151
}
152152

153-
/**
154-
* for node project adding a plugin by a simple way
155-
*/
156-
addPlugin (name: string, plugin: IPicGoPlugin): void {
157-
if (!name || !plugin || (typeof plugin !== 'function')) {
158-
this.log.warn('Please provide valid plugin')
159-
return
160-
}
161-
try {
162-
plugin(this).register()
163-
} catch (e) {
164-
this.log.warn('Please provide valid plugin')
165-
this.log.error(e)
166-
}
167-
}
168-
169153
// for v1.5.0+
170154
get request (): RequestPromiseAPI {
171155
// TODO: replace request with got: https://github.com/sindresorhus/got

src/lib/Commander.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import PicGo from '../core/PicGo'
21
import program, { CommanderStatic } from 'commander'
32
import inquirer, { Inquirer } from 'inquirer'
4-
import { IPlugin } from '../types'
3+
import { IPlugin, ICommander, IPicGo } from '../types'
54
import commanders from '../plugins/commander'
65
import { version } from '../../package.json'
76

8-
class Commander {
9-
list: {
7+
class Commander implements ICommander {
8+
private list: {
109
[propName: string]: IPlugin
1110
}
1211

1312
program: CommanderStatic
1413
inquirer: Inquirer
15-
private readonly ctx: PicGo
14+
private readonly ctx: IPicGo
1615

17-
constructor (ctx: PicGo) {
16+
constructor (ctx: IPicGo) {
1817
this.list = {}
1918
this.program = program
2019
this.inquirer = inquirer

src/lib/LifecyclePlugins.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { IPlugin } from '../types'
1+
import { IPlugin, ILifecyclePlugins } from '../types'
22

3-
class LifecyclePlugins {
3+
class LifecyclePlugins implements ILifecyclePlugins {
44
static currentPlugin: string | null
55
private readonly list: Map<string, IPlugin>
66
private readonly pluginIdMap: Map<string, string[]>

src/lib/Logger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from 'chalk'
2-
import PicGo from '../core/PicGo'
32
import dayjs from 'dayjs'
43
import fs from 'fs-extra'
54
import path from 'path'
@@ -10,7 +9,8 @@ import {
109
ILogArgvTypeWithError,
1110
Undefinable,
1211
ILogColor,
13-
ILogger
12+
ILogger,
13+
IPicGo
1414
} from '../types'
1515

1616
class Logger implements ILogger {
@@ -21,10 +21,10 @@ class Logger implements ILogger {
2121
[ILogType.error]: 'red'
2222
}
2323

24-
private readonly ctx: PicGo
24+
private readonly ctx: IPicGo
2525
private logLevel!: string
2626
private logPath!: string
27-
constructor (ctx: PicGo) {
27+
constructor (ctx: IPicGo) {
2828
this.ctx = ctx
2929
}
3030

src/lib/PluginHandler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import PicGo from '../core/PicGo'
21
import spawn from 'cross-spawn'
32
import {
43
IResult,
54
IProcessEnv,
65
IPluginProcessResult,
76
IPluginHandler,
87
IPluginHandlerOptions,
9-
Undefinable
8+
Undefinable,
9+
IPicGo
1010
} from '../types'
1111
import { IBuildInEvent } from '../utils/enum'
1212
import { getProcessPluginName, getNormalPluginName } from '../utils/common'
1313

1414
class PluginHandler implements IPluginHandler {
1515
// Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js
16-
private readonly ctx: PicGo
17-
constructor (ctx: PicGo) {
16+
private readonly ctx: IPicGo
17+
constructor (ctx: IPicGo) {
1818
this.ctx = ctx
1919
}
2020

@@ -191,7 +191,7 @@ class PluginHandler implements IPluginHandler {
191191
* @param ctx
192192
* @param nameOrPath
193193
*/
194-
const handlePluginNameProcess = (ctx: PicGo, nameOrPath: string): IPluginProcessResult => {
194+
const handlePluginNameProcess = (ctx: IPicGo, nameOrPath: string): IPluginProcessResult => {
195195
const res = {
196196
success: false,
197197
fullName: '',

src/lib/PluginLoader.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import PicGo from '../core/PicGo'
21
import fs from 'fs-extra'
32
import path from 'path'
43
import resolve from 'resolve'
54
import { IBuildInEvent } from '../utils/enum'
5+
import { IPicGo, IPicGoPlugin, IPluginLoader, IPicGoPluginInterface } from '../types/index'
66

77
/**
88
* Local plugin loader, file system is required
99
*/
10-
class PluginLoader {
11-
ctx: PicGo
10+
class PluginLoader implements IPluginLoader {
11+
private readonly ctx: IPicGo
1212
private list: string[] = []
1313
private readonly fullList: Set<string> = new Set()
14-
constructor (ctx: PicGo) {
14+
private readonly pluginMap: Map<string, IPicGoPluginInterface> = new Map()
15+
constructor (ctx: IPicGo) {
1516
this.ctx = ctx
1617
this.init()
1718
}
1819

19-
init (): void {
20+
private init (): void {
2021
const packagePath = path.join(this.ctx.baseDir, 'package.json')
2122
if (!fs.existsSync(packagePath)) {
2223
const pkg = {
@@ -30,7 +31,7 @@ class PluginLoader {
3031
}
3132

3233
// get plugin entry
33-
resolvePlugin (ctx: PicGo, name: string): string {
34+
private resolvePlugin (ctx: IPicGo, name: string): string {
3435
try {
3536
return resolve.sync(name, { basedir: ctx.baseDir })
3637
} catch (err) {
@@ -60,28 +61,45 @@ class PluginLoader {
6061
return true
6162
}
6263

63-
registerPlugin (name: string): void {
64+
registerPlugin (name: string, plugin?: IPicGoPlugin): void {
65+
if (!name || typeof name !== 'string') {
66+
this.ctx.log.warn('Please provide valid plugin')
67+
return
68+
}
6469
this.fullList.add(name)
65-
if (this.ctx.getConfig(`picgoPlugins.${name}`) === true || (this.ctx.getConfig(`picgoPlugins.${name}`) === undefined)) {
66-
try {
70+
try {
71+
// register local plugin
72+
if (!plugin) {
73+
if (this.ctx.getConfig(`picgoPlugins.${name}`) === true || (this.ctx.getConfig(`picgoPlugins.${name}`) === undefined)) {
74+
this.list.push(name)
75+
this.ctx.setCurrentPluginName(name)
76+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
77+
this.getPlugin(name)!.register()
78+
const plugin = `picgoPlugins[${name}]`
79+
this.ctx.saveConfig(
80+
{
81+
[plugin]: true
82+
}
83+
)
84+
}
85+
} else {
86+
// register provided plugin
87+
// && won't write config to files
6788
this.list.push(name)
6889
this.ctx.setCurrentPluginName(name)
69-
this.getPlugin(name).register()
70-
const plugin = `picgoPlugins[${name}]`
71-
this.ctx.saveConfig(
72-
{
73-
[plugin]: true
74-
}
75-
)
76-
} catch (e) {
77-
this.list = this.list.filter((item: string) => item !== name)
78-
this.fullList.delete(name)
79-
this.ctx.log.error(e)
80-
this.ctx.emit(IBuildInEvent.NOTIFICATION, {
81-
title: `Plugin ${name} Load Error`,
82-
body: e
83-
})
90+
const pluginInterface = plugin(this.ctx)
91+
this.pluginMap.set(name, pluginInterface)
92+
plugin(this.ctx).register()
8493
}
94+
} catch (e) {
95+
this.pluginMap.delete(name)
96+
this.list = this.list.filter((item: string) => item !== name)
97+
this.fullList.delete(name)
98+
this.ctx.log.error(e)
99+
this.ctx.emit(IBuildInEvent.NOTIFICATION, {
100+
title: `Plugin ${name} Load Error`,
101+
body: e
102+
})
85103
}
86104
}
87105

@@ -98,10 +116,15 @@ class PluginLoader {
98116
}
99117

100118
// get plugin by name
101-
getPlugin (name: string): any {
119+
getPlugin (name: string): IPicGoPluginInterface | undefined {
120+
if (this.pluginMap.has(name)) {
121+
return this.pluginMap.get(name)
122+
}
102123
const pluginDir = path.join(this.ctx.baseDir, 'node_modules/')
103124
// eslint-disable-next-line @typescript-eslint/no-var-requires
104-
return require(pluginDir + name)(this.ctx)
125+
const plugin = require(pluginDir + name)(this.ctx)
126+
this.pluginMap.set(name, plugin)
127+
return plugin
105128
}
106129

107130
/**

src/lib/Request.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import PicGo from '../core/PicGo'
21
import request, { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'
3-
import { Undefinable, IConfigChangePayload, IConfig } from '../types'
2+
import { IPicGo, Undefinable, IConfigChangePayload, IConfig } from '../types'
43
import { CONFIG_CHANGE } from '../utils/buildInEvent'
54
import { eventBus } from '../utils/eventBus'
65

76
class Request {
8-
private readonly ctx: PicGo
7+
private readonly ctx: IPicGo
98
private proxy: Undefinable<string> = ''
109
options: RequestPromiseOptions = {}
11-
constructor (ctx: PicGo) {
10+
constructor (ctx: IPicGo) {
1211
this.ctx = ctx
1312
this.init()
1413
eventBus.on(CONFIG_CHANGE, (data: IConfigChangePayload<string | IConfig['picBed']>) => {
@@ -25,10 +24,10 @@ class Request {
2524
})
2625
}
2726

28-
init (): void {
27+
private init (): void {
2928
const proxy = this.ctx.getConfig<Undefinable<string>>('picBed.proxy')
3029
if (proxy) {
31-
this.options.proxy = proxy
30+
this.proxy = proxy
3231
}
3332
}
3433

src/plugins/commander/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import PicGo from '../../core/PicGo'
2-
import { IPlugin } from '../../types'
1+
import { IPicGo, IPlugin } from '../../types'
32

43
const config: IPlugin = {
5-
handle: (ctx: PicGo) => {
4+
handle: (ctx: IPicGo) => {
65
const cmd = ctx.cmd
76
cmd.program
87
.option('-c, --config <path>', 'set config path')
8+
// will handle in `bin/picgo`
99
}
1010
}
1111

0 commit comments

Comments
 (0)