From a8e19a31d752f7643c686775fdc71547bf6dfd4d Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Mon, 21 Feb 2022 02:25:13 +0800 Subject: [PATCH] feat(scripts): support build command --- packages/scripts/src/bin.ts | 6 +- packages/scripts/src/build.ts | 74 +++++++++++++++++++++ packages/scripts/src/{create.ts => init.ts} | 4 +- packages/scripts/src/utils.ts | 11 +++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 packages/scripts/src/build.ts rename packages/scripts/src/{create.ts => init.ts} (96%) diff --git a/packages/scripts/src/bin.ts b/packages/scripts/src/bin.ts index 606dddee03..d3366c08f4 100644 --- a/packages/scripts/src/bin.ts +++ b/packages/scripts/src/bin.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node -import registerCreateCommand from './create' +import registerBuildCommand from './build' +import registerInitCommand from './init' import registerPublishCommand from './publish' import CAC from 'cac' @@ -8,7 +9,8 @@ const { version } = require('../package.json') const cli = CAC('koishi-scripts').help().version(version) -registerCreateCommand(cli) +registerBuildCommand(cli) +registerInitCommand(cli) registerPublishCommand(cli) cli.parse() diff --git a/packages/scripts/src/build.ts b/packages/scripts/src/build.ts new file mode 100644 index 0000000000..77c7f208cb --- /dev/null +++ b/packages/scripts/src/build.ts @@ -0,0 +1,74 @@ +import { CAC } from 'cac' +import { writeFile } from 'fs-extra' +import { cwd, getPackages, PackageJson, spawnAsync, TsConfig } from './utils' + +interface Node { + path?: string + meta?: PackageJson + prev?: string[] + next?: Set +} + +function initGraph(names: string[]) { + const packages = getPackages(names) + const nodes: Record = {} + for (const path in packages) { + const meta = packages[path] + if (!meta || meta.private) return + nodes[meta.name] = { path, meta, prev: [], next: new Set() } + } + + for (const name in nodes) { + const { meta } = nodes[name] + const deps = { + ...meta.dependencies, + ...meta.devDependencies, + ...meta.peerDependencies, + } + for (const dep in deps) { + if (!nodes[dep]) continue + nodes[name].prev.push(dep) + nodes[dep].next.add(name) + } + delete nodes[name].meta + } + + return nodes +} + +async function buildGraph(nodes: Record) { + function check(name: string) { + const node = nodes[name] + if (node.next.size) return true + delete nodes[name] + config.references.unshift({ path: './' + node.path }) + node.prev.forEach(dep => { + nodes[dep].next.delete(name) + }) + } + + let names: string[] + const config: TsConfig = { files: [], references: [] } + do { + names = Object.keys(nodes) + } while (names.length && !names.every(check)) + + if (names.length) { + console.log(nodes) + throw new Error('circular dependency detected') + } + + if (!config.references.length) return + await writeFile(cwd + '/tsconfig.temp.json', JSON.stringify(config, null, 2)) + + const code = await spawnAsync(['tsc', '-b', 'tsconfig.temp.json']) + if (code) process.exit(code) +} + +export default function (cli: CAC) { + cli.command('build [...name]', 'build packages') + .action(async (names: string[], options) => { + const nodes = initGraph(names) + await buildGraph(nodes) + }) +} diff --git a/packages/scripts/src/create.ts b/packages/scripts/src/init.ts similarity index 96% rename from packages/scripts/src/create.ts rename to packages/scripts/src/init.ts index 74b30d04c2..dadc3dd518 100644 --- a/packages/scripts/src/create.ts +++ b/packages/scripts/src/init.ts @@ -51,6 +51,7 @@ class Runner { async writeManifest() { await fsp.writeFile(this.root + '/package.json', JSON.stringify({ name: this.fullname, + private: true, version: '1.0.0', main: 'lib/index.js', typings: 'lib/index.d.ts', @@ -96,7 +97,8 @@ class Runner { } export default function (cli: CAC) { - cli.command('create [name]', 'create a new plugin') + cli.command('init [name]', 'init a new plugin') + .alias('create') .action(async (name: string, options) => { const meta = require(process.cwd() + '/package.json') new Runner(meta).start(name) diff --git a/packages/scripts/src/utils.ts b/packages/scripts/src/utils.ts index 7cc5ce99dd..e606dd5f00 100644 --- a/packages/scripts/src/utils.ts +++ b/packages/scripts/src/utils.ts @@ -1,5 +1,6 @@ import spawn from 'cross-spawn' import globby from 'globby' +import ts from 'typescript' export const cwd = process.cwd() export const meta: PackageJson = require(cwd + '/package.json') @@ -51,6 +52,16 @@ export interface PackageJson extends Partial((resolve) => {