Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(frontend): add single-file kea typegen wrapper (#66267)

Marius Andra37b390c81ee1
diff --git a/docs/published/handbook/engineering/manual-dev-setup.md b/docs/published/handbook/engineering/manual-dev-setup.md
index ebd9be09b558..79c4f5914121 100644
--- a/docs/published/handbook/engineering/manual-dev-setup.md
+++ b/docs/published/handbook/engineering/manual-dev-setup.md
@@ -132,6 +132,9 @@ On Linux you often have separate packages: `postgres` for the tools, `postgres-s
5. Run `pnpm --filter=@posthog/frontend typegen:write` to generate types for [Kea](https://keajs.org/) state management logics used all over the frontend.
+When iterating on one logic file, run `pnpm --filter=@posthog/frontend typegen:file <path-to-logic-file>` instead.
+The path can be absolute, repo-relative like `frontend/src/scenes/foo/fooLogic.ts`, or frontend-relative like `src/scenes/foo/fooLogic.ts`.
+
> The first time you run typegen, it may get stuck in a loop. If so, cancel the process (`Ctrl+C`), discard all changes in the working directory (`git reset --hard`), and run `pnpm typegen:write` again. You may need to discard all changes once more when the second round of type generation completes.
## 3. Prepare nodejs services
diff --git a/frontend/bin/typegen-file.mjs b/frontend/bin/typegen-file.mjs
new file mode 100644
index 000000000000..63a6ada620aa
--- /dev/null
+++ b/frontend/bin/typegen-file.mjs
@@ -0,0 +1,109 @@
+#!/usr/bin/env node
+import { spawnSync } from 'child_process'
+import fs from 'fs'
+import path from 'path'
+import { fileURLToPath } from 'url'
+
+const scriptPath = fileURLToPath(import.meta.url)
+const frontendDir = path.resolve(path.dirname(scriptPath), '..')
+const repoRoot = path.resolve(frontendDir, '..')
+const localKeaTypegenBin = path.resolve(
+ frontendDir,
+ 'node_modules',
+ '.bin',
+ process.platform === 'win32' ? 'kea-typegen.cmd' : 'kea-typegen'
+)
+
+const { fileArg, passthroughArgs } = parseArgs(process.argv.slice(2))
+
+if (!fileArg) {
+ console.error('Usage: pnpm --filter=@posthog/frontend typegen:file <logic-file> [kea-typegen options]')
+ process.exit(1)
+}
+
+const resolvedFile = resolveFile(fileArg)
+
+if (!resolvedFile) {
+ console.error(`Could not find logic file: ${fileArg}`)
+ console.error('Checked paths relative to the current directory, pnpm INIT_CWD, frontend/, and the repo root.')
+ process.exit(1)
+}
+
+const nodeOptions = process.env.NODE_OPTIONS?.includes('--max-old-space-size')
+ ? process.env.NODE_OPTIONS
+ : [process.env.NODE_OPTIONS, '--max-old-space-size=4096'].filter(Boolean).join(' ')
+
+const result = spawnSync(
+ fs.existsSync(localKeaTypegenBin) ? localKeaTypegenBin : 'kea-typegen',
+ ['write', '--show-ts-errors', '--file', resolvedFile, ...passthroughArgs],
+ {
+ cwd: repoRoot,
+ env: {
+ ...process.env,
+ NODE_OPTIONS: nodeOptions,
+ },
+ stdio: 'inherit',
+ }
+)
+
+if (result.error) {
+ console.error(`Failed to run kea-typegen: ${result.error.message}`)
+ process.exit(1)
+}
+
+process.exit(result.status ?? 1)
+
+function parseArgs(args) {
+ let fileArg
+ const passthroughArgs = []
+
+ for (let index = 0; index < args.length; index++) {
+ const arg = args[index]
+
+ if (arg === '--file' || arg === '-f') {
+ fileArg = args[index + 1]
+ index += 1
+ continue
+ }
+
+ if (arg.startsWith('--file=')) {
+ fileArg = arg.slice('--file='.length)
+ continue
+ }
+
+ if (arg.startsWith('-f=')) {
+ fileArg = arg.slice('-f='.length)
+ continue
+ }
+
+ if (!fileArg && !arg.startsWith('-')) {
+ fileArg = arg
+ continue
+ }
+
+ passthroughArgs.push(arg)
+ }
+
+ return { fileArg, passthroughArgs }
+}
+
+function resolveFile(filePath) {
+ const candidates = (
+ path.isAbsolute(filePath)
+ ? [filePath]
+ : [
+ path.resolve(process.cwd(), filePath),
+ process.env.INIT_CWD && path.resolve(process.env.INIT_CWD, filePath),
+ path.resolve(frontendDir, filePath),
+ path.resolve(repoRoot, filePath),
+ ]
+ ).filter(Boolean)
+
+ for (const candidate of candidates) {
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
+ return candidate
+ }
+ }
+
+ return undefined
+}
diff --git a/frontend/package.json b/frontend/package.json
index b8222ab8962c..90652afac117 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -30,7 +30,7 @@
"openapi:types": "node ./bin/generate-openapi-types.mjs",
"typegen:check": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen check",
"typegen:clean": "cd .. && find frontend/src products common -type f -name '*Type.ts' -delete",
- "typegen:file": "cd .. && NODE_OPTIONS=--max-old-space-size=4096 kea-typegen write --show-ts-errors --file",
+ "typegen:file": "node ./bin/typegen-file.mjs",
"typegen:watch": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen watch --delete --show-ts-errors --use-cache",
"typegen:write": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen write --delete --show-ts-errors --use-cache",
"typegen:write:no-cache": "cd .. && NODE_OPTIONS=--max-old-space-size=8192 kea-typegen write --delete --show-ts-errors",
@@ -321,7 +321,7 @@
"jest-canvas-mock": "^2.4.0",
"jest-environment-jsdom": "^29.3.1",
"jest-image-snapshot": "^6.1.0",
- "kea-typegen": "3.7.0",
+ "kea-typegen": "3.7.1",
"less": "^3.12.2",
"lint-staged": "catalog:",
"mockdate": "^3.0.5",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 596be5681f13..24033dda2915 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1352,8 +1352,8 @@ importers:
specifier: ^6.1.0
kea-typegen:
- specifier: 3.7.0
- version: 3.7.0([email protected])
+ specifier: 3.7.1
+ version: 3.7.1([email protected])
less:
specifier: ^3.12.2
version: 3.13.1
@@ -13534,9 +13534,6 @@ packages:
resolution: {integrity: sha512-NfJ4UzBCcQGLDlQq7nHxH+tv3kyZ0hHQqF5BO6J7tNJeP5do1llPr8dZ8zHonfhAu0PHAdMkSo+8o0wxg9lZWw==}
engines: {node: '>=0.8'}
- resolution: {integrity: sha512-eLHpSK/Y4nhMJ07gDaAzoX/XAKS8PSaojml3M0DM4JpV1LAi5JOJ/p6H/XWrl8L+DzVEvVCW1z3vWAaB9oTsQw==}
-
resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==}
engines: {node: '>=12'}
@@ -16777,10 +16774,6 @@ packages:
resolution: {integrity: sha512-1Qed0/Hr2m+YqxnM09CjA2d/i6YZNfF6R2oRAOj36eUdS6qIV/huPJNSEpKbupewFs+ZsJlxsjjPbc0/afW6Lw==}
engines: {node: '>= 0.4'}
- resolution: {integrity: sha512-E+zBKpQ2t6MEo1VsonYmluk9NxGrbzpeeLC2xIViuO2EjU2xsXsBPwTr3Ykv9l08UYEVEdWeRZNouaZqF6RN0w==}
- engines: {node: '>= 0.4'}
-
resolution: {integrity: sha512-dqJvarLawXsFbNDeJW7zAz8ItJ9cd28YufuuFzh0G8pNHjJMnY08Dv7sYX2uF5UpQOwieAeOExEYAWWfu7ZZUA==}
engines: {node: '>= 0.4'}
@@ -17648,8 +17641,8 @@ packages:
peerDependencies:
kea: '>= 3'
- resolution: {integrity: sha512-kBVxLoD3KbEzd5EkAZ+CFnQvMaZrBeC7/7kRu/qN/Nwp5SmZM/Xgp9ziuXTAK2ynbmm5VhQ+FmGjN+UY3YBwfg==}
+ resolution: {integrity: sha512-hQuV38I9OyCui/0HviWsgblabeN6iE8t2sOCF8cmoAd1Zk5nR3/5kkaepQGvkaqgwBjy8XcWzVgkCimE8jExoQ==}
hasBin: true
peerDependencies:
typescript: 6.0.3
@@ -20605,10 +20598,6 @@ packages:
resolution: {integrity: sha512-YTUo+Flmw4ZXiWfQKGcwwc11KnoRAYgzAE2E7mXKCjSviTKShtxBsN6YUUBB2gtaBzKzeKunxhUwNHQuRryhWA==}
engines: {node: '>= 4'}
- resolution: {integrity: sha512-qtEDqIZGVcSZCHniWwZWbRy79Dc6Wp3kT/UmDA2RJKBPg7+7k51aQBZirHmUGn5uvHf2rg8DkjizrN26k61ATw==}
- engines: {node: '>= 4'}
-
resolution: {integrity: sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ==}
engines: {node: '>= 10.13.0'}
@@ -34612,14 +34601,6 @@ snapshots:
- dependencies:
- call-bind: 1.0.8
- is-nan: 1.3.2
- object-is: 1.1.5
- object.assign: 4.1.7
- util: 0.12.5
-
@@ -38583,11 +38564,6 @@ snapshots:
- dependencies:
- call-bind: 1.0.8
- define-properties: 1.2.1
-
@@ -40551,13 +40527,13 @@ snapshots:
kea: 4.0.0-pre.5([email protected])
dependencies:
'@babel/core': 7.29.0
'@babel/preset-env': 7.23.5(@babel/[email protected])
'@babel/preset-typescript': 7.23.3(@babel/[email protected])
prettier: 3.6.2
- recast: 0.23.4
+ recast: 0.23.11
ts-clone-node: 3.0.0([email protected])
typescript: 6.0.3
yargs: 16.2.0
@@ -44254,14 +44230,6 @@ snapshots:
tiny-invariant: 1.3.3
tslib: 2.8.1
- dependencies:
- assert: 2.1.0
- ast-types: 0.16.1
- esprima: 4.0.1
- source-map: 0.6.1
- tslib: 2.8.1
-
dependencies:
resolve: 1.22.8