에이전트로 새 파일 생성하기
Cursor의 AI 에이전트는 새 파일을 생성하고 관리하는 강력한 도구입니다. 이 가이드에서는 에이전트를 사용하여 파일을 효율적으로 생성하는 방법을 설명합니다.
기본 사용법
1. 파일 생성 명령
# 기본 파일 생성
/agent create file [파일명]
# 특정 경로에 파일 생성
/agent create file [경로/파일명]
# 템플릿 사용
/agent create file [파일명] --template [템플릿명]
2. 파일 유형 지정
# React 컴포넌트 생성
/agent create file components/Button.tsx --type react
# API 라우트 생성
/agent create file api/users.ts --type api
# 테스트 파일 생성
/agent create file tests/Button.test.tsx --type test
고급 기능
1. 템플릿 사용
// React 컴포넌트 템플릿
interface ComponentTemplate {
name: string;
props?: Record<string, any>;
children?: boolean;
}
// 템플릿 적용
/agent create file Button.tsx --template react-component --props "color:string,size:number"
2. 파일 구조 생성
# 전체 디렉토리 구조 생성
/agent create structure src/components/Button
# 결과:
# src/components/Button/
# ├── index.ts
# ├── Button.tsx
# ├── Button.test.tsx
# └── Button.module.css
모범 사례
1. 파일 명명 규칙
1. 컴포넌트 파일
- PascalCase 사용 (예: Button.tsx)
- 기능별 접미사 추가 (예: ButtonContainer.tsx)
2. 유틸리티 파일
- camelCase 사용 (예: utils.ts)
- 목적별 접미사 추가 (예: dateUtils.ts)
3. 테스트 파일
- 테스트 대상 파일명 + .test (예: Button.test.tsx)
- 스냅샷 테스트는 .snapshot 추가 (예: Button.snapshot.tsx)
2. 파일 구조화
1. 컴포넌트 구조
- 컴포넌트 파일
- 스타일 파일
- 테스트 파일
- 인덱스 파일
2. 기능 구조
- 기능별 디렉토리
- 관련 파일 그룹화
- 명확한 계층 구조
자동화 기능
1. 파일 생성 자동화
// 파일 생성 설정
interface FileConfig {
name: string;
type: string;
template?: string;
path?: string;
options?: Record<string, any>;
}
// 자동 생성 스크립트
async function createFiles(configs: FileConfig[]): Promise<void> {
for (const config of configs) {
await agent.createFile(config);
}
}
2. 템플릿 관리
// 템플릿 정의
const templates = {
react: {
component: `
import React from 'react';
interface Props {
// props 정의
}
export const {{name}}: React.FC<Props> = (props) => {
return (
// 컴포넌트 내용
);
};
`,
test: `
import { render, screen } from '@testing-library/react';
import { {{name}} } from './{{name}}';
describe('{{name}}', () => {
it('renders correctly', () => {
// 테스트 내용
});
});
`
}
};
문제 해결
1. 일반적인 오류
1. 파일 생성 실패
- 권한 확인
- 경로 유효성 검사
- 디스크 공간 확인
2. 템플릿 오류
- 템플릿 구문 검사
- 변수 치환 확인
- 필수 필드 검증
2. 디버깅
class FileCreator {
// 디버깅 로깅
private logCreation(config: FileConfig): void {
console.log('Creating file:', {
name: config.name,
type: config.type,
path: config.path
});
}
// 오류 처리
private handleError(error: Error): void {
console.error('File creation failed:', error);
// 오류 복구 로직
}
}
성능 최적화
1. 일괄 처리
class BatchFileCreator {
// 일괄 생성
async createBatch(files: FileConfig[]): Promise<void> {
const batches = this.createBatches(files, 5);
for (const batch of batches) {
await Promise.all(batch.map(file => this.createFile(file)));
}
}
// 배치 생성
private createBatches(files: FileConfig[], size: number): FileConfig[][] {
return files.reduce((batches, file, index) => {
const batchIndex = Math.floor(index / size);
if (!batches[batchIndex]) {
batches[batchIndex] = [];
}
batches[batchIndex].push(file);
return batches;
}, []);
}
}
2. 캐싱
class TemplateCache {
private cache: Map<string, string> = new Map();
// 템플릿 가져오기
async getTemplate(name: string): Promise<string> {
if (this.cache.has(name)) {
return this.cache.get(name);
}
const template = await this.loadTemplate(name);
this.cache.set(name, template);
return template;
}
}
보안 고려 사항
1. 파일 권한
class FilePermissionManager {
// 권한 확인
async checkPermissions(path: string): Promise<boolean> {
try {
await fs.access(path, fs.constants.W_OK);
return true;
} catch {
return false;
}
}
// 권한 설정
async setPermissions(path: string, mode: number): Promise<void> {
await fs.chmod(path, mode);
}
}
2. 입력 검증
class FileValidator {
// 파일명 검증
validateFileName(name: string): boolean {
return /^[a-zA-Z0-9-_]+$/.test(name);
}
// 경로 검증
validatePath(path: string): boolean {
return !path.includes('..') && path.startsWith('/');
}
}
결론
Cursor의 AI 에이전트를 사용하여 파일을 생성하면 개발 워크플로우를 크게 향상시킬 수 있습니다. 이 가이드의 모범 사례를 따르면 효율적이고 일관된 파일 생성 프로세스를 구축할 수 있습니다.
추가 리소스: