File edition helpers working on top of mem-fs
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Core file reading functionality for accessing file contents with support for various data formats including text, binary, and JSON files.
Reads file contents as string or buffer with support for default values and raw binary data.
/**
* Read file contents as string with optional defaults
* @param filepath - Path to the file to read
* @param options - Reading options with string/null defaults
* @returns File contents as string or null
* @throws Error if file doesn't exist and no defaults provided
*/
function read(filepath: string, options?: ReadOptionsString): string | null;
/**
* Read file contents as buffer with optional defaults
* @param filepath - Path to the file to read
* @param options - Reading options with raw mode enabled and Buffer/null defaults
* @returns File contents as Buffer, string, or null
*/
function read(filepath: string, options: ReadOptionsBuffer): Buffer | string | null;
interface ReadOptionsString {
/** Return raw Buffer instead of string */
raw?: boolean;
/** Default value to return if file doesn't exist */
defaults?: string | null;
}
interface ReadOptionsBuffer {
/** Return raw Buffer instead of string */
raw?: true;
/** Default value to return if file doesn't exist */
defaults?: Buffer | null;
}Usage Examples:
import { create as createMemFs } from "mem-fs";
import { create as createEditor } from "mem-fs-editor";
const store = createMemFs();
const fs = createEditor(store);
// Read as string (default)
const content = fs.read("package.json");
// Read as raw Buffer
const binaryData = fs.read("image.png", { raw: true });
// Read with default value
const config = fs.read("config.json", { defaults: "{}" });
// Read with null default
const optional = fs.read("optional.txt", { defaults: null });Reads and parses JSON files with automatic error handling and default value support.
/**
* Read and parse JSON file contents
* @param filepath - Path to the JSON file
* @param defaults - Default value to return if file doesn't exist
* @returns Parsed JSON object or defaults value
* @throws Error if JSON parsing fails
*/
function readJSON(filepath: string, defaults?: any): any;Usage Examples:
// Read package.json
const packageData = fs.readJSON("package.json");
// Read with object default
const config = fs.readJSON("config.json", { port: 3000, host: "localhost" });
// Read with array default
const items = fs.readJSON("data.json", []);
// Read with null default
const optional = fs.readJSON("optional.json", null);Checks if a file exists in the memory store.
/**
* Check if file exists in memory store
* @param filepath - Path to check for existence
* @returns True if file exists and has contents, false otherwise
*/
function exists(filepath: string): boolean;Usage Examples:
// Check if file exists
if (fs.exists("package.json")) {
const data = fs.readJSON("package.json");
}
// Conditional file operations
if (!fs.exists("dist/index.js")) {
fs.copy("src/index.js", "dist/index.js");
}
// Guard against missing files
const hasConfig = fs.exists("config.json");
const config = hasConfig ? fs.readJSON("config.json") : { port: 3000 };The read method throws an error when a file doesn't exist unless a defaults option is provided:
try {
const content = fs.read("missing-file.txt");
} catch (error) {
console.log("File doesn't exist");
}
// Better approach with defaults
const content = fs.read("missing-file.txt", { defaults: "" });The readJSON method throws an error if JSON parsing fails, but returns defaults for missing files:
try {
const data = fs.readJSON("invalid.json");
} catch (error) {
console.log("Invalid JSON format");
}
// Safe approach with defaults
const data = fs.readJSON("config.json", {});