TurborepoのCode Generator について調べた
まず、generator configuration fileを用意する
基本的にpromptsとactionsを定義するだけで良い
import type { PlopTypes } from "@turbo/gen"; export default function generator(plop: PlopTypes.NodePlopAPI): void { // create a generator plop.setGenerator("Generator name", { description: "Generator description", // gather information from the user prompts: [ ... ], // perform actions based on the prompts actions: [ ... ], }); }
promptsはhygenのようにユーザーに入力させたいものを定義する
actionsは入力値に応じてどのようなことをするかを定義する
actions: [ { type: "add", path: "src/{{kebabCase name}}.tsx", templateFile: "templates/component.hbs", }, { type: "append", path: "package.json", pattern: /"exports": {(?<insertion>)/g, template: '"./{{kebabCase name}}": "./src/{{kebabCase name}}.tsx",', }, ],
ここでのadd
とappend
はbuilt-in Plop actionsというのを使っており、他に以下のようなtypeが指定できる
- Add:プロジェクトにファイルを追加する
- AddMany:1 回のアクションで複数のファイルをプロジェクトに追加できる
- Modify:patternでマッチしたものを置換したり、transformでファイルの中身を変更したりできる
- Append:ファイルの特定の場所にデータを追加する
もしくは、完全に自分でaction functionをカスタムする方法もある
https://plopjs.com/documentation/#functionsignature-custom-action
これまでhygenを使っていたが、turborepo使うならこの機能を利用した方が良さそう