|
| 1 | +import { |
| 2 | + CreatePlan, |
| 3 | + DestroyPlan, |
| 4 | + ExampleConfig, |
| 5 | + FileUtils, |
| 6 | + Resource, |
| 7 | + ResourceSettings, |
| 8 | + SpawnStatus, |
| 9 | + Utils, |
| 10 | + getPty, |
| 11 | + z, |
| 12 | +} from '@codifycli/plugin-core'; |
| 13 | +import { OS } from '@codifycli/schemas'; |
| 14 | +import fs from 'node:fs/promises'; |
| 15 | +import os from 'node:os'; |
| 16 | +import path from 'node:path'; |
| 17 | + |
| 18 | +import { ExtensionsParameter } from './extensions-parameter.js'; |
| 19 | +import { McpServersParameter } from './mcp-servers-parameter.js'; |
| 20 | +import { SettingsParameter } from './settings-parameter.js'; |
| 21 | + |
| 22 | +export const CURSOR_APPLICATION_NAME = 'Cursor.app'; |
| 23 | +export const CURSOR_LOCAL_BIN = path.join(os.homedir(), '.local', 'bin'); |
| 24 | +const CURSOR_LOCAL_BIN_EXPORT = `export PATH="${CURSOR_LOCAL_BIN}:$PATH"`; |
| 25 | + |
| 26 | +export const mcpServerSchema = z.object({ |
| 27 | + command: z.string().optional(), |
| 28 | + args: z.array(z.string()).optional(), |
| 29 | + env: z.record(z.string(), z.string()).optional(), |
| 30 | + url: z.string().optional(), |
| 31 | +}); |
| 32 | +export type McpServer = z.infer<typeof mcpServerSchema>; |
| 33 | +export type McpServers = Record<string, McpServer>; |
| 34 | + |
| 35 | +const schema = z.object({ |
| 36 | + directory: z |
| 37 | + .string() |
| 38 | + .describe('Installation directory. Defaults to /Applications on macOS, ~/.local/bin on Linux.') |
| 39 | + .optional(), |
| 40 | + extensions: z |
| 41 | + .array(z.string()) |
| 42 | + .describe('Cursor extensions to install, e.g. ["ms-python.python", "eamodio.gitlens"].') |
| 43 | + .optional(), |
| 44 | + settings: z |
| 45 | + .record(z.string(), z.unknown()) |
| 46 | + .describe('Cursor editor settings to merge into settings.json.') |
| 47 | + .optional(), |
| 48 | + mcpServers: z |
| 49 | + .record(z.string(), mcpServerSchema) |
| 50 | + .describe('MCP servers to configure in ~/.cursor/mcp.json.') |
| 51 | + .optional(), |
| 52 | +}); |
| 53 | + |
| 54 | +export type CursorConfig = z.infer<typeof schema>; |
| 55 | + |
| 56 | +const defaultConfig: Partial<CursorConfig> = { |
| 57 | + extensions: [], |
| 58 | +}; |
| 59 | + |
| 60 | +const exampleAi: ExampleConfig = { |
| 61 | + title: 'AI-powered development setup', |
| 62 | + description: 'Install Cursor with popular development extensions and editor settings for productive AI-assisted coding.', |
| 63 | + configs: [{ |
| 64 | + type: 'cursor', |
| 65 | + extensions: ['ms-python.python', 'eamodio.gitlens', 'esbenp.prettier-vscode'], |
| 66 | + settings: { |
| 67 | + 'editor.fontSize': 14, |
| 68 | + 'editor.formatOnSave': true, |
| 69 | + 'editor.tabSize': 2, |
| 70 | + }, |
| 71 | + }], |
| 72 | +}; |
| 73 | + |
| 74 | +const exampleWithMcp: ExampleConfig = { |
| 75 | + title: 'Cursor with MCP servers', |
| 76 | + description: 'Configure Cursor with MCP servers for extended AI capabilities including filesystem and GitHub access.', |
| 77 | + configs: [{ |
| 78 | + type: 'cursor', |
| 79 | + extensions: ['ms-python.python', 'eamodio.gitlens'], |
| 80 | + mcpServers: { |
| 81 | + filesystem: { |
| 82 | + command: 'npx', |
| 83 | + args: ['-y', '@modelcontextprotocol/server-filesystem', '/home/user/projects'], |
| 84 | + }, |
| 85 | + github: { |
| 86 | + command: 'npx', |
| 87 | + args: ['-y', '@modelcontextprotocol/server-github'], |
| 88 | + env: { GITHUB_PERSONAL_ACCESS_TOKEN: '<Replace me here!' }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }], |
| 92 | +}; |
| 93 | + |
| 94 | +export class CursorResource extends Resource<CursorConfig> { |
| 95 | + getSettings(): ResourceSettings<CursorConfig> { |
| 96 | + return { |
| 97 | + id: 'cursor', |
| 98 | + operatingSystems: [OS.Darwin, OS.Linux], |
| 99 | + schema, |
| 100 | + defaultConfig, |
| 101 | + exampleConfigs: { |
| 102 | + example1: exampleAi, |
| 103 | + example2: exampleWithMcp, |
| 104 | + }, |
| 105 | + parameterSettings: { |
| 106 | + directory: { |
| 107 | + type: 'directory', |
| 108 | + default: Utils.isMacOS() ? '/Applications' : CURSOR_LOCAL_BIN, |
| 109 | + }, |
| 110 | + extensions: { type: 'stateful', definition: new ExtensionsParameter(), order: 1 }, |
| 111 | + settings: { type: 'stateful', definition: new SettingsParameter(), order: 2 }, |
| 112 | + mcpServers: { type: 'stateful', definition: new McpServersParameter(), order: 3 }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + override async refresh(parameters: Partial<CursorConfig>): Promise<Partial<CursorConfig> | null> { |
| 118 | + const isInstalled = await this.isCursorInstalled(parameters.directory); |
| 119 | + return isInstalled ? parameters : null; |
| 120 | + } |
| 121 | + |
| 122 | + override async create(plan: CreatePlan<CursorConfig>): Promise<void> { |
| 123 | + if (Utils.isMacOS()) { |
| 124 | + await this.installMacOS(); |
| 125 | + } else if (Utils.isLinux()) { |
| 126 | + await this.installLinux(plan); |
| 127 | + } else { |
| 128 | + throw new Error('Unsupported operating system'); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + override async destroy(plan: DestroyPlan<CursorConfig>): Promise<void> { |
| 133 | + const $ = getPty(); |
| 134 | + |
| 135 | + if (Utils.isMacOS()) { |
| 136 | + const directory = plan.currentConfig.directory ?? '/Applications'; |
| 137 | + await $.spawn(`rm -rf "${path.join(directory, CURSOR_APPLICATION_NAME)}"`); |
| 138 | + } else if (Utils.isLinux()) { |
| 139 | + const directory = plan.currentConfig.directory ?? CURSOR_LOCAL_BIN; |
| 140 | + await $.spawnSafe(`rm -f "${path.join(directory, 'cursor')}"`); |
| 141 | + await FileUtils.removeLineFromShellRc(CURSOR_LOCAL_BIN_EXPORT); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private async isCursorInstalled(directory?: string | null): Promise<boolean> { |
| 146 | + if (Utils.isMacOS()) { |
| 147 | + try { |
| 148 | + const files = await fs.readdir(directory ?? '/Applications'); |
| 149 | + return files.includes(CURSOR_APPLICATION_NAME); |
| 150 | + } catch { |
| 151 | + return false; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (Utils.isLinux()) { |
| 156 | + const $ = getPty(); |
| 157 | + const result = await $.spawnSafe('which cursor'); |
| 158 | + return result.status === SpawnStatus.SUCCESS; |
| 159 | + } |
| 160 | + |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + private async installMacOS(): Promise<void> { |
| 165 | + const $ = getPty(); |
| 166 | + await $.spawn('brew install --cask cursor', { interactive: true }); |
| 167 | + } |
| 168 | + |
| 169 | + private async installLinux(plan: CreatePlan<CursorConfig>): Promise<void> { |
| 170 | + const $ = getPty(); |
| 171 | + const isArm = await Utils.isArmArch(); |
| 172 | + const downloadUrl = `https://downloader.cursor.sh/linux/appImage/${isArm ? 'arm64' : 'x64'}`; |
| 173 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'cursor-')); |
| 174 | + const tmpAppImage = path.join(tmpDir, 'cursor.AppImage'); |
| 175 | + |
| 176 | + try { |
| 177 | + await FileUtils.downloadFile(downloadUrl, tmpAppImage); |
| 178 | + const destDir = plan.desiredConfig.directory ?? CURSOR_LOCAL_BIN; |
| 179 | + await fs.mkdir(destDir, { recursive: true }); |
| 180 | + const destPath = path.join(destDir, 'cursor'); |
| 181 | + await fs.rename(tmpAppImage, destPath); |
| 182 | + await $.spawn(`chmod +x "${destPath}"`); |
| 183 | + } finally { |
| 184 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 185 | + } |
| 186 | + |
| 187 | + await FileUtils.addToShellRc(CURSOR_LOCAL_BIN_EXPORT); |
| 188 | + } |
| 189 | +} |
0 commit comments