Initial commit: DashCaddy v1.0
Full codebase including API server (32 modules + routes), dashboard frontend, DashCA certificate distribution, installer script, and deployment skills.
This commit is contained in:
65
dashcaddy-api/fs-helpers.js
Normal file
65
dashcaddy-api/fs-helpers.js
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 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 };
|
||||
Reference in New Issue
Block a user