Skip to content

Commit

Permalink
✨ Feature: new addPlugin api for node projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Dec 19, 2020
1 parent 83535b9 commit 5a18432
Show file tree
Hide file tree
Showing 28 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"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",
"copy": "copyfiles -f src/utils/clipboard/* dist/src/utils/clipboard && copyfiles -f src/types/* dist/src/types",
"dev": "tsc -w -p .",
"patch": "npm version patch && git push origin master && git push origin --tags",
"minor": "npm version minor && git push origin master && git push origin --tags",
Expand Down
2 changes: 1 addition & 1 deletion src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events'
import PicGo from './PicGo'
import { IPlugin, Undefinable } from 'src/types'
import { IPlugin, Undefinable } from '../types'
import { handleUrlEncode } from '../utils/common'
import LifecyclePlugins from '../lib/LifecyclePlugins'

Expand Down
18 changes: 17 additions & 1 deletion src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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 } from 'src/types'
import { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPicGoPlugin } from '../types'
import getClipboardImage from '../utils/getClipboardImage'
import Request from '../lib/Request'
import DB from '../utils/db'
Expand Down Expand Up @@ -139,6 +139,22 @@ class PicGo extends EventEmitter implements IPicGo {
unset(this.getConfig(key), propName)
}

/**
* for node project adding a plugin by a simple way
*/
addPlugin (name: string, plugin: IPicGoPlugin): void {
if (!name || !plugin || (typeof plugin !== 'function')) {
this.log.warn('Please provide valid plugin')
return
}
try {
plugin(this).register()
} catch (e) {
this.log.warn('Please provide valid plugin')
this.log.error(e)
}
}

async upload (input?: any[]): Promise<IImgInfo[] | Error> {
if (this.configPath === '') {
this.log.error('The configuration file only supports JSON format.')
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 'src/types'
import { IPlugin } from '../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 'src/types'
import { IPlugin } from '../types'

class LifecyclePlugins {
static currentPlugin: string | null
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IConfig,
Undefinable,
ILogColor
} from 'src/types'
} from '../types'

class Logger {
private readonly level = {
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 'src/types'
import { IResult, IProcessEnv, Undefinable } from '../types'

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

class Request {
ctx: PicGo
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 'src/types'
import { IPlugin } from '../../types'

const config: IPlugin = {
handle: (ctx: PicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { generate } from '../../utils/initUtils'
import { homedir } from 'os'
// @ts-expect-error
import download from 'download-git-repo'
import { IOptions, IPlugin } from 'src/types'
import { IOptions, IPlugin } from '../../types'
import rm from 'rimraf'

const run = (ctx: PicGo, options: IOptions): void => {
Expand Down
2 changes: 1 addition & 1 deletion 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 'src/types'
import { IPlugin } from '../../types'

const pluginHandler: IPlugin = {
handle: (ctx: PicGo) => {
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 'src/types'
import { IPlugin } from '../../types'

const proxy: IPlugin = {
handle: (ctx: PicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion 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, IStringKeyMap } from 'src/types'
import { IPluginConfig, IStringKeyMap } from '../../types'

// handle modules config -> save to picgo config file
const handleConfig = async (ctx: PicGo, prompts: IPluginConfig, module: string, name: string): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion 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 'src/types'
import { IPlugin } from '../../types'

const upload: IPlugin = {
handle: (ctx: PicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion 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, ICLIConfigs, IStringKeyMap } from 'src/types'
import { IPlugin, Undefinable, ICLIConfigs, IStringKeyMap } from '../../types'

const use: IPlugin = {
handle: async (ctx: PicGo) => {
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 'src/types'
import { IPathTransformedImgInfo, IImgInfo, IImgSize } from '../../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 'src/types'
import { IPluginConfig, IAliyunConfig } from '../../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 'src/types'
import { IPluginConfig, IGithubConfig } from '../../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 'src/types'
import { IPluginConfig, IImgurConfig } from '../../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 'src/types'
import { IPluginConfig, IQiniuConfig } from '../../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 'src/types'
import { IPluginConfig, ISmmsConfig } from '../../types'
import { Options } from 'request-promise-native'

const postOptions = (fileName: string, image: Buffer, apiToken: string): Options => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/tcyun.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PicGo from '../../core/PicGo'
import crypto from 'crypto'
import mime from 'mime-types'
import { IPluginConfig, ITcyunConfig } from 'src/types'
import { IPluginConfig, ITcyunConfig } from '../../types'
import { Options } from 'request-promise-native'

// generate COS signature string
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/uploader/upyun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PicGo from '../../core/PicGo'
import { IPluginConfig, IUpyunConfig } from 'src/types'
import { IPluginConfig, IUpyunConfig } from '../../types'
import crypto from 'crypto'
import MD5 from 'md5'
import { Options } from 'request-promise-native'
Expand Down
28 changes: 28 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ interface IPlugin {
[propName: string]: any
}

/**
* for picgo npm plugins
*/

type IPicGoPlugin = (ctx: IPicGo) => IPicGoPluginInterface

/**
* interfaces for PicGo plugin
*/
interface IPicGoPluginInterface {
/**
* since PicGo-Core v1.5, register will inject ctx
*/
register: (ctx?: PicGo) => void
/**
* this plugin's config
*/
config?: (ctx?: PicGo) => IPluginConfig[]
/**
* register uploader name
*/
uploader?: string
/**
* register transformer name
*/
transformer?: string
}

/**
* for spawn output
*/
Expand Down
2 changes: 1 addition & 1 deletion src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import lodashId from 'lodash-id'
import FileSync from 'lowdb/adapters/FileSync'
import json from 'comment-json'
import PicGo from '../core/PicGo'
import { IConfig } from 'src/types'
import { IConfig } from '../types'

class DB {
private readonly ctx: PicGo
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getClipboardImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawn } from 'child_process'
import dayjs from 'dayjs'
import os from 'os'
import fs from 'fs-extra'
import { IClipboardImage } from 'src/types'
import { IClipboardImage } from '../types'

const getCurrentPlatform = (): string => {
const platform = process.platform
Expand Down
2 changes: 1 addition & 1 deletion src/utils/initUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path'
// @ts-expect-error
import globby from 'globby'
import ejs from 'ejs'
import { IFileTree } from 'src/types'
import { IFileTree } from '../types'

/**
* Generate template files to destination files.
Expand Down
14 changes: 7 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"outDir": "dist",
"skipLibCheck": true,
"baseUrl": ".",
"typeRoots": [
"./node_modules/@types",
"./src/types",
],
"paths": {
"*": ["*", "./src/types/*"]
},
// "typeRoots": [
// "./node_modules/@types",
// "./src/types",
// ],
// "paths": {
// "*": ["*", "./src/types/*"]
// },
"lib": [
"es2017",
"es2015",
Expand Down

0 comments on commit 5a18432

Please sign in to comment.