feat: 更新构建配置,添加清理输出目录功能并修改版本号

This commit is contained in:
2026-02-02 06:08:21 +08:00
parent 1ebd6659b7
commit d273e82b73
5 changed files with 75 additions and 36 deletions

View File

@@ -1,3 +1,5 @@
import { buildWithBun } from './src/index.ts' import { buildWithBun } from './src/index.ts'
await buildWithBun({ dts: true }) // await buildWithBun({ dts: true })
await buildWithBun({ naming: 'app', entry: 'src/index.ts', dts: true });

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/code-builder", "name": "@kevisual/code-builder",
"version": "0.0.3", "version": "0.0.4",
"description": "", "description": "",
"main": "src/index.ts", "main": "src/index.ts",
"bin": { "bin": {
@@ -25,9 +25,9 @@
"type": "module", "type": "module",
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@kevisual/types": "^0.0.10", "@kevisual/types": "^0.0.12",
"commander": "^14.0.2", "commander": "^14.0.3",
"@types/bun": "^1.3.5", "@types/bun": "^1.3.8",
"@types/node": "^25.0.3" "@types/node": "^25.2.0"
} }
} }

50
pnpm-lock.yaml generated
View File

@@ -9,34 +9,34 @@ importers:
.: .:
devDependencies: devDependencies:
'@kevisual/types': '@kevisual/types':
specifier: ^0.0.10 specifier: ^0.0.12
version: 0.0.10 version: 0.0.12
'@types/bun': '@types/bun':
specifier: ^1.3.5 specifier: ^1.3.8
version: 1.3.5 version: 1.3.8
'@types/node': '@types/node':
specifier: ^25.0.3 specifier: ^25.2.0
version: 25.0.3 version: 25.2.0
commander: commander:
specifier: ^14.0.2 specifier: ^14.0.3
version: 14.0.2 version: 14.0.3
packages: packages:
'@kevisual/types@0.0.10': '@kevisual/types@0.0.12':
resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==} resolution: {integrity: sha512-zJXH2dosir3jVrQ6QG4i0+iLQeT9gJ3H+cKXs8ReWboxBSYzUZO78XssVeVrFPsJ33iaAqo4q3DWbSS1dWGn7Q==}
'@types/bun@1.3.5': '@types/bun@1.3.8':
resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==} resolution: {integrity: sha512-3LvWJ2q5GerAXYxO2mffLTqOzEu5qnhEAlh48Vnu8WQfnmSwbgagjGZV6BoHKJztENYEDn6QmVd949W4uESRJA==}
'@types/node@25.0.3': '@types/node@25.2.0':
resolution: {integrity: sha512-W609buLVRVmeW693xKfzHeIV6nJGGz98uCPfeXI1ELMLXVeKYZ9m15fAMSaUPBHYLGFsVRcMmSCksQOrZV9BYA==} resolution: {integrity: sha512-DZ8VwRFUNzuqJ5khrvwMXHmvPe+zGayJhr2CDNiKB1WBE1ST8Djl00D0IC4vvNmHMdj6DlbYRIaFE7WHjlDl5w==}
bun-types@1.3.5: bun-types@1.3.8:
resolution: {integrity: sha512-inmAYe2PFLs0SUbFOWSVD24sg1jFlMPxOjOSSCYqUgn4Hsc3rDc7dFvfVYjFPNHtov6kgUeulV4SxbuIV/stPw==} resolution: {integrity: sha512-fL99nxdOWvV4LqjmC+8Q9kW3M4QTtTR1eePs94v5ctGqU8OeceWrSUaRw3JYb7tU3FkMIAjkueehrHPPPGKi5Q==}
commander@14.0.2: commander@14.0.3:
resolution: {integrity: sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==} resolution: {integrity: sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==}
engines: {node: '>=20'} engines: {node: '>=20'}
undici-types@7.16.0: undici-types@7.16.0:
@@ -44,20 +44,20 @@ packages:
snapshots: snapshots:
'@kevisual/types@0.0.10': {} '@kevisual/types@0.0.12': {}
'@types/bun@1.3.5': '@types/bun@1.3.8':
dependencies: dependencies:
bun-types: 1.3.5 bun-types: 1.3.8
'@types/node@25.0.3': '@types/node@25.2.0':
dependencies: dependencies:
undici-types: 7.16.0 undici-types: 7.16.0
bun-types@1.3.5: bun-types@1.3.8:
dependencies: dependencies:
'@types/node': 25.0.3 '@types/node': 25.2.0
commander@14.0.2: {} commander@14.0.3: {}
undici-types@7.16.0: {} undici-types@7.16.0: {}

View File

@@ -19,4 +19,6 @@ code-builder build
import { buildWithBun } from '@kevisual/code-builder' import { buildWithBun } from '@kevisual/code-builder'
await buildWithBun({ meta: import.meta, dts: true }) await buildWithBun({ meta: import.meta, dts: true })
await buildWithBun({ naming: 'app', entry: 'src/index.ts' });
``` ```

View File

@@ -1,6 +1,7 @@
import { execSync } from 'node:child_process'; import { execSync } from 'node:child_process';
import { BuildConfig } from 'bun'; import { BuildConfig } from 'bun';
import path from 'node:path'; import path from 'node:path';
import fs from 'node:fs';
type EntryOptions = { type EntryOptions = {
/** 入口文件路径 */ /** 入口文件路径 */
entry?: string; entry?: string;
@@ -20,6 +21,7 @@ type EntryOptions = {
* 相对路径基准比如bun.config.ts所在路径 * 相对路径基准比如bun.config.ts所在路径
*/ */
meta?: ImportMeta; meta?: ImportMeta;
clean?: boolean;
}; };
const resolvePath = (cwd: string, relativePath: string) => { const resolvePath = (cwd: string, relativePath: string) => {
@@ -63,6 +65,12 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo
export const buildWithBun = async (options: EntryOptions = {}) => { export const buildWithBun = async (options: EntryOptions = {}) => {
const { config, dtsCommand } = gererateBunConfig(options); const { config, dtsCommand } = gererateBunConfig(options);
if (options.clean) {
execSync(`rm -rf ${config.outdir}`, {
stdio: 'inherit',
cwd: options.cwd || process.cwd(),
});
}
await Bun.build(config); await Bun.build(config);
if (dtsCommand) { if (dtsCommand) {
execSync(dtsCommand, { execSync(dtsCommand, {
@@ -80,14 +88,16 @@ export const parse = async () => {
program program
.name('code-builder') .name('code-builder')
.description('使用 Bun 的代码构建工具') .description('使用 Bun 的代码构建工具')
.version('0.0.2'); .version('0.0.3');
const build = new Command('build').description('使用 Bun 构建项目'); const build = new Command('build').description('使用 Bun 构建项目');
build build
.option('-e, --entry <path>', '入口文件', 'src/index.ts') .option('-e, --entry <path>', '入口文件', 'src/index.ts')
.option('-o, --outDir <path>', '输出目录', './dist') .option('-o, --outDir <path>', '输出目录', './dist')
.option('-n, --naming <name>', '输出文件命名', 'app') .option('-n, --naming <name>', '输出文件命名', 'app')
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node') .option('-t, --target <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(); const cwd = process.cwd();
await buildWithBun({ await buildWithBun({
@@ -97,8 +107,33 @@ export const parse = async () => {
naming: options.naming, naming: options.naming,
target: options.target, target: options.target,
dts: options.dts, 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);
}