变量系统

让模板具备「形态」,适配不同场景

变量让同一套工程模式适配不同场景与约束,是模板灵活性的核心。

变量类型

AgileBuilder 支持以下变量类型:

string - 字符串

用于项目名称、包名、端口号等:

{
  "name": "projectName",
  "type": "string",
  "description": "项目名称",
  "required": true,
  "default": "my-app",
  "pattern": "^[a-z][a-z0-9-]*$"
}

boolean - 布尔值

用于启用/禁用某些功能模块:

{
  "name": "useTypeScript",
  "type": "boolean",
  "description": "是否使用 TypeScript",
  "default": true
}

select - 单选

从预定义选项中选择一个:

{
  "name": "packageManager",
  "type": "select",
  "description": "包管理器",
  "options": ["npm", "pnpm", "yarn"],
  "default": "pnpm"
}

multiselect - 多选

选择多个选项:

{
  "name": "features",
  "type": "multiselect",
  "description": "选择功能",
  "options": ["eslint", "prettier", "husky", "jest"],
  "default": ["eslint", "prettier"]
}

在模板中使用变量

文件内容替换

使用 Handlebars 语法:

{
  "name": "{{projectName}}",
  "version": "1.0.0"
}

条件渲染

{{#if useTypeScript}}
  "typescript": "^5.0.0",
{{/if}}

循环渲染

{{#each features}}
  "{{this}}": "latest",
{{/each}}

文件名变量

文件名也可以使用变量:

template/
├── {{projectName}}.config.js
└── src/
    └── {{#if useTypeScript}}index.ts{{else}}index.js{{/if}}

下一步