@@ -32,7 +32,7 @@ export class ButtonActionRunner {
32
32
33
33
resolveFilePath ( filePath : string , relativeFilePath ?: string | undefined ) : string {
34
34
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 ) ;
36
36
if ( resolvedFilePath === undefined ) {
37
37
throw new MetaBindParsingError ( {
38
38
errorLevel : ErrorLevel . ERROR ,
@@ -270,8 +270,8 @@ export class ButtonActionRunner {
270
270
if ( action . openIfAlreadyExists && action . fileName ) {
271
271
const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
272
272
// 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 ) ;
275
275
return ;
276
276
}
277
277
}
@@ -316,39 +316,44 @@ export class ButtonActionRunner {
316
316
if ( action . openIfAlreadyExists ) {
317
317
const filePath = ensureFileExtension ( joinPath ( action . folderPath ?? '' , action . fileName ) , 'md' ) ;
318
318
// 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 ) ;
321
321
return ;
322
322
}
323
323
}
324
324
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
+ ) ;
326
331
}
327
332
328
333
async runReplaceInNoteAction ( action : ReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
329
334
if ( action . fromLine > action . toLine ) {
330
335
throw new Error ( 'From line cannot be greater than to line' ) ;
331
336
}
332
337
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
-
341
338
const replacement = action . templater
342
339
? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . replacement ) , filePath )
343
340
: action . replacement ;
344
341
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' ) ;
350
344
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
+ } ) ;
352
357
}
353
358
354
359
async runReplaceSelfAction (
@@ -368,59 +373,59 @@ export class ButtonActionRunner {
368
373
throw new Error ( 'Position of the button in the note is invalid' ) ;
369
374
}
370
375
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 ;
378
377
379
378
const replacement = action . templater
380
379
? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . replacement ) , filePath )
381
380
: action . replacement ;
382
381
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' ) ;
388
384
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
+ } ) ;
390
397
}
391
398
392
399
async runRegexpReplaceInNoteAction ( action : RegexpReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
393
400
if ( action . regexp === '' ) {
394
401
throw new Error ( 'Regexp cannot be empty' ) ;
395
402
}
396
403
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
+ } ) ;
402
407
}
403
408
404
409
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
-
413
410
const insertString = action . templater
414
411
? await this . plugin . internal . evaluateTemplaterTemplate ( this . resolveFilePath ( action . value ) , filePath )
415
412
: action . value ;
416
413
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
+ ] ;
422
426
423
- await this . plugin . internal . writeFilePath ( filePath , splitContent . join ( '\n' ) ) ;
427
+ return splitContent . join ( '\n' ) ;
428
+ } ) ;
424
429
}
425
430
426
431
async runInlineJsAction (
0 commit comments