Skip to content

Commit

Permalink
✨ Feature: finish request -> axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Sep 2, 2022
1 parent 45424d1 commit b89cf1e
Show file tree
Hide file tree
Showing 20 changed files with 448 additions and 364 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
"@types/md5": "^2.1.32",
"@types/mime-types": "^2.1.0",
"@types/minimatch": "^3.0.3",
"@types/node": "^10.5.2",
"@types/node": "16.11.7",
"@types/request-promise-native": "^1.0.15",
"@types/resolve": "^0.0.8",
"@types/rimraf": "^3.0.0",
"@types/tunnel": "^0.0.3",
"@typescript-eslint/eslint-plugin": "3",
"@typescript-eslint/parser": "^3.2.0",
"babel-eslint": "^10.1.0",
Expand All @@ -90,11 +91,12 @@
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"typescript": "^4.4.3"
"typescript": "^4.8.2"
},
"dependencies": {
"@picgo/i18n": "^1.0.0",
"@picgo/store": "^2.0.2",
"axios": "^0.27.2",
"chalk": "^2.4.1",
"commander": "^8.1.0",
"comment-json": "^2.3.1",
Expand All @@ -114,10 +116,9 @@
"minimatch": "^3.0.4",
"minimist": "^1.2.5",
"qiniu": "^7.2.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
"resolve": "^1.8.1",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"tunnel": "^0.0.6"
},
"repository": {
"type": "git",
Expand Down
8 changes: 3 additions & 5 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import uploaders from '../plugins/uploader'
import transformers from '../plugins/transformer'
import PluginLoader from '../lib/PluginLoader'
import { get, set, unset } from 'lodash'
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPluginLoader, II18nManager, IPicGoPlugin, IPicGoPluginInterface } from '../types'
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPluginLoader, II18nManager, IPicGoPlugin, IPicGoPluginInterface, IRequest } from '../types'
import getClipboardImage from '../utils/getClipboardImage'
import Request from '../lib/Request'
import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'
import { IBuildInEvent, IBusEvent } from '../utils/enum'
import { eventBus } from '../utils/eventBus'
import { RequestPromiseAPI } from 'request-promise-native'
import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'
import { I18nManager } from '../i18n'

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

get request (): RequestPromiseAPI {
// TODO: replace request with got: https://github.com/sindresorhus/got
return this.Request.request
get request (): IRequest['request'] {
return this.Request.request.bind(this.Request)
}

async upload (input?: any[]): Promise<IImgInfo[] | Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const EN: ILocales = {
// smms
PICBED_SMMS: 'SM.MS',
PICBED_SMMS_TOKEN: 'Set Token',
PICBED_SMMS_BACKUP_DOMAIN: 'Set Backup Domain',
PICBED_SMMS_BACKUP_DOMAIN: 'Set Backup Upload Domain',
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: 'Ex. smms.app',

// Ali-cloud
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ZH_CN = {
// smms
PICBED_SMMS: 'SM.MS',
PICBED_SMMS_TOKEN: '设定Token',
PICBED_SMMS_BACKUP_DOMAIN: '备用域名',
PICBED_SMMS_BACKUP_DOMAIN: '备用上传域名',
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: '例如 smms.app',

// Ali-cloud
Expand Down
58 changes: 45 additions & 13 deletions src/lib/Commander.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import { Command } from 'commander'
import inquirer, { Inquirer } from 'inquirer'
import { IPlugin, ICommander, IPicGo } from '../types'
import commanders from '../plugins/commander'
import { getCurrentPluginName } from './LifecyclePlugins'

export class Commander implements ICommander {
private list: {
[propName: string]: IPlugin
}
private readonly name = 'commander'
static currentPlugin: string | null
private readonly list: Map<string, IPlugin> = new Map()
private readonly pluginIdMap: Map<string, string[]> = new Map()
private readonly ctx: IPicGo

program: Command
inquirer: Inquirer
private readonly ctx: IPicGo

constructor (ctx: IPicGo) {
this.list = {}
this.program = new Command()
this.inquirer = inquirer
this.ctx = ctx
}

getName (): string {
return this.name
}

init (): void {
this.program
.version(process.env.PICGO_VERSION, '-v, --version')
Expand All @@ -41,24 +47,50 @@ export class Commander implements ICommander {
commanders(this.ctx)
}

register (name: string, plugin: IPlugin): void {
if (!name) throw new TypeError('name is required!')
register (id: string, plugin: IPlugin): void {
if (!id) throw new TypeError('name is required!')
if (typeof plugin.handle !== 'function') throw new TypeError('plugin.handle must be a function!')
if (name in this.list) throw new TypeError('duplicate name!')
if (this.list.has(id)) throw new TypeError(`${this.name} plugin duplicate id: ${id}!`)
this.list.set(id, plugin)
const currentPluginName = getCurrentPluginName()
if (currentPluginName !== null) {
if (this.pluginIdMap.has(currentPluginName)) {
this.pluginIdMap.get(currentPluginName)?.push(id)
} else {
this.pluginIdMap.set(currentPluginName, [id])
}
}
}

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

loadCommands (): void {
Object.keys(this.list).map((item: string) => this.list[item].handle(this.ctx))
this.getList().forEach((item: IPlugin) => {
try {
item.handle(this.ctx)
} catch (e: any) {
this.ctx.log.error(e)
}
})
}

get (name: string): IPlugin {
return this.list[name]
get (id: string): IPlugin | undefined {
return this.list.get(id)
}

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

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

Expand Down
4 changes: 4 additions & 0 deletions src/lib/LifecyclePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ export const setCurrentPluginName = (name: string | null = null): void => {
LifecyclePlugins.currentPlugin = name
}

export const getCurrentPluginName = (): string | null => {
return LifecyclePlugins.currentPlugin
}

export default LifecyclePlugins
4 changes: 2 additions & 2 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export class Logger implements ILogger {
if (this.checkLogLevel(type, this.logLevel)) {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ${type.toUpperCase()}] `
msg.forEach((item: ILogArgvTypeWithError) => {
if (typeof item === 'object' && type === 'error') {
if (item instanceof Error && type === 'error') {
log += `\n------Error Stack Begin------\n${util.format(item?.stack)}\n-------Error Stack End------- `
} else {
if (typeof item === 'object') {
item = JSON.stringify(item)
item = JSON.stringify(item, null, 2)
}
log += `${item as string} `
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ export class PluginHandler implements IPluginHandler {
const npm = spawn('npm', args, { cwd: where, env: Object.assign({}, process.env, env) })

let output = ''
npm.stdout.on('data', (data: string) => {
npm.stdout?.on('data', (data: string) => {
output += data
}).pipe(process.stdout)

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

Expand Down
1 change: 1 addition & 0 deletions src/lib/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class PluginLoader implements IPluginLoader {
this.ctx.helper.beforeTransformPlugins.unregister(name)
this.ctx.helper.beforeUploadPlugins.unregister(name)
this.ctx.helper.afterUploadPlugins.unregister(name)
this.ctx.cmd.unregister(name)
this.ctx.removeConfig('picgoPlugins', name)
}

Expand Down
Loading

0 comments on commit b89cf1e

Please sign in to comment.