11import { existsSync , mkdirSync , writeFileSync } from "node:fs" ;
22import path from "node:path" ;
3+ import * as readline from "node:readline/promises" ;
34
45import { TemplateManager } from "../../templates/index.js" ;
56
67export interface InitCommandOptions {
78 targetPath ?: string ;
89}
910
11+ async function promptYesNo ( message : string ) : Promise < boolean > {
12+ const rl = readline . createInterface ( {
13+ input : process . stdin ,
14+ output : process . stdout ,
15+ } ) ;
16+
17+ try {
18+ const answer = await rl . question ( `${ message } (y/N): ` ) ;
19+ return answer . toLowerCase ( ) === "y" || answer . toLowerCase ( ) === "yes" ;
20+ } finally {
21+ rl . close ( ) ;
22+ }
23+ }
24+
1025export async function initCommand ( options : InitCommandOptions = { } ) : Promise < void > {
1126 const targetPath = path . resolve ( options . targetPath ?? "." ) ;
1227 const githubDir = path . join ( targetPath , ".github" ) ;
28+ const agentvDir = path . join ( targetPath , ".agentv" ) ;
29+
30+ // Get templates
31+ const githubTemplates = TemplateManager . getGithubTemplates ( ) ;
32+ const agentvTemplates = TemplateManager . getAgentvTemplates ( ) ;
33+
34+ // Check if any files already exist
35+ const existingFiles : string [ ] = [ ] ;
36+ if ( existsSync ( githubDir ) ) {
37+ for ( const template of githubTemplates ) {
38+ const targetFilePath = path . join ( githubDir , template . path ) ;
39+ if ( existsSync ( targetFilePath ) ) {
40+ existingFiles . push ( path . relative ( targetPath , targetFilePath ) ) ;
41+ }
42+ }
43+ }
44+ if ( existsSync ( agentvDir ) ) {
45+ for ( const template of agentvTemplates ) {
46+ const targetFilePath = path . join ( agentvDir , template . path ) ;
47+ if ( existsSync ( targetFilePath ) ) {
48+ existingFiles . push ( path . relative ( targetPath , targetFilePath ) ) ;
49+ }
50+ }
51+ }
52+
53+ // If files exist, prompt user
54+ if ( existingFiles . length > 0 ) {
55+ console . log ( "We detected an existing setup:" ) ;
56+ existingFiles . forEach ( ( file ) => console . log ( ` - ${ file } ` ) ) ;
57+ console . log ( ) ;
58+
59+ const shouldReplace = await promptYesNo ( "Do you want to replace these files?" ) ;
60+ if ( ! shouldReplace ) {
61+ console . log ( "\nInit cancelled. No files were changed." ) ;
62+ return ;
63+ }
64+ console . log ( ) ;
65+ }
1366
1467 // Create .github directory if it doesn't exist
1568 if ( ! existsSync ( githubDir ) ) {
1669 mkdirSync ( githubDir , { recursive : true } ) ;
1770 }
1871
19- // Get templates
20- const templates = TemplateManager . getTemplates ( ) ;
72+ // Create .agentv directory if it doesn't exist
73+ if ( ! existsSync ( agentvDir ) ) {
74+ mkdirSync ( agentvDir , { recursive : true } ) ;
75+ }
2176
22- // Copy each template to .github
23- for ( const template of templates ) {
77+ // Copy each .github template
78+ for ( const template of githubTemplates ) {
2479 const targetFilePath = path . join ( githubDir , template . path ) ;
2580 const targetDirPath = path . dirname ( targetFilePath ) ;
2681
@@ -34,8 +89,28 @@ export async function initCommand(options: InitCommandOptions = {}): Promise<voi
3489 console . log ( `Created ${ path . relative ( targetPath , targetFilePath ) } ` ) ;
3590 }
3691
92+ // Copy each .agentv template
93+ for ( const template of agentvTemplates ) {
94+ const targetFilePath = path . join ( agentvDir , template . path ) ;
95+ const targetDirPath = path . dirname ( targetFilePath ) ;
96+
97+ // Create directory if needed
98+ if ( ! existsSync ( targetDirPath ) ) {
99+ mkdirSync ( targetDirPath , { recursive : true } ) ;
100+ }
101+
102+ // Write file
103+ writeFileSync ( targetFilePath , template . content , "utf-8" ) ;
104+ console . log ( `Created ${ path . relative ( targetPath , targetFilePath ) } ` ) ;
105+ }
106+
37107 console . log ( "\nAgentV initialized successfully!" ) ;
38108 console . log ( `\nFiles installed to ${ path . relative ( targetPath , githubDir ) } :` ) ;
39- templates . forEach ( ( t ) => console . log ( ` - ${ t . path } ` ) ) ;
40- console . log ( "\nYou can now create eval files using the schema and prompt templates." ) ;
109+ githubTemplates . forEach ( ( t ) => console . log ( ` - ${ t . path } ` ) ) ;
110+ console . log ( `\nFiles installed to ${ path . relative ( targetPath , agentvDir ) } :` ) ;
111+ agentvTemplates . forEach ( ( t ) => console . log ( ` - ${ t . path } ` ) ) ;
112+ console . log ( "\nYou can now:" ) ;
113+ console . log ( " 1. Edit .agentv/.env with your API credentials" ) ;
114+ console . log ( " 2. Configure targets in .agentv/targets.yaml" ) ;
115+ console . log ( " 3. Create eval files using the schema and prompt templates" ) ;
41116}
0 commit comments