1.1.0 • Published 5 months ago
@elzazo/path-utils v1.1.0
Path Utils
A comprehensive Node.js utility library for file and directory operations with robust error handling.
Features
- CreatePath: Create directories and files with proper path handling
- JsonFile: Read and write JSON files with error handling
- TextFile: Read, write, append, and prepend text files
- readDirectory: List directory contents with filtering options
Installation
npm install @elzazo/path-utilsUsage Examples
CreatePath
Create directories and files with proper path handling:
const { CreatePath } = require("@elzazo/path-utils");
// Initialize with default options
const CP = new CreatePath();
// Create a file with JSON content
CP.make_file("files/file1.json", { message: "Hello World!" });
// Create nested directories
CP.make_dir("test2/test3/test4");
CP.make_dir("test2/test4/test3");JsonFile
Manage JSON files with proper error handling:
const { JsonFile } = require("@elzazo/path-utils");
async function run() {
// Initialize with file path
const JF = new JsonFile("./test/test.json");
// Read data
const data = await JF.read();
// Update data
data.type = "desc";
// Write data back to file
JF.write(data);
}
run();TextFile
Manage text files with read, write, append, and prepend operations:
const { TextFile } = require("@elzazo/path-utils");
async function run() {
// Initialize with file path
const TF = new TextFile("./test/test.txt");
// Write content (replaces existing content)
TF.write("1. First Line");
// Append content
await TF.push("2. New Line At End");
await TF.push("3. New Line At End", true); // with new line
// Prepend content
await TF.unshift("4. New Line At Start\n");
await TF.unshift("5. New Line At Start", true); // with new line
// Read content
const content = await TF.read();
console.log(content);
}
run();Output:
5. New Line At Start
4. New Line At Start
1. First Line
2. New Line At End
3. New Line At EndreadDirectory
List directory contents with filtering options:
const { readDirectory } = require("@elzazo/path-utils");
async function run() {
// Get all files in current directory
const result = await readDirectory(__dirname);
console.log(result);
// Get all files except node_modules and .git
const filtered = await readDirectory(__dirname, ["node_modules", ".git"]);
console.log(filtered);
}
run();API Reference
CreatePath
const CP = new CreatePath(display_info, CM);display_info(boolean, optional): Whether to display console messages (default: true)CM(ConsoleMessages, optional): Console message handler instance
Methods:
make_dir(path): Creates a directory pathmake_file(path, content): Creates a file with the specified content
JsonFile
const JF = new JsonFile(file_path, options);file_path(string): Path to the JSON fileoptions(object, optional):initial_value(object): Initial value if file doesn't exist (default: {})CM(ConsoleMessages): Console message handlerdisplay(boolean): Whether to display console messages (default: true)
Methods:
read(): Reads and parses the JSON file contentwrite(data): Writes data to the JSON file
TextFile
const TF = new TextFile(file_path, options);file_path(string): Path to the text fileoptions(object, optional):initial_value(string): Initial value if file doesn't exist (default: "")CM(ConsoleMessages): Console message handlerdisplay(boolean): Whether to display console messages (default: true)
Methods:
read(): Reads the text file contentwrite(text): Writes text to the filepush(text, in_new_ligne): Appends text to the end of the fileunshift(text, in_new_ligne): Prepends text to the beginning of the file
readDirectory
readDirectory(path, names_to_scape);path(string): Directory path to readnames_to_scape(array, optional): Array of file/directory names to exclude
Returns a Promise that resolves to an object with:
items(array): Array of file/directory nameserr(boolean): Whether an error occurrederr_msg(string): Error message if an error occurred
License
ISC