feat(turbo): turbo gen feature generator (Phase 1, single-entity)
Adds `pnpm turbo gen feature` to scaffold a Lazar-conformant feature package matching the navigation reference shape: entity + Zod schema, single use case (`get<Entity>`), controller, mock + Payload-stub real repository (with span + capture), DI module/container/symbols, and tRPC router with full BAD_REQUEST/NOT_FOUND error mapping. The generated `bind-production.ts` and `bind-dev-seed.ts` compose the post-R44 `withSpan(tracer, opts, withCapture(logger, tags, factory(deps)))` sandwich at bind time. Verified by generating a sample `packages/example/` feature and running `pnpm --filter @repo/example lint typecheck test` — all three pass (9 test files, 25 tests). Cleaned up after verification so no example package is committed. Phase-1 limitations (documented in `docs/guides/scaffolding-a-feature.md` and printed by the generator on success): no Payload CMS templates, no React Query helpers, faker-driven factories left as stubs, single entity / single use case, and aggregator wiring (core-api/root, apps/web-next bindAll) is left as a manual checklist. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
withSpan,
|
||||
withCapture,
|
||||
INSTRUMENTATION_SYMBOLS,
|
||||
type ITracer,
|
||||
type ILogger,
|
||||
} from "@repo/core-shared/instrumentation";
|
||||
import { {{camelCase name}}Container } from "./container.js";
|
||||
import { {{constantCase name}}_SYMBOLS } from "./symbols.js";
|
||||
import { Mock{{pascalCase entity}}Repository } from "../infrastructure/repositories/{{kebabCase entity}}.repository.mock.js";
|
||||
import { buildDev{{pascalCase entity}}Map } from "../__seeds__/dev.js";
|
||||
import { get{{pascalCase entity}}UseCase } from "../application/use-cases/get-{{kebabCase entity}}.use-case.js";
|
||||
import { get{{pascalCase entity}}Controller } from "../interface-adapters/controllers/get-{{kebabCase entity}}.controller.js";
|
||||
import type { I{{pascalCase entity}}Repository } from "../application/repositories/{{kebabCase entity}}.repository.interface.js";
|
||||
|
||||
/**
|
||||
* Replace the default mock with a populated one for dev mode + storybook.
|
||||
*
|
||||
* Mutually exclusive with `bindProduction{{pascalCase name}}(config, tracer, logger)`.
|
||||
* Tests must NOT call this — they construct `new Mock{{pascalCase entity}}Repository()`
|
||||
* directly and seed via factories per-test.
|
||||
*
|
||||
* Idempotent: safe to call multiple times; each call rebuilds a fresh
|
||||
* populated repo and rebinds the symbol.
|
||||
*/
|
||||
export async function bindDevSeed{{pascalCase name}}(
|
||||
tracer: ITracer,
|
||||
logger: ILogger,
|
||||
): Promise<void> {
|
||||
// Bind shared instrumentation into feature container
|
||||
if ({{camelCase name}}Container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
|
||||
{{camelCase name}}Container.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
|
||||
}
|
||||
if ({{camelCase name}}Container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
|
||||
{{camelCase name}}Container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
|
||||
}
|
||||
{{camelCase name}}Container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
|
||||
{{camelCase name}}Container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
|
||||
|
||||
if ({{camelCase name}}Container.isBound({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)) {
|
||||
{{camelCase name}}Container.unbind({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository);
|
||||
}
|
||||
const repo = new Mock{{pascalCase entity}}Repository(buildDev{{pascalCase entity}}Map(), tracer, logger);
|
||||
{{camelCase name}}Container
|
||||
.bind<I{{pascalCase entity}}Repository>({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)
|
||||
.toConstantValue(repo);
|
||||
|
||||
// Wrap use case + controller identically to bind-production
|
||||
const wrappedGet{{pascalCase entity}} = withSpan(
|
||||
tracer,
|
||||
{ name: "{{camelCase name}}.get{{pascalCase entity}}", op: "use-case" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "{{kebabCase name}}", layer: "use-case", name: "{{camelCase name}}.get{{pascalCase entity}}" },
|
||||
get{{pascalCase entity}}UseCase(repo),
|
||||
),
|
||||
);
|
||||
|
||||
for (const sym of [
|
||||
{{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase,
|
||||
{{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller,
|
||||
]) {
|
||||
if ({{camelCase name}}Container.isBound(sym)) {{camelCase name}}Container.unbind(sym);
|
||||
}
|
||||
{{camelCase name}}Container
|
||||
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase)
|
||||
.toConstantValue(wrappedGet{{pascalCase entity}});
|
||||
|
||||
{{camelCase name}}Container
|
||||
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller)
|
||||
.toConstantValue(
|
||||
withSpan(
|
||||
tracer,
|
||||
{ name: "{{camelCase name}}.get{{pascalCase entity}}", op: "controller" },
|
||||
withCapture(
|
||||
logger,
|
||||
{ feature: "{{kebabCase name}}", layer: "controller", name: "{{camelCase name}}.get{{pascalCase entity}}" },
|
||||
get{{pascalCase entity}}Controller(wrappedGet{{pascalCase entity}}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user