|
| 1 | +import * as fs from 'fs'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import path from 'path'; |
| 4 | +import { Integration } from '../../lib/constants'; |
| 5 | +import { analytics } from '../analytics'; |
| 6 | +import clack from '../clack'; |
| 7 | +import { traceStep } from '../../telemetry'; |
| 8 | +import { abortIfCancelled } from '../clack-utils'; |
| 9 | + |
| 10 | +type AddEditorRulesOptions = { |
| 11 | + installDir: string; |
| 12 | + rulesName: string; |
| 13 | + integration: Integration; |
| 14 | + default?: boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +export const addEditorRules = async ({ |
| 18 | + installDir, |
| 19 | + rulesName, |
| 20 | + integration, |
| 21 | + default: defaultAddEditorRules, |
| 22 | +}: AddEditorRulesOptions) => { |
| 23 | + // Add rules file if in Cursor environment |
| 24 | + if (process.env.CURSOR_TRACE_ID) { |
| 25 | + const addEditorRules: boolean = defaultAddEditorRules |
| 26 | + ? true |
| 27 | + : await abortIfCancelled( |
| 28 | + clack.select({ |
| 29 | + message: |
| 30 | + 'Would you like to have PostHog added to your Cursor rules?', |
| 31 | + options: [ |
| 32 | + { |
| 33 | + label: 'Yes, please!', |
| 34 | + value: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + label: 'No, thanks', |
| 38 | + value: false, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }), |
| 42 | + ); |
| 43 | + |
| 44 | + if (!addEditorRules) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + return traceStep('add-editor-rules', async () => { |
| 49 | + const docsDir = path.join(installDir, '.cursor', 'rules'); |
| 50 | + |
| 51 | + await fs.promises.mkdir(docsDir, { recursive: true }); |
| 52 | + |
| 53 | + const frameworkRules = await fs.promises.readFile( |
| 54 | + path.join(__dirname, rulesName), |
| 55 | + 'utf8', |
| 56 | + ); |
| 57 | + const universalRulesPath = path.join(__dirname, 'universal.md'); |
| 58 | + |
| 59 | + const universalRules = await fs.promises.readFile( |
| 60 | + universalRulesPath, |
| 61 | + 'utf8', |
| 62 | + ); |
| 63 | + |
| 64 | + // Replace {universal} placeholder with universal rules content |
| 65 | + const combinedRules = frameworkRules.replace( |
| 66 | + '{universal}', |
| 67 | + universalRules, |
| 68 | + ); |
| 69 | + const targetPath = path.join(docsDir, 'posthog-integration.mdc'); |
| 70 | + |
| 71 | + // Write the combined rules |
| 72 | + await fs.promises.writeFile(targetPath, combinedRules, 'utf8'); |
| 73 | + |
| 74 | + analytics.capture('wizard interaction', { |
| 75 | + action: 'added editor rules', |
| 76 | + integration, |
| 77 | + }); |
| 78 | + |
| 79 | + clack.log.info(`Added Cursor rules to ${chalk.bold.cyan(docsDir)}`); |
| 80 | + }); |
| 81 | + } |
| 82 | +}; |
0 commit comments