@bunli/generator

中文文档
0.6.5发布于 2026/03/28ES Module近 30 天下载 7131

The Bunli command type generator that creates TypeScript definitions from your CLI commands using Bun's native APIs.

原始使用文档
文档目录13 节
  1. Features
  2. Usage
  3. Basic Generation
  4. With Watch Mode
  5. Generated Output
  6. Command File Structure
  7. Integration with Bunli CLI
  8. Configuration
  9. API Reference
  10. Generator
  11. Types
  12. Performance
  13. License

@bunli/generator

The Bunli command type generator that creates TypeScript definitions from your CLI commands using Bun's native APIs.

Features

  • Bun-Native: Uses Bun.Glob for fast file scanning
  • Type-Safe: Generates full TypeScript definitions with module augmentation
  • Schema-Aware: Extracts metadata from Zod, Valibot, and other schema libraries
  • Watch Mode: Integrated with Bun's native watch capabilities
  • Minimal Dependencies: Uses Babel for AST parsing, Bun APIs for everything else

Usage

Basic Generation

import { Generator } from '@bunli/generator'
import { Result } from 'better-result'

const generator = new Generator({
  entry: './src/cli.ts',
  directory: './src/commands',
  outputFile: './.bunli/commands.gen.ts'
})

const runResult = await generator.run()
if (Result.isError(runResult)) {
  console.error(runResult.error.message)
}

With Watch Mode

import { Generator } from '@bunli/generator'
import { Result } from 'better-result'

const generator = new Generator({
  entry: './src/cli.ts',
  directory: './src/commands',
  outputFile: './.bunli/commands.gen.ts'
})

// Initial generation
const runResult = await generator.run()
if (Result.isError(runResult)) {
  console.error(runResult.error.message)
}

// Watch for changes (integrated with Bun's watch mode)
// This is handled automatically by bunli dev

Generated Output

The generator creates a commands.gen.ts file with:

// Generated command registry
export interface CommandRegistry {
  'deploy': {
    name: 'deploy'
    description: 'Deploy your application'
    options: {
      env: { type: 'string', required: true, description: 'Environment' }
      force: { type: 'boolean', required: false, default: false }
    }
    filePath: './commands/deploy.ts'
    exportPath: './commands/deploy'
  }
}

// Module augmentation for @bunli/core
declare module '@bunli/core' {
  interface GeneratedCommands extends CommandRegistry {}
}

// Helper functions
export function getCommandApi<T extends keyof CommandRegistry>(name: T): CommandRegistry[T]
export function getCommandNames(): (keyof CommandRegistry)[]
export function hasCommand(name: string): name is keyof CommandRegistry
export function getCommandByAlias(alias: string): keyof CommandRegistry | undefined
export function listCommands(): Array<{...}>

Command File Structure

The generator discovers command modules from your CLI entry (commands.entry / build.entry) and parses default-exported defineCommand / defineGroup calls:

import { defineCommand, option } from '@bunli/core'
import { z } from 'zod'

export default defineCommand({
  name: 'deploy',
  description: 'Deploy your application',
  alias: 'd',
  options: {
    env: option(z.string(), { 
      description: 'Environment to deploy to',
      short: 'e'
    }),
    force: option(z.boolean().default(false), {
      description: 'Force deployment'
    })
  },
  handler: async ({ flags }) => {
    // Implementation
  }
})

Integration with Bunli CLI

The generator is automatically integrated with:

  • bunli dev - Generates types and watches for changes
  • bunli build - Pre-build codegen step
  • bunli generate - Standalone generation command

Configuration

Configure the generator in your bunli.config.ts:

import { defineConfig } from '@bunli/core'

export default defineConfig({
  name: 'my-cli',
  version: '1.0.0',
})

API Reference

Generator

class Generator {
  constructor(config: GeneratorConfig)
  async run(event?: GeneratorEvent): Promise<Result<void, GeneratorRunError>>
  getConfig(): GeneratorConfig
  updateConfig(updates: Partial<GeneratorConfig>): void
}

Types

interface GeneratorConfig {
  entry: string
  directory?: string
  outputFile: string
  config?: any
}

interface GeneratorEvent {
  type: 'create' | 'update' | 'delete'
  path: string
}

interface CommandMetadata {
  name: string
  description: string
  alias?: string | string[]
  options?: Record<string, OptionMetadata>
  filePath: string
  exportPath: string
}

Performance

  • Fast Scanning: Uses Bun's native Bun.Glob for file discovery
  • Incremental: Only regenerates when command files change
  • Memory Efficient: Processes files one at a time
  • TypeScript Optimized: Generates minimal, efficient type definitions

License

MIT

版本与发布记录

@bunli/generator 共发布 15 个版本,首次发布于 2025/10/24;过去 12 个月发布了 12 个版本。

版本发布日期距今
0.6.5latest2026/03/287 个月前
0.6.42026/03/048 个月前
0.6.32026/03/018 个月前
0.6.22026/03/018 个月前
0.6.12026/02/278 个月前
0.6.02026/02/278 个月前

关于 @bunli/generator 的常见问题

@bunli/generator 是什么?

The Bunli command type generator that creates TypeScript definitions from your CLI commands using Bun's native APIs.

如何安装 @bunli/generator?

在项目目录运行 npm install @bunli/generator,或使用 pnpm add / yarn add / bun add。

@bunli/generator 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bunli__generator 是否存在。

@bunli/generator 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bunli/generator 可以商用吗?

@bunli/generator 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bunli/generator 的下载量有多少?

根据 npm 官方下载统计,@bunli/generator 在 2026/10/09 – 2026/11/07 这 30 天内被下载 7,131 次,日均约 238 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bunli/generator 还在维护吗?

最新版本 0.6.5 发布于 2026/03/28(7 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 12 个;首次发布于 2025/10/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。