diff --git a/.gitignore b/.gitignore index 57bf1bc..2ada266 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dist/ yarn-error.log temp.js package-lock.json +.eslintrc diff --git a/package.json b/package.json index 600ebe3..5fd9fd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "picgo-plugin-super-prefix", - "version": "1.2.2", + "version": "1.2.3", "description": "A PicGo plugin for elegant file name prefix", "main": "dist/index.js", "publishConfig": { @@ -23,17 +23,19 @@ "author": "gclove", "license": "MIT", "devDependencies": { - "@types/node": "^10.10.1", - "eslint": "^5.0.1", - "eslint-config-standard": "^11.0.0", - "eslint-plugin-import": "^2.13.0", - "eslint-plugin-node": "^6.0.1", - "eslint-plugin-promise": "^3.8.0", + "@types/node": "16.9.1", + "@typescript-eslint/eslint-plugin": "^5.40.0", + "@typescript-eslint/parser": "^5.40.0", + "eslint-config-standard-with-typescript": "^23.0.0", + "eslint-plugin-node": "^11.1.0", "eslint-plugin-standard": "^3.1.0", - "picgo": "^1.2.0", - "tslint": "^5.10.0", - "tslint-config-standard": "^7.1.0", - "typescript": "^3.0.3" + "picgo": "^1.5.0-alpha.13", + "typescript": "^4.8.4", + "eslint": "^8.25.0", + "eslint-config-standard": "^17.0.0", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-n": "^15.3.0", + "eslint-plugin-promise": "^6.1.0" }, "dependencies": { "dayjs": "^1.8.17" diff --git a/src/index.ts b/src/index.ts index 41e9c14..c10b09e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,95 @@ -import picgo from 'picgo' +import { PicGo } from 'picgo' import dayjs from 'dayjs' function sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)) } +async function beforeTransformHandle (ctx) { + const autoRename = ctx.getConfig('settings.autoRename'); + if (autoRename) { + ctx.emit('notification', { + title: '❌ 警告', + body: '请关闭 PicGo 的 ​``【oaicite:0】``​ 功能,\nsuper-prefix 插件重命名方式会被覆盖', + }); + await sleep(10000); + throw new Error('super-prefix conflict'); + } + + let userConfig = ctx.getConfig('picgo-plugin-super-prefix'); + if (!userConfig) { + userConfig = { + prefix: '', + fileFormat: '', + }; + } + + for (let i = 0; i < ctx.output.length; i++) { + let fileName = ctx.output[i].fileName; + let prefix = ''; + if (userConfig.prefixFormat !== undefined && userConfig.prefixFormat !== '') { + prefix = dayjs().format(userConfig.prefixFormat); + } + + if (userConfig.fileFormat !== undefined && userConfig.fileFormat !== '') { + if (i > 0) { + fileName = prefix + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname; + } else { + fileName = prefix + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname; + } + } else { + fileName = prefix + fileName; + } + + ctx.output[i].fileName = fileName; + } +} + +async function afterUploadHandle (ctx) { + const autoRename = ctx.getConfig('settings.autoRename'); + if (autoRename) { + ctx.emit('notification', { + title: '❌ 警告', + body: '请关闭 PicGo 的 ​``【oaicite:0】``​ 功能,\nsuper-prefix 插件重命名方式会被覆盖', + }); + await sleep(10000); + throw new Error('super-prefix conflict'); + } + + let userConfig = ctx.getConfig('picgo-plugin-super-prefix'); + if (!userConfig) { + userConfig = { + prefix: '', + fileFormat: '', + }; + } + + // prefix + fileformat + extname + for (let i = 0; i < ctx.output.length; i++) { + let url = ctx.output[i].imgUrl; + let fileName = ''; + if (userConfig.prefixFormat !== undefined && userConfig.prefixFormat !== '') { + fileName = dayjs().format(userConfig.prefixFormat); + } + + if (userConfig.fileFormat !== undefined && userConfig.fileFormat !== '') { + if (i > 0) { + fileName = fileName + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname; + } else { + fileName = fileName + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname; + } + } + + let tmpArray = url.split("/"); + if (tmpArray.length > 0) { + tmpArray[tmpArray.length - 1] = fileName; + } + url = tmpArray.join("/"); + + ctx.output[i].imgUrl = url; + } +} + const pluginConfig = ctx => { let userConfig = ctx.getConfig('picgo-plugin-super-prefix') if (!userConfig) { @@ -30,49 +115,14 @@ const pluginConfig = ctx => { ] } -export = (ctx: picgo) => { - const register = () => { - ctx.helper.beforeUploadPlugins.register('super-prefix', { - async handle (ctx) { - // console.log(ctx) - const autoRename = ctx.getConfig('settings.autoRename') - if (autoRename) { - ctx.emit('notification', { - title: '❌ 警告', - body: '请关闭 PicGo 的 【时间戳重命名】 功能,\nsuper-prefix 插件重命名方式会被覆盖' - }) - await sleep(10000) - throw new Error('super-prefix conflict') - } - - let userConfig = ctx.getConfig('picgo-plugin-super-prefix') - if (!userConfig) { - userConfig = { - prefix: '', - fileFormat: '' - } - } - - for (let i = 0; i < ctx.output.length; i++) { - let fileName = ctx.output[i].fileName - let prefix = '' - if (userConfig.prefixFormat != undefined && userConfig.prefixFormat != '') { - prefix = dayjs().format(userConfig.prefixFormat) - } - - if (userConfig.fileFormat != undefined && userConfig.fileFormat != '') { - if (i > 0) { - fileName = prefix + dayjs().format(userConfig.fileFormat) + '-' + i + ctx.output[i].extname - } else { - fileName = prefix + dayjs().format(userConfig.fileFormat) + ctx.output[i].extname - } - }else{ - fileName = prefix + fileName - } - - ctx.output[i].fileName = fileName - } - }, +export = (ctx: PicGo) => { + const register = (): void => { + ctx.helper.beforeTransformPlugins.register('super-prefix', { + handle: beforeTransformHandle, + config: pluginConfig + }) + ctx.helper.afterUploadPlugins.register('super-prefix', { + handle: afterUploadHandle, config: pluginConfig }) }