Skip to content

Commit

Permalink
🐛 Fix(type): some type error
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Dec 19, 2020
1 parent 2d9271b commit 233a6ca
Show file tree
Hide file tree
Showing 32 changed files with 497 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"uploaders",
"upyun",
"weibo"
]
],
"typescript.tsdk": "node_modules\\typescript\\lib"
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"start": "node ./bin/picgo",
"lint": "eslint src/**/*.ts",
"lint": "eslint src/**/*.ts && npm run build",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p . && npm run copy",
"copy": "copyfiles -f src/utils/clipboard/* dist/src/utils/clipboard",
Expand Down Expand Up @@ -53,15 +53,19 @@
"@commitlint/cli": "^7.5.2",
"@picgo/bump-version": "^1.0.1",
"@types/cross-spawn": "^6.0.0",
"@types/ejs": "^3.0.5",
"@types/fs-extra": "^5.0.4",
"@types/globby": "^9.1.0",
"@types/image-size": "^0.0.29",
"@types/inquirer": "^0.0.42",
"@types/lowdb": "^1.0.4",
"@types/md5": "^2.1.32",
"@types/mime-types": "^2.1.0",
"@types/minimatch": "^3.0.3",
"@types/node": "^10.5.2",
"@types/request-promise-native": "^1.0.15",
"@types/resolve": "^0.0.8",
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "3",
"@typescript-eslint/parser": "^3.2.0",
"babel-eslint": "^10.1.0",
Expand Down
3 changes: 1 addition & 2 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { EventEmitter } from 'events'
import PicGo from './PicGo'
import { IPlugin, Undefinable } from '../utils/interfaces'
import { IPlugin, Undefinable } from 'src/types'
import { handleUrlEncode } from '../utils/common'
import LifecyclePlugins from '../lib/LifecyclePlugins'

class Lifecycle extends EventEmitter {
configPath: string
ctx: PicGo

constructor (ctx: PicGo) {
Expand Down
28 changes: 14 additions & 14 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ 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 } from '../utils/interfaces'
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap } from 'src/types'
import getClipboardImage from '../utils/getClipboardImage'
import Request from '../lib/Request'
import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'

