Files
dashcaddy/dashcaddy-api/src/utilities/fs-helpers.js
T
Hermes 7bc2a207f3 DC-005: Fix all 138 broken test paths after src/ refactor
After the DC-005 module reorganization (41 files moved into src/ subdirs),
138 test suites failed because the refactor script's path-rewrite logic
missed three categories:

1. Files inside src/ doing 'require("./src/...")' — should be 'require("../...")'
2. Files in src/X/Y/ doing 'require("../../../src/...")' — should be 'require("../../...")'
3. Test files in __tests__/ with leftover 'require("../../../src/...")' paths

Root cause: the original refactor script ran before all files were moved,
so it computed relative paths against stale filesystem state.

Result:
- 30/30 test suites pass
- 879/879 tests pass (was: 18/30 suites, 614/687 tests)

Also fixed:
- routes/apps/restore.js: wrong responses import path
- routes/*/*.js: '../../src/utilities/X' → '../src/utilities/X' (depth 2 routes)
2026-06-13 12:16:56 -07:00

66 lines
1.5 KiB
JavaScript

/**
* Async File System Helpers for DashCaddy
* Replaces common sync patterns with async equivalents.
*/
const fsp = require('fs').promises;
const fs = require('fs');
/**
* Async file existence check (replaces fs.existsSync)
*/
async function exists(filePath) {
try {
await fsp.access(filePath);
return true;
} catch {
return false;
}
}
/**
* Read and parse a JSON file with fallback (replaces existsSync + readFileSync + JSON.parse)
*/
async function readJsonFile(filePath, fallback = null) {
try {
const content = await fsp.readFile(filePath, 'utf8');
return JSON.parse(content);
} catch (e) {
if (e.code === 'ENOENT') return fallback;
throw e;
}
}
/**
* Write data as formatted JSON (replaces writeFileSync + JSON.stringify)
*/
async function writeJsonFile(filePath, data) {
await fsp.writeFile(filePath, JSON.stringify(data, null, 2), 'utf8');
}
/**
* Read a text file with fallback (replaces existsSync + readFileSync)
*/
async function readTextFile(filePath, fallback = '') {
try {
return await fsp.readFile(filePath, 'utf8');
} catch (e) {
if (e.code === 'ENOENT') return fallback;
throw e;
}
}
/**
* Check if path is accessible with given mode (replaces accessSync)
*/
async function isAccessible(filePath, mode = fs.constants.R_OK) {
try {
await fsp.access(filePath, mode);
return true;
} catch {
return false;
}
}
module.exports = { exists, readJsonFile, writeJsonFile, readTextFile, isAccessible };