diff --git a/build/compile.ts b/build/compile.ts deleted file mode 100644 index 3f13915ac4..0000000000 --- a/build/compile.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { build, BuildFailure, BuildOptions, Message, Platform, Plugin } from 'esbuild' -import { resolve } from 'path' -import { cyan, red, yellow } from 'kleur' -import { existsSync, readdir, readFile } from 'fs-extra' -import escapeRegExp from 'escape-string-regexp' -import { getPackages, PackageJson, requireSafe } from './utils' -import yaml from 'js-yaml' -import cac from 'cac' - -const { args } = cac().help().parse() - -const ignored = [ - 'This call to "require" will not be bundled because the argument is not a string literal', - 'Indirect calls to "require" will not be bundled', - 'should be marked as external for use with "require.resolve"', -] - -function display(prefix: string) { - return ({ location, text }: Message) => { - if (ignored.some(message => text.includes(message))) return - if (!location) return console.log(prefix, text) - const { file, line, column } = location - console.log(cyan(`${file}:${line}:${column}:`), prefix, text) - } -} - -const displayError = display(red('error:')) -const displayWarning = display(yellow('warning:')) - -let code = 0 - -function bundle(options: BuildOptions) { - for (const path of options.entryPoints as string[]) { - if (process.env.CI) console.log('entry:', path) - } - return build(options).then(({ warnings }) => { - warnings.forEach(displayWarning) - }, ({ warnings, errors }: BuildFailure) => { - errors.forEach(displayError) - warnings.forEach(displayWarning) - if (errors.length) code = 1 - }) -} - -const { version } = require('../packages/core/package.json') -const KOISHI_VERSION = JSON.stringify(version) -const root = resolve(__dirname, '..') + '/' - -async function compile(name: string) { - // filter out private packages - const meta: PackageJson = requireSafe(`../${name}/package.json`) - if (!meta || meta.private) return - - const filter = /^[@/\w-]+$/ - const externalPlugin: Plugin = { - name: 'external library', - setup(build) { - build.onResolve({ filter }, () => ({ external: true })) - }, - } - - const base = root + name - const entryPoints = [base + '/src/index.ts'] - const options: BuildOptions = { - entryPoints, - bundle: true, - platform: 'node', - target: 'node12.22', - charset: 'utf8', - outdir: base + '/lib', - logLevel: 'silent', - sourcemap: true, - keepNames: true, - define: { - KOISHI_VERSION, - }, - plugins: [externalPlugin, yamlPlugin()], - } - - // bundle for both node and browser - if (meta.module) { - delete options.outdir - - const modules: string[] = [] - try { - for (const name of await readdir(base + '/src')) { - if (existsSync(base + '/src/' + name + '/package.json')) { - modules.push(name) - } - } - } catch { - return - } - - const filter = new RegExp(`^.+\\/(${modules.map(escapeRegExp).join('|')})$`) - const usePlatformPlugin = (platform: Platform): Plugin => ({ - name: 'platform specific modules', - setup(build) { - build.onResolve({ filter }, ({ path, resolveDir }) => { - for (const module of modules) { - if (!path.includes(module)) continue - return { path: resolve(resolveDir, `${module}/${platform}.ts`) } - } - }) - }, - }) - - return Promise.all([ - bundle({ - ...options, - outfile: base + '/' + meta.module.replace('browser', 'node'), - plugins: [ - usePlatformPlugin('node'), - externalPlugin, - yamlPlugin(), - ], - }), - bundle({ - ...options, - format: 'esm', - target: 'esnext', - platform: 'browser', - sourcemap: false, - minify: true, - outfile: base + '/' + meta.module, - plugins: [ - usePlatformPlugin('browser'), - externalPlugin, - yamlPlugin(), - ], - }), - ]) - } - - try { - const helper = require(base + '/build/compile') - const result = await helper(base, options) as BuildOptions[] - if (result) return Promise.all(result.map(bundle)).then(() => {}) - } catch {} - - return bundle(options) -} - -const yamlPlugin = (options: yaml.LoadOptions = {}): Plugin => ({ - name: 'i18n', - setup(build) { - build.onResolve({ filter: /\/locales\/[\w-]+$/ }, ({ path, resolveDir }) => ({ - path: resolve(resolveDir, path) + '.yml', - namespace: 'yaml', - })) - - build.onLoad({ namespace: 'yaml', filter: /.*/ }, async ({ path }) => { - const source = await readFile(path, 'utf8') - return { - loader: 'json', - contents: JSON.stringify(yaml.load(source, options)), - } - }) - }, -}) - -;(async () => { - const folders = await getPackages(args) - await Promise.all(folders.map(compile)) - - process.exit(code) -})() diff --git a/build/publish.ts b/build/publish.ts deleted file mode 100644 index fd4c557a6b..0000000000 --- a/build/publish.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { cwd, getPackages, PackageJson, spawnAsync, spawnSync } from './utils' -import { gt, maxSatisfying, prerelease } from 'semver' -import { Octokit } from '@octokit/rest' -import { draft } from './release' -import { copyFile } from 'fs-extra' -import latest from 'latest-version' -import ora from 'ora' -import cac from 'cac' - -const { args } = cac().help().parse() - -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.') - process.exit(0) -} - -function getVersion(name: string, isNext = false) { - if (isNext) { - return latest(name, { version: 'next' }).catch(() => getVersion(name)) - } else { - return latest(name).catch(() => '0.0.1') - } -} - -;(async () => { - const folders = await getPackages(args) - const spinner = ora() - const bumpMap: Record = {} - - let progress = 0 - spinner.start(`Loading workspaces (0/${folders.length})`) - await Promise.all(folders.map(async (name) => { - let meta: PackageJson - try { - meta = require(`../${name}/package.json`) - if (!meta.private) { - const version = await getVersion(meta.name, isNext(meta.version)) - if (gt(meta.version, version)) { - bumpMap[name] = meta - } - } - } catch { /* pass */ } - spinner.text = `Loading workspaces (${++progress}/${folders.length})` - })) - spinner.succeed() - - function isNext(version: string) { - const parts = prerelease(version) - if (!parts) return false - return parts[0] !== 'rc' - } - - function publish(folder: string, name: string, version: string, tag: string) { - console.log(`publishing ${name}@${version} ...`) - return spawnAsync([ - 'yarn', 'publish', folder, - '--new-version', version, - '--tag', tag, - '--access', 'public', - ]) - } - - if (Object.keys(bumpMap).length) { - for (const folder in bumpMap) { - const { name, version } = bumpMap[folder] - if (name === 'koishi') { - await copyFile(`${cwd}/README.md`, `${cwd}/${folder}/README.md`) - } - await publish(folder, name, version, isNext(version) ? 'next' : 'latest') - } - } - - const { version } = require('../packages/koishi/package') as PackageJson - if (!CI || isNext(version)) return - - const tags = spawnSync(['git', 'tag', '-l']).split(/\r?\n/) - if (tags.includes(version)) { - return console.log(`Tag ${version} already exists.`) - } - - const body = draft(maxSatisfying(tags, '*'), bumpMap) - console.log(body) - - if (!GITHUB_TOKEN) return - const github = new Octokit({ - auth: GITHUB_TOKEN, - }) - - console.log(`Start to release a new version with tag ${version} ...`) - await github.repos.createRelease({ - repo: 'koishi', - owner: 'koishijs', - tag_name: version, - name: `Koishi ${version}`, - prerelease: isNext(version), - body, - }) - console.log('Release created successfully.') -})() diff --git a/build/release.ts b/build/release.ts deleted file mode 100644 index ec6c67fc20..0000000000 --- a/build/release.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { maxSatisfying } from 'semver' -import { PackageJson, spawnSync } from './utils' - -const headerMap = { - feat: 'Features', - fix: 'Bug Fixes', - dep: 'Dependencies', - '': 'Other Changes', -} - -const prefixes = Object.keys(headerMap) -const prefixRegExp = new RegExp(`^(${prefixes.join('|')})(?:\\((\\S+)\\))?: (.+)$`) - -export function draft(base: string, bumpMap: Record = {}) { - const updates = {} - const commits = spawnSync(['git', 'log', `${base}..HEAD`, '--format=%H %s']).split(/\r?\n/).reverse() - for (const commit of commits) { - const hash = commit.slice(0, 40) - const message = commit.slice(41) - // skip merge commits - if (message.startsWith('Merge')) continue - - const details = prefixRegExp.exec(message) || ['', '', '', message] - let body = details[3] - if (details[2]) body = `**${details[2]}:** ${body}` - if (!updates[details[1]]) updates[details[1]] = '' - updates[details[1]] += `- ${body} (${hash})\n` - } - - let output = Object.values(bumpMap) - .map(({ name, version }) => `- ${name}@${version}`) - .sort().join('\n') + '\n' - for (const type in headerMap) { - if (!updates[type]) continue - output += `\n## ${headerMap[type]}\n\n${updates[type]}` - } - return output -} - -if (require.main === module) { - let version = process.argv[2] - if (!version) { - const tags = spawnSync(['git', 'tag', '-l']).split(/\r?\n/) - version = maxSatisfying(tags, '*') - } - console.log(draft(version)) -} diff --git a/build/template/package.json b/build/template/package.json index 75590fd382..acdec12597 100644 --- a/build/template/package.json +++ b/build/template/package.json @@ -1,5 +1,5 @@ { - "name": "@koishijs/test", + "name": "test", "version": "1.0.0", "private": true, "scripts": { diff --git a/build/yaml.ts b/build/yaml.ts new file mode 100644 index 0000000000..34c6bccd5f --- /dev/null +++ b/build/yaml.ts @@ -0,0 +1,28 @@ +import { addHook } from 'yakumo' +import { Plugin } from 'esbuild' +import {} from 'yakumo-esbuild' +import yaml from 'js-yaml' +import { resolve } from 'path' +import { promises as fsp } from 'fs' + +const yamlPlugin = (options: yaml.LoadOptions = {}): Plugin => ({ + name: 'i18n', + setup(build) { + build.onResolve({ filter: /\/locales\/[\w-]+$/ }, ({ path, resolveDir }) => ({ + path: resolve(resolveDir, path) + '.yml', + namespace: 'yaml', + })) + + build.onLoad({ namespace: 'yaml', filter: /.*/ }, async ({ path }) => { + const source = await fsp.readFile(path, 'utf8') + return { + loader: 'json', + contents: JSON.stringify(yaml.load(source, options)), + } + }) + }, +}) + +addHook('esbuild.before', (options) => { + options.plugins.push(yamlPlugin()) +}) diff --git a/package.json b/package.json index 0ecad9c105..3645e6332a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "external/*", "external/console/packages/*", "external/dialogue/packages/*", + "external/minato/packages/*", + "external/satori/packages/*", + "external/yakumo/packages/*", ".yalc/*", ".yalc/@*/*", "packages/*", @@ -18,27 +21,27 @@ "plugins/cache/*", "plugins/common/*", "plugins/database/*", - "plugins/frontend/*", "plugins/*" ], "scripts": { - "build": "yarn compile && yarn dtsc", + "docs": "yarn workspace @koishijs/docs", + "console": "yarn workspace @root/console", + "dialogue": "yarn workspace @root/dialogue", + "minato": "yarn workspace @root/minato", + "satori": "yarn workspace @root/satori", + "yakumo": "yarn workspace @root/yakumo", "clean": "node -r esbuild-register build/clean", - "compile": "node -r esbuild-register build/compile", - "draft": "node -r esbuild-register build/release", - "dtsc": "yakumo tsc", + "build": "yakumo esbuild && yakumo tsc", "bump": "yakumo version", "dep": "yakumo upgrade", "pub": "yakumo publish", - "docs": "yarn workspace @koishijs/docs", "lint": "eslint packages plugins --ext=ts --cache", "test": "mocha --no-warnings --experimental-vm-modules --enable-source-maps", "test:json": "shx rm -rf coverage && c8 -r json yarn test", "test:html": "shx rm -rf coverage && c8 -r html yarn test", "test:text": "shx rm -rf coverage && c8 -r text yarn test", "scaffold": "shx rm -rf test && shx cp -rf build/template/ test/ && yarn", - "start": "yarn compile && cross-env TS_NODE_PROJECT=../tsconfig.json yarn workspace @koishijs/test koishi start --watch .. -r ../build/register --experimental-vm-modules", - "dev": "yarn compile && yarn workspace @koishijs/test dev", + "start": "yakumo esbuild && yarn workspace test dev", "shiki": "yarn workspace bot-shiki" }, "license": "MIT", @@ -62,7 +65,6 @@ "chai-as-promised": "^7.1.1", "cross-env": "^7.0.3", "cross-spawn": "^7.0.3", - "del": "^6.1.1", "esbuild": "^0.14.48", "esbuild-register": "^3.3.3", "eslint": "^7.32.0", @@ -75,30 +77,21 @@ "fs-extra": "^10.1.0", "globby": "^11.1.0", "jest-mock": "^28.1.0", - "json5": "^2.2.1", "kleur": "^4.1.4", "latest-version": "^5.1.0", "mocha": "^9.2.2", - "open": "^8.4.0", "ora": "^5.4.1", - "p-map": "^4.0.0", "prompts": "^2.4.2", "semver": "^7.3.7", "shx": "^0.3.4", "source-map-support": "^0.5.21", "typescript": "^4.7.2", - "yakumo": "^0.2.9", - "yakumo-mocha": "^0.2.6", - "yakumo-publish": "^0.2.5", - "yakumo-tsc": "^0.2.5", - "yakumo-upgrade": "^0.2.3", - "yakumo-version": "^0.2.6", + "yakumo": "^0.3.1", + "yakumo-mocha": "^0.3.0", + "yakumo-publish": "^0.3.0", + "yakumo-tsc": "^0.3.0", + "yakumo-upgrade": "^0.3.0", + "yakumo-version": "^0.3.0", "yml-register": "^1.0.0" - }, - "yakumo": { - "require": [ - "esbuild-register", - "yml-register" - ] } } diff --git a/packages/bootstrap/build/compile.ts b/packages/bootstrap/build/compile.ts deleted file mode 100644 index 0f9f22f827..0000000000 --- a/packages/bootstrap/build/compile.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { defineBuild } from '../../../build' - -export = defineBuild(async (base, options) => { - options.entryPoints = [ - base + '/src/bin.ts', - ] -}) diff --git a/packages/bootstrap/tsconfig.json b/packages/bootstrap/tsconfig.json index 389708cea6..6f3da3eff7 100644 --- a/packages/bootstrap/tsconfig.json +++ b/packages/bootstrap/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../../tsconfig.base", "compilerOptions": { "rootDir": "src", - "outDir": "lib" + "outDir": "lib", + "emitDeclarationOnly": false, }, "include": [ "src", diff --git a/packages/cli/build/compile.ts b/packages/cli/build/compile.ts deleted file mode 100644 index cd1e354bf0..0000000000 --- a/packages/cli/build/compile.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { defineBuild } from '../../../build' - -export = defineBuild(async (base, options) => { - options.entryPoints = [ - base + '/src/bin.ts', - base + '/src/worker/index.ts', - ] -}) diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index 389708cea6..6f3da3eff7 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -2,7 +2,8 @@ "extends": "../../tsconfig.base", "compilerOptions": { "rootDir": "src", - "outDir": "lib" + "outDir": "lib", + "emitDeclarationOnly": false, }, "include": [ "src", diff --git a/packages/koishi/package.json b/packages/koishi/package.json index df36bc6c57..c642c8e709 100644 --- a/packages/koishi/package.json +++ b/packages/koishi/package.json @@ -22,13 +22,12 @@ "homepage": "https://koishi.js.org", "keywords": [ "bot", - "qqbot", - "cqhttp", - "coolq", + "chatbot", "discord", "telegram", - "chatbot", - "koishi" + "koishi", + "onebot", + "framework" ], "devDependencies": { "@types/parseurl": "^1.3.1" diff --git a/plugins/adapter/qqguild/package.json b/plugins/adapter/qqguild/package.json index 4465a6dedb..dba7598d50 100644 --- a/plugins/adapter/qqguild/package.json +++ b/plugins/adapter/qqguild/package.json @@ -20,6 +20,7 @@ "homepage": "https://koishi.js.org/plugins/adapter/qqguild.html", "keywords": [ "bot", + "chatbot", "qqbot", "qqguild", "koishi" diff --git a/yakumo.yml b/yakumo.yml new file mode 100644 index 0000000000..c0f3f35c16 --- /dev/null +++ b/yakumo.yml @@ -0,0 +1,3 @@ +require: + - esbuild-register + - ./build/yaml