Skip to content

Commit

Permalink
✨ Feature(custom): optimize commander
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jul 3, 2024
1 parent c7f5659 commit 9e42652
Show file tree
Hide file tree
Showing 26 changed files with 247 additions and 223 deletions.
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { string } from 'rollup-plugin-string'
import json from '@rollup/plugin-json'
import builtins from 'builtins'
import replace from '@rollup/plugin-replace'

const version = process.env.VERSION || pkg.version
const sourcemap = 'inline'
const banner = `/*
Expand Down
1 change: 1 addition & 0 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { createContext } from '../utils/createContext'

const watermarkMsg = 'Add watermark to image'
const compressMsg = 'Compress or convert image'

export class Lifecycle extends EventEmitter {
private readonly ctx: IPicGo
private readonly ttfLink: string = 'https://release.piclist.cn/simhei.ttf'
Expand Down
11 changes: 5 additions & 6 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { Request } from '../lib/Request'

import { Lifecycle } from './Lifecycle'

import uploaders from '../plugins/uploader'
import transformers from '../plugins/transformer'
import buildInUploaders from '../plugins/uploader'
import buildInTransformers from '../plugins/transformer'

import getClipboardImage from '../utils/getClipboardImage'

Expand Down Expand Up @@ -112,8 +112,8 @@ export class PicGo extends EventEmitter implements IPicGo {
this._pluginLoader = new PluginLoader(this)
// load self plugins
setCurrentPluginName('picgo')
uploaders(this).register(this)
transformers(this).register(this)
buildInUploaders().register(this)
buildInTransformers().register(this)
setCurrentPluginName('')
// load third-party plugins
this._pluginLoader.load()
Expand Down Expand Up @@ -212,7 +212,7 @@ export class PicGo extends EventEmitter implements IPicGo {
this.log.error('No config file found, please check your config file path')
return []
}
// upload from clipboard

if (input === undefined || input.length === 0) {
try {
const { imgPath, shouldKeepAfterUploading } = await getClipboardImage(this)
Expand All @@ -236,7 +236,6 @@ export class PicGo extends EventEmitter implements IPicGo {
throw e
}
} else {
// upload from path
const { output } = await this.lifecycle.start(input)
return output
}
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const EN: ILocales = {
// smms
PICBED_SMMS: 'SM.MS',
PICBED_SMMS_TOKEN: 'Set Token',
PICBED_SMMS_MESSAGE_TOKEN: 'Please enter the token',
PICBED_SMMS_BACKUP_DOMAIN: 'Set Backup Upload Domain',
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: 'Ex. smms.app',

Expand Down Expand Up @@ -234,9 +235,11 @@ export const EN: ILocales = {
PICBED_AWSS3PLIST_MESSAGE_ACL: 'Ex. public-read',
PICBED_AWSS3PLIST_MESSAGE_DISABLEBUCKETPREFIXTOURL: 'Enable when `pathStyleAccess` is enabled',

BUILDIN_RENAME: 'Rename Settings',
BUILDIN_RENAME_FORMAT: 'Set rename format',
BUILDIN_RENAME_ENABLE: 'Enable rename',

BUILDIN_WATERMARK: 'Watermark Settings',
BUILDIN_WATERMARK_ISADDWATERMARK: 'Whether to add watermark',
BUILDIN_WATERMARK_MESSAGE_ISADDWATERMARK: 'Enable when watermark is required',
BUILDIN_WATERMARK_WATERMARKTYPE: 'Set watermark type',
Expand All @@ -258,6 +261,7 @@ export const EN: ILocales = {
BUILDIN_WATERMARK_WATERMARKPOSITION: 'Set watermark position',
BUILDIN_WATERMARK_MESSAGE_WATERMARKPOSITION: 'Please select watermark position',

BUILDIN_COMPRESS: 'Processing Settings',
BUILDIN_COMPRESS_QUALITY: 'Set image quality',
BUILDIN_COMPRESS_MESSAGE_QUALITY: 'Please enter an integer between 0-100',
BUILDIN_COMPRESS_ISCONVERT: 'Whether to perform format conversion',
Expand Down
6 changes: 5 additions & 1 deletion src/i18n/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ZH_CN = {
// smms
PICBED_SMMS: 'SM.MS',
PICBED_SMMS_TOKEN: '设定Token',
PICBED_SMMS_MESSAGE_TOKEN: '请填写Token',
PICBED_SMMS_BACKUP_DOMAIN: '备用上传域名',
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: '例如 smms.app',

Expand Down Expand Up @@ -188,7 +189,7 @@ export const ZH_CN = {
PICBED_PICLIST_KEY: '设定鉴权Key',

// Lsky Pro
PICBED_LSKY_PLIST: 'Lsky',
PICBED_LSKY_PLIST: '兰空图床',
PICBED_LSKY_VERSION: '设定版本',
PICBED_LSKY_MESSAGE_VERSION: '例如V1',
PICBED_LSKY_HOST: '设定主机',
Expand Down Expand Up @@ -229,6 +230,7 @@ export const ZH_CN = {
PICBED_AWSS3PLIST_MESSAGE_ACL: '例如:public-read',
PICBED_AWSS3PLIST_MESSAGE_DISABLEBUCKETPREFIXTOURL: '需要时启用',

BUILDIN_WATERMARK: '水印设置',
BUILDIN_WATERMARK_ISADDWATERMARK: '是否添加水印',
BUILDIN_WATERMARK_MESSAGE_ISADDWATERMARK: '需要添加水印时启用',
BUILDIN_WATERMARK_WATERMARKTYPE: '设定水印类型',
Expand All @@ -250,9 +252,11 @@ export const ZH_CN = {
BUILDIN_WATERMARK_WATERMARKPOSITION: '设定水印位置',
BUILDIN_WATERMARK_MESSAGE_WATERMARKPOSITION: '请选择水印位置',

BUILDIN_RENAME: '重命名设置',
BUILDIN_RENAME_FORMAT: '设定重命名格式',
BUILDIN_RENAME_ENABLE: '是否启用重命名',

BUILDIN_COMPRESS: '图片处理',
BUILDIN_COMPRESS_QUALITY: '设定图片质量',
BUILDIN_COMPRESS_MESSAGE_QUALITY: '请输入0-100的整数',
BUILDIN_COMPRESS_ISCONVERT: '是否进行格式转换',
Expand Down
6 changes: 5 additions & 1 deletion src/i18n/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const ZH_TW: ILocales = {
// smms
PICBED_SMMS: 'SM.MS',
PICBED_SMMS_TOKEN: '設定Token',
PICBED_SMMS_MESSAGE_TOKEN: '請輸入Token',
PICBED_SMMS_BACKUP_DOMAIN: '備用上傳網址',
PICBED_SMMS_MESSAGE_BACKUP_DOMAIN: '例如 smms.app',

Expand Down Expand Up @@ -190,7 +191,7 @@ export const ZH_TW: ILocales = {
PICBED_PICLIST_KEY: '設定鑒權Key',

// Lsky Pro
PICBED_LSKY_PLIST: 'Lsky',
PICBED_LSKY_PLIST: '蘭空圖床',
PICBED_LSKY_VERSION: '設定版本',
PICBED_LSKY_MESSAGE_VERSION: '請選擇版本',
PICBED_LSKY_HOST: '設定主機',
Expand Down Expand Up @@ -231,6 +232,7 @@ export const ZH_TW: ILocales = {
PICBED_AWSS3PLIST_MESSAGE_ACL: '例如:public-read',
PICBED_AWSS3PLIST_MESSAGE_DISABLEBUCKETPREFIXTOURL: '需要時啟用',

BUILDIN_WATERMARK: '水印設定',
BUILDIN_WATERMARK_ISADDWATERMARK: '是否添加水印',
BUILDIN_WATERMARK_MESSAGE_ISADDWATERMARK: '需要添加水印時啟用',
BUILDIN_WATERMARK_WATERMARKTYPE: '設定水印類型',
Expand All @@ -252,9 +254,11 @@ export const ZH_TW: ILocales = {
BUILDIN_WATERMARK_WATERMARKPOSITION: '設定水印位置',
BUILDIN_WATERMARK_MESSAGE_WATERMARKPOSITION: '請選擇水印位置',

BUILDIN_RENAME: '重命名設定',
BUILDIN_RENAME_FORMAT: '設定重命名格式',
BUILDIN_RENAME_ENABLE: '是否啟用重命名',

BUILDIN_COMPRESS: '圖片處理設定',
BUILDIN_COMPRESS_QUALITY: '設定圖片質量',
BUILDIN_COMPRESS_MESSAGE_QUALITY: '請輸入0-100的整數',
BUILDIN_COMPRESS_ISCONVERT: '是否進行格式轉換',
Expand Down
14 changes: 13 additions & 1 deletion src/lib/LifecyclePlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import { IPlugin, ILifecyclePlugins } from '../types'

export class LifecyclePlugins implements ILifecyclePlugins {
static currentPlugin: string | null

/**
* The name of the plugin
*/
private readonly name: string
/**
* The list of plugins
*/
private readonly list: Map<string, IPlugin>
/**
* The map of plugin id
*/
private readonly pluginIdMap: Map<string, string[]>
private readonly name: string

constructor(name: string) {
this.name = name
Expand All @@ -16,7 +26,9 @@ export class LifecyclePlugins implements ILifecyclePlugins {
if (!id) throw new TypeError('id is required!')
if (typeof plugin.handle !== 'function') throw new TypeError('plugin.handle must be a function!')
if (this.list.has(id)) throw new TypeError(`${this.name} duplicate id: ${id}!`)

this.list.set(id, plugin)

if (LifecyclePlugins.currentPlugin) {
if (this.pluginIdMap.has(LifecyclePlugins.currentPlugin)) {
this.pluginIdMap.get(LifecyclePlugins.currentPlugin)?.push(id)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export class PluginLoader implements IPluginLoader {

// load all third party plugin
load(): boolean {
const packagePath = path.join(this.ctx.baseDir, 'package.json')
const pluginDir = path.join(this.ctx.baseDir, 'node_modules/')
// Thanks to hexo -> https://github.com/hexojs/hexo/blob/master/lib/hexo/load_plugins.js
if (!fs.existsSync(pluginDir)) {
return false
}
const packagePath = path.join(this.ctx.baseDir, 'package.json')
const json = readJSONSync(packagePath)
const deps = Object.keys(json.dependencies || {})
const devDeps = Object.keys(json.devDependencies || {})
Expand Down
2 changes: 0 additions & 2 deletions src/lib/Request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/promise-function-async */
import axios from 'axios'
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
import FormData from 'form-data'
Expand Down
15 changes: 13 additions & 2 deletions src/plugins/commander/setting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ILocalesKey } from '../../i18n/zh-CN'
import { IPicGo, IPluginConfig, IStringKeyMap } from '../../types'
import compress from '../beforetransformer/compress'
import watermark from '../beforetransformer/watermark'
import rename from '../beforeupload/buildInRename'
import { uploaderTranslators } from './utils'

// handle modules config -> save to picgo config file
const handleConfig = async (ctx: IPicGo, prompts: IPluginConfig[], module: string, name: string): Promise<void> => {
Expand Down Expand Up @@ -56,7 +58,11 @@ const setting = {
{
type: 'list',
name: 'buildin',
choices: ['compress', 'watermark', 'rename'],
choices: [
{ name: ctx.i18n.translate<ILocalesKey>('BUILDIN_COMPRESS'), value: 'compress' },
{ name: ctx.i18n.translate<ILocalesKey>('BUILDIN_WATERMARK'), value: 'watermark' },
{ name: ctx.i18n.translate<ILocalesKey>('BUILDIN_RENAME'), value: 'rename' }
],
message: 'Choose a buildin module'
}
]
Expand Down Expand Up @@ -86,7 +92,12 @@ const setting = {
{
type: 'list',
name: `${module}`,
choices: ctx.helper[module].getIdList(),
choices: ctx.helper[module].getIdList().map((item: string) => {
return {
name: uploaderTranslators(ctx)[item] || item,
value: item
}
}),
message: `Choose a(n) ${module}`
// default: ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current')
}
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/commander/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IPicGo, IPlugin, Undefinable, ICLIConfigs, IStringKeyMap } from '../../types'
import { uploaderTranslators } from './utils'

const use: IPlugin = {
handle: async (ctx: IPicGo) => {
Expand All @@ -16,7 +17,12 @@ const use: IPlugin = {
type: 'list',
name: 'uploader',
message: 'Use an uploader',
choices: ctx.helper.uploader.getIdList(),
choices: ctx.helper.uploader.getIdList().map((item: string) => {
return {
name: uploaderTranslators(ctx)[item] || item,
value: item
}
}),
default: ctx.getConfig('picBed.uploader') || ctx.getConfig('picBed.current') || 'smms'
},
transformer: {
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/commander/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ILocalesKey } from '../../i18n/zh-CN'
import { IPicGo, IStringKeyMap } from '../../types'

export const uploaderTranslators = (ctx: IPicGo) => {
return {
alistplist: ctx.i18n.translate<ILocalesKey>('PICBED_ALIST_PLIST'),
aliyun: ctx.i18n.translate<ILocalesKey>('PICBED_ALICLOUD'),
'aws-s3-plist': ctx.i18n.translate<ILocalesKey>('PICBED_AWSS3PLIST'),
github: ctx.i18n.translate<ILocalesKey>('PICBED_GITHUB'),
imgur: ctx.i18n.translate<ILocalesKey>('PICBED_IMGUR'),
local: ctx.i18n.translate<ILocalesKey>('PICBED_LOCAL'),
lskyplist: ctx.i18n.translate<ILocalesKey>('PICBED_LSKY_PLIST'),
piclist: ctx.i18n.translate<ILocalesKey>('PICBED_PICLIST'),
qiniu: ctx.i18n.translate<ILocalesKey>('PICBED_QINIU'),
sftpplist: ctx.i18n.translate<ILocalesKey>('PICBED_SFTPPLIST'),
smms: ctx.i18n.translate<ILocalesKey>('PICBED_SMMS'),
tcyun: ctx.i18n.translate<ILocalesKey>('PICBED_TENCENTCLOUD'),
telegraphplist: ctx.i18n.translate<ILocalesKey>('PICBED_TELEGRAPH'),
upyun: ctx.i18n.translate<ILocalesKey>('PICBED_UPYUN'),
webdavplist: ctx.i18n.translate<ILocalesKey>('PICBED_WEBDAVPLIST')
} as IStringKeyMap<any>
}
4 changes: 2 additions & 2 deletions src/plugins/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IPicGo, IPicGoPlugin } from '../../types'
import { IPicGo } from '../../types'
import ImgFromPath from './path'
import ImgFromBase64 from './base64'

const buildInTransformers: IPicGoPlugin = () => {
const buildInTransformers = () => {
return {
register(ctx: IPicGo) {
ctx.helper.transformer.register('path', ImgFromPath)
Expand Down
Loading

0 comments on commit 9e42652

Please sign in to comment.