From 764b816b50a84c09e577cb6b315ebdf584156747 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Tue, 7 Jan 2020 14:24:33 +0800 Subject: [PATCH] feat(utils): add `CQCode.stringifyAll` and `CQCode.parseAll` --- build/bump.ts | 15 ++++----- packages/database-level/package.json | 8 ++--- packages/database-mysql/package.json | 6 ++-- packages/koishi-cli/package.json | 8 ++--- packages/koishi-core/package.json | 8 ++--- packages/koishi-utils/package.json | 2 +- packages/koishi-utils/src/cqCode.ts | 39 +++++++++++++++++----- packages/koishi-utils/tests/cqCode.spec.ts | 30 +++++++++++++++++ packages/plugin-common/package.json | 6 ++-- packages/plugin-schedule/package.json | 8 ++--- packages/plugin-teach/package.json | 10 +++--- packages/test-utils/package.json | 6 ++-- 12 files changed, 98 insertions(+), 48 deletions(-) diff --git a/build/bump.ts b/build/bump.ts index 99d020f01b..f92eee32c9 100644 --- a/build/bump.ts +++ b/build/bump.ts @@ -2,6 +2,7 @@ import { writeJson } from 'fs-extra' import { resolve } from 'path' import { SemVer, gt } from 'semver' import { cyan, green } from 'kleur' +import { PackageJson } from './utils' import latest from 'latest-version' import globby from 'globby' import CAC from 'cac' @@ -15,17 +16,9 @@ const { args, options } = CAC() type BumpType = 'major' | 'minor' | 'patch' | 'auto' -interface PackageJSON { - name: string - private?: boolean - version: string - dependencies: Record - devDependencies: Record -} - class Package { name: string - meta: PackageJSON + meta: PackageJson oldVersion: string version: SemVer dirty: boolean @@ -113,6 +106,10 @@ function bumpPkg (source: Package, flag: BumpType, depth: number) { Object.keys(meta.devDependencies || {}).forEach((name) => { if (name !== source.name) return meta.devDependencies[name] = '^' + newVersion + }) + Object.keys(meta.peerDependencies || {}).forEach((name) => { + if (name !== source.name) return + meta.peerDependencies[name] = '^' + newVersion dependents.add(target) }) Object.keys(meta.dependencies || {}).forEach((name) => { diff --git a/packages/database-level/package.json b/packages/database-level/package.json index 6e1033a1ee..5aab81e37e 100644 --- a/packages/database-level/package.json +++ b/packages/database-level/package.json @@ -1,7 +1,7 @@ { "name": "koishi-database-level", "description": "Leveldb support for Koishi", - "version": "1.0.2", + "version": "1.0.3", "main": "dist/index.js", "files": [ "dist" @@ -33,13 +33,13 @@ "leveldb" ], "devDependencies": { - "koishi-test-utils": "^1.0.2" + "koishi-test-utils": "^1.0.3" }, "dependencies": { "@types/leveldown": "^4.0.2", "@types/levelup": "^4.3.0", - "koishi-core": "^1.1.0", - "koishi-utils": "^1.0.1", + "koishi-core": "^1.1.1", + "koishi-utils": "^1.0.2", "leveldown": "^5.4.1", "levelup": "^4.3.2", "subleveldown": "^4.1.4" diff --git a/packages/database-mysql/package.json b/packages/database-mysql/package.json index 68c45dd407..3638b8c1d6 100644 --- a/packages/database-mysql/package.json +++ b/packages/database-mysql/package.json @@ -1,7 +1,7 @@ { "name": "koishi-database-mysql", "description": "MySQL support for Koishi", - "version": "1.0.2", + "version": "1.0.3", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -35,8 +35,8 @@ "@types/mysql": "^2.15.8" }, "dependencies": { - "koishi-core": "^1.1.0", - "koishi-utils": "^1.0.1", + "koishi-core": "^1.1.1", + "koishi-utils": "^1.0.2", "mysql": "^2.17.1" } } diff --git a/packages/koishi-cli/package.json b/packages/koishi-cli/package.json index dea00e6bbb..096bf64b7c 100644 --- a/packages/koishi-cli/package.json +++ b/packages/koishi-cli/package.json @@ -1,7 +1,7 @@ { "name": "koishi", "description": "A QQ bot framework based on CQHTTP", - "version": "1.1.0", + "version": "1.1.1", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -38,9 +38,9 @@ "dependencies": { "cac": "^6.5.3", "kleur": "^3.0.3", - "koishi-core": "^1.1.0", - "koishi-plugin-common": "^1.0.2", - "koishi-utils": "^1.0.1", + "koishi-core": "^1.1.1", + "koishi-plugin-common": "^1.0.3", + "koishi-utils": "^1.0.2", "prompts": "^2.3.0" } } diff --git a/packages/koishi-core/package.json b/packages/koishi-core/package.json index 7cd35df57b..96055590bc 100644 --- a/packages/koishi-core/package.json +++ b/packages/koishi-core/package.json @@ -1,7 +1,7 @@ { "name": "koishi-core", "description": "Core features for Koishi", - "version": "1.1.0", + "version": "1.1.1", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -34,14 +34,14 @@ "devDependencies": { "@types/debug": "^4.1.5", "@types/ws": "^6.0.4", - "koishi-database-level": "^1.0.1", - "koishi-test-utils": "^1.0.1" + "koishi-database-level": "^1.0.3", + "koishi-test-utils": "^1.0.3" }, "dependencies": { "axios": "^0.19.0", "debug": "^4.1.1", "escape-string-regexp": "^2.0.0", - "koishi-utils": "^1.0.0", + "koishi-utils": "^1.0.2", "leven": "^3.1.0", "ws": "^7.2.1" } diff --git a/packages/koishi-utils/package.json b/packages/koishi-utils/package.json index db14272b5a..1a2b5dee95 100644 --- a/packages/koishi-utils/package.json +++ b/packages/koishi-utils/package.json @@ -1,7 +1,7 @@ { "name": "koishi-utils", "description": "Utilities for Koishi", - "version": "1.0.1", + "version": "1.0.2", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ diff --git a/packages/koishi-utils/src/cqCode.ts b/packages/koishi-utils/src/cqCode.ts index a080277d6e..5d2ad502ed 100644 --- a/packages/koishi-utils/src/cqCode.ts +++ b/packages/koishi-utils/src/cqCode.ts @@ -1,10 +1,13 @@ +type CQCodeData = Record + interface CQCode { type: string - data: Record + data: CQCodeData + capture?: RegExpMatchArray } namespace CQCode { - export function escape (source: string, insideCQ = false) { + export function escape (source: any, insideCQ = false) { const result = String(source) .replace(/&/g, '&') .replace(/\[/g, '[') @@ -22,7 +25,8 @@ namespace CQCode { .replace(/&/g, '&') } - export function stringify (type: string, data: Record) { + export function stringify (type: string, data: CQCodeData) { + if (type === 'text') return data.text let output = '[CQ:' + type for (const key in data) { if (data[key]) output += `,${key}=${escape(data[key], true)}` @@ -30,18 +34,37 @@ namespace CQCode { return output + ']' } + export function stringifyAll (codes: CQCode[]) { + return codes.map(code => stringify(code.type, code.data)).join('') + } + const regexp = /\[CQ:(\w+)((,\w+=[^,\]]*)+)\]/ - export function parse (source: string) { - const result = source.match(regexp) - if (!result) return null - const [_, type, attrs] = result + export function parse (source: string): CQCode { + const capture = source.match(regexp) + if (!capture) return null + const [_, type, attrs] = capture const data: Record = {} attrs.slice(1).split(/,/g).forEach((str) => { const [_, key, value] = str.match(/^(\w+)=(.+)$/) data[key] = unescape(value) }) - return { type, data } + return { type, data, capture } + } + + export function parseAll (source: string) { + const codes: CQCode[] = [] + let result: CQCode + while ((result = parse(source))) { + const { capture } = result + if (capture.index) { + codes.push({ type: 'text', data: { text: source.slice(0, capture.index) } }) + } + codes.push(result) + source = source.slice(capture.index + capture[0].length) + } + if (source) codes.push({ type: 'text', data: { text: source } }) + return codes } } diff --git a/packages/koishi-utils/tests/cqCode.spec.ts b/packages/koishi-utils/tests/cqCode.spec.ts index 49f955a901..76402c3d40 100644 --- a/packages/koishi-utils/tests/cqCode.spec.ts +++ b/packages/koishi-utils/tests/cqCode.spec.ts @@ -14,6 +14,19 @@ describe('CQ Code', () => { expect(CQCode.stringify('share', { title: '', url: 'https://test.com', content: 'Hello' })).toBe('[CQ:share,url=https://test.com,content=Hello]') }) + test('stringifyAll', () => { + expect(CQCode.stringifyAll([{ + type: 'text', + data: { text: 'foo' }, + }, { + type: 'bar', + data: { text: 'bar' }, + }, { + type: 'text', + data: { text: 'baz' }, + }])).toBe('foo[CQ:bar,text=bar]baz') + }) + test('parse', () => { expect(CQCode.parse('[CQ:image,')).toBeNull() expect(CQCode.parse('[CQ:image,file=https://test.com/?foo=1&bar=2,3]')).toMatchObject({ @@ -21,4 +34,21 @@ describe('CQ Code', () => { data: { file: 'https://test.com/?foo=1&bar=2,3' }, }) }) + + test('parseAll', () => { + expect(CQCode.parseAll('foo[CQ:bar,text=bar]')).toMatchObject([{ + type: 'text', + data: { text: 'foo' }, + }, { + type: 'bar', + data: { text: 'bar' }, + }]) + expect(CQCode.parseAll('[CQ:bar,text=bar]baz')).toMatchObject([{ + type: 'bar', + data: { text: 'bar' }, + }, { + type: 'text', + data: { text: 'baz' }, + }]) + }) }) diff --git a/packages/plugin-common/package.json b/packages/plugin-common/package.json index 6173043b9e..8f261ec689 100644 --- a/packages/plugin-common/package.json +++ b/packages/plugin-common/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-common", "description": "Common plugins for Koishi", - "version": "1.0.2", + "version": "1.0.3", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -28,7 +28,7 @@ "plugin" ], "dependencies": { - "koishi-core": "^1.1.0", - "koishi-utils": "^1.0.1" + "koishi-core": "^1.1.1", + "koishi-utils": "^1.0.2" } } diff --git a/packages/plugin-schedule/package.json b/packages/plugin-schedule/package.json index 55a4378101..6be005f45d 100644 --- a/packages/plugin-schedule/package.json +++ b/packages/plugin-schedule/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-schedule", "description": "Schedule plugin for Koishi", - "version": "0.1.0", + "version": "0.1.1", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -30,10 +30,10 @@ "task" ], "devDependencies": { - "koishi-database-level": "^1.0.2", - "koishi-database-mysql": "^1.0.2" + "koishi-database-level": "^1.0.3", + "koishi-database-mysql": "^1.0.3" }, "dependencies": { - "koishi-core": "^1.1.0" + "koishi-core": "^1.1.1" } } diff --git a/packages/plugin-teach/package.json b/packages/plugin-teach/package.json index f13661712b..9ac354623f 100644 --- a/packages/plugin-teach/package.json +++ b/packages/plugin-teach/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-teach", "description": "Teach plugin for Koishi", - "version": "0.1.8", + "version": "0.1.9", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -31,11 +31,11 @@ "conversation" ], "devDependencies": { - "koishi-database-level": "^1.0.2", - "koishi-database-mysql": "^1.0.2" + "koishi-database-level": "^1.0.3", + "koishi-database-mysql": "^1.0.3" }, "dependencies": { - "koishi-core": "^1.1.0", - "koishi-utils": "^1.0.1" + "koishi-core": "^1.1.1", + "koishi-utils": "^1.0.2" } } diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index cf1a93a529..8af3a2a507 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -1,7 +1,7 @@ { "name": "koishi-test-utils", "description": "Test utilities for Koishi", - "version": "1.0.2", + "version": "1.0.3", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -39,7 +39,7 @@ "axios": "^0.19.0", "debug": "^4.1.1", "express": "^4.17.1", - "koishi-core": "^1.1.0", - "koishi-utils": "^1.0.1" + "koishi-core": "^1.1.1", + "koishi-utils": "^1.0.2" } }