Skip to content
← Public packages

@kentcdodds/codemod-runner

Run codemods over your own saved packages: scan, dry-run with real publish checks and diffs, apply, and revert.

src/events.ts

43 lines · 1.2 KB · TypeScript
import { events, packageContext } from 'kody:runtime'

/**
 * Best-effort scoped event dispatch. The topic scope is derived from the
 * running package's own name so forks emit under their own scope. Dispatch
 * failures never fail a run.
 */
export async function dispatchCodemodEvent(input: {
	kind: 'applied' | 'reverted'
	runId: string
	codemod: {
		kodyId: string
		specifier?: string
		exportName: string
	} | null
	packageId: string
	kodyId: string
	changedPaths: Array<string>
	beforeCommit: string | null
	afterCommit: string | null
}): Promise<void> {
	try {
		const name = (packageContext as { name?: string } | null)?.name
		const scope = name?.startsWith('@') ? name.split('/')[0] : null
		if (!scope || !events) return
		const topic = `${scope}/codemod.${input.kind}`
		await events.dispatch({
			topic,
			idempotencyKey: `codemod-runner:${input.runId}:${input.packageId}:${topic}`,
			payload: {
				run_id: input.runId,
				codemod: input.codemod,
				package_id: input.packageId,
				kody_id: input.kodyId,
				changed_paths: input.changedPaths,
				before_commit: input.beforeCommit,
				after_commit: input.afterCommit,
			},
		})
	} catch {
		// Best-effort only.
	}
}