Skip to content

Commit

Permalink
chore: dist -> lib
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 29, 2021
1 parent d8b0f75 commit c8c7ad8
Show file tree
Hide file tree
Showing 51 changed files with 156 additions and 146 deletions.
4 changes: 1 addition & 3 deletions build/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const KOISHI_VERSION = JSON.stringify(version)
await Promise.all(workspaces.map(async (name) => {
if (name.startsWith('.')) return

let outdir = 'dist'
const base = `${root}/${name}`
const entryPoints = [base + '/src/index.ts']

Expand All @@ -61,7 +60,6 @@ const KOISHI_VERSION = JSON.stringify(version)
} else if (name === 'koishi-test-utils') {
await tasks[chai]
} else if (name === 'plugin-status') {
outdir = 'lib'
entryPoints.splice(0, 1, base + '/server/index.ts')
}

Expand All @@ -72,7 +70,7 @@ const KOISHI_VERSION = JSON.stringify(version)
platform: 'node',
target: 'node12.19',
charset: 'utf8',
outdir: `${root}/${name}/${outdir}`,
outdir: `${root}/${name}/lib`,
logLevel: 'silent',
sourcemap: true,
define: {
Expand Down
97 changes: 55 additions & 42 deletions build/dtsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fs from 'fs-extra'
import globby from 'globby'
import json5 from 'json5'
import cac from 'cac'
import ts from 'typescript'

const { args, options } = cac().help().parse()
delete options['--']
Expand All @@ -17,54 +18,76 @@ async function readJson(path: string) {
return json5.parse(data)
}

async function getModules(srcpath: string) {
const files = await globby(srcpath)
return files.map(file => file.slice(srcpath.length + 1, -3))
}

async function compile(path: string, filename: string) {
const code = await spawnAsync(['tsc', '-b', 'packages/' + path, ...tsArgs])
if (code) process.exit(code)
return fs.readFile(filename, 'utf8')
}

async function bundle(path: string) {
const fullpath = resolve(cwd, 'packages', path)
const srcpath = fullpath.replace(/\\/g, '/') + '/src'
const [files, code, config] = await Promise.all([
globby(srcpath),
spawnAsync(['tsc', '-b', 'packages/' + path, ...tsArgs]),
readJson(fullpath + '/tsconfig.json'),
])
if (code) process.exit(code)
const config = await readJson(fullpath + '/tsconfig.json')
const { outFile, rootDir } = config.compilerOptions as ts.CompilerOptions
if (!outFile) return

const srcpath = `${fullpath.replace(/\\/g, '/')}/${rootDir}`
const [files, content] = await Promise.all([getModules(srcpath), compile(path, resolve(fullpath, outFile))])

if (!config.compilerOptions.outFile) return
const entry = resolve(fullpath, config.compilerOptions.outFile)
const modules = files.map(file => file.slice(srcpath.length + 1, -3))
const moduleRE = `"(${modules.join('|')})"`
const moduleRE = `"(${files.join('|')})"`
const internalImport = new RegExp('import\\(' + moduleRE + '\\)\\.', 'g')
const internalExport = new RegExp('^ {4}export .+ from ' + moduleRE + ';$')
const importMap: Record<string, Record<string, string>> = {}
const namespaceMap: Record<string, string> = {}

let prolog = '', epilog = '', cap: RegExpExecArray
let content = await fs.readFile(entry, 'utf8')
content = content.split(EOL).filter((line) => {
if (cap = /^ {4}import \* as (.+) from ["'](.+)["'];$/.exec(line)) {
if (modules.includes(cap[2])) {
let prolog = '', epilog = '', cap: RegExpExecArray, identifier: string
const output = content.split(EOL).filter((line) => {
if (cap = /^ {4}import ["'](.+)["'];$/.exec(line)) {
if (!files.includes(cap[1])) prolog += line.trimStart() + EOL
} else if (cap = /^ {4}import \* as (.+) from ["'](.+)["'];$/.exec(line)) {
if (files.includes(cap[2])) {
namespaceMap[cap[2]] = cap[1]
} else {
prolog += line.trimStart() + EOL
}
} else if (cap = /^ {4}import +(\S*)(?:, *)?(?:\{(.+)\})? from ["'](.+)["'];$/.exec(line)) {
if (!modules.includes(cap[3])) {
const map = importMap[cap[3]] ||= {}
cap[1] && Object.defineProperty(map, 'default', { value: cap[1] })
cap[2] && cap[2].split(',').map((part) => {
part = part.trim()
if (part.includes(' as ')) {
const [left, right] = part.split(' as ')
map[left.trimEnd()] = right.trimStart()
} else {
map[part] = part
}
})
}
if (files.includes(cap[3])) return
const map = importMap[cap[3]] ||= {}
cap[1] && Object.defineProperty(map, 'default', { value: cap[1] })
cap[2] && cap[2].split(',').map((part) => {
part = part.trim()
if (part.includes(' as ')) {
const [left, right] = part.split(' as ')
map[left.trimEnd()] = right.trimStart()
} else {
map[part] = part
}
})
} else if (line.startsWith('///')) {
prolog += line + EOL
} else if (line.startsWith(' export default ')) {
epilog = line.trimStart() + EOL
} else {
return true
}
}).join(EOL)
}).map((line) => {
if (cap = /^declare module ["'](.+)["'] \{( \})?$/.exec(line)) {
if (cap[2]) return
identifier = namespaceMap[cap[1]]
return identifier ? `declare namespace ${identifier} {` : ''
} else if (line === '}') {
return identifier ? '}' : ''
} else if (!internalExport.exec(line)) {
if (!identifier) line = line.slice(4)
return line
.replace(internalImport, '')
.replace(/^((module|class|namespace) .+ \{)$/, (_) => `declare ${_}`)
}
}).filter(line => line).join(EOL)

Object.entries(importMap).forEach(([name, map]) => {
const output: string[] = []
Expand All @@ -79,17 +102,7 @@ async function bundle(path: string) {
prolog += `import ${output.join(', ')} from '${name}';${EOL}`
})

await fs.writeFile(resolve(fullpath, 'dist/index.d.ts'), prolog + content
.replace(new RegExp('import\\(' + moduleRE + '\\)\\.', 'g'), '')
.replace(new RegExp('\r?\n {4}export .+ from ' + moduleRE + ';', 'g'), '')
.replace(/^declare module ["'](.+)["'] \{\r?\n/gm, (_, $1) => {
const identifier = namespaceMap[$1]
if (identifier) return `declare namespace ${identifier} {`
return ''
})
.replace(/^( {4})((module|class|namespace) .+ \{)$/gm, (_, $1, $2) => `${$1}declare ${$2}`)
.replace(/\r?\n}/g, '')
.replace(/^ {4}/gm, '') + epilog)
await fs.writeFile(resolve(fullpath, 'lib/index.d.ts'), prolog + output + epilog)
}

async function bundleAll(names: readonly string[]) {
Expand All @@ -98,7 +111,7 @@ async function bundleAll(names: readonly string[]) {
}
}

const targets = ['koishi-utils', 'koishi-core', 'plugin-mysql', 'plugin-mongo']
const targets = ['koishi-utils', 'koishi-core', 'plugin-mysql', 'plugin-mongo', 'plugin-status']
const corePlugins = ['common', 'eval', 'puppeteer', 'teach']

function precedence(name: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-discord/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-adapter-discord",
"description": "Discord adapter for Koishi",
"version": "1.0.4",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist"
"lib"
],
"author": "LittleC <i@ltlec.cn>",
"maintainers": [
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-discord/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"include": [
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-kaiheila/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-adapter-kaiheila",
"description": "Kaiheila adapter for Koishi",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist"
"lib"
],
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-kaiheila/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"include": [
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-onebot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-adapter-onebot",
"description": "CQHTTP adapter for Koishi",
"version": "3.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist"
"lib"
],
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-onebot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"include": [
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-adapter-telegram",
"description": "Telegram adapter for Koishi",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist"
"lib"
],
"author": "undefined <i@undefined.moe>",
"maintainers": [
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-telegram/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"include": [
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-adapter-tomon",
"description": "Tomon adapter for Koishi",
"version": "1.0.0-beta.11",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist"
"lib"
],
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-tomon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"outFile": "dist/index.d.ts",
"tsBuildInfoFile": "tsconfig.tsbuildinfo"
},
"include": [
"src",
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "3.5.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"engines": {
"node": ">=12.0.0"
},
"files": [
"dist"
"lib"
],
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-test-utils/chai/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "chai-extended",
"private": true,
"main": "dist/index.js",
"main": "lib/index.js",
"typings": "index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
"files": [
"index.d.ts",
"dist"
"lib"
]
}
2 changes: 1 addition & 1 deletion packages/koishi-test-utils/chai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"include": [
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "koishi-test-utils",
"description": "Test utilities for Koishi",
"version": "6.0.0-beta.11",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"engines": {
"node": ">=12.0.0"
},
"files": [
"dist",
"chai/dist",
"lib",
"chai/lib",
"chai/index.d.ts",
"chai/package.json"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-test-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
},
"references": [
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "4.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"dist",
"lib",
"chinese.txt"
],
"author": "Shigma <1700011071@pku.edu.cn>",
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"node": ">=12.0.0"
},
"files": [
"dist",
"lib",
"index.d.ts",
"index.js"
],
"bin": "dist/index.js",
"bin": "lib/index.js",
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"outDir": "lib",
"rootDir": "src",
"noEmit": true,
"emitDeclarationOnly": false
Expand Down
Loading

0 comments on commit c8c7ad8

Please sign in to comment.