SuperAPI AI 工具集

typescript-cli-tsgen

编程与开发

查看项目

更新于 10-16 15:55

技能介绍

TypeScript CLI tsgen 技能

用途

该仓库是 csdx(Contentstack CLI)的一个 OCLIF 插件,用于提供 tsgen 命令,从而为 Contentstack 内容栈生成 TypeScript 类型代码。

主要能力

  • 以 OCLIF 命令形式提供 tsgen(实现 TypeScriptCodeGeneratorCommand,位于 src/commands/tsgen.ts)。
  • 真正的类型生成逻辑委托给 npm 包 @contentstack/types-generator;package.json 中声明的版本为 ^3.9.3。资料明确说明:不要假设存在本地克隆,应使用 npm 与已发布的源码。

职责划分

关注点 所在位置
新标志、输出路径、csdx 交互、printFormattedError 本仓库(src/commands/、src/lib/)
Schema 映射、Delivery SDK 调用、GraphQL introspection @contentstack/types-generator

辅助文件:

  • src/lib/helper.ts:sanitizePath、printFormattedError(FormattedError、error_code 分支)。
  • src/types/index.ts:StackConnectionConfig 及相关 CLI 类型。
  • OCLIF:命令编译到 lib/commands;prepack 会刷新 oclif.manifest.json 与 README。

使用方式(命令结构)

  • 继承 @contentstack/cli-command 的 Command(src/commands/tsgen.ts)。
  • 标志包括:token-alias(-a,必填)、output(-o)、prefix、doc、branch、include-system-fields、include-editable-tags、include-referenced-entry、api-type(rest | graphql)、namespace(GraphQL)。
  • 通过 this.getToken(flags["token-alias"]) 解析令牌;如果 token.type !== "delivery" 会给出警告(管理令牌可能破坏 Delivery/GraphQL 流程)。
  • 构造 StackConnectionConfig:apiKey、token、region、environment、branch、host,与既有代码一致。
  • 当 api-type === "graphql" 时使用交付令牌调用 graphqlTS(...);REST 情况调用 generateTS,并设置 tokenType: "delivery";随后用 fs.writeFileSync 写入解析后的路径。
  • 使用 src/lib/helper.ts 的 printFormattedError 以保持一致的 CLI 消息。

适用场景

适用于使用 Contentstack 并希望基于内容栈自动生成 TypeScript 类型的开发者,尤其是需要在 GraphQL 或 REST(Delivery)流程下生成类型、并希望以命令行方式集成到开发流程中的情况。

限制

资料未提供具体的安装命令、环境要求、定价或使用效果等信息,暂未提供。