|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { Rule } from '@angular-devkit/schematics'; |
| 10 | +import { RunSchematicTask } from '@angular-devkit/schematics/tasks'; |
| 11 | +import { execFileSync } from 'node:child_process'; |
| 12 | +import { resolve } from 'node:path'; |
| 13 | +import { latestVersions } from '../utility/latest-versions'; |
| 14 | +import { Tool } from './schema'; |
| 15 | + |
| 16 | +type SkillsTool = Exclude<Tool, Tool.None>; |
| 17 | + |
| 18 | +interface AngularSkillsInstallOptions { |
| 19 | + tools: readonly string[]; |
| 20 | + workingDirectory?: string; |
| 21 | +} |
| 22 | + |
| 23 | +const SKILLS_AGENT: { [key in SkillsTool]: string } = { |
| 24 | + ['claude-code']: 'claude-code', |
| 25 | + cursor: 'cursor', |
| 26 | + ['gemini-cli']: 'gemini-cli', |
| 27 | + ['open-ai-codex']: 'codex', |
| 28 | + vscode: 'github-copilot', |
| 29 | +}; |
| 30 | + |
| 31 | +const ANGULAR_SKILLS_REPOSITORY = 'https://github.com/angular/skills'; |
| 32 | + |
| 33 | +export function getAngularSkillsRepository(angularVersion = latestVersions.Angular): string { |
| 34 | + const version = angularVersion.match(/(\d+)\.(\d+)/); |
| 35 | + if (!version) { |
| 36 | + throw new Error(`Unable to determine the Angular release line from '${angularVersion}'.`); |
| 37 | + } |
| 38 | + |
| 39 | + return `${ANGULAR_SKILLS_REPOSITORY}/tree/${version[1]}.${version[2]}.x`; |
| 40 | +} |
| 41 | + |
| 42 | +export function getAngularSkillsInstallArguments( |
| 43 | + tools: readonly string[], |
| 44 | + angularVersion = latestVersions.Angular, |
| 45 | +): string[] { |
| 46 | + const agents = tools.flatMap((tool) => { |
| 47 | + const agent = SKILLS_AGENT[tool as SkillsTool]; |
| 48 | + if (!agent) { |
| 49 | + throw new Error(`Unsupported AI tool '${tool}' for Angular Agent Skills.`); |
| 50 | + } |
| 51 | + |
| 52 | + return ['--agent', agent]; |
| 53 | + }); |
| 54 | + |
| 55 | + return [ |
| 56 | + '--yes', |
| 57 | + 'skills', |
| 58 | + 'add', |
| 59 | + getAngularSkillsRepository(angularVersion), |
| 60 | + '--skill', |
| 61 | + 'angular-developer', |
| 62 | + '--skill', |
| 63 | + 'angular-new-app', |
| 64 | + ...agents, |
| 65 | + '--copy', |
| 66 | + '--yes', |
| 67 | + ]; |
| 68 | +} |
| 69 | + |
| 70 | +export function createAngularSkillsTask( |
| 71 | + tools: readonly string[], |
| 72 | + workingDirectory?: string, |
| 73 | +): RunSchematicTask<AngularSkillsInstallOptions> { |
| 74 | + return new RunSchematicTask( |
| 75 | + 'ai-config-install-skills', |
| 76 | + workingDirectory === undefined ? { tools } : { tools, workingDirectory }, |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export default function (options: AngularSkillsInstallOptions): Rule { |
| 81 | + return (_tree, context) => { |
| 82 | + const args = getAngularSkillsInstallArguments(options.tools); |
| 83 | + |
| 84 | + try { |
| 85 | + execFileSync('npx', args, { |
| 86 | + cwd: resolve(options.workingDirectory ?? '.'), |
| 87 | + env: { ...process.env, DISABLE_TELEMETRY: '1' }, |
| 88 | + stdio: 'inherit', |
| 89 | + shell: process.platform === 'win32', |
| 90 | + }); |
| 91 | + } catch { |
| 92 | + context.logger.warn( |
| 93 | + 'Angular Agent Skills could not be installed.\n' + |
| 94 | + `When you are online, install them manually with:\n npx ${args.join(' ')}`, |
| 95 | + ); |
| 96 | + } |
| 97 | + }; |
| 98 | +} |
0 commit comments