Skip to content

Commit

Permalink
feat: kotori-bot core
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Dec 30, 2023
1 parent ffbe658 commit ab3894e
Show file tree
Hide file tree
Showing 44 changed files with 454 additions and 339 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
4
],
"space-before-function-paren": 0,
/* special */
"@typescript-eslint/no-var-requires": 0,
/* typescript need */
"import/no-unresolved": 0,
"import/extensions": 0,
Expand Down Expand Up @@ -71,6 +73,7 @@
}, */
"ignorePatterns": [
"*.js",
"*.d.ts",
"node_modules"
]
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ lib
*.tgz
tsconfig.tsbuildinfo
pnpm-lock.yaml
kotori.dev.yml
kotori.dev.yml

scripts/*.js
1 change: 1 addition & 0 deletions kotori.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ adapter:

plugin:
console:
test: 1
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@
"description": "ChatBot Framework",
"private": true,
"scripts": {
"loader": "pnpm --filter @kotori-bot/loader",
"kotori": "pnpm --filter kotori-bot",
"build": "pnpm -r build",
"start": "pnpm run loader start",
"dev:only": "pnpm run loader dev",
"dev": "pnpm run build && pnpm run dev:only",
"serve": "kotori serve",
"dev:only": "pnpm kotori dev",
"dev": "pnpm kotori build && pnpm kotori dev",
"test": "echo testing",
"lint": "eslint \"{packages,modules}/*/src/*.{ts,tsx}\" --fix",
"format": "prettier --config .prettierrc \"{packages,modules}/*/src/*.ts\" --write",
"version": "pnpm changeset && pnpm changeset version && pnpm exec conventional-changelog -p eslint -i CHANGELOG.md -s && git add {CHANGELOG.md,package.json}",
"release": "pnpmr run version && pnpm exec release",
"release": "pnpm run version && pnpm exec release",
"publish": "pnpm changeset publish --no-git-tag"
},
"license": "GPL-3.0",
"author": "Hotaru <biyuehuya@gmail.com>",
"bin": {
"release": "./scripts/release.ts"
},
"files": [
"{packages,modules}/*/{lib,dist}/**/*.js",
"{packages,modules}/*/{package.json,LICENSE,README.md}",
Expand All @@ -29,21 +26,24 @@
"LICENSE",
"kotori.yml"
],
"bin": {
"release": "./scripts/release.ts"
},
"devDependencies": {
"tsconfig-paths": "^4.2.0",
"@changesets/cli": "^2.27.1",
"@release-it/conventional-changelog": "^8.0.1",
"@types/inquirer": "^9.0.7",
"@types/node": "^20.8.7",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"cli-pkg": "^0.0.2",
"conventional-changelog-cli": "^4.1.0",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-prettier": "^5.0.0",
"execa": "^8.0.1",
"inquirer": "^9.2.12",
"prettier": "^3.0.2"
}
Expand Down
30 changes: 30 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@kotori-bot/core",
"version": "1.0.0",
"description": "Kotori Core",
"main": "lib/index.js",
"license": "GPL-3.0",
"author": "Hotaru <biyuehuya@gmail.com>",
"keywords": [
"chatbot",
"bot",
"kotori"
],
"files": [
"lib"
],
"bugs": {
"url": "https://github.com/kotorijs/kotori/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kotorijs/kotori.git"
},
"homepage": "https://kotori.js.org",
"dependencies": {
"@kotori-bot/i18n": "workspace:^",
"@kotori-bot/logger": "workspace:^",
"@kotori-bot/tools": "workspace:^",
"tsukiko": "^1.2.1"
}
}
File renamed without changes.
62 changes: 62 additions & 0 deletions packages/core/src/base/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { loadConfig, obj } from '@kotori-bot/tools';
import path from 'path';
import { Parser } from 'tsukiko';
import {
type AdapterConstructor,
type BaseDir,
type GlobalConfig,
type GlobalOptions,
type KotoriConfig,
type PackageInfo,
packageInfoSchema,
kotoriConfigSchema,
} from '../types';
import type Api from '../components/api';
import { CoreError } from '../utils/errror';
import { DEFAULT_COMMAND_PREFIX, DEFAULT_ENV, DEFAULT_LANG, DEFAULT_MODULES_DIR, DEFAULT_ROOT_DIR } from '../consts';

export const defaultConfig = {
baseDir: {
root: path.resolve(DEFAULT_ROOT_DIR),
modules: path.resolve(DEFAULT_MODULES_DIR),
},
config: {
global: {
lang: DEFAULT_LANG,
'command-prefix': DEFAULT_COMMAND_PREFIX,
},
adapter: {},
},
options: {
env: DEFAULT_ENV,
},
};

