Skip to content

Commit b89cf1e

Browse files
committed
✨ Feature: finish request -> axios
1 parent 45424d1 commit b89cf1e

File tree

20 files changed

+448
-364
lines changed

20 files changed

+448
-364
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@
6565
"@types/md5": "^2.1.32",
6666
"@types/mime-types": "^2.1.0",
6767
"@types/minimatch": "^3.0.3",
68-
"@types/node": "^10.5.2",
68+
"@types/node": "16.11.7",
6969
"@types/request-promise-native": "^1.0.15",
7070
"@types/resolve": "^0.0.8",
7171
"@types/rimraf": "^3.0.0",
72+
"@types/tunnel": "^0.0.3",
7273
"@typescript-eslint/eslint-plugin": "3",
7374
"@typescript-eslint/parser": "^3.2.0",
7475
"babel-eslint": "^10.1.0",
@@ -90,11 +91,12 @@
9091
"rollup-plugin-string": "^3.0.0",
9192
"rollup-plugin-terser": "^7.0.2",
9293
"rollup-plugin-typescript2": "^0.30.0",
93-
"typescript": "^4.4.3"
94+
"typescript": "^4.8.2"
9495
},
9596
"dependencies": {
9697
"@picgo/i18n": "^1.0.0",
9798
"@picgo/store": "^2.0.2",
99+
"axios": "^0.27.2",
98100
"chalk": "^2.4.1",
99101
"commander": "^8.1.0",
100102
"comment-json": "^2.3.1",
@@ -114,10 +116,9 @@
114116
"minimatch": "^3.0.4",
115117
"minimist": "^1.2.5",
116118
"qiniu": "^7.2.1",
117-
"request": "^2.87.0",
118-
"request-promise-native": "^1.0.5",
119119
"resolve": "^1.8.1",
120-
"rimraf": "^3.0.2"
120+
"rimraf": "^3.0.2",
121+
"tunnel": "^0.0.6"
121122
},
122123
"repository": {
123124
"type": "git",

src/core/PicGo.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ 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, IPluginLoader, II18nManager, IPicGoPlugin, IPicGoPluginInterface } from '../types'
13+
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPluginLoader, II18nManager, IPicGoPlugin, IPicGoPluginInterface, IRequest } from '../types'
1414
import getClipboardImage from '../utils/getClipboardImage'
1515
import Request from '../lib/Request'
1616
import DB from '../utils/db'
1717
import PluginHandler from '../lib/PluginHandler'
1818
import { IBuildInEvent, IBusEvent } from '../utils/enum'
1919
import { eventBus } from '../utils/eventBus'
20-
import { RequestPromiseAPI } from 'request-promise-native'
2120
import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'
2221
import { I18nManager } from '../i18n'
2322

@@ -176,9 +175,8 @@ export class PicGo extends EventEmitter implements IPicGo {
176175
unset(this.getConfig(key), propName)
177176
}
178177

