@contextjs/io
Version:
File system utilities for reading, writing, and inspecting files, directories, and paths.
57 lines (40 loc) • 2.11 kB
Markdown
# @contextjs/io
[](https://github.com/contextjs/context/actions/workflows/tests.yaml)
[](https://www.npmjs.com/package/@contextjs/io)
[](https://github.com/contextjs/context/blob/main/LICENSE)
> File system utilities for reading, writing, and inspecting files, directories, and paths — with clean APIs and exception-based error handling.
## ✨ Features
- Create, rename, and delete files and directories
- Automatic parent directory creation for files
- Path utilities: `isFile`, `isDirectory`, `exists`
- Clear and consistent exceptions
- Null-safe input validation
- Zero dependencies
## Installation
```bash
npm i @contextjs/io
```
## Usage
```ts
import { File, Directory, Path } from '@contextjs/io';
if (!Directory.exists("logs"))
Directory.create("logs");
File.save("logs/app.log", "Application started", true);
const log = File.read("logs/app.log");
File.save("logs/app.log", log + "\nSecond line", true);
File.rename("logs/app.log", "logs/app-archived.log");
if (Path.isFile("logs/app-archived.log"))
File.delete("logs/app-archived.log");
if (Directory.exists("logs"))
Directory.delete("logs");
```
These custom exceptions are thrown by the `File` and `Directory` APIs:
| Exception | Thrown When |
|---------------------------|--------------------------------------------------|
| `FileExistsException` | File already exists and `overwrite = false` |
| `FileNotFoundException` | File does not exist during read/rename/delete |
| `DirectoryExistsException`| Directory already exists on rename |
| `PathNotFoundException` | Path does not exist for directory/file operations |
| `NullReferenceException` | Input path is null or whitespace (from system) |
## API Reference
For detailed API documentation, please refer to the [API Reference](https://contextjs.dev/api/io#api-reference).