|
1 | 1 | import * as ts from "typescript"; |
2 | 2 | import { DependencyNotFoundException } from "@angular-devkit/core"; |
3 | 3 | import { chain, FileDoesNotExistException, Rule, SchematicContext, Tree } from "@angular-devkit/schematics"; |
4 | | -import * as jsonc from "jsonc-parser"; |
5 | | -import { addClassToBody, App, copyAISkillsToProject, FormatSettings, NPM_ANGULAR, resolvePackage, TEMPLATE_MANAGER, TypeScriptAstTransformer, TypeScriptUtils } from "@igniteui/cli-core"; |
| 4 | +import { addClassToBody, addMcpServers, App, copyAISkillsToProject, FormatSettings, McpServerEntry, NPM_ANGULAR, resolvePackage, TEMPLATE_MANAGER, TypeScriptAstTransformer, TypeScriptUtils, VS_CODE_MCP_PATH } from "@igniteui/cli-core"; |
6 | 5 | import { AngularTypeScriptFileUpdate } from "@igniteui/angular-templates"; |
7 | 6 | import { createCliConfig } from "../utils/cli-config"; |
8 | 7 | import { setVirtual } from "../utils/NgFileSystem"; |
@@ -119,72 +118,47 @@ function importStyles(): Rule { |
119 | 118 | }; |
120 | 119 | } |
121 | 120 |
|
122 | | -export function addAIConfig(): Rule { |
| 121 | +/** Initialize the App container with TemplateManager and virtual FS */ |
| 122 | +function appInit(tree: Tree) { |
| 123 | + App.initialize("angular-cli"); |
| 124 | + // must be initialized with physical fs first: |
| 125 | + App.container.set(TEMPLATE_MANAGER, new SchematicsTemplateManager()); |
| 126 | + setVirtual(tree); |
| 127 | +} |
| 128 | + |
| 129 | +function aiConfig({ init } = { init: true }): Rule { |
123 | 130 | return (tree: Tree) => { |
| 131 | + if (init) { |
| 132 | + appInit(tree); |
| 133 | + } |
124 | 134 | copyAISkillsToProject(); |
125 | 135 |
|
126 | | - const mcpFilePath = "/.vscode/mcp.json"; |
127 | | - const angularCliServer = { |
128 | | - command: "npx", |
129 | | - args: ["-y", "@angular/cli", "mcp"] |
130 | | - }; |
131 | | - const igniteuiServer = { |
132 | | - command: "npx", |
133 | | - args: ["-y", "igniteui-cli@next", "mcp"] |
134 | | - }; |
135 | | - const igniteuiThemingServer = { |
136 | | - command: "npx", |
137 | | - args: ["-y", "igniteui-theming", "igniteui-theming-mcp"] |
| 136 | + const angularCliServer: Record<string, McpServerEntry> = { |
| 137 | + "angular-cli": { |
| 138 | + command: "npx", |
| 139 | + args: ["-y", "@angular/cli", "mcp"] |
| 140 | + } |
138 | 141 | }; |
139 | 142 |
|
140 | | - if (tree.exists(mcpFilePath)) { |
141 | | - let text = tree.read(mcpFilePath)!.toString(); |
142 | | - const content = jsonc.parse(text); |
143 | | - const servers = content.servers ?? {}; |
144 | | - const formattingOptions: jsonc.FormattingOptions = { tabSize: 2, insertSpaces: true }; |
145 | | - const newServers: Record<string, object> = {}; |
146 | | - if (!servers["angular-cli"]) { |
147 | | - newServers["angular-cli"] = angularCliServer; |
148 | | - } |
149 | | - if (!servers["igniteui-cli"]) { |
150 | | - newServers["igniteui-cli"] = igniteuiServer; |
151 | | - } |
152 | | - if (!servers["igniteui-theming"]) { |
153 | | - newServers["igniteui-theming"] = igniteuiThemingServer; |
154 | | - } |
155 | | - for (const [key, value] of Object.entries(newServers)) { |
156 | | - const edits = jsonc.modify(text, ["servers", key], value, { formattingOptions }); |
157 | | - text = jsonc.applyEdits(text, edits); |
158 | | - } |
159 | | - if (Object.keys(newServers).length > 0) { |
160 | | - tree.overwrite(mcpFilePath, text); |
161 | | - } |
162 | | - } else { |
163 | | - const mcpConfig = { |
164 | | - servers: { |
165 | | - "angular-cli": angularCliServer, |
166 | | - "igniteui-cli": igniteuiServer, |
167 | | - "igniteui-theming": igniteuiThemingServer |
168 | | - } |
169 | | - }; |
170 | | - tree.create(mcpFilePath, JSON.stringify(mcpConfig, null, 2)); |
171 | | - } |
| 143 | + addMcpServers(VS_CODE_MCP_PATH, angularCliServer); |
172 | 144 | }; |
173 | 145 | } |
174 | 146 |
|
| 147 | +/** Standalone `ai-config` schematic entry */ |
| 148 | +export function addAIConfig(): Rule { |
| 149 | + return aiConfig(); |
| 150 | +} |
| 151 | + |
175 | 152 | export default function (): Rule { |
176 | 153 | return (tree: Tree) => { |
177 | | - App.initialize("angular-cli"); |
178 | | - // must be initialized with physical fs first: |
179 | | - App.container.set(TEMPLATE_MANAGER, new SchematicsTemplateManager()); |
180 | | - setVirtual(tree); |
| 154 | + appInit(tree); |
181 | 155 | return chain([ |
182 | 156 | importStyles(), |
183 | 157 | addTypographyToProj(), |
184 | 158 | importBrowserAnimations(), |
185 | 159 | createCliConfig(), |
186 | 160 | displayVersionMismatch(), |
187 | | - addAIConfig() |
| 161 | + aiConfig({ init: false }) |
188 | 162 | ]); |
189 | 163 | }; |
190 | 164 | } |
0 commit comments