@@ -17,8 +17,7 @@ interface BaseParams {
1717
1818export interface CreateParams extends BaseParams {
1919 command : 'create'
20- fileText ?: string
21- newStr ?: string
20+ fileText : string
2221}
2322
2423export interface StrReplaceParams extends BaseParams {
@@ -111,31 +110,50 @@ export class FsWrite {
111110 oldContent = await fs . readFileText ( sanitizedPath )
112111 isNew = false
113112 } catch ( err ) {
113+ this . logger . warn ( 'old content does not exist, set it to empty string' )
114114 oldContent = ''
115115 isNew = true
116116 }
117117 return { filePath : sanitizedPath , content : oldContent , isNew }
118118 }
119119
120120 public async validate ( ) : Promise < void > {
121+ if ( ! this . params . path ) {
122+ throw new Error ( 'Path must not be empty' )
123+ }
124+ const sanitizedPath = sanitizePath ( this . params . path )
121125 switch ( this . params . command ) {
122- case 'create' :
123- if ( ! this . params . path ) {
124- throw new Error ( 'Path must not be empty' )
126+ case 'create' : {
127+ if ( this . params . fileText === undefined ) {
128+ throw new Error ( 'fileText must be provided for create command' )
129+ }
130+ const fileExists = await fs . existsFile ( sanitizedPath )
131+ if ( fileExists ) {
132+ const oldContent = await fs . readFileText ( sanitizedPath )
133+ if ( oldContent === this . params . fileText ) {
134+ throw new Error ( 'The file already exists with the same content' )
135+ }
125136 }
126137 break
127- case 'strReplace' :
138+ }
139+ case 'strReplace' : {
140+ if ( this . params . oldStr === this . params . newStr ) {
141+ throw new Error ( 'The provided oldStr and newStr are the exact same, this is a no-op' )
142+ }
143+ const fileExists = await fs . existsFile ( sanitizedPath )
144+ if ( ! fileExists ) {
145+ throw new Error ( 'The provided path must exist in order to replace contents into it' )
146+ }
147+ break
148+ }
128149 case 'insert' : {
129- const fileExists = await fs . existsFile ( this . params . path )
150+ const fileExists = await fs . existsFile ( sanitizedPath )
130151 if ( ! fileExists ) {
131- throw new Error ( 'The provided path must exist in order to replace or insert contents into it' )
152+ throw new Error ( 'The provided path must exist in order to insert contents into it' )
132153 }
133154 break
134155 }
135156 case 'append' :
136- if ( ! this . params . path ) {
137- throw new Error ( 'Path must not be empty' )
138- }
139157 if ( ! this . params . newStr ) {
140158 throw new Error ( 'Content to append must not be empty' )
141159 }
@@ -220,15 +238,7 @@ export class FsWrite {
220238 }
221239
222240 private getCreateCommandText ( params : CreateParams ) : string {
223- if ( params . fileText ) {
224- return params . fileText
225- }
226- if ( params . newStr ) {
227- this . logger . warn ( 'Required field `fileText` is missing, use the provided `newStr` instead' )
228- return params . newStr
229- }
230- this . logger . warn ( 'No content provided for the create command' )
231- return ''
241+ return params . fileText
232242 }
233243
234244 private escapeRegExp ( string : string ) : string {
0 commit comments