Skip to content

Commit

Permalink
fix: 修正中间件 修正npx init
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Aug 13, 2024
1 parent d26da74 commit e6395f7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 35 deletions.
81 changes: 53 additions & 28 deletions src/cli/karin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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}`)
Expand All @@ -271,14 +256,54 @@ 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 - 命令
*/
exec (cmd: string): Promise<string> {
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)
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/init/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 】后再安装依赖~')
Expand Down
18 changes: 14 additions & 4 deletions src/core/karin/karin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import {

type FncFunction = (e: KarinMessage) => Promise<boolean>
type FncElement = string | KarinElement | Array<KarinElement>
type UseReceive = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>
type UseReply = (e: KarinMessageType, element: KarinElement[], next: Function, exit: Function) => Promise<void>
type UseRecord = (uid: string, contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise<void>
type MiddlewareFn<T extends MiddlewareType> = T extends `${MiddlewareType.ReceiveMsg}`
? UseReceive
: T extends `${MiddlewareType.ReplyMsg}` ? UseReply : T extends `${MiddlewareType.SendMsg}` ? UseRecord : never

/**
* 中间件类型
Expand Down Expand Up @@ -323,15 +329,19 @@ export class Karin {
}
}

use (type: `${MiddlewareType.ReceiveMsg}`, fn: (e: KarinMessageType, next: Function) => Promise<void>, options: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.ReplyMsg}`, fn: (e: KarinMessageType, element: KarinElement[], next: Function) => Promise<void>, options: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.SendMsg}`, fn: (uid: string, contact: Contact, elements: KarinElement[]) => Promise<void>, options: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.ReceiveMsg}`, fn: UseReceive, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.ReplyMsg}`, fn: UseReply, options?: Omit<Options, 'log'>): UseInfo
use (type: `${MiddlewareType.SendMsg}`, fn: UseRecord, options?: Omit<Options, 'log'>): UseInfo
/**
* 中间件
* @param type 中间件类型
* @param fn 中间件函数
*/
use (type: `${MiddlewareType}`, fn: Function, options: Options): UseInfo {
use<T extends MiddlewareType> (
type: `${T}`,
fn: MiddlewareFn<T>,
options?: Omit<Options, 'log'>
): UseInfo {
return {
fn,
key: type,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class Common {
*/
async getNpmPlugins<T extends boolean> (showDetails: T): Promise<T extends true ? NpmInfo[] : string[]> {
/** 屏蔽的依赖包列表 */
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))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/core/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down

0 comments on commit e6395f7

Please sign in to comment.