feat: 更新构建配置,修复解析逻辑并添加程序入口

This commit is contained in:
2026-02-03 16:40:45 +08:00
parent ce574e9fa7
commit d638370db1
6 changed files with 64 additions and 60 deletions

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env bun
import { parse } from '../dist/app.js';
import { parse } from '../dist/program.js';
const args = process.argv.slice(2);
const result = parse(args);
const result = await parse(args);
console.log(result, args);

View File

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

1
demo/src/index.ts Normal file
View File

@@ -0,0 +1 @@
console.log('abc')

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/code-builder",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"main": "src/index.ts",
"bin": {

View File

@@ -1,7 +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;
@@ -82,58 +82,3 @@ export const buildWithBun = async (options: EntryOptions = {}) => {
export const parse = async () => {
const { Command } = await import('commander');
const program = new Command();
program
.name('code-builder')
.description('使用 Bun 的代码构建工具')
.version('0.0.3');
const build = new Command('build').description('使用 Bun 构建项目');
build
.option('-e, --entry <path>', '入口文件', 'src/index.ts')
.option('-o, --outDir <path>', '输出目录', './dist')
.option('-n, --naming <name>', '输出文件命名', 'app')
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node')
.option('--dts', '生成 TypeScript 声明文件', false)
.option('--clean', '清理输出目录', false)
.action(async (options) => {
const cwd = process.cwd();
await buildWithBun({
entry: options.entry,
cwd,
outDir: options.outDir,
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);
});
}

56
src/program.ts Normal file
View File

@@ -0,0 +1,56 @@
import { Command } from 'commander';
import path from 'node:path';
import fs from 'node:fs';
import { buildWithBun } from './index.ts';
import { execSync } from 'node:child_process';
const program = new Command();
program
.name('code-builder')
.description('使用 Bun 的代码构建工具')
.version('0.0.3');
const build = new Command('build').description('使用 Bun 构建项目');
build
.option('-e, --entry <path>', '入口文件', 'src/index.ts')
.option('-o, --outDir <path>', '输出目录', './dist')
.option('-n, --naming <name>', '输出文件命名', 'app')
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node')
.option('--dts', '生成 TypeScript 声明文件', false)
.option('--clean', '清理输出目录', false)
.action(async (options) => {
const cwd = process.cwd();
await buildWithBun({
entry: options.entry,
cwd,
outDir: options.outDir,
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);
export const parse = async () => {
program.parse(process.argv);
}