@bunli/generator

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

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

中文使用文档
文档目录13 节
  1. 特征
  2. 使用量
  3. 基础一代
  4. 用监视模式
  5. 生成输出
  6. 命令文件结构
  7. 与 Bunli CLI 合并
  8. 配置
  9. API 参考
  10. 发电机
  11. 类型
  12. 业绩
  13. 许可证

@bunli/生成器

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

特征

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

使用量

基础一代

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

用监视模式

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 文件为 :

// 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 电话:

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数字 :

import { defineConfig } from '@bunli/core'

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

API 参考

发电机

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

类型

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 共发布 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 是什么?

使用 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 和提交记录判断。