class PicGo extends EventEmitter {
private config: IConfig
private lifecycle: Lifecycle
private db: DB
class PicGo extends EventEmitter implements IPicGo {
private config!: IConfig
private lifecycle!: Lifecycle
private db!: DB
configPath: string
baseDir: string
helper: IHelper
baseDir!: string
helper!: IHelper
log: Logger
cmd: Commander
output: IImgInfo[]
input: any[]
pluginLoader: PluginLoader
pluginLoader!: PluginLoader
pluginHandler: PluginHandler
Request: Request
Request!: Request

constructor (configPath: string = '') {
super()
Expand Down Expand Up @@ -127,7 +127,7 @@ class PicGo extends EventEmitter {

// set config for ctx but will not be saved to db
// it's more lightweight
setConfig (config: object): void {
setConfig (config: IStringKeyMap<any>): void {
Object.keys(config).forEach((name: string) => {
set(this.config, name, config[name])
})
Expand All @@ -139,10 +139,10 @@ class PicGo extends EventEmitter {
unset(this.getConfig(key), propName)
}

async upload (input?: any[]): Promise<string | Error> {
async upload (input?: any[]): Promise<IImgInfo[] | Error> {
if (this.configPath === '') {
this.log.error('The configuration file only supports JSON format.')
return ''
return []
}
// upload from clipboard
if (input === undefined || input.length === 0) {
Expand All @@ -163,17 +163,17 @@ class PicGo extends EventEmitter {
}
})
await this.lifecycle.start([imgPath])
return this.output
}
} catch (e) {
this.log.error(e)
this.emit('failed', e)
throw e
}
return ''
} else {
// upload from path
await this.lifecycle.start(input)
return ''
return this.output
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Commander.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PicGo from '../core/PicGo'
import program, { CommanderStatic } from 'commander'
import inquirer, { Inquirer } from 'inquirer'
import { IPlugin } from '../utils/interfaces'
import { IPlugin } from 'src/types'
import commanders from '../plugins/commander'
import pkg from '../../package.json'

Expand Down
2 changes: 1 addition & 1 deletion src/lib/LifecyclePlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPlugin } from '../utils/interfaces'
import { IPlugin } from 'src/types'

class LifecyclePlugins {
static currentPlugin: string | null
Expand Down
11 changes: 6 additions & 5 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
ILogArgvType,
ILogArgvTypeWithError,
IConfig,
Undefinable
} from '../utils/interfaces'
Undefinable,
ILogColor
} from 'src/types'

class Logger {
private readonly level = {
Expand All @@ -21,8 +22,8 @@ class Logger {
}

private readonly ctx: PicGo
private logLevel: string
private logPath: string
private logLevel!: string
private logPath!: string
constructor (ctx: PicGo) {
this.ctx = ctx
}
Expand All @@ -31,7 +32,7 @@ class Logger {
// if configPath is invalid then this.ctx.config === undefined
// if not then check config.silent
if (this.ctx.getConfig<IConfig>() === undefined || !this.ctx.getConfig<Undefinable<string>>('silent')) {
const logHeader = chalk[this.level[type]](`[PicGo ${type.toUpperCase()}]:`)
const logHeader = chalk[this.level[type] as ILogColor](`[PicGo ${type.toUpperCase()}]:`)
console.log(logHeader, ...msg)
this.logLevel = this.ctx.getConfig('settings.logLevel')
const logPath = this.checkLogPathChange()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PicGo from '../core/PicGo'
import spawn from 'cross-spawn'
import { IResult, IProcessEnv, Undefinable } from '../utils/interfaces'
import { IResult, IProcessEnv, Undefinable } from 'src/types'

class PluginHandler {
// Thanks to feflow -> https://github.com/feflow/feflow/blob/master/lib/internal/install/plugin.js
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PicGo from '../core/PicGo'
import request, { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'
import { Undefinable } from '../utils/interfaces'
import { Undefinable } from 'src/types'

class Request {
ctx: PicGo
request: RequestPromiseAPI
request!: RequestPromiseAPI
constructor (ctx: PicGo) {
this.ctx = ctx
this.init()
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPlugin } from '../../utils/interfaces'
import { IPlugin } from 'src/types'

const config: IPlugin = {
handle: (ctx: PicGo) => {
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/commander/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import path from 'path'
import fs from 'fs-extra'
import { generate } from '../../utils/initUtils'
import { homedir } from 'os'
// @ts-expect-error
import download from 'download-git-repo'
import { IOptions, IPlugin } from '../../utils/interfaces'
import { IOptions, IPlugin } from 'src/types'
import rm from 'rimraf'

const run = (ctx: PicGo, options: IOptions): void => {
// const name = options.inPlace ? path.relative('../', process.cwd()) : options.project
if (options.offline) { // offline mode
if (fs.existsSync(options.template)) {
generate(ctx, options).catch((e) => { this.ctx.log.error(e) })
generate(ctx, options).catch((e) => { ctx.log.error(e) })
} else {
ctx.log.error(`Local template ${options.template} not found`)
}
Expand All @@ -39,7 +40,7 @@ const downloadAndGenerate = (ctx: PicGo, options: IOptions): void => {
return ctx.log.error(err)
}
ctx.log.success('Template files are downloaded!')
generate(ctx, options).catch((e) => { this.ctx.log.error(e) })
generate(ctx, options).catch((e) => { ctx.log.error(e) })
})
}

Expand Down Expand Up @@ -102,7 +103,7 @@ const init: IPlugin = {
throw e
}
}
})().catch((e) => { this.ctx.log.error(e) })
})().catch((e) => { ctx.log.error(e) })
})
.on('--help', () => {
console.log()
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/commander/pluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPlugin } from '../../utils/interfaces'
import { IPlugin } from 'src/types'

const pluginHandler: IPlugin = {
handle: (ctx: PicGo) => {
Expand All @@ -11,20 +11,20 @@ const pluginHandler: IPlugin = {
.alias('add')
.option('-p, --proxy <proxy>', 'Add proxy for installing')
.action((plugins: string[], program: any) => {
ctx.pluginHandler.install(plugins, program.proxy).catch((e) => { this.ctx.log.error(e) })
ctx.pluginHandler.install(plugins, program.proxy).catch((e) => { ctx.log.error(e) })
})
cmd.program
.command('uninstall <plugins...>')
.alias('rm')
.description('uninstall picgo plugin')
.action((plugins: string[]) => {
ctx.pluginHandler.uninstall(plugins).catch((e) => { this.ctx.log.error(e) })
ctx.pluginHandler.uninstall(plugins).catch((e) => { ctx.log.error(e) })
})
cmd.program
.command('update <plugins...>')
.description('update picgo plugin')
.action((plugins: string[]) => {
ctx.pluginHandler.update(plugins).catch((e) => { this.ctx.log.error(e) })
ctx.pluginHandler.update(plugins).catch((e) => { ctx.log.error(e) })
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPlugin } from '../../utils/interfaces'
import { IPlugin } from 'src/types'

const proxy: IPlugin = {
handle: (ctx: PicGo) => {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/commander/setting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig } from '../../utils/interfaces'
import { IPluginConfig, IStringKeyMap } from 'src/types'

// handle modules config -> save to picgo config file
const handleConfig = async (ctx: PicGo, prompts: IPluginConfig, module: string, name: string): Promise<void> => {
Expand Down Expand Up @@ -47,7 +47,7 @@ const setting = {
default: ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current')
}
]
const answer = await ctx.cmd.inquirer.prompt<string>(prompts)
const answer = await ctx.cmd.inquirer.prompt<IStringKeyMap<any>>(prompts)
const item = ctx.helper[module].get(answer[module])
if (item?.config) {
await handleConfig(ctx, item.config(ctx), module, answer[module])
Expand Down Expand Up @@ -92,7 +92,7 @@ const setting = {
throw e
}
}
})().catch((e) => { this.ctx.log.error(e) })
})().catch((e) => { ctx.log.error(e) })
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/commander/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import PicGo from '../../core/PicGo'
import path from 'path'
import fs from 'fs-extra'
import { isUrl } from '../../utils/common'
import { IPlugin } from '../../utils/interfaces'
import { IPlugin } from 'src/types'

const upload: IPlugin = {
handle: (ctx: PicGo) => {
Expand All @@ -26,7 +26,7 @@ const upload: IPlugin = {
return exist
})
await ctx.upload(inputList)
})().catch((e) => { this.ctx.log.error(e) })
})().catch((e) => { ctx.log.error(e) })
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/commander/use.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPlugin, Undefinable } from '../../utils/interfaces'
import { IPlugin, Undefinable, ICLIConfigs, IStringKeyMap } from 'src/types'

const use: IPlugin = {
handle: async (ctx: PicGo) => {
Expand All @@ -14,7 +14,7 @@ const use: IPlugin = {
// // load third-party plugins
// await ctx.pluginLoader.load()
let prompts: any[] = []
const config = {
const config: ICLIConfigs = {
uploader: {
type: 'list',
name: 'uploader',
Expand Down Expand Up @@ -52,7 +52,7 @@ const use: IPlugin = {

// handle for plugins option from Array to object
if (answer.plugins) {
const plugins = ctx.getConfig<object>('picgoPlugins')
const plugins = ctx.getConfig<IStringKeyMap<boolean>>('picgoPlugins')
Object.keys(plugins).map((item: string) => {
if (answer.plugins.includes(item)) {
plugins[item] = true
Expand All @@ -78,7 +78,7 @@ const use: IPlugin = {
throw e
}
}
})().catch((e) => { this.ctx.log.error(e) })
})().catch((e) => { ctx.log.error(e) })
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/transformer/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getFSFile,
getURLFile
} from '../../utils/common'
import { IPathTransformedImgInfo, IImgInfo, IImgSize } from '../../utils/interfaces'
import { IPathTransformedImgInfo, IImgInfo, IImgSize } from 'src/types'

const handle = async (ctx: PicGo): Promise<PicGo> => {
const results: IImgInfo[] = ctx.output
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/aliyun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig, IAliyunConfig } from '../../utils/interfaces'
import { IPluginConfig, IAliyunConfig } from 'src/types'
import crypto from 'crypto'
import mime from 'mime-types'
import { Options } from 'request-promise-native'
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig, IGithubConfig } from '../../utils/interfaces'
import { IPluginConfig, IGithubConfig } from 'src/types'
import { Options } from 'request-promise-native'

const postOptions = (fileName: string, options: IGithubConfig, data: any): Options => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/imgur.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig, IImgurConfig } from '../../utils/interfaces'
import { IPluginConfig, IImgurConfig } from 'src/types'
import { Options } from 'request-promise-native'

const postOptions = (options: IImgurConfig, fileName: string, imgBase64: string): Options => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/qiniu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PicGo from '../../core/PicGo'
import qiniu from 'qiniu'
import { IPluginConfig, IQiniuConfig } from '../../utils/interfaces'
import { IPluginConfig, IQiniuConfig } from 'src/types'
import { Options } from 'request-promise-native'

function postOptions (options: IQiniuConfig, fileName: string, token: string, imgBase64: string): Options {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/smms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig, ISmmsConfig } from '../../utils/interfaces'
import { IPluginConfig, ISmmsConfig } from 'src/types'
import { Options } from 'request-promise-native'

const postOptions = (fileName: string, image: Buffer, apiToken: string): Options => {
Expand Down
Loading

0 comments on commit 233a6ca

Please sign in to comment.