From a00ad8c9d85793583562db6fc8ceb2a8c16251f6 Mon Sep 17 00:00:00 2001 From: YunYouJun Date: Mon, 23 Sep 2024 03:51:25 +0800 Subject: [PATCH] feat; add sigint isTTY judge for nohup --- examples/simple/package.json | 2 +- examples/simple/src/index.ts | 16 ++++----- examples/simple/tsconfig.json | 6 ++-- examples/simple/tsup.config.ts | 3 +- packages/cli/src/install/repo.ts | 2 +- packages/create-app/src/index.ts | 2 +- packages/el-bot/bin/el-bot.ts | 2 +- packages/el-bot/bump.config.ts | 8 +++++ packages/el-bot/core/bot/index.ts | 40 +++++++++++++++++++---- packages/el-bot/core/bot/logger/index.ts | 1 + packages/el-bot/core/bot/plugins/class.ts | 2 +- packages/el-bot/core/config/el.ts | 38 +-------------------- packages/el-bot/core/config/index.ts | 2 +- packages/el-bot/core/node/utils.ts | 2 +- packages/el-bot/core/utils/config.ts | 2 +- packages/el-bot/node/cli/commands/dev.ts | 11 +++---- packages/el-bot/package.json | 15 ++++----- packages/el-bot/plugins/qrcode/index.ts | 17 +++++----- packages/el-bot/plugins/rss/index.ts | 2 +- packages/el-bot/plugins/workflow/index.ts | 2 +- packages/el-bot/scripts/plugins.ts | 2 +- packages/el-bot/scripts/utils.ts | 2 +- pnpm-lock.yaml | 18 ---------- scripts/fix-pnpm.ts | 2 +- 24 files changed, 84 insertions(+), 115 deletions(-) create mode 100644 packages/el-bot/bump.config.ts diff --git a/examples/simple/package.json b/examples/simple/package.json index 7b9c814..bcaa49e 100644 --- a/examples/simple/package.json +++ b/examples/simple/package.json @@ -7,7 +7,7 @@ "dev": "el-bot", "dev:w": "vite-node -w ../../packages/el-bot/bin/el-bot.ts", "build": "tsup", - "start": "tsx src/index.ts", + "start": "vite-node src/index.ts", "start:prod": "nodemon dist/index.js" }, "devDependencies": { diff --git a/examples/simple/src/index.ts b/examples/simple/src/index.ts index c61be96..1bf1c77 100644 --- a/examples/simple/src/index.ts +++ b/examples/simple/src/index.ts @@ -1,12 +1,8 @@ -// import process from 'node:process' -// import { createBot } from 'el-bot' +import { createBot } from 'el-bot' -// import 'dotenv/config' +export async function main() { + const bot = await createBot() + await bot.start() +} -// export async function main() { -// const bot = createBot({ -// napcat: {}, -// }) -// } - -// main() +main() diff --git a/examples/simple/tsconfig.json b/examples/simple/tsconfig.json index 99fb7a9..9cd06b5 100644 --- a/examples/simple/tsconfig.json +++ b/examples/simple/tsconfig.json @@ -1,13 +1,11 @@ { - "extends": "../../tsconfig.json", "compilerOptions": { "target": "ESNext", "lib": ["ESNext"], + "emitDecoratorMetadata": false, + "rootDir": ".", "module": "ESNext", "moduleResolution": "Bundler", - "paths": { - "el-bot": ["../../packages/el-bot/src/index.ts"] - }, "resolveJsonModule": true, "strict": true, "strictNullChecks": true, diff --git a/examples/simple/tsup.config.ts b/examples/simple/tsup.config.ts index 7e68cfb..85a693e 100644 --- a/examples/simple/tsup.config.ts +++ b/examples/simple/tsup.config.ts @@ -3,6 +3,7 @@ import { defineConfig } from 'tsup' export default defineConfig({ entry: ['src/index.ts'], clean: true, - dts: true, + // dts: true, format: ['esm'], + shims: false, }) diff --git a/packages/cli/src/install/repo.ts b/packages/cli/src/install/repo.ts index 17ad826..e151b9f 100644 --- a/packages/cli/src/install/repo.ts +++ b/packages/cli/src/install/repo.ts @@ -1,6 +1,6 @@ -import fs from 'node:fs' import axios from 'axios' import download from 'download' +import fs from 'fs-extra' import { createLogger } from 'packages/el-bot' import ProgressBar from 'progress' diff --git a/packages/create-app/src/index.ts b/packages/create-app/src/index.ts index 46983cf..55fb5af 100755 --- a/packages/create-app/src/index.ts +++ b/packages/create-app/src/index.ts @@ -4,10 +4,10 @@ /* eslint-env node */ import { exec } from 'node:child_process' -import fs from 'node:fs' import path from 'node:path' import process from 'node:process' import { prompt } from 'enquirer' +import fs from 'fs-extra' import c from 'picocolors' const argv = process.argv.slice(2) diff --git a/packages/el-bot/bin/el-bot.ts b/packages/el-bot/bin/el-bot.ts index a7bf883..6e38729 100755 --- a/packages/el-bot/bin/el-bot.ts +++ b/packages/el-bot/bin/el-bot.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env vite-node --watch --script +#!/usr/bin/env vite-node --script import { run } from '../node' diff --git a/packages/el-bot/bump.config.ts b/packages/el-bot/bump.config.ts new file mode 100644 index 0000000..f377ed3 --- /dev/null +++ b/packages/el-bot/bump.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'bumpp' + +export default defineConfig({ + all: false, + commit: false, + tag: false, + push: false, +}) diff --git a/packages/el-bot/core/bot/index.ts b/packages/el-bot/core/bot/index.ts index 35ef7b5..0f703be 100644 --- a/packages/el-bot/core/bot/index.ts +++ b/packages/el-bot/core/bot/index.ts @@ -1,9 +1,9 @@ import type mongoose from 'mongoose' import type { Server } from 'node:net' import type { ElConfig, ElUserConfig } from '../config/el' -import fs from 'node:fs' -import { resolve } from 'node:path' +import path, { resolve } from 'node:path' import process from 'node:process' +import fs from 'fs-extra' import { GroupMessage, NCWebsocket, PrivateMessage, Send, Structs } from 'node-napcat-ts' import colors from 'picocolors' import { resolveElConfig } from '../config/el' @@ -35,10 +35,30 @@ export * from './plugins' /** * 创建机器人 - * @param el + * @param el 用户配置 | el-bot.config.ts 所在目录 */ -export function createBot(el: ElUserConfig) { - return new Bot(el) +export async function createBot(el: ElUserConfig | string = process.cwd()) { + let elConfig: ElUserConfig + + // resolve el-bot.config.ts + if (typeof el === 'string') { + const rootDir = el + const elConfigFile = path.resolve(rootDir, 'el-bot.config.ts') + if (!(await fs.exists(elConfigFile))) { + consola.error('el-bot.config.ts not found') + consola.error('Please create `el-bot.config.ts` in the root directory.') + } + const config = (await import(elConfigFile)).default as ElUserConfig + elConfig = config + } + else if (typeof el === 'object') { + elConfig = el + } + else { + throw new TypeError('`createBot` option must be a string or object') + } + + return new Bot(elConfig) } export class Bot { @@ -240,10 +260,16 @@ export class Bot { } } + // 如何解决持久运行 // 意外退出 process.on('SIGINT', () => { - this.stop() - process.exit(0) + /** + * 如果程序在前台运行(即,process.stdin.isTTY 为 true),那么它会在收到 SIGINT 信号时退出。如果程序在后台运行(即,process.stdin.isTTY 为 false),那么它会忽略 SIGINT 信号。 + */ + if (process.stdin.isTTY) { + this.stop() + process.exit(0) + } }) } diff --git a/packages/el-bot/core/bot/logger/index.ts b/packages/el-bot/core/bot/logger/index.ts index de7025a..08b396b 100644 --- a/packages/el-bot/core/bot/logger/index.ts +++ b/packages/el-bot/core/bot/logger/index.ts @@ -1,6 +1,7 @@ // import colors from 'picocolors' import { createLogger } from './winston' +export * from './consola' export * from './winston' const defaultLogger = createLogger() diff --git a/packages/el-bot/core/bot/plugins/class.ts b/packages/el-bot/core/bot/plugins/class.ts index 7de49ad..9fed830 100644 --- a/packages/el-bot/core/bot/plugins/class.ts +++ b/packages/el-bot/core/bot/plugins/class.ts @@ -107,7 +107,7 @@ export class Plugins { } // load plugin package.json - const packagePath = path.resolve(__dirname, pluginPath, `package.json`) + const packagePath = path.resolve(import.meta.dirname, pluginPath, `package.json`) if (await fs.exists(packagePath)) { pkg = await fs.readJson(packagePath) } diff --git a/packages/el-bot/core/config/el.ts b/packages/el-bot/core/config/el.ts index b85e79b..b7f6912 100644 --- a/packages/el-bot/core/config/el.ts +++ b/packages/el-bot/core/config/el.ts @@ -1,15 +1,14 @@ import type { WebhookConfig } from '../bot/webhook' import type { BotConfig, BotUserConfig } from './bot' -import fs from 'node:fs' import { resolve } from 'node:path' import process from 'node:process' +import fs from 'fs-extra' // import type { MiraiApiHttpSetting } from 'mirai-ts' import { NCWebsocketOptions } from 'node-napcat-ts' import { Target } from '../../types' import { mergeConfig } from '../utils/config' -const assetsFolder = 'data/net.mamoe.mirai-api-http' export interface dbConfig { /** * 是否启用 @@ -46,15 +45,6 @@ export interface ElConfig { * 开启调试模式 */ debug?: boolean - /** - * MiraiApiHttp setting.yml 路径 - * 或传入 MiraiApiHttpConfig 对象配置 - */ - // setting: MiraiApiHttpSetting | string - /** - * mirai info - */ - mirai: { folder: string } /** * mongodb 数据库默认配置 */ @@ -87,19 +77,6 @@ export interface ElConfig { * 根目录 */ base: string - /** - * mirai-api-http 文件路径 - */ - path: { - /** - * 图片路径 - */ - image: string - /** - * 语音路径 - */ - voice: string - } } export type ElUserConfig = Partial> @@ -108,13 +85,7 @@ export type ElUserConfig = Partial> * 解析 El Config */ export function resolveElConfig(userConfig: ElUserConfig) { - const cwd = process.cwd() - const miraiConfig = { - folder: 'mcl', - } const defaultElConfig: ElConfig = { - // setting: '../mcl/config/net.mamoe.mirai-api-http/setting.yml', - mirai: miraiConfig, db: { enable: false, }, @@ -154,18 +125,11 @@ export function resolveElConfig(userConfig: ElUserConfig) { enable: false, }, base: process.cwd(), - path: { - image: resolve(cwd, miraiConfig.folder, `${assetsFolder}/images`), - voice: resolve(cwd, miraiConfig.folder, `${assetsFolder}/voices`), - }, } // 合并 const config = mergeConfig(defaultElConfig, userConfig) as ElConfig - config.pkg = pkg - config.path.image = resolve(config.base, config.mirai.folder, `${assetsFolder}/images`) - config.path.image = resolve(config.base, config.mirai.folder, `${assetsFolder}/voices`) return config } diff --git a/packages/el-bot/core/config/index.ts b/packages/el-bot/core/config/index.ts index 38532fc..29ccef6 100644 --- a/packages/el-bot/core/config/index.ts +++ b/packages/el-bot/core/config/index.ts @@ -1,8 +1,8 @@ import type { BotConfig, BotUserConfig } from './bot' import type { ElUserConfig } from './el' -import fs from 'node:fs' import path from 'node:path' import process from 'node:process' +import fs from 'fs-extra' import { createLogger } from '../bot/logger/winston' export * from './bot' diff --git a/packages/el-bot/core/node/utils.ts b/packages/el-bot/core/node/utils.ts index d3fb75b..3fb28ab 100644 --- a/packages/el-bot/core/node/utils.ts +++ b/packages/el-bot/core/node/utils.ts @@ -1,5 +1,5 @@ -import fs from 'node:fs' import path from 'node:path' +import fs from 'fs-extra' /** * 寻找文件 diff --git a/packages/el-bot/core/utils/config.ts b/packages/el-bot/core/utils/config.ts index 1b3fbe3..8318123 100644 --- a/packages/el-bot/core/utils/config.ts +++ b/packages/el-bot/core/utils/config.ts @@ -1,4 +1,4 @@ -import fs from 'node:fs' +import fs from 'fs-extra' import yaml from 'js-yaml' /** diff --git a/packages/el-bot/node/cli/commands/dev.ts b/packages/el-bot/node/cli/commands/dev.ts index cb99c64..58edd10 100644 --- a/packages/el-bot/node/cli/commands/dev.ts +++ b/packages/el-bot/node/cli/commands/dev.ts @@ -1,9 +1,9 @@ import { exec } from 'node:child_process' -import path from 'node:path' import consola from 'consola' import { Argv } from 'yargs' -import { Bot, createBot, ElUserConfig } from '../../../core' +import { Bot, createBot } from '../../../core' import { commonOptions } from '../options' + import { bindShortcut } from '../utils' let bot: Bot @@ -29,11 +29,8 @@ export function registerDevCommand(cli: Argv) { consola.start('Link Start ...') consola.log('') - // resolve el-bot.config.ts - const elConfigFile = path.resolve(root, 'el-bot.config.ts') - const config = (await import(elConfigFile)).default as ElUserConfig - - bot = createBot(config) + // set root dir + bot = await createBot(root) await bot.start() const SHORTCUTS = [ diff --git a/packages/el-bot/package.json b/packages/el-bot/package.json index 5eedd2b..2718f7b 100644 --- a/packages/el-bot/package.json +++ b/packages/el-bot/package.json @@ -1,7 +1,7 @@ { "name": "el-bot", "type": "module", - "version": "1.0.0-beta.6", + "version": "1.0.0-beta.9", "description": "A quick qq bot framework for mirai.", "author": { "name": "云游君", @@ -27,13 +27,10 @@ ], "exports": { "./*": "./*", - ".": { - "import": "./index.ts", - "require": "./dist/index.js" - } + ".": "./index.ts" }, "main": "./index.ts", - "types": "dist/index.d.ts", + "types": "./index.ts", "bin": { "el": "bin/el-bot.ts", "el-bot": "bin/el-bot.ts" @@ -41,6 +38,7 @@ "files": [ "bin", "core", + "index.ts", "node", "plugins", "types" @@ -53,7 +51,8 @@ }, "scripts": { "build:api": "npx typedoc", - "dev": "npm run watch" + "dev": "npm run watch", + "release": "bumpp && npm publish" }, "dependencies": { "@koa/cors": "^5.0.0", @@ -101,8 +100,6 @@ "@types/yargs": "^17.0.33", "cross-env": "^7.0.3", "dotenv": "^16.4.5", - "esbuild": "^0.23.1", - "esbuild-register": "^3.6.0", "tsup": "^8.3.0", "typedoc": "^0.26.7", "typescript": "^5.6.2" diff --git a/packages/el-bot/plugins/qrcode/index.ts b/packages/el-bot/plugins/qrcode/index.ts index e81fc8e..69e7552 100644 --- a/packages/el-bot/plugins/qrcode/index.ts +++ b/packages/el-bot/plugins/qrcode/index.ts @@ -1,7 +1,5 @@ import type { Bot } from '../../core' import type { QRCodeOptions } from './options' -import fs from 'node:fs' -import { resolve } from 'node:path' import QRCode from 'qrcode' import qrcodeOptions from './options' @@ -17,16 +15,17 @@ export async function generateQR(text: string, folder: string) { return filename } -export default function (ctx: Bot, options: QRCodeOptions = qrcodeOptions) { +export default function (ctx: Bot, _options: QRCodeOptions = qrcodeOptions) { // const { cli } = ctx - const name = 'qrcode' - const folder = resolve(ctx.el.path!.image, name) + // const name = 'qrcode' + // TODO + // const folder = resolve('', name) - if (!fs.existsSync(folder)) - fs.mkdirSync(folder, { recursive: true }) + // if (!fs.existsSync(folder)) + // fs.mkdirSync(folder, { recursive: true }) - if (options.autoClearCache) - fs.rmSync(folder, { recursive: true }) + // if (options.autoClearCache) + // fs.rmSync(folder, { recursive: true }) // cli // .command('qrcode ') diff --git a/packages/el-bot/plugins/rss/index.ts b/packages/el-bot/plugins/rss/index.ts index 485d57c..1e570e1 100644 --- a/packages/el-bot/plugins/rss/index.ts +++ b/packages/el-bot/plugins/rss/index.ts @@ -1,8 +1,8 @@ import type { CustomFields } from 'rss-parser' -import fs from 'node:fs' import consola from 'consola' import dayjs from 'dayjs' import { type Bot, defineBotPlugin } from 'el-bot' +import fs from 'fs-extra' import { htmlToText } from 'html-to-text' import schedule from 'node-schedule' diff --git a/packages/el-bot/plugins/workflow/index.ts b/packages/el-bot/plugins/workflow/index.ts index 179de0b..b9e8ad6 100644 --- a/packages/el-bot/plugins/workflow/index.ts +++ b/packages/el-bot/plugins/workflow/index.ts @@ -1,5 +1,5 @@ import type { Bot } from 'el-bot' -import fs from 'node:fs' +import fs from 'fs-extra' /** * ref github actions diff --git a/packages/el-bot/scripts/plugins.ts b/packages/el-bot/scripts/plugins.ts index dca537b..6596be2 100644 --- a/packages/el-bot/scripts/plugins.ts +++ b/packages/el-bot/scripts/plugins.ts @@ -1,6 +1,6 @@ -import fs from 'node:fs' import path, { dirname } from 'node:path' import { fileURLToPath } from 'node:url' +import fs from 'fs-extra' import { getAllPlugins } from './utils' diff --git a/packages/el-bot/scripts/utils.ts b/packages/el-bot/scripts/utils.ts index 261c187..17cb22a 100644 --- a/packages/el-bot/scripts/utils.ts +++ b/packages/el-bot/scripts/utils.ts @@ -1,6 +1,6 @@ -import fs from 'node:fs' import path, { dirname } from 'node:path' import { fileURLToPath } from 'node:url' +import fs from 'fs-extra' const __dirname = dirname(fileURLToPath(import.meta.url)) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b3c0e5e..cacc3c3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -273,12 +273,6 @@ importers: dotenv: specifier: ^16.4.5 version: 16.4.5 - esbuild: - specifier: ^0.23.1 - version: 0.23.1 - esbuild-register: - specifier: ^3.6.0 - version: 3.6.0(esbuild@0.23.1) tsup: specifier: ^8.3.0 version: 8.3.0(jiti@1.21.6)(postcss@8.4.47)(tsx@4.19.1)(typescript@5.6.2)(yaml@2.5.1) @@ -2325,11 +2319,6 @@ packages: resolution: {integrity: sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==} engines: {node: '>= 0.4'} - esbuild-register@3.6.0: - resolution: {integrity: sha512-H2/S7Pm8a9CL1uhp9OvjwrBh5Pvx0H8qVOxNu8Wed9Y7qv56MPtq+GGM8RJpq6glYJn9Wspr8uw7l55uyinNeg==} - peerDependencies: - esbuild: '>=0.12 <1' - esbuild@0.21.5: resolution: {integrity: sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==} engines: {node: '>=12'} @@ -7544,13 +7533,6 @@ snapshots: is-date-object: 1.0.5 is-symbol: 1.0.4 - esbuild-register@3.6.0(esbuild@0.23.1): - dependencies: - debug: 4.3.7(supports-color@5.5.0) - esbuild: 0.23.1 - transitivePeerDependencies: - - supports-color - esbuild@0.21.5: optionalDependencies: '@esbuild/aix-ppc64': 0.21.5 diff --git a/scripts/fix-pnpm.ts b/scripts/fix-pnpm.ts index 2027daf..02efe33 100644 --- a/scripts/fix-pnpm.ts +++ b/scripts/fix-pnpm.ts @@ -1,4 +1,4 @@ -import fs from 'node:fs' +import fs from 'fs-extra' fs.mkdirSync('./packages/el-bot/dist/cli', { recursive: true }) fs.writeFileSync('./packages/el-bot/dist/cli/index.js', '#! /usr/bin/env node')