Skip to content

Commit

Permalink
feat; add sigint isTTY judge for nohup
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Sep 22, 2024
1 parent 25c7596 commit a00ad8c
Show file tree
Hide file tree
Showing 24 changed files with 84 additions and 115 deletions.
2 changes: 1 addition & 1 deletion examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 6 additions & 10 deletions examples/simple/src/index.ts
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 2 additions & 4 deletions examples/simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 2 additions & 1 deletion examples/simple/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
2 changes: 1 addition & 1 deletion packages/cli/src/install/repo.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/el-bot/bin/el-bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env vite-node --watch --script
#!/usr/bin/env vite-node --script

import { run } from '../node'

Expand Down
8 changes: 8 additions & 0 deletions packages/el-bot/bump.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'bumpp'

export default defineConfig({
all: false,
commit: false,
tag: false,
push: false,
})
40 changes: 33 additions & 7 deletions packages/el-bot/core/bot/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions packages/el-bot/core/bot/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import colors from 'picocolors'
import { createLogger } from './winston'

export * from './consola'
export * from './winston'

const defaultLogger = createLogger()
Expand Down
2 changes: 1 addition & 1 deletion packages/el-bot/core/bot/plugins/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
38 changes: 1 addition & 37 deletions packages/el-bot/core/config/el.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
* 是否启用
Expand Down Expand Up @@ -46,15 +45,6 @@ export interface ElConfig<T = BotConfig> {
* 开启调试模式
*/
debug?: boolean
/**
* MiraiApiHttp setting.yml 路径
* 或传入 MiraiApiHttpConfig 对象配置
*/
// setting: MiraiApiHttpSetting | string
/**
* mirai info
*/
mirai: { folder: string }
/**
* mongodb 数据库默认配置
*/
Expand Down Expand Up @@ -87,19 +77,6 @@ export interface ElConfig<T = BotConfig> {
* 根目录
*/
base: string
/**
* mirai-api-http 文件路径
*/
path: {
/**
* 图片路径
*/
image: string
/**
* 语音路径
*/
voice: string
}
}

export type ElUserConfig = Partial<ElConfig<BotUserConfig>>
Expand All @@ -108,13 +85,7 @@ export type ElUserConfig = Partial<ElConfig<BotUserConfig>>
* 解析 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,
},
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion packages/el-bot/core/config/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion packages/el-bot/core/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import fs from 'fs-extra'

/**
* 寻找文件
Expand Down
2 changes: 1 addition & 1 deletion packages/el-bot/core/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs'
import fs from 'fs-extra'
import yaml from 'js-yaml'

/**
Expand Down
11 changes: 4 additions & 7 deletions packages/el-bot/node/cli/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand Down
15 changes: 6 additions & 9 deletions packages/el-bot/package.json
Original file line number Diff line number Diff line change
@@ -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": "云游君",
Expand All @@ -27,20 +27,18 @@
],
"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"
},
"files": [
"bin",
"core",
"index.ts",
"node",
"plugins",
"types"
Expand All @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
17 changes: 8 additions & 9 deletions packages/el-bot/plugins/qrcode/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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 <text...>')
Expand Down
2 changes: 1 addition & 1 deletion packages/el-bot/plugins/rss/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Loading

0 comments on commit a00ad8c

Please sign in to comment.