|
| 1 | +/* Generic JavaScript Web (client-side) wizard using posthog-agent with PostHog MCP */ |
| 2 | +import type { WizardOptions } from '../utils/types'; |
| 3 | +import type { FrameworkConfig } from '../lib/framework-config'; |
| 4 | +import { Integration } from '../lib/constants'; |
| 5 | +import * as fs from 'node:fs'; |
| 6 | +import * as path from 'node:path'; |
| 7 | +import { hasPackageInstalled } from '../utils/package-json'; |
| 8 | +import { tryGetPackageJson } from '../utils/clack-utils'; |
| 9 | +import { |
| 10 | + FRAMEWORK_PACKAGES, |
| 11 | + detectJsPackageManager, |
| 12 | + detectBundler, |
| 13 | + type JavaScriptContext, |
| 14 | +} from './utils'; |
| 15 | +import { detectNodePackageManagers } from '../lib/package-manager-detection'; |
| 16 | + |
| 17 | +export const JAVASCRIPT_WEB_AGENT_CONFIG: FrameworkConfig<JavaScriptContext> = { |
| 18 | + metadata: { |
| 19 | + name: 'JavaScript (Web)', |
| 20 | + integration: Integration.javascript_web, |
| 21 | + beta: true, |
| 22 | + docsUrl: 'https://posthog.com/docs/libraries/js', |
| 23 | + gatherContext: (options: WizardOptions) => { |
| 24 | + const packageManagerName = detectJsPackageManager(options); |
| 25 | + const hasTypeScript = fs.existsSync( |
| 26 | + path.join(options.installDir, 'tsconfig.json'), |
| 27 | + ); |
| 28 | + const hasBundler = detectBundler(options); |
| 29 | + return Promise.resolve({ packageManagerName, hasTypeScript, hasBundler }); |
| 30 | + }, |
| 31 | + }, |
| 32 | + |
| 33 | + detection: { |
| 34 | + packageName: 'posthog-js', |
| 35 | + packageDisplayName: 'JavaScript (Web)', |
| 36 | + usesPackageJson: false, |
| 37 | + getVersion: () => undefined, |
| 38 | + detectPackageManager: detectNodePackageManagers, |
| 39 | + detect: async (options) => { |
| 40 | + const packageJson = await tryGetPackageJson(options); |
| 41 | + if (!packageJson) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + // Exclude projects with known framework packages |
| 46 | + for (const frameworkPkg of FRAMEWORK_PACKAGES) { |
| 47 | + if (hasPackageInstalled(frameworkPkg, packageJson)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Ensure this is actually a JS project, not just a package.json for tooling |
| 53 | + const { installDir } = options; |
| 54 | + |
| 55 | + // Check for a lockfile |
| 56 | + const hasLockfile = [ |
| 57 | + 'package-lock.json', |
| 58 | + 'yarn.lock', |
| 59 | + 'pnpm-lock.yaml', |
| 60 | + 'bun.lockb', |
| 61 | + 'bun.lock', |
| 62 | + ].some((lockfile) => fs.existsSync(path.join(installDir, lockfile))); |
| 63 | + |
| 64 | + if (hasLockfile) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // Fallback: check if package.json has actual dependencies |
| 69 | + const hasDeps = |
| 70 | + (packageJson.dependencies && |
| 71 | + Object.keys(packageJson.dependencies).length > 0) || |
| 72 | + (packageJson.devDependencies && |
| 73 | + Object.keys(packageJson.devDependencies).length > 0); |
| 74 | + |
| 75 | + return !!hasDeps; |
| 76 | + }, |
| 77 | + }, |
| 78 | + |
| 79 | + environment: { |
| 80 | + uploadToHosting: false, |
| 81 | + getEnvVars: (apiKey: string, host: string) => ({ |
| 82 | + POSTHOG_API_KEY: apiKey, |
| 83 | + POSTHOG_HOST: host, |
| 84 | + }), |
| 85 | + }, |
| 86 | + |
| 87 | + analytics: { |
| 88 | + getTags: (context) => { |
| 89 | + const tags: Record<string, string> = { |
| 90 | + packageManager: context.packageManagerName ?? 'unknown', |
| 91 | + }; |
| 92 | + if (context.hasBundler) { |
| 93 | + tags.bundler = context.hasBundler; |
| 94 | + } |
| 95 | + return tags; |
| 96 | + }, |
| 97 | + }, |
| 98 | + |
| 99 | + prompts: { |
| 100 | + projectTypeDetection: |
| 101 | + 'This is a JavaScript/TypeScript project. Look for package.json and lockfiles (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb) to confirm.', |
| 102 | + packageInstallation: |
| 103 | + 'Look for lockfiles to determine the package manager (npm, yarn, pnpm, bun). Do not manually edit package.json.', |
| 104 | + getAdditionalContextLines: (context) => { |
| 105 | + const lines = [ |
| 106 | + `Package manager: ${context.packageManagerName ?? 'unknown'}`, |
| 107 | + `Has TypeScript: ${context.hasTypeScript ? 'yes' : 'no'}`, |
| 108 | + `Framework docs ID: js (use posthog://docs/frameworks/js for documentation if available)`, |
| 109 | + `Project type: Generic JavaScript/TypeScript application (no specific framework detected)`, |
| 110 | + ]; |
| 111 | + |
| 112 | + if (context.hasBundler) { |
| 113 | + lines.unshift(`Bundler: ${context.hasBundler}`); |
| 114 | + } |
| 115 | + |
| 116 | + return lines; |
| 117 | + }, |
| 118 | + }, |
| 119 | + |
| 120 | + ui: { |
| 121 | + successMessage: 'PostHog integration complete', |
| 122 | + estimatedDurationMinutes: 5, |
| 123 | + getOutroChanges: (context) => { |
| 124 | + const packageManagerName = |
| 125 | + context.packageManagerName ?? 'package manager'; |
| 126 | + return [ |
| 127 | + `Analyzed your JavaScript project structure`, |
| 128 | + `Installed the posthog-js package using ${packageManagerName}`, |
| 129 | + `Created PostHog initialization code`, |
| 130 | + `Configured autocapture, error tracking, and event capture`, |
| 131 | + ]; |
| 132 | + }, |
| 133 | + getOutroNextSteps: () => [ |
| 134 | + 'Ensure posthog.init() is called before any capture calls', |
| 135 | + 'Autocapture tracks clicks, form submissions, and pageviews automatically', |
| 136 | + 'Use posthog.capture() for custom events and posthog.identify() for users', |
| 137 | + 'NEVER send PII in event properties (no emails, names, or user content)', |
| 138 | + 'Visit your PostHog dashboard to see incoming events', |
| 139 | + ], |
| 140 | + }, |
| 141 | +}; |
0 commit comments