UNPKG

@posthog/nextjs-config

Version:

NextJS configuration helper for Posthog 🦔

94 lines (93 loc) • 3.54 kB
import { fileURLToPath as __webpack_fileURLToPath__ } from "node:url"; import { dirname as __webpack_dirname__ } from "node:path"; import path from "path"; import fs from "fs"; import { spawn } from "child_process"; import next_package from "next/package.json" with { type: "json" }; import semver from "semver"; var utils_dirname = __webpack_dirname__(__webpack_fileURLToPath__(import.meta.url)); function resolveBinaryPath(envPath, cwd, binName) { const envLocations = envPath.split(path.delimiter); const localLocations = buildLocalBinaryPaths(cwd); const directories = [ ...new Set([ ...localLocations, ...envLocations ]) ]; for (const directory of directories){ const binaryPath = path.join(directory, binName); if (fs.existsSync(binaryPath)) return binaryPath; } throw new Error(`Binary ${binName} not found`); } const buildLocalBinaryPaths = (cwd)=>{ const localPaths = getLocalPaths(path.resolve(cwd)).map((localPath)=>path.join(localPath, 'node_modules/.bin')); return localPaths; }; const getLocalPaths = (startPath)=>{ const paths = []; let currentPath = startPath; while(true){ paths.push(currentPath); const parentPath = path.resolve(currentPath, '..'); if (parentPath === currentPath) break; currentPath = parentPath; } return paths; }; function getNextJsVersion() { return next_package.version; } function hasCompilerHook() { const nextJsVersion = getNextJsVersion(); return semver.gte(nextJsVersion, '15.4.1'); } async function processSourceMaps(posthogOptions, directory) { const cliOptions = []; if (posthogOptions.host) cliOptions.push('--host', posthogOptions.host); cliOptions.push('sourcemap', 'process'); cliOptions.push('--directory', directory); if (posthogOptions.sourcemaps.project) cliOptions.push('--project', posthogOptions.sourcemaps.project); if (posthogOptions.sourcemaps.version) cliOptions.push('--version', posthogOptions.sourcemaps.version); if (posthogOptions.sourcemaps.deleteAfterUpload) cliOptions.push('--delete-after'); const envVars = { ...process.env, POSTHOG_CLI_TOKEN: posthogOptions.personalApiKey, POSTHOG_CLI_ENV_ID: posthogOptions.envId }; await callPosthogCli(cliOptions, envVars, posthogOptions.verbose); } async function callPosthogCli(args, env, verbose) { let binaryLocation; try { var _process_env_PATH; binaryLocation = resolveBinaryPath(null != (_process_env_PATH = process.env.PATH) ? _process_env_PATH : '', utils_dirname, 'posthog-cli'); } catch (e) { throw new Error(`Binary ${e} not found. Make sure postinstall script has been allowed for @posthog/cli`); } if (verbose) console.log('running posthog-cli from ', binaryLocation); const child = spawn(binaryLocation, [ ...args ], { shell: true, stdio: verbose ? 'inherit' : 'ignore', env, cwd: process.cwd() }); await new Promise((resolve, reject)=>{ child.on('close', (code)=>{ if (0 === code) resolve(); else reject(new Error(`Command failed with code ${code}`)); }); child.on('error', (error)=>{ reject(error); }); }); } function isTurbopackEnabled() { return '1' === process.env.TURBOPACK; } export { buildLocalBinaryPaths, getNextJsVersion, hasCompilerHook, isTurbopackEnabled, processSourceMaps, resolveBinaryPath };