Skip to content

Commit

Permalink
✨ Feature(custom): refresh before getconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jun 13, 2024
1 parent e82100c commit 387abe7
Show file tree
Hide file tree
Showing 52 changed files with 485 additions and 455 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
extends: 'standard-with-typescript',
parserOptions: {
parser: '@typescript-eslint/parser',
project: './tsconfig.json'
},
rules: {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@
"@types/text-to-svg": "^3.1.4",
"@types/tunnel": "^0.0.6",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"babel-eslint": "^10.1.0",
"builtins": "^4.0.0",
"conventional-changelog": "^3.0.6",
"cross-env": "^7.0.3",
"cz-customizable": "^5.10.0",
"dpdm": "^3.14.0",
"eslint": "^8.53.0",
"eslint": "^8.57.0",
"eslint-config-standard-with-typescript": "^39.1.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.3.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-promise": "^6.2.0",
"eslint-plugin-standard": "^5.0.0",
"husky": "^1.3.1",
"node-bump-version": "^1.0.2",
Expand Down
13 changes: 7 additions & 6 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import axios from 'axios'
import { EventEmitter } from 'events'
import { type IBuildInWaterMarkOptions, type IBuildInCompressOptions, type ILifecyclePlugins, type IPathTransformedImgInfo, type IPicGo, type IPlugin, type Undefinable, type IImgInfo } from '../types'
import fs from 'fs-extra'
import heicConvert from 'heic-convert'
import { cloneDeep } from 'lodash'
import path from 'path'

import type { IBuildInWaterMarkOptions, IBuildInCompressOptions, ILifecyclePlugins, IPathTransformedImgInfo, IPicGo, IPlugin, Undefinable, IImgInfo } from '../types'
import { getURLFile, handleUrlEncode, imageProcess, isUrl, needCompress, needAddWatermark, imageAddWaterMark, removeExif, renameFileNameWithCustomString, getConvertedFormat } from '../utils/common'
import { IBuildInEvent } from '../utils/enum'
import { createContext } from '../utils/createContext'
import path from 'path'
import fs from 'fs-extra'
import axios from 'axios'
import { cloneDeep } from 'lodash'
import heicConvert from 'heic-convert'

export class Lifecycle extends EventEmitter {
private readonly ctx: IPicGo
Expand Down
26 changes: 17 additions & 9 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import fs from 'fs-extra'
import path from 'path'
import { EventEmitter } from 'events'
import fs from 'fs-extra'
import { get, set, unset } from 'lodash'
import { homedir } from 'os'
import path from 'path'

import Commander from '../lib/Commander'
import LifecyclePlugins, { setCurrentPluginName } from '../lib/LifecyclePlugins'
import { Logger } from '../lib/Logger'
import PluginHandler from '../lib/PluginHandler'
import PluginLoader from '../lib/PluginLoader'
import Request from '../lib/Request'

import Lifecycle from './Lifecycle'
import LifecyclePlugins, { setCurrentPluginName } from '../lib/LifecyclePlugins'

import uploaders from '../plugins/uploader'
import transformers from '../plugins/transformer'
import PluginLoader from '../lib/PluginLoader'
import { get, set, unset } from 'lodash'
import { type IHelper, type IImgInfo, type IConfig, type IPicGo, type IStringKeyMap, type IPluginLoader, type II18nManager, type IPicGoPlugin, type IPicGoPluginInterface, type IRequest } from '../types'

import getClipboardImage from '../utils/getClipboardImage'
import Request from '../lib/Request'

import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'
import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'
import { IBuildInEvent, IBusEvent } from '../utils/enum'
import { eventBus } from '../utils/eventBus'
import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'

import { I18nManager } from '../i18n'

import type { IHelper, IImgInfo, IConfig, IPicGo, IStringKeyMap, IPluginLoader, II18nManager, IPicGoPlugin, IPicGoPluginInterface, IRequest } from '../types'

export class PicGo extends EventEmitter implements IPicGo {
private _config!: IConfig
private lifecycle!: Lifecycle
Expand Down Expand Up @@ -131,6 +138,7 @@ export class PicGo extends EventEmitter implements IPicGo {

getConfig<T> (name?: string): T {
if (!name) {
this._config = this.db.read(true) as IConfig
return this._config as unknown as T
} else {
return get(this._config, name)
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Commander.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import { Command } from 'commander'
import inquirer, { type Inquirer } from 'inquirer'
import { type IPlugin, type ICommander, type IPicGo } from '../types'

import commanders from '../plugins/commander'
import { getCurrentPluginName } from './LifecyclePlugins'

import type { IPlugin, ICommander, IPicGo } from '../types'

export class Commander implements ICommander {
private readonly name = 'commander'
static currentPlugin: string | null
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 { type IPlugin, type ILifecyclePlugins } from '../types'
import type { IPlugin, ILifecyclePlugins } from '../types'

export class LifecyclePlugins implements ILifecyclePlugins {
static currentPlugin: string | null
Expand Down
15 changes: 8 additions & 7 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import dayjs from 'dayjs'
import fs from 'fs-extra'
import path from 'path'
import util from 'util'

import { ILogType } from '../utils/enum'
import {
type ILogArgvType,
type ILogArgvTypeWithError,
type Undefinable,
type ILogColor,
type ILogger,
type IPicGo
import type {
ILogArgvType,
ILogArgvTypeWithError,
Undefinable,
ILogColor,
ILogger,
IPicGo
} from '../types'
import { forceNumber, isDev } from '../utils/common'

Expand Down
20 changes: 10 additions & 10 deletions src/lib/PluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import spawn from 'cross-spawn'
import {
type IResult,
type IProcessEnv,
type IPluginProcessResult,
type IPluginHandler,
type IPluginHandlerOptions,
type Undefinable,
type IPicGo,
type IPluginHandlerResult
import type {
IResult,
IProcessEnv,
IPluginProcessResult,
IPluginHandler,
IPluginHandlerOptions,
Undefinable,
IPicGo,
IPluginHandlerResult
} from '../types'
import { IBuildInEvent } from '../utils/enum'
import { getProcessPluginName, getNormalPluginName } from '../utils/common'
import { type ILocalesKey } from '../i18n/zh-CN'
import type { ILocalesKey } from '../i18n/zh-CN'

export class PluginHandler implements IPluginHandler {
// 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/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra'
import path from 'path'
import resolve from 'resolve'
import { IBuildInEvent } from '../utils/enum'
import { type IPicGo, type IPicGoPlugin, type IPluginLoader, type IPicGoPluginInterface } from '../types/index'
import type { IPicGo, IPicGoPlugin, IPluginLoader, IPicGoPluginInterface } from '../types/index'
import { setCurrentPluginName } from './LifecyclePlugins'

/**
Expand Down
13 changes: 8 additions & 5 deletions src/lib/Request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/promise-function-async */
import axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios'
import { type IPicGo, type Undefinable, type IConfigChangePayload, type IConfig, type IRequestConfig, type IOldReqOptions, type IResponse, type IFullResponse, type IRequest } from '../types'
import { IBusEvent } from '../utils/enum'
import { eventBus } from '../utils/eventBus'
import { URL } from 'url'
import axios from 'axios'
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
import FormData from 'form-data'
import https from 'https'
import tunnel from 'tunnel'
import { URL } from 'url'

import type { IPicGo, Undefinable, IConfigChangePayload, IConfig, IRequestConfig, IOldReqOptions, IResponse, IFullResponse, IRequest } from '../types'
import { IBusEvent } from '../utils/enum'
import { eventBus } from '../utils/eventBus'

const httpsAgent = new https.Agent({
maxVersion: 'TLSv1.2',
minVersion: 'TLSv1.2',
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/beforetransformer/compress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IPicGo, type IPluginConfig, type IBuildInCompressOptions } from '../../types'
import { type ILocalesKey } from '../../i18n/zh-CN'
import type { IPicGo, IPluginConfig, IBuildInCompressOptions } from '../../types'
import type { ILocalesKey } from '../../i18n/zh-CN'

const config = (ctx: IPicGo): IPluginConfig[] => {
const userConfig = ctx.getConfig<IBuildInCompressOptions>('buildIn.compress') || {}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/beforetransformer/watermark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IPicGo, type IPluginConfig, type IBuildInWaterMarkOptions } from '../../types'
import { type ILocalesKey } from '../../i18n/zh-CN'
import type { IPicGo, IPluginConfig, IBuildInWaterMarkOptions } from '../../types'
import type { ILocalesKey } from '../../i18n/zh-CN'

const config = (ctx: IPicGo): IPluginConfig[] => {
const userConfig = ctx.getConfig<IBuildInWaterMarkOptions>('buildIn.watermark') || {}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/beforeupload/buildInRename.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IPicGo, type IPluginConfig } from '../../types'
import { type ILocalesKey } from '../../i18n/zh-CN'
import type { IPicGo, IPluginConfig } from '../../types'
import type { ILocalesKey } from '../../i18n/zh-CN'

const config = (ctx: IPicGo): IPluginConfig[] => {
const userConfig = ctx.getConfig<any>('buildIn.rename') || {}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPlugin } from '../../types'
import type { IPicGo, IPlugin } from '../../types'

const config: IPlugin = {
handle: (ctx: IPicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPlugin, type IPicGo, type IStringKeyMap } from '../../types'
import type { IPlugin, IPicGo, IStringKeyMap } from '../../types'

const i18n: IPlugin = {
handle: (ctx: IPicGo) => {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/commander/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import use from './use'
import proxy from './proxy'
import init from './init'
import i18n from './i18n'
import { type IPicGo } from '../../types'

import type { IPicGo } from '../../types'

export default (ctx: IPicGo): void => {
ctx.cmd.register('pluginHandler', pluginHandler)
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/commander/init.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import chalk from 'chalk'
import path from 'path'
import download from 'download-git-repo'
import fs from 'fs-extra'
import { generate } from '../../utils/initUtils'
import { homedir } from 'os'
import download from 'download-git-repo'
import { type IOptions, type IPlugin, type IPicGo } from '../../types'
import path from 'path'
import rm from 'rimraf'

import { generate } from '../../utils/initUtils'
import type { IOptions, IPlugin, IPicGo } from '../../types'

const run = (ctx: IPicGo, options: IOptions): void => {
// const name = options.inPlace ? path.relative('../', process.cwd()) : options.project
if (options.offline) { // offline mode
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/pluginHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPlugin } from '../../types'
import type { IPicGo, IPlugin } from '../../types'

type actionFunc = (plugins: string[], program: any) => void

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPlugin } from '../../types'
import type { IPicGo, IPlugin } from '../../types'

const proxy: IPlugin = {
handle: (ctx: IPicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/setting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPluginConfig, type IStringKeyMap } from '../../types'
import type { IPicGo, IPluginConfig, IStringKeyMap } from '../../types'
import compress from '../beforetransformer/compress'
import watermark from '../beforetransformer/watermark'
import rename from '../beforeupload/buildInRename'
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/upload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs-extra'
import { isUrl } from '../../utils/common'
import { type IPicGo, type IPlugin } from '../../types'
import type { IPicGo, IPlugin } from '../../types'

const upload: IPlugin = {
handle: (ctx: IPicGo) => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/commander/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPlugin, type Undefinable, type ICLIConfigs, type IStringKeyMap } from '../../types'
import type { IPicGo, IPlugin, Undefinable, ICLIConfigs, IStringKeyMap } from '../../types'

const use: IPlugin = {
handle: async (ctx: IPicGo) => {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/transformer/base64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type IPicGo } from '../../types'
import type { IPicGo } from '../../types'

const handle = async (ctx: IPicGo): Promise<IPicGo> => {
ctx.output.push(...ctx.input)
return ctx
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type IPicGo, type IPicGoPlugin } from '../../types'
import type { IPicGo, IPicGoPlugin } from '../../types'
import ImgFromPath from './path'
import ImgFromBase64 from './base64'

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/transformer/path.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import dayjs from 'dayjs'

import {
isUrl,
getImageSize,
getFSFile,
getURLFile
} from '../../utils/common'
import { type IPicGo, type IPathTransformedImgInfo, type IImgInfo, type IImgSize } from '../../types'
import dayjs from 'dayjs'
import type { IPicGo, IPathTransformedImgInfo, IImgInfo, IImgSize } from '../../types'

const handle = async (ctx: IPicGo): Promise<IPicGo> => {
const results: IImgInfo[] = ctx.output
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/uploader/aliyun.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type IPicGo, type IPluginConfig, type IAliyunConfig, type IOldReqOptionsWithFullResponse } from '../../types'
import crypto from 'crypto'
import mime from 'mime-types'
import { IBuildInEvent } from '../../utils/enum'
import { type ILocalesKey } from '../../i18n/zh-CN'
import type { ILocalesKey } from '../../i18n/zh-CN'
import { encodePath, formatPathHelper } from './utils'
import type { IPicGo, IPluginConfig, IAliyunConfig, IOldReqOptionsWithFullResponse } from '../../types'

const getCurrentUTCDate = (): string => new Date().toUTCString()

Expand Down
Loading

0 comments on commit 387abe7

Please sign in to comment.