diff --git a/bin/code.js b/bin/code.js index 4c58ee0..4406773 100755 --- a/bin/code.js +++ b/bin/code.js @@ -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); diff --git a/bun.config.ts b/bun.config.ts index b6a966d..3c3473b 100644 --- a/bun.config.ts +++ b/bun.config.ts @@ -2,4 +2,5 @@ import { buildWithBun } from './src/index.ts' // await buildWithBun({ dts: true }) -await buildWithBun({ naming: 'app', entry: 'src/index.ts', dts: true }); \ No newline at end of file +await buildWithBun({ naming: 'app', entry: 'src/index.ts', dts: true }); +await buildWithBun({ naming: 'program', entry: 'src/program.ts', dts: true }); \ No newline at end of file diff --git a/demo/src/index.ts b/demo/src/index.ts new file mode 100644 index 0000000..2bbdc2c --- /dev/null +++ b/demo/src/index.ts @@ -0,0 +1 @@ +console.log('abc') \ No newline at end of file diff --git a/package.json b/package.json index 7692e93..a9f6d1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-builder", - "version": "0.0.5", + "version": "0.0.6", "description": "", "main": "src/index.ts", "bin": { diff --git a/src/index.ts b/src/index.ts index de1592f..7452720 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 ', '入口文件', 'src/index.ts') - .option('-o, --outDir ', '输出目录', './dist') - .option('-n, --naming ', '输出文件命名', 'app') - .option('-t, --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); - }); -} \ No newline at end of file diff --git a/src/program.ts b/src/program.ts new file mode 100644 index 0000000..781e1df --- /dev/null +++ b/src/program.ts @@ -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 ', '入口文件', 'src/index.ts') + .option('-o, --outDir ', '输出目录', './dist') + .option('-n, --naming ', '输出文件命名', 'app') + .option('-t, --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); +} \ No newline at end of file