From 35e15b06480c08fab056b8e411940b7bf513e1f0 Mon Sep 17 00:00:00 2001 From: PiEgg Date: Sun, 31 Jan 2021 20:46:21 +0800 Subject: [PATCH] :sparkles: Feature: supporting install specific version of plugin --- src/utils/common.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/utils/common.ts b/src/utils/common.ts index f4bdee8..5c57c99 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -237,6 +237,7 @@ export const getProcessPluginName = (nameOrPath: string, logger: ILogger | Conso * 4. /absolutePath/.../picgo-plugin-xxx -> picgo-plugin-xxx * 5. an exception: [package.json's name] !== [folder name] * then use [package.json's name], usually match the scope package. + * 6. if plugin name has version: picgo-plugin-xxx@x.x.x then remove the version * @param nameOrPath */ export const getNormalPluginName = (nameOrPath: string, logger: ILogger | Console = console): string => { @@ -244,9 +245,9 @@ export const getNormalPluginName = (nameOrPath: string, logger: ILogger | Consol switch (pluginNameType) { case 'normal': case 'scope': - return nameOrPath + return removePluginVersion(nameOrPath) case 'simple': - return handleCompletePluginName(nameOrPath) + return removePluginVersion(handleCompletePluginName(nameOrPath)) default: { // now, the nameOrPath must be path // the nameOrPath here will be ensured with unix style @@ -282,3 +283,22 @@ export const handleUnixStylePath = (pathStr: string): string => { const pathArr = pathStr.split(path.sep) return pathArr.join('/') } + +/** + * remove plugin version when register plugin name + * 1. picgo-plugin-xxx@1.0.0 -> picgo-plugin-xxx + * @param nameOrPath + */ +export const removePluginVersion = (nameOrPath: string): string => { + if (!nameOrPath.includes('@')) { + return nameOrPath + } else { + const matchArr = nameOrPath.match(/(.+\/)?(picgo-plugin-\w+)(@.+)*/) + if (!matchArr) { + console.warn('can not remove plugin version') + return nameOrPath + } else { + return matchArr[2] + } + } +}