# @bunli/generator 中文文档

规范地址：https://npmcn.com/package/%40bunli/generator/

版本：0\.6\.5
npm 元数据更新时间：2026-03-27T23:26:25.050Z
中文译文更新时间：2026-11-08T19:52:59.329Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40bunli/generator
原文对照：https://npmcn.com/package/%40bunli/generator/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：88.1 kB，15 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- 运行依赖：6 个
- 首次发布：2025-10-24T09:53:47.997Z
- 最新版本发布：2026-03-27T23:26:24.936Z
- 近 30 天下载量：7131（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：15（近 12 个月 12 个）

## 中文简介

使用 Bun 本地 API 从您的 CLI 命令创建 TypeScript 定义的 Bunli 命令类型生成器 .

## 安装

```sh
npm install @bunli/generator
```

也可以使用：`pnpm add @bunli/generator`、`yarn add @bunli/generator`、`bun add @bunli/generator`

## README

# @bunli/生成器

使用 Bun 本地 API 从您的 CLI 命令创建 TypeScript 定义的 Bunli 命令类型生成器 .

## 特征

-   **Bun- Native 软件包**: 用途 `Bun.Glob` 用于快速文件扫描
-   **类型安全**: 生成带有模块增强的完整类型脚本定义
-   **Schema- Aware 软件**: 从 Zod, Valibot 和其他计划库提取元数据
-   **监视模式**: 与 Bun 的本地监视能力相融合
-   **最小依赖性**:将 Babel 用于 AST 解析,将 Bun APIS 用于其他所有

## 使用量

### 基础一代

```typescript
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)
}
```

### 用监视模式

```typescript
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
```

## 生成输出

生成器创建 `commands.gen.ts` 文件为 :

```typescript
// 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<{...}>
```

## 命令文件结构

生成器从您的 CLI 条目中发现命令模块( Q)`commands.entry` 编号 : `build.entry`并解析默认导出 `defineCommand` 编号 : `defineGroup` 电话:

```typescript
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
  }
})
```

## 与 Bunli CLI 合并

生成器自动与 :

-   `bunli dev` - 生成更改的类型和手表
-   `bunli build` - 预建代码阶梯
-   `bunli generate` - 独立的生成命令

## 配置

配置您的生成器 `bunli.config.ts`数字 :

```typescript
import { defineConfig } from '@bunli/core'

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

## API 参考

### 发电机

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

### 类型

```typescript
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
}
```

## 业绩

-   **快速扫描**: 使用 Bun 的本地语言 `Bun.Glob` 用于查找文件
-   **递增**: 只有命令文件更改时重新生成
-   **内存效率**: 一次处理一个文件
-   **类型脚本优化**: 生成最低限度的高效类型定义

## 许可证

麻省理工学院

## 常见问题

### @bunli/generator 是什么？

使用 Bun 本地 API 从您的 CLI 命令创建 TypeScript 定义的 Bunli 命令类型生成器 \.

### 如何安装 @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 和提交记录判断。

