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 20, 2020
2 parents 294e46f + 976c6e8 commit 29a638c
Show file tree
Hide file tree
Showing 65 changed files with 1,039 additions and 589 deletions.
9 changes: 2 additions & 7 deletions build/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { writeJson } from 'fs-extra'
import { resolve } from 'path'
import { SemVer, gt } from 'semver'
import { cyan, green } from 'kleur'
import { PackageJson } from './utils'
import { PackageJson, getWorkspaces } from './utils'
import latest from 'latest-version'
import globby from 'globby'
import CAC from 'cac'
import ora from 'ora'

Expand Down Expand Up @@ -126,11 +125,7 @@ function bumpPkg (source: Package, flag: BumpType, only = false) {
const flag = options.major ? 'major' : options.minor ? 'minor' : options.patch ? 'patch' : 'auto'

;(async () => {
const folders = await globby(require('../package').workspaces, {
deep: 0,
onlyDirectories: true,
})

const folders = await getWorkspaces()
const spinner = ora()
let progress = 0
spinner.start(`loading packages 0/${folders.length}`)
Expand Down
2 changes: 1 addition & 1 deletion build/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ interface Dependency {
for (const { name, type } of dependents) {
const workspace = workspaces[name]
const oldVersion = workspace.meta[type][dep]
if (gt(version, oldVersion.slice(1))) {
if (gt(version, oldVersion.replace(/^[~^]/, ''))) {
let update: boolean
const message = `${name} > ${yellow(dep)}: ${cyan(oldVersion)} -> ${green(version)}`
if (!satisfies(version, oldVersion)) {
Expand Down
17 changes: 11 additions & 6 deletions build/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ora from 'ora'
import latest from 'latest-version'
import Octokit from '@octokit/rest'

const { CI, GITHUB_EVENT_NAME, GITHUB_REF } = process.env
const { CI, GITHUB_EVENT_NAME, GITHUB_REF, GITHUB_TOKEN } = process.env

if (CI && (GITHUB_REF !== 'refs/heads/master' || GITHUB_EVENT_NAME !== 'push')) {
console.log('publish skipped.')
Expand All @@ -17,7 +17,11 @@ const headerMap = {
}

;(async () => {
const folders = await getWorkspaces()
let folders = await getWorkspaces()
if (process.argv[2]) {
folders = folders.filter(path => path.startsWith(process.argv[2]))
}

const spinner = ora()
const bumpMap: Record<string, string> = {}

Expand Down Expand Up @@ -45,6 +49,11 @@ const headerMap = {
}
}

if (!GITHUB_TOKEN) return
const github = new Octokit({
auth: GITHUB_TOKEN,
})

const { version } = require('../packages/koishi-cli/package') as PackageJson
const tags = spawnSync('git tag -l').split(/\r?\n/)
if (tags.includes(version)) {
Expand All @@ -69,10 +78,6 @@ const headerMap = {
body += `## ${headerMap[type]}\n\n${updates[type]}\n`
}

const github = new Octokit({
auth: process.env.GITHUB_TOKEN,
})

console.log(`Start to release a new version with tag ${version} ...`)
await github.repos.createRelease({
repo: 'koishi',
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
"docs": "cd docs & yarn dev",
"jest": "jest .*\\.spec\\.ts --coverage",
"lint": "eslint packages/*/src/**/*.ts --cache",
"pub": "ts-node build/publish",
"start": "ts-node build/start"
},
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@octokit/rest": "^16.36.0",
"@octokit/rest": "^16.37.0",
"@types/cross-spawn": "^6.0.1",
"@types/fs-extra": "^8.0.1",
"@types/jest": "^24.0.25",
"@types/node": "^13.1.6",
"@types/jest": "^24.9.0",
"@types/node": "^13.1.8",
"@types/semver": "^6.2.0",
"@typescript-eslint/eslint-plugin": "^2.15.0",
"@typescript-eslint/parser": "^2.15.0",
"cac": "^6.5.4",
"@typescript-eslint/eslint-plugin": "^2.16.0",
"@typescript-eslint/parser": "^2.16.0",
"cac": "^6.5.5",
"cross-spawn": "^7.0.1",
"del": "^5.1.0",
"eslint": "^6.8.0",
Expand All @@ -47,7 +48,7 @@
"prompts": "^2.3.0",
"semver": "^7.1.1",
"ts-jest": "^24.3.0",
"ts-node": "^8.6.1",
"typescript": "^3.7.4"
"ts-node": "^8.6.2",
"typescript": "^3.7.5"
}
}
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.6",
"version": "1.0.7",
"main": "dist/index.js",
"files": [
"dist"
Expand Down Expand Up @@ -33,12 +33,12 @@
"leveldb"
],
"devDependencies": {
"koishi-test-utils": "^1.2.2"
"koishi-test-utils": "^2.0.0"
},
"dependencies": {
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.0",
"koishi-core": "^1.4.0",
"koishi-core": "^1.5.0",
"koishi-utils": "^1.0.2",
"leveldown": "^5.4.1",
"levelup": "^4.3.2",
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/database-memory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# koishi-database-memory

[![Status](https://img.shields.io/github/workflow/status/koishijs/koishi/CI/master?style=flat-square)](https://github.com/koishijs/koishi/actions?query=workflow:CI)
[![npm](https://img.shields.io/npm/v/koishi-database-memory?style=flat-square)](https://www.npmjs.com/package/koishi-database-memory)

An in-memory database for Koishi.

## Usage

- [Using Database with Koishi](https://koishi.js.org/guide/using-database.html)
- [Full API Reference](https://koishi.js.org/api/database.html)
28 changes: 28 additions & 0 deletions packages/database-memory/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "koishi-database-memory",
"description": "An in-memory database implementation for Koishi",
"version": "1.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"author": "Shigma <[email protected]>",
"license": "MIT",
"scripts": {
"lint": "eslint src --ext .ts",
"prepack": "tsc -b"
},
"repository": {
"type": "git",
"url": "git+https://github.com/koishijs/koishi.git"
},
"bugs": {
"url": "https://github.com/koishijs/koishi/issues"
},
"homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-memory#readme",
"dependencies": {
"koishi-core": "^1.5.0",
"koishi-utils": "^1.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectMethods, TableType, TableData, createUser, User, createGroup, getSelfIds, Group, InjectConfig } from 'koishi-core'
import { injectMethods, TableType, TableData, createUser, User, createGroup, getSelfIds, Group, InjectConfig, registerDatabase } from 'koishi-core'
import { observe } from 'koishi-utils'

declare module 'koishi-core/dist/database' {
Expand All @@ -23,33 +23,59 @@ export class MemoryDatabase {
this.store[key] = []
}
}

async create <K extends TableType> (table: K, data: Partial<TableData[K]>) {
const store = this.store[table]
if (typeof data.id !== 'number') {
let index = 1
while (index in store) index++
data.id = index
}
return store[data.id] = data as TableData[K]
}

async remove <K extends TableType> (table: K, id: number) {
delete this.store[table][id]
}

async update <K extends TableType> (table: K, id: number, data: Partial<TableData[K]>) {
Object.assign(this.store[table][id], clone(data))
}

async count (table: TableType) {
return Object.keys(this.store[table]).length
}
}

registerDatabase('memory', MemoryDatabase)

function clone <T> (source: T): T {
return JSON.parse(JSON.stringify(source))
}

injectMethods('memory', 'user', {
async getUser (userId, authority) {
authority = typeof authority === 'number' ? authority : 0
const data = this.store.user.find(u => u.id === userId)
const data = this.store.user[userId]
if (data) return clone(data)
if (authority < 0) return null
const fallback = createUser(userId, authority)
if (authority) this.store.user.push(fallback)
if (authority) this.store.user[userId] = fallback
return clone(fallback)
},

async getUsers (...args) {
if (args.length > 1 || args.length && typeof args[0][0] !== 'string') {
return this.store.user.filter(u => args[0].includes(u.id)).map(clone)
return Object.keys(this.store.user)
.filter(id => args[0].includes(+id))
.map(id => clone(this.store.user[id]))
} else {
return this.store.user.slice()
return Object.values(this.store.user)
}
},

async setUser (userId, data) {
Object.assign(this.store.user.find(u => u.id === userId), clone(data))
return this.update('user', userId, data)
},

async observeUser (user, authority) {
Expand All @@ -63,18 +89,18 @@ injectMethods('memory', 'user', {
return observe(Object.assign(user, clone(data)), diff => this.setUser(user.id, diff), `user ${user.id}`)
},

async getUserCount () {
return this.store.user.length
getUserCount () {
return this.count('user')
},
})

injectMethods('memory', 'group', {
async getGroup (groupId, selfId) {
selfId = typeof selfId === 'number' ? selfId : 0
const data = this.store.group.find(g => g.id === groupId)
const data = this.store.group[groupId]
if (data) return clone(data)
const fallback = createGroup(groupId, selfId)
if (selfId && groupId) this.store.group.push(fallback)
if (selfId && groupId) this.store.group[groupId] = fallback
return clone(fallback)
},

Expand All @@ -83,11 +109,13 @@ injectMethods('memory', 'group', {
: args.length && typeof args[0][0] === 'number' ? args[0] as never
: await getSelfIds()
if (!assignees.length) return []
return this.store.group.filter(g => assignees.includes(g.assignee)).map(clone)
return Object.keys(this.store.group)
.filter(id => assignees.includes(this.store.group[id].assignee))
.map(id => clone(this.store.group[id]))
},

async setGroup (groupId, data) {
Object.assign(this.store.group.find(u => u.id === groupId), clone(data))
return this.update('group', groupId, data)
},

async observeGroup (group, selfId) {
Expand All @@ -102,6 +130,6 @@ injectMethods('memory', 'group', {
},

async getGroupCount () {
return this.store.group.length
return this.count('group')
},
})
68 changes: 68 additions & 0 deletions packages/database-memory/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { testDatabase } from 'koishi-test-utils'
import { injectMethods } from 'koishi-core'
import '../src'

declare module 'koishi-core/dist/database' {
interface TableMethods {
foo: FooMethods
}

interface TableData {
foo: FooData
}
}

interface FooMethods {
createFoo (data?: Partial<FooData>): Promise<FooData>
removeFoo (id: number): Promise<void>
getFooCount (): Promise<number>
}

interface FooData {
id: number
bar: string
}

injectMethods('memory', 'foo', {
async createFoo (data: Partial<FooData> = {}) {
return await this.create('foo', data) as FooData
},

removeFoo (id: number) {
return this.remove('foo', id)
},

getFooCount () {
return this.count('foo')
}
})

const app = testDatabase({
memory: {},
}, {
beforeEachUser: app => app.database.memory.store.user = [],
beforeEachGroup: app => app.database.memory.store.group = [],
})

describe('other methods', () => {
const { database: db } = app
beforeAll(() => db.memory.store.foo = [])

test('create & remove', async () => {
await expect(db.getFooCount()).resolves.toBe(0)
await expect(db.createFoo()).resolves.toMatchObject({ id: 1 })
await expect(db.getFooCount()).resolves.toBe(1)
await expect(db.createFoo()).resolves.toMatchObject({ id: 2 })
await expect(db.getFooCount()).resolves.toBe(2)
await expect(db.removeFoo(1)).resolves.toBeUndefined()
await expect(db.getFooCount()).resolves.toBe(1)
await expect(db.createFoo()).resolves.toMatchObject({ id: 1 })
await expect(db.getFooCount()).resolves.toBe(2)
await expect(db.createFoo({ id: 100 })).resolves.toMatchObject({ id: 100 })
await expect(db.getFooCount()).resolves.toBe(3)
await expect(db.removeFoo(1)).resolves.toBeUndefined()
await expect(db.getFooCount()).resolves.toBe(2)
await expect(db.createFoo()).resolves.toMatchObject({ id: 1 })
await expect(db.getFooCount()).resolves.toBe(3)
})
})
10 changes: 10 additions & 0 deletions packages/database-memory/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
}
4 changes: 2 additions & 2 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.6",
"version": "1.0.7",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,7 +35,7 @@
"@types/mysql": "^2.15.8"
},
"dependencies": {
"koishi-core": "^1.4.0",
"koishi-core": "^1.5.0",
"koishi-utils": "^1.0.2",
"mysql": "^2.17.1"
}
Expand Down
Loading

0 comments on commit 29a638c

Please sign in to comment.