Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 23, 2020
2 parents 29a638c + 6889e5d commit cb00d8f
Show file tree
Hide file tree
Showing 45 changed files with 795 additions and 337 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ temp
/bots
/coverage
/plugins
/tsconfig.extend.json

todo.md
yarn.lock
Expand Down
3 changes: 2 additions & 1 deletion build/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ interface Dependency {

for (const type of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as DependencyType[]) {
for (const dep in meta[type] || {}) {
if (workspaces[dep]) continue
// skip workspaces and symlinks
if (workspaces[dep] || meta[type][dep].startsWith('file:')) continue
if (!dependencies[dep]) dependencies[dep] = { dependents: [] }
dependencies[dep].dependents.push({ name, type })
}
Expand Down
20 changes: 20 additions & 0 deletions build/jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import spawn from 'cross-spawn'
import open from 'open'
import { resolve } from 'path'

const args = ['jest', '--coverage']

if (process.argv[2]) {
args.push(process.argv[2])
if (process.argv[3]) {
args.push('--collectCoverageFrom', `**/${process.argv[3]}/**/*.ts`)
}
} else {
args.push('packages/.+\\.spec\\.ts')
}

const child = spawn('npx', args, { stdio: 'inherit' })

child.on('close', () => {
open(resolve(__dirname, '../coverage/lcov-report/index.html'))
})
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ module.exports = {
testEnvironment: 'node',
moduleFileExtensions: ['ts', 'js', 'json'],
moduleNameMapper: {
"koishi-(test-utils|(database|plugin)-[\\w-]+)": "<rootDir>/packages/$1/src",
"koishi-(test-utils|(database|plugin)-[\\w-]+)(/dist)?": "<rootDir>/packages/$1/src",
"koishi-[\\w-]+": "<rootDir>/packages/$0/src",
"shiki-universe": "<rootDir>/bots/shiki/lib/shiki-universe/src",
},
coverageReporters: ['text', 'lcov'],
coveragePathIgnorePatterns: [
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
],
"scripts": {
"build": "tsc -b",
"build:extend": "tsc -b tsconfig.extend.json",
"build:shiki": "ts-node bots/shiki/build",
"build:dict": "ts-node bots/shiki/build/dict",
"bump": "ts-node build/bump",
"dep": "ts-node build/dep",
"docs": "cd docs & yarn dev",
"jest": "jest .*\\.spec\\.ts --coverage",
"jest": "ts-node build/jest",
"lint": "eslint packages/*/src/**/*.ts --cache",
"pub": "ts-node build/publish",
"pub:plugins": "yarn pub plugins",
"start": "ts-node build/start"
},
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@octokit/rest": "^16.37.0",
"@octokit/rest": "^16.38.1",
"@types/cross-spawn": "^6.0.1",
"@types/fs-extra": "^8.0.1",
"@types/jest": "^24.9.0",
"@types/node": "^13.1.8",
"@types/semver": "^6.2.0",
"@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.16.0",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"cac": "^6.5.5",
"cross-spawn": "^7.0.1",
"del": "^5.1.0",
Expand All @@ -43,6 +47,7 @@
"jest": "^24.9.0",
"kleur": "^3.0.3",
"latest-version": "^5.1.0",
"open": "^7.0.0",
"ora": "^4.0.3",
"p-map": "^3.0.0",
"prompts": "^2.3.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/database-level/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-level",
"description": "Leveldb support for Koishi",
"version": "1.0.7",
"version": "1.0.8",
"main": "dist/index.js",
"files": [
"dist"
Expand Down Expand Up @@ -33,12 +33,12 @@
"leveldb"
],
"devDependencies": {
"koishi-test-utils": "^2.0.0"
"koishi-test-utils": "^2.1.0"
},
"dependencies": {
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.0",
"koishi-core": "^1.5.0",
"koishi-core": "^1.6.0",
"koishi-utils": "^1.0.2",
"leveldown": "^5.4.1",
"levelup": "^4.3.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/database-memory/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-memory",
"description": "An in-memory database implementation for Koishi",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand All @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-memory#readme",
"dependencies": {
"koishi-core": "^1.5.0",
"koishi-core": "^1.6.0",
"koishi-utils": "^1.0.2"
}
}
6 changes: 3 additions & 3 deletions packages/database-mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-mysql",
"description": "MySQL support for Koishi",
"version": "1.0.7",
"version": "1.0.8",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,8 +35,8 @@
"@types/mysql": "^2.15.8"
},
"dependencies": {
"koishi-core": "^1.5.0",
"koishi-core": "^1.6.0",
"koishi-utils": "^1.0.2",
"mysql": "^2.17.1"
"mysql": "^2.18.0"
}
}
6 changes: 3 additions & 3 deletions packages/database-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koishi-database-sqlite",
"version": "1.0.0-alpha.3",
"version": "1.0.0-alpha.4",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand All @@ -22,10 +22,10 @@
"homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-sqlite#readme",
"devDependencies": {
"@types/sqlite3": "^3.1.6",
"koishi-test-utils": "^2.0.0"
"koishi-test-utils": "^2.1.0"
},
"dependencies": {
"koishi-core": "^1.5.0",
"koishi-core": "^1.6.0",
"koishi-utils": "^1.0.2",
"sqlite3": "^4.1.1"
}
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "1.5.0",
"version": "1.6.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -40,9 +40,9 @@
"cac": "^6.5.5",
"js-yaml": "^3.13.1",
"kleur": "^3.0.3",
"koishi-core": "^1.5.0",
"koishi-plugin-common": "^2.0.0",
"koishi-plugin-schedule": "^1.0.3",
"koishi-core": "^1.6.0",
"koishi-plugin-common": "^2.0.1",
"koishi-plugin-schedule": "^1.0.4",
"koishi-utils": "^1.0.2",
"prompts": "^2.3.0"
}
Expand Down
8 changes: 6 additions & 2 deletions packages/koishi-cli/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const cwd = process.cwd()
function loadEcosystem (type: string, name: string) {
const modules = [resolve(cwd, name)]
const prefix = `koishi-${type}-`
if (name.includes(prefix)) {
if (name.includes(prefix) || name.startsWith('.')) {
modules.unshift(name)
} else {
const index = name.lastIndexOf('/')
Expand All @@ -37,7 +37,11 @@ function loadEcosystem (type: string, name: string) {
for (const name of modules) {
try {
return require(name)
} catch {}
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') {
throw error
}
}
}
throw new Error(`cannot resolve ${type} ${name}`)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "1.5.0",
"version": "1.6.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,11 +35,11 @@
"@types/debug": "^4.1.5",
"@types/ws": "^7.2.0",
"get-port": "^5.1.1",
"koishi-database-memory": "^1.0.0",
"koishi-test-utils": "^2.0.0"
"koishi-database-memory": "^1.0.1",
"koishi-test-utils": "^2.1.0"
},
"dependencies": {
"axios": "^0.19.1",
"axios": "^0.19.2",
"escape-string-regexp": "^2.0.0",
"koishi-utils": "^1.0.2",
"leven": "^3.1.0",
Expand Down
47 changes: 30 additions & 17 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Command, ShortcutConfig, ParsedCommandLine } from './command'
import { Context, Middleware, NextFunction, ContextScope, Events, EventMap } from './context'
import { GroupFlag, UserFlag, UserField, createDatabase, DatabaseConfig, GroupField } from './database'
import { showSuggestions } from './utils'
import { Meta, MessageMeta } from './meta'
import { Meta } from './meta'
import { simplify, noop } from 'koishi-utils'
import { errors, messages } from './messages'
import { ParsedLine } from './parser'
Expand Down Expand Up @@ -85,17 +85,17 @@ export class App extends Context {
atMeRE: RegExp
prefixRE: RegExp
nicknameRE: RegExp
users: MajorContext
groups: MajorContext
discusses: MajorContext
status = Status.closed

_commands: Command[] = []
_commandMap: Record<string, Command> = {}
_shortcuts: ShortcutConfig[] = []
_shortcutMap: Record<string, Command> = {}
_middlewares: [Context, Middleware][] = []

private status = Status.closed
private _users: MajorContext
private _groups: MajorContext
private _discusses: MajorContext
private _isReady = false
private _middlewareCounter = 0
private _middlewareSet = new Set<number>()
Expand Down Expand Up @@ -126,14 +126,27 @@ export class App extends Context {
this.receiver.on('before-user', Command.attachUserFields)
this.receiver.on('before-group', Command.attachGroupFields)
this.middleware(this._preprocess)
}

get users () {
if (this._users) return this._users
const users = this.createContext([[null, []], [[], null], [[], null]]) as MajorContext
users.except = (...ids) => this.createContext([[null, ids], [[], null], [[], null]])
return this._users = users
}

get groups () {
if (this._groups) return this._groups
const groups = this.createContext([[[], null], [null, []], [[], null]]) as MajorContext
groups.except = (...ids) => this.createContext([[[], null], [null, ids], [[], null]])
return this._groups = groups
}

// create built-in contexts
this.users = this.createContext([[null, []], [[], null], [[], null]]) as MajorContext
this.groups = this.createContext([[[], null], [null, []], [[], null]]) as MajorContext
this.discusses = this.createContext([[[], null], [[], null], [null, []]]) as MajorContext
this.users.except = (...ids) => this.createContext([[null, ids], [[], null], [[], null]])
this.groups.except = (...ids) => this.createContext([[[], null], [null, ids], [[], null]])
this.discusses.except = (...ids) => this.createContext([[[], null], [[], null], [null, ids]])
get discusses () {
if (this._discusses) return this._discusses
const discusses = this.createContext([[[], null], [[], null], [null, []]]) as MajorContext
discusses.except = (...ids) => this.createContext([[[], null], [[], null], [null, ids]])
return this._discusses = discusses
}

get selfId () {
Expand Down Expand Up @@ -259,7 +272,7 @@ export class App extends Context {
}
}

private _preprocess = async (meta: MessageMeta, next: NextFunction) => {
private _preprocess = async (meta: Meta<'message'>, next: NextFunction) => {
// strip prefix
let capture: RegExpMatchArray
let atMe = false, nickname = '', prefix: string = null
Expand All @@ -284,7 +297,7 @@ export class App extends Context {
}

// store parsed message
meta.$stripped = { atMe, nickname, prefix, message }
meta.$parsed = { atMe, nickname, prefix, message }

// parse as command
if (prefix !== null || nickname || meta.messageType === 'private') {
Expand Down Expand Up @@ -366,7 +379,7 @@ export class App extends Context {
})
}

parseCommandLine (message: string, meta: MessageMeta): ParsedCommandLine {
parseCommandLine (message: string, meta: Meta<'message'>): ParsedCommandLine {
const name = message.split(/\s/, 1)[0].toLowerCase()
const command = this._commandMap[name]
if (command?.context.match(meta)) {
Expand All @@ -375,14 +388,14 @@ export class App extends Context {
}
}

executeCommandLine (message: string, meta: MessageMeta, next: NextFunction = noop) {
executeCommandLine (message: string, meta: Meta<'message'>, next: NextFunction = noop) {
if (!('$ctxType' in meta)) this.server.parseMeta(meta)
const argv = this.parseCommandLine(message, meta)
if (argv) return argv.command.execute(argv, next)
return next()
}

private _applyMiddlewares = async (meta: MessageMeta) => {
private _applyMiddlewares = async (meta: Meta<'message'>) => {
// preparation
const counter = this._middlewareCounter++
this._middlewareSet.add(counter)
Expand Down
Loading

0 comments on commit cb00d8f

Please sign in to comment.