extractTrigger
Extracts the first useEffect(() => { ... }, []) in a component into a dedicated *.trigger.ts file. The original component is rewritten to fire the new event instead of running the effect inline. Provided as both a programmatic API (extractTrigger({ ... })) and a CLI (triggery-codemod extract-trigger).
This is a pragmatic V1 — it handles the common shape of a single useEffect with a clear side-effect body. Complex effects (cleanup functions, multiple effects in one file, dynamic deps) need a manual follow-up; the codemod stops at the first match and reports what it did.
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Param | Type | Required | Description |
|---|---|---|---|
file | string | yes | Path to the .tsx (or .ts) source. |
name | string | yes | Trigger id in kebab-case. Used to derive the symbol name (newMessageTrigger) and file name (new-message.trigger.ts). |
outDir | string | no | Directory for the generated trigger file. Defaults to the source file’s directory. |
dryRun | boolean | no | Plan the changes and return them without writing to disk. |
project | ts-morph.Project | no | Pre-existing project. Lets the CLI reuse one tsconfig across batches. |
Returns
Section titled “Returns”| Field | Type | Description |
|---|---|---|
sourceUpdated | boolean | Whether the original file was rewritten. |
triggerFilePath | string | Absolute path of the generated *.trigger.ts file. |
triggerFileContent | string | Contents of the new trigger file. |
originalEffectBody | string | The body that was extracted, unindented. |
| Flag | Required | Description |
|---|---|---|
--name | yes | kebab-case trigger id. |
--out-dir | no | Override the default output directory. |
--dry-run | no | Print planned changes without writing. |
The CLI prints the generated path and a one-line hint with the import you need to add to the component file (the codemod deliberately doesn’t edit imports — keeps AST changes minimal and predictable).
Examples
Section titled “Examples”Programmatic — dry run
Section titled “Programmatic — dry run”Programmatic — write to a dedicated triggers folder
Section titled “Programmatic — write to a dedicated triggers folder”Output:
What gets generated
Section titled “What gets generated”The generated file is a starter — the codemod cannot infer the runtime “events / conditions / actions” surface without your input. It produces a void-payload event and Record<string, never> for conditions/actions; you flesh out the schema by hand.