179-
get request (): RequestPromiseAPI {
180-
// TODO: replace request with got: https://github.com/sindresorhus/got
181-
return this.Request.request
178+
get request (): IRequest['request'] {
179+
return this.Request.request.bind(this.Request)
182180
}
183181

184182
async upload (input?: any[]): Promise<IImgInfo[] | Error> {

src/i18n/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const EN: ILocales = {
1212
// smms
1313
PICBED_SMMS: 'SM.MS',
1414
PICBED_SMMS_TOKEN: 'Set Token',
15-
PICBED_SMMS_BACKUP_DOMAIN: 'Set Backup Domain',
15+
PICBED_SMMS_BACKUP_DOMAIN: 'Set Backup Upload Domain',
1616
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: 'Ex. smms.app',
1717

1818
// Ali-cloud

src/i18n/zh-CN.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const ZH_CN = {
1010
// smms
1111
PICBED_SMMS: 'SM.MS',
1212
PICBED_SMMS_TOKEN: '设定Token',
13-
PICBED_SMMS_BACKUP_DOMAIN: '备用域名',
13+
PICBED_SMMS_BACKUP_DOMAIN: '备用上传域名',
1414
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: '例如 smms.app',
1515

1616
// Ali-cloud

src/lib/Commander.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
/* eslint-disable @typescript-eslint/no-misused-promises */
12
import { Command } from 'commander'
23
import inquirer, { Inquirer } from 'inquirer'
34
import { IPlugin, ICommander, IPicGo } from '../types'
45
import commanders from '../plugins/commander'
6+
import { getCurrentPluginName } from './LifecyclePlugins'
57

68
export class Commander implements ICommander {
7-
private list: {
8-
[propName: string]: IPlugin
9-
}
9+
private readonly name = 'commander'
10+
static currentPlugin: string | null
11+
private readonly list: Map<string, IPlugin> = new Map()
12+
private readonly pluginIdMap: Map<string, string[]> = new Map()
13+
private readonly ctx: IPicGo
1014

1115
program: Command
1216
inquirer: Inquirer
13-
private readonly ctx: IPicGo
1417

1518
constructor (ctx: IPicGo) {
16-
this.list = {}
1719
this.program = new Command()
1820
this.inquirer = inquirer
1921
this.ctx = ctx
2022
}
2123

24+
getName (): string {
25+
return this.name
26+
}
27+
2228
init (): void {
2329
this.program
2430
.version(process.env.PICGO_VERSION, '-v, --version')
@@ -41,24 +47,50 @@ export class Commander implements ICommander {
4147
commanders(this.ctx)
4248
}
4349

44-
register (name: string, plugin: IPlugin): void {
45-
if (!name) throw new TypeError('name is required!')
50+
register (id: string, plugin: IPlugin): void {
51+
if (!id) throw new TypeError('name is required!')
4652
if (typeof plugin.handle !== 'function') throw new TypeError('plugin.handle must be a function!')
47-
if (name in this.list) throw new TypeError('duplicate name!')
53+
if (this.list.has(id)) throw new TypeError(`${this.name} plugin duplicate id: ${id}!`)
54+
this.list.set(id, plugin)
55+
const currentPluginName = getCurrentPluginName()
56+
if (currentPluginName !== null) {
57+
if (this.pluginIdMap.has(currentPluginName)) {
58+
this.pluginIdMap.get(currentPluginName)?.push(id)
59+
} else {
60+
this.pluginIdMap.set(currentPluginName, [id])
61+
}
62+
}
63+
}
4864

49-
this.list[name] = plugin
65+
unregister (pluginName: string): void {
66+
if (this.pluginIdMap.has(pluginName)) {
67+
const pluginList = this.pluginIdMap.get(pluginName)
68+
pluginList?.forEach((plugin: string) => {
69+
this.list.delete(plugin)
70+
})
71+
}
5072
}
5173

5274
loadCommands (): void {
53-
Object.keys(this.list).map((item: string) => this.list[item].handle(this.ctx))
75+
this.getList().forEach((item: IPlugin) => {
76+
try {
77+
item.handle(this.ctx)
78+
} catch (e: any) {
79+
this.ctx.log.error(e)
80+
}
81+
})
5482
}
5583

56-
get (name: string): IPlugin {
57-
return this.list[name]
84+
get (id: string): IPlugin | undefined {
85+
return this.list.get(id)
5886
}
5987

6088
getList (): IPlugin[] {
61-
return Object.keys(this.list).map((item: string) => this.list[item])
89+
return [...this.list.values()]
90+
}
91+
92+
getIdList (): string[] {
93+
return [...this.list.keys()]
6294
}
6395
}
6496

src/lib/LifecyclePlugins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ export const setCurrentPluginName = (name: string | null = null): void => {
5656
LifecyclePlugins.currentPlugin = name
5757
}
5858

59+
export const getCurrentPluginName = (): string | null => {
60+
return LifecyclePlugins.currentPlugin
61+
}
62+
5963
export default LifecyclePlugins

src/lib/Logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ export class Logger implements ILogger {
8686
if (this.checkLogLevel(type, this.logLevel)) {
8787
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
8888
msg.forEach((item: ILogArgvTypeWithError) => {
89-
if (typeof item === 'object' && type === 'error') {
89+
if (item instanceof Error && type === 'error') {
9090
log += `\n------Error Stack Begin------\n${util.format(item?.stack)}\n-------Error Stack End------- `
9191
} else {
9292
if (typeof item === 'object') {
93-
item = JSON.stringify(item)
93+
item = JSON.stringify(item, null, 2)
9494
}
9595
log += `${item as string} `
9696
}

src/lib/PluginHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ export class PluginHandler implements IPluginHandler {
218218
const npm = spawn('npm', args, { cwd: where, env: Object.assign({}, process.env, env) })
219219

220220
let output = ''
221-
npm.stdout.on('data', (data: string) => {
221+
npm.stdout?.on('data', (data: string) => {
222222
output += data
223223
}).pipe(process.stdout)
224224

225-
npm.stderr.on('data', (data: string) => {
225+
npm.stderr?.on('data', (data: string) => {
226226
output += data
227227
}).pipe(process.stderr)
228228

src/lib/PluginLoader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class PluginLoader implements IPluginLoader {
114114
this.ctx.helper.beforeTransformPlugins.unregister(name)
115115
this.ctx.helper.beforeUploadPlugins.unregister(name)
116116
this.ctx.helper.afterUploadPlugins.unregister(name)
117+
this.ctx.cmd.unregister(name)
117118
this.ctx.removeConfig('picgoPlugins', name)
118119
}
119120

0 commit comments

Comments
 (0)