@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用于查找文件 - 递增: 只有命令文件更改时重新生成
- 内存效率: 一次处理一个文件
- 类型脚本优化: 生成最低限度的高效类型定义
许可证
麻省理工学院