|
| 1 | +import { FsFileSystem, GoogleAnalytics, IFileSystem, Util } from "@igniteui/cli-core"; |
| 2 | +import { ArgumentsCamelCase, CommandModule } from "yargs"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +const IGNITEUI_SERVER_KEY = "igniteui-cli"; |
| 6 | +const IGNITEUI_THEMING_SERVER_KEY = "igniteui-theming"; |
| 7 | + |
| 8 | +const igniteuiServer = { |
| 9 | + command: "npx", |
| 10 | + args: ["-y", "igniteui-cli@next", "mcp"] |
| 11 | +}; |
| 12 | + |
| 13 | +const igniteuiThemingServer = { |
| 14 | + command: "npx", |
| 15 | + args: ["-y", "igniteui-theming", "igniteui-theming-mcp"] |
| 16 | +}; |
| 17 | + |
| 18 | +interface McpServerEntry { |
| 19 | + command: string; |
| 20 | + args: string[]; |
| 21 | +} |
| 22 | + |
| 23 | +interface VsCodeMcpConfig { |
| 24 | + servers: Record<string, McpServerEntry>; |
| 25 | +} |
| 26 | + |
| 27 | +function getConfigPath(): string { |
| 28 | + return path.join(process.cwd(), ".vscode", "mcp.json"); |
| 29 | +} |
| 30 | + |
| 31 | +function readJson<T>(filePath: string, fallback: T, fileSystem: IFileSystem): T { |
| 32 | + try { |
| 33 | + return JSON.parse(fileSystem.readFile(filePath)) as T; |
| 34 | + } catch { |
| 35 | + return fallback; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function writeJson(filePath: string, data: unknown, fileSystem: IFileSystem): void { |
| 40 | + fileSystem.writeFile(filePath, JSON.stringify(data, null, 2) + "\n"); |
| 41 | +} |
| 42 | + |
| 43 | +export function configureMCP(fileSystem: IFileSystem = new FsFileSystem()): void { |
| 44 | + const configPath = getConfigPath(); |
| 45 | + const config = readJson<VsCodeMcpConfig>(configPath, { servers: {} }, fileSystem); |
| 46 | + config.servers = config.servers || {}; |
| 47 | + |
| 48 | + let modified = false; |
| 49 | + if (!config.servers[IGNITEUI_SERVER_KEY]) { |
| 50 | + config.servers[IGNITEUI_SERVER_KEY] = igniteuiServer; |
| 51 | + modified = true; |
| 52 | + } |
| 53 | + if (!config.servers[IGNITEUI_THEMING_SERVER_KEY]) { |
| 54 | + config.servers[IGNITEUI_THEMING_SERVER_KEY] = igniteuiThemingServer; |
| 55 | + modified = true; |
| 56 | + } |
| 57 | + |
| 58 | + if (!modified) { |
| 59 | + Util.log(` Ignite UI MCP servers already configured in ${configPath}`); |
| 60 | + return; |
| 61 | + } |
| 62 | + writeJson(configPath, config, fileSystem); |
| 63 | + Util.log(Util.greenCheck() + ` MCP servers configured in ${configPath}`); |
| 64 | +} |
| 65 | + |
| 66 | +export function configure(fileSystem: IFileSystem = new FsFileSystem()): void { |
| 67 | + configureMCP(fileSystem); |
| 68 | +} |
| 69 | + |
| 70 | +const command: CommandModule = { |
| 71 | + command: "ai-config", |
| 72 | + describe: "Configure Ignite UI AI tooling (MCP servers)", |
| 73 | + builder: (yargs) => yargs.usage(""), |
| 74 | + async handler(_argv: ArgumentsCamelCase) { |
| 75 | + GoogleAnalytics.post({ |
| 76 | + t: "screenview", |
| 77 | + cd: "MCP" |
| 78 | + }); |
| 79 | + |
| 80 | + GoogleAnalytics.post({ |
| 81 | + t: "event", |
| 82 | + ec: "$ig ai-config", |
| 83 | + ea: "client: vscode" |
| 84 | + }); |
| 85 | + |
| 86 | + configure(); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +export default command; |
0 commit comments