From e1397a13fa2dc762f0c7f16849dabf1a363f8972 Mon Sep 17 00:00:00 2001 From: CakmLexi Date: Wed, 26 Jun 2024 16:09:22 +0800 Subject: [PATCH] feat: npm --- .github/workflows/release-please.yml | 39 ++++ .gitignore | 1 + package.json | 42 +++- src/index.ts | 1 + src/types/index.ts | 8 + src/types/onebots11.ts | 22 +- src/utils/app.js | 197 ------------------ .../{updateVersion.js => updateVersion.ts} | 27 ++- 8 files changed, 115 insertions(+), 222 deletions(-) create mode 100644 .github/workflows/release-please.yml create mode 100644 src/types/index.ts delete mode 100644 src/utils/app.js rename src/utils/{updateVersion.js => updateVersion.ts} (87%) diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml new file mode 100644 index 0000000..f14d898 --- /dev/null +++ b/.github/workflows/release-please.yml @@ -0,0 +1,39 @@ +name: Release and Publish +on: + push: + branches: + - dev +permissions: + contents: write + pull-requests: write +jobs: + release-please: + runs-on: ubuntu-latest + steps: + - uses: google-github-actions/release-please-action@v3 + id: release + with: + release-type: node + package-name: node-karin + # Checkout + - uses: actions/checkout@v4 + if: ${{ steps.release.outputs.release_created }} + # Setup node + - uses: actions/setup-node@v3 + with: + node-version: 16 + registry-url: 'https://registry.npmjs.org' + if: ${{ steps.release.outputs.release_created }} + # Install dependencies + - run: npm install + env: + NODE_AUTH_TOKEN: ${{secrets.DOCS_TOKEN}} + if: ${{ steps.release.outputs.release_created }} + # Build output + - run: npm run build + if: ${{ steps.release.outputs.release_created }} + # Publish to npm + - run: npm run pub + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} + if: ${{ steps.release.outputs.release_created }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 32e73e6..ab50db3 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,6 @@ lib/adapter/kritor/kritor lib/adapter/kritor/kritorGitee # 编译后的文件 +lib/ dist/ build/ \ No newline at end of file diff --git a/package.json b/package.json index c7e9b8b..cd0766e 100644 --- a/package.json +++ b/package.json @@ -1,34 +1,54 @@ { - "name": "karin", + "name": "node-karin", "version": "0.0.3", - "private": true, + "private": false, + "homepage": "https://github.com/KarinJS/Karin#readme", + "bugs": { + "url": "https://github.com/KarinJS/Karin/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/KarinJS/Karin.git" + }, + "license": "GPL-3.0-only", "author": "Karin", "type": "module", "imports": { "#Karin": "./lib/index.js" }, "main": "./lib/index.js", + "files": [ + "/lib/**/*.js", + "/lib/**/*.d.ts", + "/config/defSet/*.yaml", + "/config/view/*.yaml", + "LICENSE", + "package.json", + "README.md" + ], "workspaces": [ "plugins/**" ], "scripts": { "app": "node .", - "fix": "npx eslint lib/**/*.js --fix", - "delete": "pm2 delete ./config/config/pm2.yaml", - "js": "node .", - "js:dev": "node . --dev", - "ts": "tsx ./src/index.ts --dev", - "ts:dev": "tsx ./src/index.ts --dev", "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && npm run fix", + "delete": "pm2 delete ./config/config/pm2.yaml", + "fix": "eslint lib/**/*.js --fix", + "fix:all": "eslint lib/**/*.js --fix && eslint src/**/*.ts --fix", "init": "node lib/tools/install.js", "init:dev": "node lib/tools/install.js", "init:pack": "node lib/tools/install.js", + "js": "node .", + "js:dev": "node . --dev", "log": "node lib/tools/pm2Log.js", "monit": "pm2 monit", + "pub": "npm publish --access public", "restart": "pm2 restart ./config/config/pm2.yaml", "sort:pack": "npx sort-package-json", "start": "pm2 start ./config/config/pm2.yaml && pm2 monit", "stop": "pm2 delete ./config/config/pm2.yaml", + "ts": "tsx ./src/index.ts --dev", + "ts:dev": "tsx ./src/index.ts --dev", "uninstall": "node lib/tools/uninstall.js", "ver": "node lib/tools/updateVersion.js" }, @@ -65,5 +85,9 @@ }, "engines": { "node": ">=18" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 2f9de8e..d24dec0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,6 +32,7 @@ import { RedisClientType } from 'redis' // proto import { kritor } from 'kritor-proto' +export * as types from './types/index' // 初始化 server.init() diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..d3a3fd4 --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,8 @@ +export * from './adapter' +export * from './config' +export * from './logger' +export * from './onebots11' +export * from './plugin' +export * from './render' +export * from './reply' +export * from './types' diff --git a/src/types/onebots11.ts b/src/types/onebots11.ts index 7668d79..87d6536 100644 --- a/src/types/onebots11.ts +++ b/src/types/onebots11.ts @@ -1,23 +1,23 @@ /** * - OneBot 11 标准事件 */ -export type PostType = 'message' | 'notice' | 'request' | 'meta_event' | 'message_sent' +export type EventPostType = 'message' | 'notice' | 'request' | 'meta_event' | 'message_sent' /** * - 消息事件类型 */ -export type MessageType = 'private' | 'group' +export type EventMessageType = 'private' | 'group' /** * - 消息子类型 */ -export type MessageSubType = 'friend' | 'group' | 'other' | 'normal' | 'anonymous' | 'notice' +export type EventMessageSubType = 'friend' | 'group' | 'other' | 'normal' | 'anonymous' | 'notice' /** * - 通知事件类型 */ -export type NoticeType = 'group_upload' | 'group_admin' | 'group_decrease' | 'group_increase' | 'group_ban' | 'friend_add' | 'group_recall' | 'friend_recall' | 'notify' | 'group_msg_emoji_like' +export type EventNoticeType = 'group_upload' | 'group_admin' | 'group_decrease' | 'group_increase' | 'group_ban' | 'friend_add' | 'group_recall' | 'friend_recall' | 'notify' | 'group_msg_emoji_like' /** * - 请求类型 */ -export type RequestType = 'friend' | 'group' +export type EventRequestType = 'friend' | 'group' /** * - 消息事件映射 @@ -30,7 +30,7 @@ export interface MessageToSubType { /** * - 消息子类型映射 */ -export type MessageTypeToSubEvent = E extends keyof MessageToSubType ? MessageToSubType[E] : never +export type MessageTypeToSubEvent = E extends keyof MessageToSubType ? MessageToSubType[E] : never /** * - 事件基类 @@ -43,7 +43,7 @@ export interface OneBot11 { /** * - 事件类型 */ - post_type: PostType + post_type: EventPostType /** * - 收到事件的机器人 QQ 号 */ @@ -61,7 +61,7 @@ export interface OneBot11Notice extends OneBot11 { /** * - 通知类型 */ - notice_type: NoticeType + notice_type: EventNoticeType } /** @@ -755,11 +755,11 @@ export interface OneBot11Message extends OneBot11 { /** * - 消息类型 */ - message_type: MessageType + message_type: EventMessageType /** * - 消息子类型 */ - sub_type: MessageTypeToSubEvent + sub_type: MessageTypeToSubEvent /** * - 消息 ID */ @@ -897,7 +897,7 @@ export type OneBot11Event = OneBot11GroupMessage | OneBot11PrivateMessage | OneB /** * - 传入 post_type 返回对应的事件类型 */ -export type ByPostType = Extract +export type ByPostType = Extract /** * - OneBot11公开Api diff --git a/src/utils/app.js b/src/utils/app.js deleted file mode 100644 index 3806bc7..0000000 --- a/src/utils/app.js +++ /dev/null @@ -1,197 +0,0 @@ -import lodash from 'lodash' -import plugin from '../core/plugin' -import { PluginRule, PluginTask } from '../types/plugin' -import { Event, SubEvent } from '../types/types' - -/** - * 插件构建器 - */ -export class Karin { - /** - * - 开始上下文 - */ -} - -export default class App { - name: string - dsc: string - event: Event | `${Event}.${SubEvent}` - priority: number - rule: Array - task: Array - constructor({ name = '插件名称', dsc = '', event = 'message', priority = 5000, task = [], rule = [] }) { - this.name = name - this.dsc = dsc || name - this.event = event as Event | `${Event}.${SubEvent}` - this.priority = priority - this.rule = rule - this.task = task - } - - /** - * karin插件构建器 - */ - static init(params: { - /** - * - 插件名称 - */ - name: string - /** - * - 插件描述 - */ - dsc?: string - /** - * - 监听事件 - */ - event?: Event | `${Event}.${SubEvent}` - /** - * - 插件优先级 - */ - priority?: number - /** - * - 插件任务 - */ - task?: Array - /** - * - 插件规则 - */ - rule?: Array - }) { - return new App(params) - } - - /** - * 注册插件规则 - * @param {Object} rule - 插件规则对象 - * @param {string|Function} rule.fnc - 插件函数名或函数 - * @param {string} [rule.reg=''] - 插件规则 - * @throws {Error} 如果缺少fnc或fnc类型错误 - * @throws {Error} 如果指定的方法不存在 - */ - reg(rule) { - /** 判断是否传入reg和fnc */ - if (!rule.fnc) throw new Error('[插件构建] 缺少fnc') - /** 如果fnc是字符串,则在传入的对象中查找对应的方法 */ - if (typeof rule.fnc === 'string') { - if (!rule[rule.fnc]) throw new Error(`[插件构建] ${rule.fnc} 方法不存在`) - this[rule.fnc] = rule[rule.fnc] - /** 删除掉这个函数 */ - delete rule[rule.fnc] - } else if (typeof rule.fnc === 'function') { - /** 随机生成一个方法名 */ - const fnc_name = `fnc_${Math.random().toString(36)}` - const fnc = rule.fnc - this[fnc_name] = fnc - rule.fnc = fnc_name - delete rule[fnc_name] - } - - /** 将函数注册到当前对象中 */ - lodash.forIn(rule, (value, key) => { - if (typeof value === 'function') this[key] = value - }) - - this.rule.push({ - fnc: rule.fnc, - reg: rule.reg || '', - log: rule.log || true, - permission: rule.permission || 'all', - }) - } - - /** - * 将函数或函数对象注册到当前对象中 - * @param {string} name - 函数名 - * @param {Function|Object} fnc - 要注册的函数或函数对象 - */ - fnc(name, fnc) { - if (!name) throw new Error('[插件构建] 缺少name') - if (typeof fnc !== 'function') throw new Error('[插件构建] fnc类型错误') - this[name] = fnc - } - - /** - * 设置插件接收函数 - * @param {Function} fnc - 接收函数 - */ - accept(fnc) { - if (typeof fnc === 'function') { - this.accept = fnc - } - } - - /** - * 新增插件定时任务 - * @param {{ - * name: string, - * cron: string, - * fnc: string|Function, - * log?: boolean, - * }} task - 插件定时任务对象 - * @param {string} task.name - 定时任务名称 - * @param {string} task.cron - 定时任务表达式 - * @param {string|Function} task.fnc - 定时任务函数名或函数 - * @param {boolean} [task.log=true] - 是否记录日志 - */ - cron(task) { - /** 检查传入的参数是否符合规则 */ - if (!task.name) throw new Error('[插件构建][定时任务] 缺少name') - if (!task.cron) throw new Error('[插件构建][定时任务] 缺少cron') - if (!task.fnc) throw new Error('[插件构建][定时任务] 缺少fnc') - if (typeof task.fnc !== 'function') throw new Error('[插件构建][定时任务] fnc类型错误,必须为function') - - /** 如果fnc是字符串,则在传入的对象中查找对应的方法 */ - if (typeof task.fnc === 'string') { - this[task.fnc] = task[task.fnc] - } else if (typeof task.fnc === 'function') { - /** 随机生成一个方法名 */ - const fnc_name = `fnc_${Math.random().toString(36)}` - const fnc = task.fnc - this[fnc_name] = fnc - task.fnc = fnc_name - } - - /** 将函数注册到当前对象中 */ - lodash.forIn(task, (value, key) => { - if (typeof value === 'function') this[key] = value - }) - - this.task.push({ - name: task.name, - cron: task.cron, - fnc: task.fnc, - log: task.log || true, - }) - } - - /** - * 创建插件类 - * @param {Object} app - 插件配置对象 - * @param {string} app.name - 插件名称 - * @param {string} app.dsc - 插件描述 - * @param {string} app.event - 插件事件 - * @param {number} app.priority - 插件优先级 - * @param {string} app.task - 插件任务 - * @param {Object} app.rule - 插件规则对象 - * @returns {Class} - 返回创建的插件类 - */ - plugin(app) { - const cla = class extends plugin { - constructor() { - super({ - name: app.name, - dsc: app.dsc, - event: app.event, - priority: app.priority, - task: app.task, - rule: app.rule, - }) - } - } - // 循环app中的所有函数,构建到cla的原型中 - lodash.forIn(app, (value, key) => { - if (typeof value === 'function') cla.prototype[key] = value - }) - return cla - } -} diff --git a/src/utils/updateVersion.js b/src/utils/updateVersion.ts similarity index 87% rename from src/utils/updateVersion.js rename to src/utils/updateVersion.ts index cbd608a..0fc0686 100644 --- a/src/utils/updateVersion.js +++ b/src/utils/updateVersion.ts @@ -2,7 +2,24 @@ import fs from 'fs' import { exec } from 'child_process' export default class Version { - constructor (path) { + path: string + packageDir: string + CHANGELOGDir: string + packageJson: any + version: string + commitsMap: { + [key: string]: string[] + } + + HEAD: string + cmd: string + stdout: string | string[] + url: string + constructor (path: string) { + this.cmd = '' + this.stdout = '' + this.url = '' + this.HEAD = '' this.path = path this.packageDir = this.path + '/packageon' this.CHANGELOGDir = this.path + '/CHANGELOG.md' @@ -19,7 +36,7 @@ export default class Version { perf: ['### 性能优化'], chore: ['### 构建工具相关'], revert: ['### 回滚'], - others: ['### 其他提交'] + others: ['### 其他提交'], } } @@ -32,7 +49,7 @@ export default class Version { const HEAD = this.HEAD ? `${this.HEAD}..HEAD ` : '' this.cmd = `git log ${HEAD}--pretty=format:"HEAD: %H=分割=sha: %h=分割=log: %s"` - this.stdout = await this.exce(this.cmd) + this.stdout = (await this.exce(this.cmd)) if (!this.stdout) throw new Error('commit为空...') this.stdout = this.stdout.trim().split('\n') @@ -109,7 +126,7 @@ export default class Version { */ async updateVersion () { /** 10进1 */ - const versionArr = this.version.split('.').map(item => parseInt(item)) + const versionArr = this.version.split('.').map((item: string) => parseInt(item)) if (versionArr[2] === 9) { versionArr[1]++ versionArr[2] = 0 @@ -132,7 +149,7 @@ export default class Version { * 封装exce方法 * @param {string} cmd 执行的命令 */ - exce (cmd) { + exce (cmd: string): Promise { return new Promise((resolve, reject) => { exec(cmd, { cwd: this.path }, (error, stdout) => { if (error) {