From e6395f75559c3a98d685b81b589f957542562676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E7=91=BE?= Date: Tue, 13 Aug 2024 21:19:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=20=E4=BF=AE=E6=AD=A3`npx=20init`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/karin.ts | 81 +++++++++++++++++++++++++------------- src/core/init/config.ts | 2 +- src/core/karin/karin.ts | 18 +++++++-- src/utils/common/common.ts | 2 +- src/utils/core/handler.ts | 2 +- 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/cli/karin.ts b/src/cli/karin.ts index f469b586..dd217cdd 100644 --- a/src/cli/karin.ts +++ b/src/cli/karin.ts @@ -209,14 +209,12 @@ class KarinCli { async update () { /** 屏蔽的依赖包列表 */ const pkgdependencies = [ - '@grpc/grpc-js', - '@grpc/proto-loader', 'art-template', 'axios', 'chalk', 'chokidar', + 'commander', 'express', - 'kritor-proto', 'level', 'lodash', 'log4js', @@ -227,40 +225,27 @@ class KarinCli { 'yaml', ] - let cmd = '' const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key)) /** 获取包管理器 */ const pkg = new KarinCfgInit().getRegistry() - switch (pkg) { - case 'pnpm': { - cmd = 'pnpm update' - break - } - case 'yarn': { - cmd = 'yarn upgrade' - break - } - case 'npm': { - cmd = 'npm update' - break - } - case 'cnpm': { - cmd = 'cnpm update' - break - } - } + const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update` /** 异步并发更新依赖 */ await Promise.all(list.map(async item => { try { - const res = await this.exec(`${cmd} ${item}@latest`) - /** 已经是最新 */ - if (res.includes('is up to date')) { + /** 检查是否已经是最新版本 */ + const local = await this.getLocalVersion(item, pkg) + const remote = await this.getRemoteVersion(item, pkg) + if (local === remote) { console.log(`[依赖更新] ${item} 已经是最新~`) - } else { - console.log(`[依赖更新] ${item} 更新完成~`) + return } + + console.log(`[依赖更新] ${item} 当前版本: ${local} 最新版本: ${remote}`) + + await this.exec(`${cmd} ${item}@latest`) + console.log(`[依赖更新] ${item} 更新完成~`) } catch (error: any) { console.error(`[依赖更新] ${item} 更新失败:`) console.error(`error.stack: ${error.stack}`) @@ -271,6 +256,46 @@ class KarinCli { console.log('所有依赖已更新完成~') } + /** + * 获取指定包的本地版本 + * @param name - 包名 + * @param pkg - 包管理器 + * @returns - 版本号 + */ + async getLocalVersion (name: string, pkg: 'pnpm' | 'cnpm' | 'yarn' | 'npm') { + const cmd = pkg === 'yarn' ? `yarn list --pattern ${name}` : `${pkg} list ${name} --depth=0` + const text = await this.exec(cmd) + + /** pnpm特殊处理 */ + if (pkg === 'pnpm') { + const reg = new RegExp(`${name}\\s+([\\d.]+)`, 'gm') + const res = reg.exec(text) + return res?.[1] || '0.0.0' + } + + const reg = new RegExp(`${name}@(\\d+\\.\\d+\\.\\d+)`, 'gm') + const res = reg.exec(text) + return res?.[1] || '0.0.0' + } + + /** + * 获取指定包的最新版本 + * @param name - 包名 + * @param pkg - 包管理器 + */ + async getRemoteVersion (name: string, pkg: 'pnpm' | 'cnpm' | 'yarn' | 'npm') { + const cmd = `${pkg} info ${name} version` + const text = await this.exec(cmd) + /** yarn特殊处理 */ + if (pkg === 'yarn') { + const lines = text.split('\n').map(line => line.trim()) + const ver = lines.find(line => /^\d+\.\d+\.\d+$/.test(line)) + return ver || '' + } + + return text.trim() + } + /** * 封装exec * @param cmd - 命令 @@ -278,7 +303,7 @@ class KarinCli { exec (cmd: string): Promise { return new Promise((resolve, reject) => { execCmd(cmd, (error, stdout, stderr) => { - if (stdout) return resolve(stdout) + if (stdout) return resolve(stdout.trim()) if (error) return reject(error) return reject(stderr) }) diff --git a/src/core/init/config.ts b/src/core/init/config.ts index d3288864..faa213fc 100644 --- a/src/core/init/config.ts +++ b/src/core/init/config.ts @@ -166,7 +166,7 @@ export class KarinCfgInit { } /** 安装依赖 */ - if (!(await this.shell(`${type} -P --force`))) { + if (!(await this.shell(`${type} install -P --force`))) { console.log('安装依赖失败,请手动安装依赖!') console.log(`可尝试手动执行 【 ${type} install -P 】 安装依赖~`) console.log('如中国大陆用户安装失败,请尝试执行换源 【 npm config set registry https://registry.npmmirror.com 】后再安装依赖~') diff --git a/src/core/karin/karin.ts b/src/core/karin/karin.ts index bb26f57b..d8d7b89c 100644 --- a/src/core/karin/karin.ts +++ b/src/core/karin/karin.ts @@ -27,6 +27,12 @@ import { type FncFunction = (e: KarinMessage) => Promise type FncElement = string | KarinElement | Array +type UseReceive = (e: KarinMessageType, next: Function, exit: Function) => Promise +type UseReply = (e: KarinMessageType, element: KarinElement[], next: Function, exit: Function) => Promise +type UseRecord = (uid: string, contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise +type MiddlewareFn = T extends `${MiddlewareType.ReceiveMsg}` + ? UseReceive + : T extends `${MiddlewareType.ReplyMsg}` ? UseReply : T extends `${MiddlewareType.SendMsg}` ? UseRecord : never /** * 中间件类型 @@ -323,15 +329,19 @@ export class Karin { } } - use (type: `${MiddlewareType.ReceiveMsg}`, fn: (e: KarinMessageType, next: Function) => Promise, options: Omit): UseInfo - use (type: `${MiddlewareType.ReplyMsg}`, fn: (e: KarinMessageType, element: KarinElement[], next: Function) => Promise, options: Omit): UseInfo - use (type: `${MiddlewareType.SendMsg}`, fn: (uid: string, contact: Contact, elements: KarinElement[]) => Promise, options: Omit): UseInfo + use (type: `${MiddlewareType.ReceiveMsg}`, fn: UseReceive, options?: Omit): UseInfo + use (type: `${MiddlewareType.ReplyMsg}`, fn: UseReply, options?: Omit): UseInfo + use (type: `${MiddlewareType.SendMsg}`, fn: UseRecord, options?: Omit): UseInfo /** * 中间件 * @param type 中间件类型 * @param fn 中间件函数 */ - use (type: `${MiddlewareType}`, fn: Function, options: Options): UseInfo { + use ( + type: `${T}`, + fn: MiddlewareFn, + options?: Omit + ): UseInfo { return { fn, key: type, diff --git a/src/utils/common/common.ts b/src/utils/common/common.ts index e928656a..46ccdfb7 100644 --- a/src/utils/common/common.ts +++ b/src/utils/common/common.ts @@ -423,7 +423,7 @@ class Common { */ async getNpmPlugins (showDetails: T): Promise { /** 屏蔽的依赖包列表 */ - const exclude = ['art-template', 'axios', 'chalk', 'chokidar', 'express', 'level', 'lodash', 'log4js', 'moment', 'node-karin', 'node-schedule', 'redis', 'ws', 'yaml'] + const exclude = ['art-template', 'axios', 'chalk', 'chokidar', 'commander', 'express', 'level', 'lodash', 'log4js', 'moment', 'node-karin', 'node-schedule', 'redis', 'ws', 'yaml'] const pkg = this.readJson('./package.json') const dependencies = Object.keys(pkg.dependencies).filter((name) => !exclude.includes(name)) diff --git a/src/utils/core/handler.ts b/src/utils/core/handler.ts index 656f7eb3..96d748a3 100644 --- a/src/utils/core/handler.ts +++ b/src/utils/core/handler.ts @@ -13,7 +13,7 @@ export const handler = new (class EventHandler { async call (key: string, args: { [key: string]: any, e?: any }) { let res for (const info of loader.handler[key] || []) { - const plugin = loader.plugin[info.key] + const plugin = loader.plugin.get(info.key)! try { let done = true /**