@@ -39,6 +39,7 @@ import {
3939 Cause ,
4040} from "effect" ;
4141import * as Semaphore from "effect/Semaphore" ;
42+ import { writeFileStringAtomically } from "./atomicWrite.ts" ;
4243import { ServerConfig } from "./config.ts" ;
4344import { type DeepPartial , deepMerge } from "@t3tools/shared/Struct" ;
4445import { fromLenientJson } from "@t3tools/shared/schemaJson" ;
@@ -233,14 +234,14 @@ const makeServerSettings = Effect.gen(function* () {
233234 const getSettingsFromCache = Cache . get ( settingsCache , cacheKey ) ;
234235
235236 const writeSettingsAtomically = ( settings : ServerSettings ) => {
236- const tempPath = `${ settingsPath } .${ process . pid } .${ Date . now ( ) } .tmp` ;
237237 const sparseSettings = stripDefaultServerSettings ( settings , DEFAULT_SERVER_SETTINGS ) ?? { } ;
238238
239- return Effect . succeed ( `${ JSON . stringify ( sparseSettings , null , 2 ) } \n` ) . pipe (
240- Effect . tap ( ( ) => fs . makeDirectory ( pathService . dirname ( settingsPath ) , { recursive : true } ) ) ,
241- Effect . tap ( ( encoded ) => fs . writeFileString ( tempPath , encoded ) ) ,
242- Effect . flatMap ( ( ) => fs . rename ( tempPath , settingsPath ) ) ,
243- Effect . ensuring ( fs . remove ( tempPath , { force : true } ) . pipe ( Effect . ignore ( { log : true } ) ) ) ,
239+ return writeFileStringAtomically ( {
240+ filePath : settingsPath ,
241+ contents : `${ JSON . stringify ( sparseSettings , null , 2 ) } \n` ,
242+ } ) . pipe (
243+ Effect . provideService ( FileSystem . FileSystem , fs ) ,
244+ Effect . provideService ( Path . Path , pathService ) ,
244245 Effect . mapError (
245246 ( cause ) =>
246247 new ServerSettingsError ( {
0 commit comments