From d273e82b73afa2469bb826a2e7e23ecc47bad9c0 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 2 Feb 2026 06:08:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B8=85=E7=90=86?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E7=9B=AE=E5=BD=95=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bun.config.ts | 4 +++- package.json | 10 +++++----- pnpm-lock.yaml | 50 +++++++++++++++++++++++++------------------------- readme.md | 2 ++ src/index.ts | 45 ++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/bun.config.ts b/bun.config.ts index 4c3de9f..b6a966d 100644 --- a/bun.config.ts +++ b/bun.config.ts @@ -1,3 +1,5 @@ import { buildWithBun } from './src/index.ts' -await buildWithBun({ dts: true }) \ No newline at end of file +// await buildWithBun({ dts: true }) + +await buildWithBun({ naming: 'app', entry: 'src/index.ts', dts: true }); \ No newline at end of file diff --git a/package.json b/package.json index 6657447..1781c6d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-builder", - "version": "0.0.3", + "version": "0.0.4", "description": "", "main": "src/index.ts", "bin": { @@ -25,9 +25,9 @@ "type": "module", "dependencies": {}, "devDependencies": { - "@kevisual/types": "^0.0.10", - "commander": "^14.0.2", - "@types/bun": "^1.3.5", - "@types/node": "^25.0.3" + "@kevisual/types": "^0.0.12", + "commander": "^14.0.3", + "@types/bun": "^1.3.8", + "@types/node": "^25.2.0" } } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e414b13..93a73ee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,34 +9,34 @@ importers: .: devDependencies: '@kevisual/types': - specifier: ^0.0.10 - version: 0.0.10 + specifier: ^0.0.12 + version: 0.0.12 '@types/bun': - specifier: ^1.3.5 - version: 1.3.5 + specifier: ^1.3.8 + version: 1.3.8 '@types/node': - specifier: ^25.0.3 - version: 25.0.3 + specifier: ^25.2.0 + version: 25.2.0 commander: - specifier: ^14.0.2 - version: 14.0.2 + specifier: ^14.0.3 + version: 14.0.3 packages: - '@kevisual/types@0.0.10': - resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==} + '@kevisual/types@0.0.12': + resolution: {integrity: sha512-zJXH2dosir3jVrQ6QG4i0+iLQeT9gJ3H+cKXs8ReWboxBSYzUZO78XssVeVrFPsJ33iaAqo4q3DWbSS1dWGn7Q==} - '@types/bun@1.3.5': - resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==} + '@types/bun@1.3.8': + resolution: {integrity: sha512-3LvWJ2q5GerAXYxO2mffLTqOzEu5qnhEAlh48Vnu8WQfnmSwbgagjGZV6BoHKJztENYEDn6QmVd949W4uESRJA==} - '@types/node@25.0.3': - resolution: {integrity: sha512-W609buLVRVmeW693xKfzHeIV6nJGGz98uCPfeXI1ELMLXVeKYZ9m15fAMSaUPBHYLGFsVRcMmSCksQOrZV9BYA==} + '@types/node@25.2.0': + resolution: {integrity: sha512-DZ8VwRFUNzuqJ5khrvwMXHmvPe+zGayJhr2CDNiKB1WBE1ST8Djl00D0IC4vvNmHMdj6DlbYRIaFE7WHjlDl5w==} - bun-types@1.3.5: - resolution: {integrity: sha512-inmAYe2PFLs0SUbFOWSVD24sg1jFlMPxOjOSSCYqUgn4Hsc3rDc7dFvfVYjFPNHtov6kgUeulV4SxbuIV/stPw==} + bun-types@1.3.8: + resolution: {integrity: sha512-fL99nxdOWvV4LqjmC+8Q9kW3M4QTtTR1eePs94v5ctGqU8OeceWrSUaRw3JYb7tU3FkMIAjkueehrHPPPGKi5Q==} - commander@14.0.2: - resolution: {integrity: sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==} + commander@14.0.3: + resolution: {integrity: sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==} engines: {node: '>=20'} undici-types@7.16.0: @@ -44,20 +44,20 @@ packages: snapshots: - '@kevisual/types@0.0.10': {} + '@kevisual/types@0.0.12': {} - '@types/bun@1.3.5': + '@types/bun@1.3.8': dependencies: - bun-types: 1.3.5 + bun-types: 1.3.8 - '@types/node@25.0.3': + '@types/node@25.2.0': dependencies: undici-types: 7.16.0 - bun-types@1.3.5: + bun-types@1.3.8: dependencies: - '@types/node': 25.0.3 + '@types/node': 25.2.0 - commander@14.0.2: {} + commander@14.0.3: {} undici-types@7.16.0: {} diff --git a/readme.md b/readme.md index 4ccc056..65db7c7 100644 --- a/readme.md +++ b/readme.md @@ -19,4 +19,6 @@ code-builder build import { buildWithBun } from '@kevisual/code-builder' await buildWithBun({ meta: import.meta, dts: true }) + +await buildWithBun({ naming: 'app', entry: 'src/index.ts' }); ``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 729efe0..de1592f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { execSync } from 'node:child_process'; import { BuildConfig } from 'bun'; import path from 'node:path'; +import fs from 'node:fs'; type EntryOptions = { /** 入口文件路径 */ entry?: string; @@ -20,6 +21,7 @@ type EntryOptions = { * 相对路径基准,比如bun.config.ts所在路径 */ meta?: ImportMeta; + clean?: boolean; }; const resolvePath = (cwd: string, relativePath: string) => { @@ -63,6 +65,12 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo export const buildWithBun = async (options: EntryOptions = {}) => { const { config, dtsCommand } = gererateBunConfig(options); + if (options.clean) { + execSync(`rm -rf ${config.outdir}`, { + stdio: 'inherit', + cwd: options.cwd || process.cwd(), + }); + } await Bun.build(config); if (dtsCommand) { execSync(dtsCommand, { @@ -80,14 +88,16 @@ export const parse = async () => { program .name('code-builder') .description('使用 Bun 的代码构建工具') - .version('0.0.2'); + .version('0.0.3'); const build = new Command('build').description('使用 Bun 构建项目'); build .option('-e, --entry ', '入口文件', 'src/index.ts') .option('-o, --outDir ', '输出目录', './dist') .option('-n, --naming ', '输出文件命名', 'app') .option('-t, --target ', '构建目标 (node 或 browser)', 'node') - .option('--dts', '生成 TypeScript 声明文件', false).action(async (options) => { + .option('--dts', '生成 TypeScript 声明文件', false) + .option('--clean', '清理输出目录', false) + .action(async (options) => { const cwd = process.cwd(); await buildWithBun({ @@ -97,8 +107,33 @@ export const parse = async () => { naming: options.naming, target: options.target, dts: options.dts, + clean: options.clean, }); + program.addCommand(build); + + const init = new Command('init').option('-f, --force', 'force').description('初始化build配置文件'); + init + .action((opts) => { + const cwd = process.cwd(); + const configPath = path.join(cwd, 'bun.config.ts'); + const template = `import { buildWithBun } from '@kevisual/code-builder'; + +await buildWithBun({ naming: 'app', entry: 'src/index.ts', dts: true, clean: true }); +`; + const exists = fs.existsSync(configPath); + if (exists && !opts?.force) { + console.error('bun.config.ts 已存在,使用 --force 强制覆盖'); + process.exit(1); + } + execSync(`echo "${template}" > ${configPath}`, { + stdio: 'inherit', + cwd, + }); + console.log('已生成 bun.config.ts 配置文件'); + }); + + program.addCommand(init); + + program.parse(process.argv); }); - program.addCommand(build); - program.parse(process.argv); -} +} \ No newline at end of file