Skip to content

Commit

Permalink
feat: new logger
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Feb 6, 2024
1 parent 8984ba3 commit f2cfd06
Show file tree
Hide file tree
Showing 35 changed files with 504 additions and 687 deletions.
4 changes: 2 additions & 2 deletions modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-07-11 14:18:27
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2024-02-04 18:37:11
* @LastEditTime: 2024-02-06 20:01:48
*/

import { Context, Symbols, formatTime, stringTemp } from 'kotori-bot';
Expand All @@ -24,7 +24,7 @@ export function main(ctx: Context) {
{
lang: config.global.lang,
root: baseDir.root,
mode: options.env,
mode: options.mode,
modules: ctx[Symbols.modules] ? ctx[Symbols.modules]!.size : 0,
services: ctx[Symbols.adapter].size,
bots: botsLength,
Expand Down
4 changes: 2 additions & 2 deletions modules/weather/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kotori-plugin-weather",
"version": "1.0.0",
"description": "weather. plugin",
"description": "weather plugin",
"main": "lib/index.js",
"scripts": {
"build": "tsc --build"
Expand Down Expand Up @@ -32,4 +32,4 @@
]
}
}
}
}
5 changes: 5 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @kotori-bot/core

## Reference

- [Kotori Docs](https://kotori.js.org/)
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"bot"
],
"files": [
"lib"
"lib",
"LICENSE",
"README.md"
],
"bugs": {
"url": "https://github.com/kotorijs/kotori/issues"
Expand All @@ -23,7 +25,6 @@
"homepage": "https://kotori.js.org",
"dependencies": {
"@kotori-bot/i18n": "workspace:^",
"@kotori-bot/logger": "workspace:^",
"@kotori-bot/tools": "workspace:^",
"@types/minimist": "^1.2.5",
"minimist": "^1.2.8",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/components/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class Config {
/* load package.json */
const info = loadConfig(join(__dirname, '../../package.json')) as unknown;
if (!info || Object.values(info).length === 0) {
console.error(`Cannot find kotori-bot package.json`);
process.stderr.write(`Cannot find kotori-bot package.json\n`);
process.exit();
}
const result = packageInfoSchema.parseSafe(info);
if (!result.value) {
console.error(`File package.json format error: ${result.error.message}`);
process.stderr.write(`File package.json format error: ${result.error.message}\n`);
process.exit();
}
this.pkg = result.data;
Expand Down
14 changes: 1 addition & 13 deletions packages/core/src/components/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Events, Http, obj } from '@kotori-bot/tools';
import Logger from '@kotori-bot/logger';
import { Events, Http } from '@kotori-bot/tools';
import I18n from '@kotori-bot/i18n';
import type { Parser } from 'tsukiko';
import { Context, Symbols } from '../context';
Expand Down Expand Up @@ -33,7 +32,6 @@ declare module '../context' {
/* Inject */
http: Http;
i18n: I18n;
logger: typeof Logger;
}
}

Expand All @@ -57,16 +55,6 @@ export class Core extends Context {
this.provide('i18n', new I18n({ lang: this.config.global.lang }));
this.inject('i18n');
this.inject('i18n');
const logger = Object.assign(
Logger,
new Proxy(Logger.debug, {
apply: (target, _, argArray) => {
if ((globalThis as obj).env_mode === 'dev') target(argArray);
}
})
);
this.provide('logger', logger);
this.inject('logger');
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/components/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ export class Modules {
const exports = await import(`file://${isObject ? modules.main : resolve(modules)}`);
const error = this.handleExports(identity, ctx, exports, config);
if (error) throw error;
this.ctx.emit('ready_module', { module: modules, state: true });
this.ctx.emit('ready_module', { module: modules });
} catch (error) {
this.ctx.emit('ready_module', { module: modules, state: false });
if (error) this.ctx.emit('error', { error });
this.ctx.emit('ready_module', { module: modules, error });
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ export type EventsList = {

export interface EventsMapping {
ready(): void;
error(data: EventDataError): void;
dispose(): void;
}

interface EventDataError {
error: unknown;
}
2 changes: 1 addition & 1 deletion packages/core/src/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module './core' {

interface EventDataReadyModule {
module: ModuleInstance | string;
state: boolean;
error?: unknown;
}

interface EventDataDisposeModule {
Expand Down
19 changes: 4 additions & 15 deletions packages/core/src/utils/errror.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,37 @@
interface DevErrorExtra {
path: string;
type: 'warning' | 'info' | 'error';
}

type KotoriErrorType = 'DatabaseError' | 'ModuleError' | 'UnknownError' | 'DevError';
type KotoriErrorLevel = 'debug' | 'normal' | 'log';

interface KotoriErrorImpl {
readonly name: KotoriErrorType;
readonly level: KotoriErrorLevel;
readonly extend: () => typeof KotoriError;
}

export class KotoriError<T extends object = object> extends Error implements KotoriErrorImpl {
constructor(message?: string, extra?: T, type: KotoriErrorType = 'UnknownError', level: KotoriErrorLevel = 'debug') {
constructor(message?: string, extra?: T, type: KotoriErrorType = 'UnknownError') {
super(message);
this.name = type;
this.level = level;
this.extra = extra;
}

readonly extra?: T;

readonly name: KotoriErrorType;

readonly level: KotoriErrorLevel;

extend(): typeof KotoriError<T> {
const { message: fatherMessage, name: fatherType, level: fatherLevel, extra: fatherExtra } = this;
const { message: fatherMessage, name: fatherType, extra: fatherExtra } = this;
// const newClass: typeof KotoriError = Object.create(KotoriError);
return new Proxy(KotoriError<T>, {
construct(Class, params) {
const args = params;
args[0] = `${fatherMessage} ${args[0]}`;
args[1] = args[1] ?? fatherExtra;
args[2] = args[2] ?? fatherType;
args[3] = args[3] ?? fatherLevel;
return new Class(...args);
}
});
}
}

export const ModuleError = new KotoriError(undefined, undefined, 'ModuleError', 'normal').extend();
export const DevError = new KotoriError<DevErrorExtra>(undefined, undefined, 'DevError', 'debug').extend();
export const ModuleError = new KotoriError(undefined, undefined, 'ModuleError').extend();
export const DevError = new KotoriError(undefined, undefined, 'DevError').extend();

export default KotoriError;
6 changes: 4 additions & 2 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"tools"
],
"files": [
"dist"
"dist",
"LICENSE",
"README.md"
],
"bugs": {
"url": "https://github.com/kotorijs/kotori/issues"
Expand All @@ -25,5 +27,5 @@
"type": "git",
"url": "git+https://github.com/kotorijs/kotori.git"
},
"homepage": "https://kotori.js.org "
"homepage": "https://kotori.js.org"
}
69 changes: 0 additions & 69 deletions packages/kotori/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,72 +35,3 @@ kotori 是一个**跨平台、解耦合、现代化**于一体的 ChatBot 聊天
## Reference

- [变更日志](CHANGELOG.md)

<!--
## kotori支持的连接模式
- [x] 正向 WebSocket
- [x] 反向 WebSocket
kotori目前现已支持go-cqhttp提供的67种`Api`,19种`Event`,18种`CQ Code`
## 关于插件
> [插件中心](docs/PLUGINS.md)
收集插件将不定期更新,你可以直接通过**Pull Request**的方式将你的插件加入(或更新时)到仓库并更新`docs/PLUGINS.md`中的插件列表信息
> 以下内容已不保证时效性
### 从源码里构建
如果你想将KotoriBot作为一个NPM包或者说node项目库来导入到你自己的项目使用搭建机器人的话,由于TS语言本身的原因,无法直接导入纯TypeScript项目,需要先构建一遍TS源码
```bash
npm run build
```
构建完成后的文件将生成在`dist/`文件下,但你无需顾忌这么多,直接引入即可
- TypeScript(.ts)
```typescript
import Kotori from 'kotori-bot';
const Bot = new Kotori(
{
mode: 'WsReverse',
port: 8080,
},
(Event: any, Api: any) => {
/* ... */
},
);
Bot.create();
```
- JavaScript With CommonJS(.js .cjs)
```javascript
const Kotori = require('kotori-bot');
const Bot = new Kotori(
{
mode: 'WsReverse',
port: 8080,
},
(Event, Api) => {
/* ... */
},
);
Bot.create();
```
- JavaScript With ESModule(.mjs)
```javascript
import Kotori from 'kotori-bot';
//...
```
> 该方式下运行不会读取项目下的配置文件,需在实例化时传入配置参数,详细说明参考
-->
15 changes: 7 additions & 8 deletions packages/kotori/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import cac from 'cac';
import { Loader } from '@kotori-bot/loader';
import { Loader, Logger } from '@kotori-bot/loader';
import { readFileSync } from 'fs';
import { obj } from '.';

const program = cac();

Expand All @@ -13,18 +12,18 @@ program
.command('')
.option('--dir [path]', 'Set running root dir of program')
.action((options) => {
(globalThis as obj).env_mode = 'build';
new Loader().run();
Logger.info(options);
new Loader({ mode: 'build' }).run();
});

program
.command('ui')
.option('-l, --lang', 'Set view language of ui')
.action((options) => {
console.log('ui');
Logger.info('ui', options);
});

program.command('module').action(() => console.log('module'));
program.command('module search <name>').action(() => console.log('module search'));
program.command('module download <name>').action(() => console.log('module download'));
program.command('module').action(() => Logger.info('module'));
program.command('module search <name>').action(() => Logger.info('module search'));
program.command('module download <name>').action(() => Logger.info('module download'));
program.parse();
5 changes: 1 addition & 4 deletions packages/kotori/src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Loader } from '@kotori-bot/loader';
import { obj } from '@kotori-bot/core';

(globalThis as obj).env_mode = 'dev';

const kotori = new Loader();
const kotori = new Loader({ mode: 'dev' });
kotori.run();
5 changes: 1 addition & 4 deletions packages/kotori/src/start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Loader } from '@kotori-bot/loader';
import { obj } from '@kotori-bot/core';

(globalThis as obj).env_mode = 'build';

const kotori = new Loader();
const kotori = new Loader({ mode: 'build' });
kotori.run();
2 changes: 1 addition & 1 deletion packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Hotaru <biyuehuya@gmail.com>",
"dependencies": {
"@kotori-bot/core": "workspace:^",
"@kotori-bot/i18n": "workspace:^"
"@kotori-bot/logger": "workspace:^"
},
"files": [
"lib"
Expand Down
4 changes: 4 additions & 0 deletions packages/loader/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export const DEV_CODE_DIRS = './src/';

export const DEV_IMPORT = `${DEV_CODE_DIRS}index.ts`;

export const BUILD_CONFIG_NAME = 'kotori.yml';

export const DEV_CONFIG_NAME = 'kotori.dev.yml';

export const SUPPORTS_VERSION = /(1\.1\.0)/;

export const SUPPORTS_HALF_VERSION = /(x\.x\.(.*?))/;
3 changes: 2 additions & 1 deletion packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* @Blog: https://hotaru.icu
* @Date: 2023-10-29 16:20:51
* @LastEditors: Hotaru biyuehuya@gmail.com
* @LastEditTime: 2024-02-03 17:12:47
* @LastEditTime: 2024-02-06 19:03:21
*/

export * from './loader';
export * from './consts';
export * from '@kotori-bot/logger';

0 comments on commit f2cfd06

Please sign in to comment.