UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

137 lines 5.88 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runNextjsWizardAgent = runNextjsWizardAgent; const debug_1 = require("../utils/debug"); const agent_runner_1 = require("../lib/agent-runner"); const constants_1 = require("../lib/constants"); const package_json_1 = require("../utils/package-json"); const clack_utils_1 = require("../utils/clack-utils"); const clack_1 = __importDefault(require("../utils/clack")); const chalk_1 = __importDefault(require("chalk")); const semver = __importStar(require("semver")); const utils_1 = require("./utils"); /** * Next.js framework configuration for the universal agent runner. */ const MINIMUM_NEXTJS_VERSION = '15.3.0'; const NEXTJS_AGENT_CONFIG = { metadata: { name: 'Next.js', integration: constants_1.Integration.nextjs, docsUrl: 'https://posthog.com/docs/libraries/next-js', unsupportedVersionDocsUrl: 'https://posthog.com/docs/libraries/next-js', gatherContext: async (options) => { const router = await (0, utils_1.getNextJsRouter)(options); return { router }; }, }, detection: { packageName: 'next', packageDisplayName: 'Next.js', getVersion: (packageJson) => (0, package_json_1.getPackageVersion)('next', packageJson), getVersionBucket: utils_1.getNextJsVersionBucket, }, environment: { uploadToHosting: true, getEnvVars: (apiKey, host) => ({ NEXT_PUBLIC_POSTHOG_KEY: apiKey, NEXT_PUBLIC_POSTHOG_HOST: host, }), }, analytics: { getTags: (context) => { const router = context.router; return { router: router === utils_1.NextJsRouter.APP_ROUTER ? 'app' : 'pages', }; }, }, prompts: { projectTypeDetection: 'This is a JavaScript/TypeScript project. Look for package.json and lockfiles (package-lock.json, yarn.lock, pnpm-lock.yaml, bun.lockb) to confirm.', packageInstallation: 'Look for lockfiles to determine the package manager (npm, yarn, pnpm, bun). Do not manually edit package.json.', getAdditionalContextLines: (context) => { const router = context.router; const routerType = router === utils_1.NextJsRouter.APP_ROUTER ? 'app' : 'pages'; return [`Router: ${routerType}`]; }, }, ui: { successMessage: 'PostHog integration complete', estimatedDurationMinutes: 8, getOutroChanges: (context) => { const router = context.router; const routerName = (0, utils_1.getNextJsRouterName)(router); return [ `Analyzed your Next.js project structure (${routerName})`, `Created and configured PostHog initializers`, `Integrated PostHog into your application`, ]; }, getOutroNextSteps: () => { return [ 'Start your development server to see PostHog in action', 'Visit your PostHog dashboard to see incoming events', ]; }, }, }; /** * Next.js wizard powered by the universal agent runner. */ async function runNextjsWizardAgent(options) { if (options.debug) { (0, debug_1.enableDebugLogs)(); } // Check Next.js version - agent wizard requires >= 15.3.0 const packageJson = await (0, clack_utils_1.getPackageDotJson)(options); const nextVersion = (0, package_json_1.getPackageVersion)('next', packageJson); if (nextVersion) { const coercedVersion = semver.coerce(nextVersion); if (coercedVersion && semver.lt(coercedVersion, MINIMUM_NEXTJS_VERSION)) { const docsUrl = NEXTJS_AGENT_CONFIG.metadata.unsupportedVersionDocsUrl ?? NEXTJS_AGENT_CONFIG.metadata.docsUrl; clack_1.default.log.warn(`Sorry: the wizard can't help you with Next.js ${nextVersion}. Upgrade to Next.js ${MINIMUM_NEXTJS_VERSION} or later, or check out the manual setup guide.`); clack_1.default.log.info(`Setup Next.js manually: ${chalk_1.default.cyan(docsUrl)}`); clack_1.default.outro('PostHog wizard will see you next time!'); return; } } await (0, agent_runner_1.runAgentWizard)(NEXTJS_AGENT_CONFIG, options); } //# sourceMappingURL=nextjs-wizard-agent.js.map