@@ -32,7 +32,7 @@ export class ButtonActionRunner {
3232
3333 resolveFilePath ( filePath : string , relativeFilePath ?: string | undefined ) : string {
3434 const targetFilePath = MDLinkParser . isLink ( filePath ) ? MDLinkParser . parseLink ( filePath ) . target : filePath ;
35- const resolvedFilePath = this . plugin . internal . getFilePathByName ( targetFilePath , relativeFilePath ) ;
35+ const resolvedFilePath = this . plugin . internal . file . getPathByName ( targetFilePath , relativeFilePath ) ;
3636 if ( resolvedFilePath === undefined ) {
3737 throw new MetaBindParsingError ( {
3838 errorLevel : ErrorLevel . ERROR ,
@@ -270,8 +270,8 @@ export class ButtonActionRunner {
270270 if ( action . openIfAlreadyExists && action . fileName ) {
271271 const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
272272 // if the file already exists, open it in the same tab
273- if ( await this . plugin . internal . existsFilePath ( filePath ) ) {
274- this . plugin . internal . openFile ( filePath , '' , false ) ;
273+ if ( await this . plugin . internal . file . exists ( filePath ) ) {
274+ this . plugin . internal . file . open ( filePath , '' , false ) ;
275275 return ;
276276 }
277277 }
@@ -316,39 +316,44 @@ export class ButtonActionRunner {
316316 if ( action . openIfAlreadyExists ) {
317317 const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
318318 // if the file already exists, open it in the same tab
319- if ( await this . plugin . internal . existsFilePath ( filePath ) ) {
320- this . plugin . internal . openFile ( filePath , '' , false ) ;
319+ if ( await this . plugin . internal . file . exists ( filePath ) ) {
320+ this . plugin . internal . file . open ( filePath , '' , false ) ;
321321 return ;
322322 }
323323 }
324324
325- await this . plugin . internal . createFile ( action . folderPath ?? '' , action . fileName , 'md' , action . openNote ?? false ) ;
325+ await this . plugin . internal . file . create (
326+ action . folderPath ?? '' ,
327+ action . fileName ,
328+ 'md' ,
329+ action . openNote ?? false ,
330+ ) ;
326331 }
327332
328333 async runReplaceInNoteAction ( action : ReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
329334 if ( action . fromLine > action . toLine ) {
330335 throw new Error ( 'From line cannot be greater than to line' ) ;
331336 }
332337
333- const content = await this . plugin . internal . readFilePath ( filePath ) ;
334-
335- let splitContent = content . split ( '\n' ) ;
336-
337- if ( action . fromLine < 0 || action . toLine > splitContent . length + 1 ) {
338- throw new Error ( 'Line numbers out of bounds' ) ;
339- }
340-
341338 const replacement = action . templater
342339 ? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . replacement ) , filePath )
343340 : action . replacement ;
344341
345- splitContent = [
346- ...splitContent . slice ( 0 , action . fromLine - 1 ) ,
347- replacement ,
348- ...splitContent . slice ( action . toLine ) ,
349- ] ;
342+ await this . plugin . internal . file . atomicModify ( filePath , content => {
343+ let splitContent = content . split ( '\n' ) ;
350344
351- await this . plugin . internal . writeFilePath ( filePath , splitContent . join ( '\n' ) ) ;
345+ if ( action . fromLine < 0 || action . toLine > splitContent . length + 1 ) {
346+ throw new Error ( 'Line numbers out of bounds' ) ;
347+ }
348+
349+ splitContent = [
350+ ...splitContent . slice ( 0 , action . fromLine - 1 ) ,
351+ replacement ,
352+ ...splitContent . slice ( action . toLine ) ,
353+ ] ;
354+
355+ return splitContent . join ( '\n' ) ;
356+ } ) ;
352357 }
353358
354359 async runReplaceSelfAction (
@@ -368,59 +373,59 @@ export class ButtonActionRunner {
368373 throw new Error ( 'Position of the button in the note is invalid' ) ;
369374 }
370375
371- const content = await this . plugin . internal . readFilePath ( filePath ) ;
372-
373- let splitContent = content . split ( '\n' ) ;
374-
375- if ( buttonContext . position . lineStart < 0 || buttonContext . position . lineEnd > splitContent . length + 1 ) {
376- throw new Error ( 'Position of the button in the note is out of bounds' ) ;
377- }
376+ const position = buttonContext . position ;
378377
379378 const replacement = action . templater
380379 ? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . replacement ) , filePath )
381380 : action . replacement ;
382381
383- splitContent = [
384- ...splitContent . slice ( 0 , buttonContext . position . lineStart ) ,
385- replacement ,
386- ...splitContent . slice ( buttonContext . position . lineEnd + 1 ) ,
387- ] ;
382+ await this . plugin . internal . file . atomicModify ( filePath , content => {
383+ let splitContent = content . split ( '\n' ) ;
388384
389- await this . plugin . internal . writeFilePath ( filePath , splitContent . join ( '\n' ) ) ;
385+ if ( position . lineStart < 0 || position . lineEnd > splitContent . length + 1 ) {
386+ throw new Error ( 'Position of the button in the note is out of bounds' ) ;
387+ }
388+
389+ splitContent = [
390+ ...splitContent . slice ( 0 , position . lineStart ) ,
391+ replacement ,
392+ ...splitContent . slice ( position . lineEnd + 1 ) ,
393+ ] ;
394+
395+ return splitContent . join ( '\n' ) ;
396+ } ) ;
390397 }
391398
392399 async runRegexpReplaceInNoteAction ( action : RegexpReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
393400 if ( action . regexp === '' ) {
394401 throw new Error ( 'Regexp cannot be empty' ) ;
395402 }
396403
397- let content = await this . plugin . internal . readFilePath ( filePath ) ;
398-
399- content = content . replace ( new RegExp ( action . regexp , action . regexpFlags ?? 'g' ) , action . replacement ) ;
400-
401- await this . plugin . internal . writeFilePath ( filePath , content ) ;
404+ await this . plugin . internal . file . atomicModify ( filePath , content => {
405+ return content . replace ( new RegExp ( action . regexp , action . regexpFlags ?? 'g' ) , action . replacement ) ;
406+ } ) ;
402407 }
403408
404409 async runInsertIntoNoteAction ( action : InsertIntoNoteButtonAction , filePath : string ) : Promise < void > {
405- const content = await this . plugin . internal . readFilePath ( filePath ) ;
406-
407- let splitContent = content . split ( '\n' ) ;
408-
409- if ( action . line < 1 || action . line > splitContent . length + 1 ) {
410- throw new Error ( 'Line number out of bounds' ) ;
411- }
412-
413410 const insertString = action . templater
414411 ? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . value ) , filePath )
415412 : action . value ;
416413
417- splitContent = [
418- ...splitContent . slice ( 0 , action . line - 1 ) ,
419- insertString ,
420- ...splitContent . slice ( action . line - 1 ) ,
421- ] ;
414+ await this . plugin . internal . file . atomicModify ( filePath , content => {
415+ let splitContent = content . split ( '\n' ) ;
416+
417+ if ( action . line < 1 || action . line > splitContent . length + 1 ) {
418+ throw new Error ( 'Line number out of bounds' ) ;
419+ }
420+
421+ splitContent = [
422+ ...splitContent . slice ( 0 , action . line - 1 ) ,
423+ insertString ,
424+ ...splitContent . slice ( action . line - 1 ) ,
425+ ] ;
422426
423- await this . plugin . internal . writeFilePath ( filePath , splitContent . join ( '\n' ) ) ;
427+ return splitContent . join ( '\n' ) ;
428+ } ) ;
424429 }
425430
426431 async runInlineJsAction (
0 commit comments