diff --git a/changelog.md b/changelog.md index ff5dadc..d1dc6f3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2025-01-07 - 11.1.2 - fix(fs) +Fix issues in file stability check and directory existence verification in fs module + +- Removed unused variable 'isFileAvailable' in 'waitForFileToBeReady'. +- Fixed logic for ensuring the target directory exists before setting up file stability watcher. +- Refactored directory existence logic into 'ensureDirectoryExists' function. + ## 2025-01-07 - 11.1.1 - fix(fs) Improve waitForFileToBeReady function for file stability detection diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 4eb3bff..3a94637 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartfile', - version: '11.1.1', + version: '11.1.2', description: 'Provides comprehensive tools for efficient file management in Node.js using TypeScript, including handling streams, virtual directories, and various file operations.' } diff --git a/ts/fs.ts b/ts/fs.ts index 5c7a28a..f536ac5 100644 --- a/ts/fs.ts +++ b/ts/fs.ts @@ -205,7 +205,7 @@ export const toObjectSync = (filePathArg, fileTypeArg?) => { } catch (err) { err.message = `Failed to read file at ${filePathArg}` + err.message; throw err; - }; + } }; /** @@ -408,17 +408,34 @@ export const waitForFileToBeReady = ( return new Promise(async (resolve, reject) => { let lastFileSize = -1; let fileIsStable = false; - let isFileAvailable = false; const startTime = Date.now(); const fileDir = plugins.path.dirname(filePathArg); + const ensureDirectoryExists = async () => { + while (true) { + try { + // Check if the directory exists + await plugins.smartpromise.fromCallback((cb) => + plugins.fs.access(fileDir, plugins.fs.constants.R_OK, cb) + ); + break; // Exit the loop if the directory exists + } catch (err) { + if (Date.now() - startTime > timeoutMs) { + reject(new Error(`Timeout waiting for directory to exist: ${fileDir}`)); + return; + } + // Wait and retry + await plugins.smartdelay.delayFor(500); + } + } + }; + const checkFileStability = async () => { try { - const stats: any = await plugins.smartpromise.fromCallback((cb) => + const stats = await plugins.smartpromise.fromCallback((cb) => plugins.fs.stat(filePathArg, cb) ); - isFileAvailable = true; if (stats.size === lastFileSize) { fileIsStable = true; } else { @@ -426,27 +443,17 @@ export const waitForFileToBeReady = ( fileIsStable = false; } } catch (err) { - if (err.code === 'ENOENT') { - isFileAvailable = false; // File not available yet - } else { - throw err; // Propagate other errors + if (err.code !== 'ENOENT') { + throw err; // Only ignore ENOENT (file not found) errors } } }; - const checkDirectory = async () => { - try { - await plugins.smartpromise.fromCallback((cb) => - plugins.fs.access(fileDir, plugins.fs.constants.R_OK, cb) - ); - return true; - } catch { - return false; - } - }; + // Ensure the directory exists before setting up the watcher + await ensureDirectoryExists(); const watcher = plugins.fs.watch(filePathArg, { persistent: true }, async () => { - if (isFileAvailable && !fileIsStable) { + if (!fileIsStable) { await checkFileStability(); } }); @@ -465,12 +472,6 @@ export const waitForFileToBeReady = ( return; } - // Ensure directory exists - if (!await checkDirectory()) { - await plugins.smartdelay.delayFor(500); // Wait and retry - continue; - } - // Check file stability await checkFileStability(); if (!fileIsStable) { @@ -534,4 +535,4 @@ export let toFs = async ( export const stat = async (filePathArg: string) => { return plugins.fsPromises.stat(filePathArg); -} +};