Ecopages0.10.2

File System

The @ecopages/file-system package provides file system utilities. The implementation is Node's on every runtime, including Bun.

Features

  • Node implementation: node:fs/promises.glob(), crypto, and node:fs
  • One interface: Import fileSystem and call it from Node or Bun
  • Type Safe: One TypeScript interface

Installation

npm install @ecopages/file-system

Usage

import { fileSystem } from '@ecopages/file-system';

// Glob files
const files = await fileSystem.glob(['**/*.ts'], { cwd: './src' });

// Read files
const content = await fileSystem.readFile('file.txt');

// Hash files (SHA-256, hex)
const hash = fileSystem.hash('file.txt');

// Write files (sync)
fileSystem.write('output.txt', 'Hello World');

// Write files (async, creates missing parent directories)
await fileSystem.writeAsync('output.txt', largeBuffer);

// Check existence
if (fileSystem.exists('file.txt')) {
	// ...
}

// Ensure directory exists
fileSystem.ensureDir('./dist/client/assets');

// Copy files and directories
fileSystem.copyFile('src.txt', 'dest.txt');
fileSystem.copyDir('./src', './dist/client');

Watching paths

fileSystem.watch() recursively watches directory roots and returns a handle that can wait for indexing, add paths, and close the subscription:

import { fileSystem } from '@ecopages/file-system';

const watcher = fileSystem.watch(['./src'], {
	ignored: (path) => path.includes('node_modules'),
	onChange: ({ type, path }) => console.log(type, path),
	onError: (error) => console.error(error),
});

await watcher.ready;
await watcher.add(['./generated']);
await watcher.close();

onChange receives add, change, unlink, addDir, unlinkDir, or rescan events. Existing entries are indexed before ready resolves but are not reported. A missing root is watched from its nearest existing parent and starts reporting when it is created. add() indexes existing entries under the new paths without reporting them; if adding paths fails, the subscription rolls those paths back so they can be retried.

Native events for the same path are coalesced for 50 ms, so a change can be reported while a file is still being written. Atomic replacement of a file is reported as one change. When a native event omits its filename, the watcher reconciles the root and can report rescan for a requested path. Permission-denied directories are skipped while accessible siblings are indexed. An ignored predicate skips matching subtrees during indexing; when called without stats, return true only for paths that should remain ignored for the life of the watcher.

API Reference

File Operations

MethodDescription
readFile(path)Read file as string (async)
readFileSync(path)Read file as string (sync)
readFileAsBuffer(path)Read file as Buffer
write(path, content)Write content to file (sync)
writeAsync(path, content)Write content to file (async)
exists(path)Check if path exists
verifyFileExists(path)Throw FileNotFoundError if missing

Directory Operations

MethodDescription
glob(patterns, options)Find files matching glob patterns
ensureDir(path, forceCleanup?)Ensure directory exists
copyDir(src, dest)Copy directory recursively
emptyDir(path)Remove all contents of directory
isDirectory(path)Check if path is directory

File Management

MethodDescription
copyFile(src, dest)Copy single file
remove(path)Remove file or directory (sync)
removeAsync(path)Remove file or directory (async)
hash(path)SHA-256 hex digest of file contents
gzipFile(path)Gzip a single file
gzipDir(path, extensions)Gzip all matching files in directory

Glob Options

interface GlobOptions {
	cwd?: string; // Working directory (default: process.cwd())
	ignore?: string[]; // Patterns to ignore
}

Direct import

import { NodeFileSystem } from '@ecopages/file-system/node';
const nodeFs = new NodeFileSystem();

Error Handling

The package exports a custom error class for file not found scenarios:

import { fileSystem, FileNotFoundError } from '@ecopages/file-system';

try {
	fileSystem.verifyFileExists('/path/to/file');
} catch (error) {
	if (error instanceof FileNotFoundError) {
		console.log('File not found:', error.message);
	}
}