From 565f40f864a9f1962e43e99d19b113b2b2e9a8cf Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 11 Dec 2024 11:52:24 +0000 Subject: [PATCH] Put tmp alongside the target --- packages/astro/src/content/mutable-data-store.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/astro/src/content/mutable-data-store.ts b/packages/astro/src/content/mutable-data-store.ts index ba1e152c8b03..8fb3e4456b9e 100644 --- a/packages/astro/src/content/mutable-data-store.ts +++ b/packages/astro/src/content/mutable-data-store.ts @@ -6,21 +6,14 @@ import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { IMAGE_IMPORT_PREFIX } from './consts.js'; import { type DataEntry, ImmutableDataStore } from './data-store.js'; import { contentModuleToId } from './utils.js'; -import { tmpdir } from 'node:os'; -import { join } from 'node:path'; const SAVE_DEBOUNCE_MS = 500; -// Write a file atomically, without triggering the file watcher +// Write a file atomically async function writeFileAtomic(filePath: PathLike, data: string) { - const dir = await fs.mkdtemp(tmpdir() + '/astro-'); - const tempFile = join(dir, 'temp'); + const tempFile = filePath instanceof URL ? new URL(`${filePath.href}.tmp`) : `${filePath}.tmp`; await fs.writeFile(tempFile, data); - try { - await fs.rename(tempFile, filePath); - } finally { - await fs.rmdir(dir).catch(() => {}); - } + await fs.rename(tempFile, filePath); } /**