@@ -3,6 +3,7 @@ import type { Logger, ViteDevServer } from 'vite'
3
3
import type { TabBar , TabBarItem } from './config'
4
4
import type { PagesConfig } from './config/types'
5
5
import type { PageMetaDatum , PagePath , ResolvedOptions , SubPageMetaDatum , UserOptions } from './types'
6
+ import fs from 'node:fs'
6
7
import path from 'node:path'
7
8
import process from 'node:process'
8
9
import { slash } from '@antfu/utils'
@@ -11,11 +12,12 @@ import { stringify as cjStringify } from 'comment-json'
11
12
import dbg from 'debug'
12
13
import detectIndent from 'detect-indent'
13
14
import detectNewline from 'detect-newline'
14
- import { loadConfig } from 'unconfig '
15
+ import lockfile from 'proper-lockfile '
15
16
17
+ import { loadConfig } from 'unconfig'
16
18
import { OUTPUT_NAME } from './constant'
17
19
import { writeDeclaration } from './declaration'
18
- import { checkPagesJsonFile , getPageFiles , readFileSync , writeFileSync } from './files'
20
+ import { checkPagesJsonFile , getPageFiles } from './files'
19
21
import { resolveOptions } from './options'
20
22
import { Page } from './page'
21
23
import {
@@ -39,9 +41,9 @@ export class PageContext {
39
41
subPageMetaData : SubPageMetaDatum [ ] = [ ]
40
42
41
43
resolvedPagesJSONPath = ''
42
- resolvedPagesJSONIndent = ' '
43
- resolvedPagesJSONNewline = '\n'
44
- resolvedPagesJSONEofNewline = true
44
+ private resolvedPagesJSONIndent ?: string // ' '
45
+ private resolvedPagesJSONNewline ?: string // '\n'
46
+ private resolvedPagesJSONEofNewline ?: boolean // true
45
47
46
48
rawOptions : UserOptions
47
49
root : string
@@ -63,10 +65,6 @@ export class PageContext {
63
65
dbg . enable ( `${ prefix } ${ suffix } ` )
64
66
}
65
67
this . resolvedPagesJSONPath = path . join ( this . root , this . options . outDir , OUTPUT_NAME )
66
- const resolvedPagesJSONContent = readFileSync ( this . resolvedPagesJSONPath )
67
- this . resolvedPagesJSONIndent = detectIndent ( resolvedPagesJSONContent ) . indent || ' '
68
- this . resolvedPagesJSONNewline = detectNewline ( resolvedPagesJSONContent ) || '\n'
69
- this . resolvedPagesJSONEofNewline = ( resolvedPagesJSONContent . at ( - 1 ) ?? '\n' ) === this . resolvedPagesJSONNewline
70
68
debug . options ( this . options )
71
69
}
72
70
@@ -337,7 +335,7 @@ export class PageContext {
337
335
}
338
336
}
339
337
340
- checkPagesJsonFile ( this . resolvedPagesJSONPath )
338
+ await checkPagesJsonFile ( this . resolvedPagesJSONPath )
341
339
this . options . onBeforeLoadUserConfig ( this )
342
340
await this . loadUserPagesConfig ( )
343
341
this . options . onAfterLoadUserConfig ( this )
@@ -376,17 +374,26 @@ export class PageContext {
376
374
const pagesJson = cjStringify (
377
375
data ,
378
376
null ,
379
- this . options . minify ? undefined : this . resolvedPagesJSONIndent ,
377
+ this . options . minify ? undefined : await this . getIndent ( ) ,
380
378
) + (
381
- this . resolvedPagesJSONEofNewline ? this . resolvedPagesJSONNewline : ''
379
+ await this . getEndOfLine ( ) ? await this . getNewline ( ) : ''
382
380
)
383
381
this . generateDeclaration ( )
384
382
if ( lsatPagesJson === pagesJson ) {
385
383
debug . pages ( 'PagesJson Not have change' )
386
384
return false
387
385
}
388
386
389
- writeFileSync ( this . resolvedPagesJSONPath , pagesJson )
387
+ // 获取文件锁,如果文件不存在则创建
388
+ const relase = await lockfile . lock ( this . resolvedPagesJSONPath , { realpath : false } )
389
+
390
+ try {
391
+ await fs . promises . writeFile ( this . resolvedPagesJSONPath , pagesJson , { encoding : 'utf-8' } ) // 执行写入操作
392
+ }
393
+ finally {
394
+ await relase ( ) // 释放文件锁
395
+ }
396
+
390
397
lsatPagesJson = pagesJson
391
398
392
399
this . options . onAfterWriteFile ( this )
@@ -414,6 +421,37 @@ export class PageContext {
414
421
debug . declaration ( 'generating' )
415
422
return writeDeclaration ( this , this . options . dts )
416
423
}
424
+
425
+ private async readInfoFromPagesJSON ( ) : Promise < void > {
426
+ const resolvedPagesJSONContent = await fs . promises . readFile ( this . resolvedPagesJSONPath , { encoding : 'utf-8' } ) . catch ( ( ) => '' )
427
+ this . resolvedPagesJSONIndent = detectIndent ( resolvedPagesJSONContent ) . indent || ' '
428
+ this . resolvedPagesJSONNewline = detectNewline ( resolvedPagesJSONContent ) || '\n'
429
+ this . resolvedPagesJSONEofNewline = ( resolvedPagesJSONContent . at ( - 1 ) ?? '\n' ) === this . resolvedPagesJSONNewline
430
+ }
431
+
432
+ private async getIndent ( ) {
433
+ if ( ! this . resolvedPagesJSONIndent ) {
434
+ await this . readInfoFromPagesJSON ( )
435
+ }
436
+
437
+ return this . resolvedPagesJSONIndent !
438
+ }
439
+
440
+ private async getNewline ( ) {
441
+ if ( ! this . resolvedPagesJSONNewline ) {
442
+ await this . readInfoFromPagesJSON ( )
443
+ }
444
+
445
+ return this . resolvedPagesJSONNewline !
446
+ }
447
+
448
+ private async getEndOfLine ( ) {
449
+ if ( ! this . resolvedPagesJSONEofNewline ) {
450
+ await this . readInfoFromPagesJSON ( )
451
+ }
452
+
453
+ return this . resolvedPagesJSONEofNewline !
454
+ }
417
455
}
418
456
419
457
function getPagePaths ( dir : string , options : ResolvedOptions ) {
0 commit comments