export class Core {
protected readonly adapterStack: obj<[AdapterConstructor, Parser<unknown>?]> = {};

protected readonly botStack: obj<Api[]> = {};

public readonly baseDir: BaseDir;

public readonly config: GlobalConfig;

public readonly options: GlobalOptions;

public readonly package: PackageInfo;

public constructor(config?: KotoriConfig) {
const info = loadConfig(path.join(__dirname, '../../package.json')) as unknown;
if (!info || Object.values(info).length === 0) throw new CoreError('Cannot find kotori-bot package.json');
const result = packageInfoSchema.parseSafe(info);
if (!result.value) throw new CoreError(`File package.json format error: ${result.error.message}`);
this.package = result.data;
const handle = kotoriConfigSchema.parseSafe(config);
if (!handle.value) throw new CoreError('Unexpected error in parsing config (basedir,kotori,options)');
this.baseDir = handle.data!.baseDir;
this.config = handle.data!.config;
this.options = handle.data!.options;
}
}

export default Core;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Logger from '@kotori-bot/logger';
import { Http } from '@kotori-bot/tools';
import Locale from './utils/i18n';
import Internal from './base/internal';
import { KotoriConfig } from './types';

export class Context extends Internal {
public http = new Http({ validateStatus: () => true });

public logger = Object.assign(
Logger,
new Proxy(Logger.debug, {
apply: (target, _, argArray) => {
if (this.options.env === 'dev') target(argArray);
},
}),
);

public readonly i18n: Locale;

private initialize() {
this.registeMessageEvent();
this.midware((next, session) => {
const { selfId } = session.api.adapter;
if (session.userId !== selfId) next();
}, 50);
}

public constructor(Config?: KotoriConfig) {
super(Config);
this.i18n = new Locale(this.config.global.lang);
this.initialize();
}
}

export default Context;
32 changes: 32 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { none } from '@kotori-bot/tools';
import Context from './context';

export * from './components/adapter';
export * from './components/api';
export * from './components/elements';
export * from './context';
export * from './utils/errror';
export * from './consts';
export * from './types';
export * from '@kotori-bot/tools';
export * from 'tsukiko';

export class ContextInstance {
protected constructor() {
none();
}

private static instance: Context = {} as Context;

protected static setInstance(ctx: Context) {
this.instance = ctx;
}

public static getInstance() {
return this.instance;
}

public static getMixin() {
return Object.assign(ContextInstance.getInstance() /* , Context */);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocaleType } from '@kotori-bot/i18n';
import type Api from './components/api';
import type Adapter from './components/adapter';
import type Context from './context';
import { defaultConfig } from './core/core';
import { defaultConfig } from './base/core';
import type Elements from './components/elements';
import { DEFAULT_COMMAND_PREFIX, DEFAULT_ENV, DEFAULT_LANG, DEFAULT_MODULES_DIR, DEFAULT_ROOT_DIR } from './consts';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../tsconfig.node.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./lib"
},
"references": [
{
"path": "../i18n"
},
{
"path": "../logger"
},
{
"path": "../tools"
}
]
}
3 changes: 3 additions & 0 deletions packages/kotori/bin/kotori.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/cli');
16 changes: 11 additions & 5 deletions packages/kotori/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"name": "kotori-bot",
"version": "1.0.2",
"version": "1.0.1",
"description": "ChatBot Framework Base For NodeJS And TypeScript",
"main": "lib/index.js",
"license": "GPL-3.0",
"author": "Hotaru <biyuehuya@gmail.com>",
"scripts": {
"dev": "ts-node --esm -r tsconfig-paths/register ./src/dev.ts --no-signale",
"build": "tsc --build"
},
"bin": {
"kotori": "./lib/cli.js"
},
"keywords": [
"chatbot",
"bot",
Expand All @@ -27,9 +34,8 @@
},
"homepage": "https://kotori.js.org",
"dependencies": {
"@kotori-bot/i18n": "workspace:^",
"@kotori-bot/logger": "workspace:^",
"@kotori-bot/tools": "workspace:^",
"tsukiko": "^1.2.1"
"@kotori-bot/core": "workspace:^",
"@kotori-bot/loader": "workspace:^",
"cac": "^6.7.14"
}
}
18 changes: 18 additions & 0 deletions packages/kotori/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cac from 'cac';
import Loader from '@kotori-bot/loader';
import { readFileSync } from 'fs';

const program = cac();

const pkg = JSON.parse(readFileSync(`${__dirname}/../package.json`).toString());

program.version(pkg.verion);

program
.command('serve')
.option('--mode [value]', 'Set running mode of program')
.option('--dir [path]', 'Set running root dir of program')
.action((...args) => {
console.log(args);
new Loader().run();
});
36 changes: 0 additions & 36 deletions packages/kotori/src/context.ts

This file was deleted.

0 comments on commit ab3894e

Please sign in to comment.