Skip to content

Commit cf18e8b

Browse files
committed
refactor(core): 使用write-file-atomic替换自定义原子写入逻辑
1 parent a4383b2 commit cf18e8b

File tree

6 files changed

+115
-25
lines changed

6 files changed

+115
-25
lines changed

packages/core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"proper-lockfile": "^4.1.2",
7878
"typescript": "5.9.2",
7979
"unconfig": "^7.3.2",
80+
"write-file-atomic": "^7.0.0",
8081
"yaml": "^2.5.0"
8182
},
8283
"devDependencies": {
@@ -85,6 +86,7 @@
8586
"@types/debug": "^4.1.12",
8687
"@types/lodash.groupby": "^4.6.9",
8788
"@types/node": "^20.15.0",
89+
"@types/write-file-atomic": "^4.0.3",
8890
"cross-env": "^10.0.0",
8991
"vite": "^5.4.1"
9092
}

packages/core/src/files.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { ResolvedOptions } from './types'
22
import fs from 'node:fs'
33
import fg from 'fast-glob'
44
import lockfile from 'proper-lockfile'
5+
import writeFileAtomic from 'write-file-atomic'
56
import { FILE_EXTENSIONS } from './constant'
67
import { debug, extsToGlob, sleep } from './utils'
7-
88
/**
99
* Resolves the files that are valid pages for the given context.
1010
*/
@@ -120,10 +120,8 @@ export async function writeFileWithLock(path: string, content: string, retry = 3
120120
await sleep(500)
121121
return writeFileWithLock(path, content, retry - 1)
122122
}
123-
// Use atomic write: write to temp file first, then rename
124-
const tempPath = `${path}.${Date.now()}.tmp`
125-
await fs.promises.writeFile(tempPath, content, { encoding: 'utf-8' })
126-
await fs.promises.rename(tempPath, path)
123+
// Use atomic write
124+
await writeFileAtomic(path, content)
127125
}
128126
finally {
129127
// eslint-disable-next-line ts/ban-ts-comment

packages/playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@dcloudio/uni-automator": "3.0.0-4070520250711001",
2424
"@dcloudio/uni-cli-shared": "3.0.0-4070520250711001",
2525
"@dcloudio/vite-plugin-uni": "3.0.0-4070520250711001",
26+
"@uni-helper/vite-plugin-uni-layouts": "^0.1.11",
2627
"@uni-helper/vite-plugin-uni-pages": "workspace:*",
2728
"vite": "^5.4.1"
2829
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<view class="px-10 py-20 text-center">
3+
<slot />
4+
<app-footer />
5+
<view class="mx-auto mt-5 text-center text-sm opacity-25">
6+
[Default Layout]
7+
</view>
8+
</view>
9+
</template>

packages/playground/vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PagesConfig } from '@uni-helper/vite-plugin-uni-pages'
22
import uni from '@dcloudio/vite-plugin-uni'
3+
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
34
import UniPages from '@uni-helper/vite-plugin-uni-pages'
45
import { defineConfig } from 'vite'
56

@@ -27,6 +28,7 @@ export default defineConfig({
2728
// },
2829
// ],
2930
}),
31+
UniLayouts(),
3032
uni(),
3133
],
3234
UniPages: {

0 commit comments

Comments
 (0)