Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e993f6deb9 | |||
| 6a7f7496ea | |||
| b764942183 | |||
| 69ce0f4e64 | |||
| ebc7de4503 | |||
| c73a931dad | |||
| 3bdb014d25 | |||
| 5577e4e4de | |||
| 6308e198c7 |
39
changelog.md
39
changelog.md
@@ -1,5 +1,44 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-12-16 - 1.3.1 - fix(docs)
|
||||
docs(readme): add "Directory Copy & Move" section with examples and options
|
||||
|
||||
- Adds README documentation for recursive directory copy and move with usage examples (basic copy/move, copy with filter, overwrite, preserve timestamps, applyFilter).
|
||||
- Documents conflict handling modes for copy/move: merge (default), error, and replace.
|
||||
- Documentation-only change — no code or API changes; recommended patch version bump.
|
||||
|
||||
## 2025-12-16 - 1.3.0 - feat(smartfs.directory)
|
||||
feat(smartfs.directory): add directory copy/move with conflict handling and options
|
||||
|
||||
- Implement Directory.copy(targetPath) and Directory.move(targetPath) with provider-backed file operations (createDirectory, listDirectory, copyFile, deleteDirectory).
|
||||
- Add new directory options and fluent setters: applyFilter, overwrite, preserveTimestamps, onConflict (defaults: applyFilter=true, overwrite=false, preserveTimestamps=false, onConflict='merge').
|
||||
- Copy supports recursive listing, optional filtering (applyFilter), overwrite behavior and timestamp preservation; onConflict supports 'merge'|'error'|'replace'. Move performs copy then deletes the source.
|
||||
- Add comprehensive tests for copy/move: basic copy, recursive copy, filter-based copy, applyFilter(false) behavior, overwrite handling, onConflict error/replace cases, move semantics, and copying empty directories.
|
||||
- Update npmextra.json to use scoped keys (@git.zone/cli, @ship.zone/szci) and add release registry/access configuration.
|
||||
|
||||
## 2025-12-02 - 1.2.0 - feat(smartfs.directory)
|
||||
Add directory treeHash: deterministic content-based hashing of directory trees with streaming and algorithm option
|
||||
|
||||
- Implement treeHash(options?) on SmartFsDirectory which computes a deterministic hash of a directory tree by hashing relative file paths and streaming file contents (default algorithm: 'sha256').
|
||||
- Introduce ITreeHashOptions type (algorithm?: string) to allow selecting the hash algorithm (e.g. 'sha256', 'sha512').
|
||||
- Use Node.js crypto to update the hash incrementally while streaming file data to keep memory usage low.
|
||||
- Add tests in test/test.node.provider.ts covering treeHash behavior, determinism, algorithm selection, and empty-directory hashing.
|
||||
- Update README with documentation, examples and explanation of treeHash use cases and behavior.
|
||||
|
||||
## 2025-11-30 - 1.1.3 - fix(smartfs.provider.node)
|
||||
Default createDirectory to recursive=true when option not provided in Node provider
|
||||
|
||||
- Node provider: createDirectory now defaults to recursive=true when options.recursive is undefined.
|
||||
- Prevents errors when creating nested directories without explicitly passing the recursive option.
|
||||
- No API signature changes; behavior change is limited to the Node provider implementation.
|
||||
|
||||
## 2025-11-29 - 1.1.2 - fix(SmartFsProviderNode)
|
||||
Fix Node provider watch path handling and remove main test entry
|
||||
|
||||
- Node provider: detect at start whether the watched path is a file or directory (fs.stat) and build fullPath accordingly so watching a single file does not incorrectly join the filename onto the file path.
|
||||
- Watch callback: ensure events are evaluated against the configured filter using the correct full path.
|
||||
- Tests: removed test/test.ts (main test entry that previously imported provider test files).
|
||||
|
||||
## 2025-11-29 - 1.1.1 - fix(smartfs.provider.node)
|
||||
Default deleteDirectory to recursive=true in Node provider
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"gitzone": {
|
||||
"@git.zone/cli": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "code.foss.global",
|
||||
@@ -9,10 +9,16 @@
|
||||
"npmPackagename": "@push.rocks/smartfs",
|
||||
"license": "MIT",
|
||||
"projectDomain": "push.rocks"
|
||||
},
|
||||
"release": {
|
||||
"registries": [
|
||||
"https://verdaccio.lossless.digital",
|
||||
"https://registry.npmjs.org"
|
||||
],
|
||||
"accessLevel": "public"
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
"@ship.zone/szci": {
|
||||
"npmGlobalTools": []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartfs",
|
||||
"version": "1.1.1",
|
||||
"version": "1.3.1",
|
||||
"private": false,
|
||||
"description": "a cross platform extendable fs module",
|
||||
"main": "dist_ts/index.js",
|
||||
|
||||
132
readme.md
132
readme.md
@@ -4,23 +4,26 @@ Modern, pluggable filesystem module with fluent API, Web Streams support, and mu
|
||||
|
||||
## Issue Reporting and Security
|
||||
|
||||
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who want to sign a contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||
|
||||
## Features
|
||||
|
||||
- **🎯 Fluent API** - Action-last chainable interface for elegant code
|
||||
- **🔌 Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.)
|
||||
- **🌊 Web Streams** - Modern streaming with Web Streams API
|
||||
- **💾 Transactions** - Atomic multi-file operations with automatic rollback
|
||||
- **👀 File Watching** - Event-based file system monitoring
|
||||
- **⚡ Async-Only** - Modern async/await patterns throughout
|
||||
- **📦 Zero Dependencies** - Core functionality with minimal dependencies
|
||||
- **🎨 TypeScript** - Full type safety and IntelliSense support
|
||||
- 🎯 **Fluent API** - Action-last chainable interface for elegant code
|
||||
- 🔌 **Pluggable Providers** - Support for multiple storage backends (Node.js fs, memory, S3, etc.)
|
||||
- 🌊 **Web Streams** - Modern streaming with Web Streams API
|
||||
- 💾 **Transactions** - Atomic multi-file operations with automatic rollback
|
||||
- 👀 **File Watching** - Event-based file system monitoring
|
||||
- 🔐 **Tree Hashing** - SHA-256 directory hashing for cache-busting
|
||||
- ⚡ **Async-Only** - Modern async/await patterns throughout
|
||||
- 📦 **Zero Dependencies** - Core functionality with minimal footprint
|
||||
- 🎨 **TypeScript** - Full type safety and IntelliSense support
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pnpm install @push.rocks/smartfs
|
||||
npm install @push.rocks/smartfs
|
||||
# or
|
||||
pnpm add @push.rocks/smartfs
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
@@ -132,6 +135,88 @@ await fs.directory('/path/to/dir')
|
||||
const exists = await fs.directory('/path/to/dir').exists();
|
||||
```
|
||||
|
||||
### 📁 Directory Copy & Move
|
||||
|
||||
Copy or move entire directory trees with fine-grained control:
|
||||
|
||||
```typescript
|
||||
// Basic copy - copies all files recursively
|
||||
await fs.directory('/source').copy('/destination');
|
||||
|
||||
// Basic move - moves directory to new location
|
||||
await fs.directory('/old-location').move('/new-location');
|
||||
|
||||
// Copy with options
|
||||
await fs.directory('/source')
|
||||
.filter(/\.ts$/) // Only copy TypeScript files
|
||||
.overwrite(true) // Overwrite existing files
|
||||
.preserveTimestamps(true) // Keep original timestamps
|
||||
.copy('/destination');
|
||||
|
||||
// Copy all files (ignore filter setting)
|
||||
await fs.directory('/source')
|
||||
.filter('*.ts')
|
||||
.applyFilter(false) // Ignore filter, copy everything
|
||||
.copy('/destination');
|
||||
|
||||
// Handle target directory conflicts
|
||||
await fs.directory('/source')
|
||||
.onConflict('merge') // Default: merge contents
|
||||
.copy('/destination');
|
||||
|
||||
await fs.directory('/source')
|
||||
.onConflict('error') // Throw if target exists
|
||||
.copy('/destination');
|
||||
|
||||
await fs.directory('/source')
|
||||
.onConflict('replace') // Delete target first, then copy
|
||||
.copy('/destination');
|
||||
```
|
||||
|
||||
**Configuration Options:**
|
||||
| Method | Default | Description |
|
||||
|--------|---------|-------------|
|
||||
| `filter(pattern)` | none | Filter files by glob, regex, or function |
|
||||
| `applyFilter(bool)` | `true` | Whether to apply filter during copy/move |
|
||||
| `overwrite(bool)` | `false` | Overwrite existing files at destination |
|
||||
| `preserveTimestamps(bool)` | `false` | Preserve original file timestamps |
|
||||
| `onConflict(mode)` | `'merge'` | `'merge'`, `'error'`, or `'replace'` |
|
||||
|
||||
### 🔐 Tree Hashing (Cache-Busting)
|
||||
|
||||
Compute a deterministic hash of all files in a directory - perfect for cache invalidation:
|
||||
|
||||
```typescript
|
||||
// Hash all files in a directory recursively
|
||||
const hash = await fs.directory('/assets')
|
||||
.recursive()
|
||||
.treeHash();
|
||||
// Returns: "a3f2b8c9d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1"
|
||||
|
||||
// Hash only specific file types
|
||||
const cssHash = await fs.directory('/styles')
|
||||
.filter(/\.css$/)
|
||||
.recursive()
|
||||
.treeHash();
|
||||
|
||||
// Use different algorithm
|
||||
const sha512Hash = await fs.directory('/data')
|
||||
.recursive()
|
||||
.treeHash({ algorithm: 'sha512' });
|
||||
```
|
||||
|
||||
**How it works:**
|
||||
- Files are sorted by path for deterministic ordering
|
||||
- Hashes relative paths + file contents (streaming, memory-efficient)
|
||||
- Does NOT include metadata (mtime/size) - pure content-based
|
||||
- Same content always produces same hash, regardless of timestamps
|
||||
|
||||
**Use cases:**
|
||||
- 🚀 Cache-busting static assets
|
||||
- 📦 Detecting when served files change
|
||||
- 🔄 Incremental build triggers
|
||||
- ✅ Content verification
|
||||
|
||||
### Streaming Operations
|
||||
|
||||
SmartFS uses **Web Streams API** for efficient handling of large files:
|
||||
@@ -247,6 +332,7 @@ const fs = new SmartFs(new SmartFsProviderNode());
|
||||
- ✅ Streaming
|
||||
- ✅ Symbolic links
|
||||
- ✅ File permissions
|
||||
- ✅ Tree hashing
|
||||
|
||||
### Memory Provider
|
||||
|
||||
@@ -272,6 +358,7 @@ fs.provider.clear();
|
||||
- ✅ Streaming
|
||||
- ❌ Symbolic links
|
||||
- ✅ File permissions
|
||||
- ✅ Tree hashing
|
||||
|
||||
### Custom Providers
|
||||
|
||||
@@ -382,24 +469,12 @@ import type {
|
||||
IDirectoryEntry,
|
||||
IWatchEvent,
|
||||
ITransactionOperation,
|
||||
ITreeHashOptions,
|
||||
TEncoding,
|
||||
TFileMode,
|
||||
} from '@push.rocks/smartfs';
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
# Run all tests
|
||||
pnpm test
|
||||
|
||||
# Run specific test
|
||||
pnpm tstest test/test.memory.provider.ts --verbose
|
||||
|
||||
# Run with log output
|
||||
pnpm tstest test/test.node.provider.ts --logfile .nogit/testlogs/test.log
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
SmartFS throws descriptive errors:
|
||||
@@ -431,22 +506,25 @@ try {
|
||||
3. **Use memory provider** for testing
|
||||
4. **Enable atomic writes** for critical data
|
||||
5. **Debounce watchers** to reduce event spam
|
||||
6. **Use treeHash** instead of reading files for change detection
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||
|
||||
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
||||
@@ -256,6 +256,300 @@ tap.test('should handle file watching', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- treeHash tests ---
|
||||
|
||||
tap.test('should compute treeHash for a directory', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-test');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'file1.txt')).write('content1');
|
||||
await smartFs.file(path.join(dirPath, 'file2.txt')).write('content2');
|
||||
|
||||
const hash = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
expect(hash).toBeTruthy();
|
||||
expect(typeof hash).toEqual('string');
|
||||
expect(hash.length).toEqual(64); // SHA-256 produces 64 hex chars
|
||||
});
|
||||
|
||||
tap.test('treeHash should be deterministic (same content = same hash)', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-deterministic');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'a.txt')).write('aaa');
|
||||
await smartFs.file(path.join(dirPath, 'b.txt')).write('bbb');
|
||||
|
||||
const hash1 = await smartFs.directory(dirPath).treeHash();
|
||||
const hash2 = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
expect(hash1).toEqual(hash2);
|
||||
});
|
||||
|
||||
tap.test('treeHash should change when file content changes', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-content-change');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'file.txt')).write('original');
|
||||
|
||||
const hashBefore = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
await smartFs.file(path.join(dirPath, 'file.txt')).write('modified');
|
||||
|
||||
const hashAfter = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
expect(hashBefore).not.toEqual(hashAfter);
|
||||
});
|
||||
|
||||
tap.test('treeHash should change when file is added', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-file-add');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'file1.txt')).write('content1');
|
||||
|
||||
const hashBefore = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
await smartFs.file(path.join(dirPath, 'file2.txt')).write('content2');
|
||||
|
||||
const hashAfter = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
expect(hashBefore).not.toEqual(hashAfter);
|
||||
});
|
||||
|
||||
tap.test('treeHash should work recursively', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-recursive');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'root.txt')).write('root');
|
||||
await smartFs.directory(path.join(dirPath, 'sub')).create();
|
||||
await smartFs.file(path.join(dirPath, 'sub', 'nested.txt')).write('nested');
|
||||
|
||||
// Non-recursive should only include root.txt
|
||||
const hashNonRecursive = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
// Recursive should include both files
|
||||
const hashRecursive = await smartFs.directory(dirPath).recursive().treeHash();
|
||||
|
||||
expect(hashNonRecursive).not.toEqual(hashRecursive);
|
||||
});
|
||||
|
||||
tap.test('treeHash should respect filter', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-filter');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'file.ts')).write('typescript');
|
||||
await smartFs.file(path.join(dirPath, 'file.js')).write('javascript');
|
||||
|
||||
// Hash only .ts files
|
||||
const hashTs = await smartFs.directory(dirPath).filter(/\.ts$/).treeHash();
|
||||
|
||||
// Hash only .js files
|
||||
const hashJs = await smartFs.directory(dirPath).filter(/\.js$/).treeHash();
|
||||
|
||||
// Should be different since they're hashing different files
|
||||
expect(hashTs).not.toEqual(hashJs);
|
||||
});
|
||||
|
||||
tap.test('treeHash should support different algorithms', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-algorithm');
|
||||
await smartFs.directory(dirPath).create();
|
||||
await smartFs.file(path.join(dirPath, 'file.txt')).write('test content');
|
||||
|
||||
const sha256 = await smartFs.directory(dirPath).treeHash({ algorithm: 'sha256' });
|
||||
const sha512 = await smartFs.directory(dirPath).treeHash({ algorithm: 'sha512' });
|
||||
|
||||
expect(sha256.length).toEqual(64); // SHA-256 = 64 hex chars
|
||||
expect(sha512.length).toEqual(128); // SHA-512 = 128 hex chars
|
||||
expect(sha256).not.toEqual(sha512);
|
||||
});
|
||||
|
||||
tap.test('treeHash of empty directory should return consistent hash', async () => {
|
||||
const dirPath = path.join(tempDir, 'hash-empty');
|
||||
await smartFs.directory(dirPath).create();
|
||||
|
||||
const hash1 = await smartFs.directory(dirPath).treeHash();
|
||||
const hash2 = await smartFs.directory(dirPath).treeHash();
|
||||
|
||||
expect(hash1).toEqual(hash2);
|
||||
expect(hash1.length).toEqual(64);
|
||||
});
|
||||
|
||||
// --- Directory copy/move tests ---
|
||||
|
||||
tap.test('should copy a directory', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-dir-source');
|
||||
const destPath = path.join(tempDir, 'copy-dir-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file1.txt')).write('content1');
|
||||
await smartFs.file(path.join(sourcePath, 'file2.txt')).write('content2');
|
||||
|
||||
await smartFs.directory(sourcePath).copy(destPath);
|
||||
|
||||
// Source should still exist
|
||||
const sourceExists = await smartFs.directory(sourcePath).exists();
|
||||
expect(sourceExists).toEqual(true);
|
||||
|
||||
// Destination should exist with same files
|
||||
const destExists = await smartFs.directory(destPath).exists();
|
||||
expect(destExists).toEqual(true);
|
||||
|
||||
const destContent1 = await smartFs.file(path.join(destPath, 'file1.txt')).encoding('utf8').read();
|
||||
const destContent2 = await smartFs.file(path.join(destPath, 'file2.txt')).encoding('utf8').read();
|
||||
|
||||
expect(destContent1).toEqual('content1');
|
||||
expect(destContent2).toEqual('content2');
|
||||
});
|
||||
|
||||
tap.test('should copy a directory recursively with nested subdirectories', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-recursive-source');
|
||||
const destPath = path.join(tempDir, 'copy-recursive-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'root.txt')).write('root');
|
||||
await smartFs.directory(path.join(sourcePath, 'sub1')).create();
|
||||
await smartFs.file(path.join(sourcePath, 'sub1', 'nested1.txt')).write('nested1');
|
||||
await smartFs.directory(path.join(sourcePath, 'sub1', 'sub2')).create();
|
||||
await smartFs.file(path.join(sourcePath, 'sub1', 'sub2', 'deep.txt')).write('deep');
|
||||
|
||||
await smartFs.directory(sourcePath).copy(destPath);
|
||||
|
||||
// Verify all files copied
|
||||
expect(await smartFs.file(path.join(destPath, 'root.txt')).exists()).toEqual(true);
|
||||
expect(await smartFs.file(path.join(destPath, 'sub1', 'nested1.txt')).exists()).toEqual(true);
|
||||
expect(await smartFs.file(path.join(destPath, 'sub1', 'sub2', 'deep.txt')).exists()).toEqual(true);
|
||||
|
||||
const deepContent = await smartFs.file(path.join(destPath, 'sub1', 'sub2', 'deep.txt')).encoding('utf8').read();
|
||||
expect(deepContent).toEqual('deep');
|
||||
});
|
||||
|
||||
tap.test('should copy directory with filter applied', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-filter-source');
|
||||
const destPath = path.join(tempDir, 'copy-filter-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file.ts')).write('typescript');
|
||||
await smartFs.file(path.join(sourcePath, 'file.js')).write('javascript');
|
||||
await smartFs.file(path.join(sourcePath, 'file.txt')).write('text');
|
||||
|
||||
// Copy only .ts files
|
||||
await smartFs.directory(sourcePath).filter(/\.ts$/).copy(destPath);
|
||||
|
||||
expect(await smartFs.file(path.join(destPath, 'file.ts')).exists()).toEqual(true);
|
||||
expect(await smartFs.file(path.join(destPath, 'file.js')).exists()).toEqual(false);
|
||||
expect(await smartFs.file(path.join(destPath, 'file.txt')).exists()).toEqual(false);
|
||||
});
|
||||
|
||||
tap.test('should copy all files when applyFilter(false)', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-no-filter-source');
|
||||
const destPath = path.join(tempDir, 'copy-no-filter-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file.ts')).write('typescript');
|
||||
await smartFs.file(path.join(sourcePath, 'file.js')).write('javascript');
|
||||
|
||||
// Filter is set but applyFilter(false) ignores it
|
||||
await smartFs.directory(sourcePath).filter(/\.ts$/).applyFilter(false).copy(destPath);
|
||||
|
||||
expect(await smartFs.file(path.join(destPath, 'file.ts')).exists()).toEqual(true);
|
||||
expect(await smartFs.file(path.join(destPath, 'file.js')).exists()).toEqual(true);
|
||||
});
|
||||
|
||||
tap.test('should copy with overwrite(true) replacing existing files', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-overwrite-source');
|
||||
const destPath = path.join(tempDir, 'copy-overwrite-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file.txt')).write('new content');
|
||||
|
||||
await smartFs.directory(destPath).create();
|
||||
await smartFs.file(path.join(destPath, 'file.txt')).write('old content');
|
||||
|
||||
await smartFs.directory(sourcePath).overwrite(true).copy(destPath);
|
||||
|
||||
const content = await smartFs.file(path.join(destPath, 'file.txt')).encoding('utf8').read();
|
||||
expect(content).toEqual('new content');
|
||||
});
|
||||
|
||||
tap.test('should throw error when onConflict is error and target exists', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-conflict-error-source');
|
||||
const destPath = path.join(tempDir, 'copy-conflict-error-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file.txt')).write('content');
|
||||
await smartFs.directory(destPath).create();
|
||||
|
||||
let threw = false;
|
||||
try {
|
||||
await smartFs.directory(sourcePath).onConflict('error').copy(destPath);
|
||||
} catch (e: any) {
|
||||
threw = true;
|
||||
expect(e.message).toInclude('EEXIST');
|
||||
}
|
||||
expect(threw).toEqual(true);
|
||||
});
|
||||
|
||||
tap.test('should replace target when onConflict is replace', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-replace-source');
|
||||
const destPath = path.join(tempDir, 'copy-replace-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'new.txt')).write('new');
|
||||
|
||||
await smartFs.directory(destPath).create();
|
||||
await smartFs.file(path.join(destPath, 'old.txt')).write('old');
|
||||
|
||||
await smartFs.directory(sourcePath).onConflict('replace').copy(destPath);
|
||||
|
||||
// Old file should be gone, new file should exist
|
||||
expect(await smartFs.file(path.join(destPath, 'old.txt')).exists()).toEqual(false);
|
||||
expect(await smartFs.file(path.join(destPath, 'new.txt')).exists()).toEqual(true);
|
||||
});
|
||||
|
||||
tap.test('should move a directory', async () => {
|
||||
const sourcePath = path.join(tempDir, 'move-dir-source');
|
||||
const destPath = path.join(tempDir, 'move-dir-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'file1.txt')).write('content1');
|
||||
await smartFs.file(path.join(sourcePath, 'file2.txt')).write('content2');
|
||||
|
||||
await smartFs.directory(sourcePath).move(destPath);
|
||||
|
||||
// Source should no longer exist
|
||||
const sourceExists = await smartFs.directory(sourcePath).exists();
|
||||
expect(sourceExists).toEqual(false);
|
||||
|
||||
// Destination should exist with files
|
||||
const destExists = await smartFs.directory(destPath).exists();
|
||||
expect(destExists).toEqual(true);
|
||||
|
||||
const destContent1 = await smartFs.file(path.join(destPath, 'file1.txt')).encoding('utf8').read();
|
||||
expect(destContent1).toEqual('content1');
|
||||
});
|
||||
|
||||
tap.test('should move directory recursively', async () => {
|
||||
const sourcePath = path.join(tempDir, 'move-recursive-source');
|
||||
const destPath = path.join(tempDir, 'move-recursive-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.file(path.join(sourcePath, 'root.txt')).write('root');
|
||||
await smartFs.directory(path.join(sourcePath, 'sub')).create();
|
||||
await smartFs.file(path.join(sourcePath, 'sub', 'nested.txt')).write('nested');
|
||||
|
||||
await smartFs.directory(sourcePath).move(destPath);
|
||||
|
||||
// Source should not exist
|
||||
expect(await smartFs.directory(sourcePath).exists()).toEqual(false);
|
||||
|
||||
// All files should be at destination
|
||||
expect(await smartFs.file(path.join(destPath, 'root.txt')).exists()).toEqual(true);
|
||||
expect(await smartFs.file(path.join(destPath, 'sub', 'nested.txt')).exists()).toEqual(true);
|
||||
});
|
||||
|
||||
tap.test('should copy empty directory', async () => {
|
||||
const sourcePath = path.join(tempDir, 'copy-empty-source');
|
||||
const destPath = path.join(tempDir, 'copy-empty-dest');
|
||||
|
||||
await smartFs.directory(sourcePath).create();
|
||||
await smartFs.directory(sourcePath).copy(destPath);
|
||||
|
||||
expect(await smartFs.directory(destPath).exists()).toEqual(true);
|
||||
});
|
||||
|
||||
tap.test('cleanup temp directory', async () => {
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
expect(true).toEqual(true);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
/**
|
||||
* Main test entry point
|
||||
* Imports all test files
|
||||
*/
|
||||
|
||||
import './test.memory.provider.js';
|
||||
import './test.node.provider.js';
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartfs',
|
||||
version: '1.1.1',
|
||||
version: '1.3.1',
|
||||
description: 'a cross platform extendable fs module'
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
* Directory builder for fluent directory operations
|
||||
*/
|
||||
|
||||
import * as crypto from 'crypto';
|
||||
import type { ISmartFsProvider } from '../interfaces/mod.provider.js';
|
||||
import type {
|
||||
TFileMode,
|
||||
IFileStats,
|
||||
IDirectoryEntry,
|
||||
IListOptions,
|
||||
ITreeHashOptions,
|
||||
} from '../interfaces/mod.types.js';
|
||||
|
||||
/**
|
||||
@@ -25,7 +27,18 @@ export class SmartFsDirectory {
|
||||
mode?: TFileMode;
|
||||
filter?: string | RegExp | ((entry: IDirectoryEntry) => boolean);
|
||||
includeStats?: boolean;
|
||||
} = {};
|
||||
// Copy/move options
|
||||
applyFilter?: boolean;
|
||||
overwrite?: boolean;
|
||||
preserveTimestamps?: boolean;
|
||||
onConflict?: 'merge' | 'error' | 'replace';
|
||||
} = {
|
||||
// Defaults for copy/move
|
||||
applyFilter: true,
|
||||
overwrite: false,
|
||||
preserveTimestamps: false,
|
||||
onConflict: 'merge',
|
||||
};
|
||||
|
||||
constructor(provider: ISmartFsProvider, path: string) {
|
||||
this.provider = provider;
|
||||
@@ -80,6 +93,46 @@ export class SmartFsDirectory {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Control whether filter() is applied during copy/move operations
|
||||
* @param apply - If true, only copy/move files matching filter; if false, copy/move all files
|
||||
* @default true
|
||||
*/
|
||||
public applyFilter(apply: boolean = true): this {
|
||||
this.options.applyFilter = apply;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Control whether to overwrite existing files during copy/move
|
||||
* @param overwrite - If true, overwrite existing files; if false, throw error on conflict
|
||||
* @default false
|
||||
*/
|
||||
public overwrite(overwrite: boolean = true): this {
|
||||
this.options.overwrite = overwrite;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Control whether to preserve file timestamps during copy/move
|
||||
* @param preserve - If true, preserve original timestamps; if false, use current time
|
||||
* @default false
|
||||
*/
|
||||
public preserveTimestamps(preserve: boolean = true): this {
|
||||
this.options.preserveTimestamps = preserve;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Control behavior when target directory already exists
|
||||
* @param behavior - 'merge' to merge contents, 'error' to throw, 'replace' to delete and recreate
|
||||
* @default 'merge'
|
||||
*/
|
||||
public onConflict(behavior: 'merge' | 'error' | 'replace'): this {
|
||||
this.options.onConflict = behavior;
|
||||
return this;
|
||||
}
|
||||
|
||||
// --- Action Methods (return Promises) ---
|
||||
|
||||
/**
|
||||
@@ -130,10 +183,157 @@ export class SmartFsDirectory {
|
||||
return this.provider.directoryStat(this.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the directory to a new location
|
||||
* @param targetPath - Destination path
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Basic copy
|
||||
* await fs.directory('/source').copy('/target');
|
||||
*
|
||||
* // Copy with options
|
||||
* await fs.directory('/source')
|
||||
* .recursive()
|
||||
* .filter('*.ts')
|
||||
* .overwrite(true)
|
||||
* .preserveTimestamps(true)
|
||||
* .copy('/target');
|
||||
*
|
||||
* // Copy all files (ignore filter)
|
||||
* await fs.directory('/source')
|
||||
* .applyFilter(false)
|
||||
* .copy('/target');
|
||||
* ```
|
||||
*/
|
||||
public async copy(targetPath: string): Promise<void> {
|
||||
const normalizedTarget = this.provider.normalizePath(targetPath);
|
||||
|
||||
// Handle conflict behavior
|
||||
const targetExists = await this.provider.directoryExists(normalizedTarget);
|
||||
if (targetExists) {
|
||||
if (this.options.onConflict === 'error') {
|
||||
throw new Error(`EEXIST: directory already exists: ${normalizedTarget}`);
|
||||
}
|
||||
if (this.options.onConflict === 'replace') {
|
||||
await this.provider.deleteDirectory(normalizedTarget, { recursive: true });
|
||||
}
|
||||
// 'merge' (default) - continue and overwrite based on file settings
|
||||
}
|
||||
|
||||
// Create target directory
|
||||
await this.provider.createDirectory(normalizedTarget, { recursive: true });
|
||||
|
||||
// List entries (always recursive for copy, respects filter based on applyFilter option)
|
||||
const listOptions: IListOptions = {
|
||||
recursive: true,
|
||||
filter: this.options.applyFilter ? this.options.filter : undefined,
|
||||
includeStats: false,
|
||||
};
|
||||
const entries = await this.provider.listDirectory(this.path, listOptions);
|
||||
|
||||
// Process entries - sort to ensure directories are created before their contents
|
||||
const sortedEntries = entries.sort((a, b) => a.path.localeCompare(b.path));
|
||||
|
||||
for (const entry of sortedEntries) {
|
||||
const relativePath = entry.path.substring(this.path.length);
|
||||
const targetEntryPath = this.provider.joinPath(normalizedTarget, relativePath);
|
||||
|
||||
if (entry.isDirectory) {
|
||||
await this.provider.createDirectory(targetEntryPath, { recursive: true });
|
||||
} else {
|
||||
// Ensure parent directory exists
|
||||
const parentPath = targetEntryPath.substring(0, targetEntryPath.lastIndexOf('/'));
|
||||
if (parentPath && parentPath !== normalizedTarget) {
|
||||
await this.provider.createDirectory(parentPath, { recursive: true });
|
||||
}
|
||||
// Copy file using provider
|
||||
await this.provider.copyFile(entry.path, targetEntryPath, {
|
||||
preserveTimestamps: this.options.preserveTimestamps,
|
||||
overwrite: this.options.overwrite,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the directory to a new location
|
||||
* @param targetPath - Destination path
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Basic move
|
||||
* await fs.directory('/source').move('/target');
|
||||
*
|
||||
* // Move with conflict handling
|
||||
* await fs.directory('/source')
|
||||
* .onConflict('replace')
|
||||
* .move('/target');
|
||||
* ```
|
||||
*/
|
||||
public async move(targetPath: string): Promise<void> {
|
||||
// Copy first using current configuration
|
||||
await this.copy(targetPath);
|
||||
|
||||
// Delete source (always recursive, regardless of filter - this matches mv behavior)
|
||||
await this.provider.deleteDirectory(this.path, { recursive: true });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the directory path
|
||||
*/
|
||||
public getPath(): string {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a hash of all files in the directory tree
|
||||
* Uses configured filter and recursive options
|
||||
* @param options - Hash options (algorithm defaults to 'sha256')
|
||||
* @returns Hex-encoded hash string
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Hash all files recursively
|
||||
* const hash = await fs.directory('/assets').recursive().treeHash();
|
||||
*
|
||||
* // Hash only TypeScript files
|
||||
* const hash = await fs.directory('/src').filter('*.ts').recursive().treeHash();
|
||||
*
|
||||
* // Use different algorithm
|
||||
* const hash = await fs.directory('/data').recursive().treeHash({ algorithm: 'sha512' });
|
||||
* ```
|
||||
*/
|
||||
public async treeHash(options?: ITreeHashOptions): Promise<string> {
|
||||
const { algorithm = 'sha256' } = options ?? {};
|
||||
const hash = crypto.createHash(algorithm);
|
||||
|
||||
// Get all entries using existing filter/recursive configuration
|
||||
const entries = await this.list();
|
||||
|
||||
// Filter to files only and sort by path for deterministic ordering
|
||||
const files = entries
|
||||
.filter((entry) => entry.isFile)
|
||||
.sort((a, b) => a.path.localeCompare(b.path));
|
||||
|
||||
// Hash each file's relative path and contents
|
||||
for (const file of files) {
|
||||
// Compute relative path from directory root
|
||||
const relativePath = file.path.slice(this.path.length + 1);
|
||||
|
||||
// Hash the relative path (with null separator)
|
||||
hash.update(relativePath + '\0');
|
||||
|
||||
// Stream file contents and update hash incrementally
|
||||
const stream = await this.provider.createReadStream(file.path);
|
||||
const reader = stream.getReader();
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
hash.update(value);
|
||||
}
|
||||
}
|
||||
|
||||
return hash.digest('hex');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,3 +215,13 @@ export interface IWatchOptions {
|
||||
filter?: string | RegExp | ((path: string) => boolean);
|
||||
debounce?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tree hash options interface
|
||||
*/
|
||||
export interface ITreeHashOptions {
|
||||
/**
|
||||
* Hash algorithm to use (default: 'sha256')
|
||||
*/
|
||||
algorithm?: string;
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ export class SmartFsProviderNode implements ISmartFsProvider {
|
||||
|
||||
public async createDirectory(path: string, options?: { recursive?: boolean; mode?: number }): Promise<void> {
|
||||
await fs.mkdir(path, {
|
||||
recursive: options?.recursive,
|
||||
recursive: options?.recursive ?? true,
|
||||
mode: options?.mode,
|
||||
});
|
||||
}
|
||||
@@ -266,6 +266,10 @@ export class SmartFsProviderNode implements ISmartFsProvider {
|
||||
// --- Watch Operations ---
|
||||
|
||||
public async watch(path: string, callback: TWatchCallback, options?: IWatchOptions): Promise<IWatcherHandle> {
|
||||
// Check once at start if we're watching a file or directory
|
||||
const watchedStats = await fs.stat(path);
|
||||
const isWatchingFile = watchedStats.isFile();
|
||||
|
||||
const watcher = fsSync.watch(
|
||||
path,
|
||||
{
|
||||
@@ -274,7 +278,8 @@ export class SmartFsProviderNode implements ISmartFsProvider {
|
||||
async (eventType, filename) => {
|
||||
if (!filename) return;
|
||||
|
||||
const fullPath = pathModule.join(path, filename);
|
||||
// For file watching, path IS the file; for directory, join with filename
|
||||
const fullPath = isWatchingFile ? path : pathModule.join(path, filename);
|
||||
|
||||
// Apply filter
|
||||
if (options?.filter && !this.matchesPathFilter(fullPath, options.filter)) {
|
||||
|
||||
Reference in New Issue
Block a user