@ecopages/file-system 0.2.0-alpha.5 → 0.2.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -2
- package/README.md +7 -23
- package/package.json +1 -1
- package/src/adapters/node.js +13 -6
- package/src/adapters/node.ts +16 -6
- package/src/index.d.ts +1 -1
- package/src/index.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,6 @@ All notable changes to `@ecopages/file-system` are documented here.
|
|
|
6
6
|
|
|
7
7
|
## [UNRELEASED] — TBD
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### Bug Fixes
|
|
10
10
|
|
|
11
|
-
- `
|
|
11
|
+
- Switched Node-side globbing from `fast-glob` to native `node:fs/promises.glob()` so bundled ESM runtime paths avoid CommonJS interop failures.
|
package/README.md
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
# @ecopages/file-system
|
|
2
2
|
|
|
3
|
-
Runtime-agnostic file system utilities for Ecopages
|
|
3
|
+
Runtime-agnostic file system utilities for Ecopages that automatically select the optimal adapter (Bun or Node.js) based on the execution environment.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Runtime Detection**: Automatically
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
10
|
-
- **Type Safe**: Full TypeScript support with consistent interface
|
|
7
|
+
- **Runtime Detection**: Automatically uses `Bun.Glob`, `Bun.hash`, `Bun.file` for maximum performance on Bun, and falls back to `fast-glob` and `crypto` on Node.js.
|
|
8
|
+
- **Unified Interface**: Write file system code once; let the runtime handle the optimization.
|
|
9
|
+
- **Type Safe**: Full TypeScript support with a consistent API.
|
|
11
10
|
|
|
12
11
|
## Installation
|
|
13
12
|
|
|
14
13
|
```bash
|
|
15
|
-
|
|
14
|
+
bunx jsr add @ecopages/file-system
|
|
16
15
|
```
|
|
17
16
|
|
|
18
17
|
## Usage
|
|
@@ -41,11 +40,11 @@ if (fileSystem.exists('file.txt')) {
|
|
|
41
40
|
## API
|
|
42
41
|
|
|
43
42
|
| Method | Description |
|
|
44
|
-
|
|
|
43
|
+
| :-------------------------- | :-------------------------------- |
|
|
45
44
|
| `glob(patterns, options)` | Find files matching glob patterns |
|
|
46
45
|
| `readFile(path)` | Read file as string (async) |
|
|
47
46
|
| `readFileSync(path)` | Read file as string (sync) |
|
|
48
|
-
| `readFileAsBuffer(path)` | Read file as Buffer
|
|
47
|
+
| `readFileAsBuffer(path)` | Read file as `Buffer` |
|
|
49
48
|
| `write(path, content)` | Write content to file |
|
|
50
49
|
| `writeAsync(path, content)` | Write content to file (async) |
|
|
51
50
|
| `exists(path)` | Check if path exists |
|
|
@@ -59,18 +58,3 @@ if (fileSystem.exists('file.txt')) {
|
|
|
59
58
|
| `gzipDir(path, extensions)` | Gzip files in directory |
|
|
60
59
|
| `isDirectory(path)` | Check if path is directory |
|
|
61
60
|
| `verifyFileExists(path)` | Throw if file doesn't exist |
|
|
62
|
-
|
|
63
|
-
## Performance
|
|
64
|
-
|
|
65
|
-
Benchmark results (Apple M4):
|
|
66
|
-
|
|
67
|
-
| Operation | BunFileSystem | NodeFileSystem |
|
|
68
|
-
| ---------------- | ------------- | -------------- |
|
|
69
|
-
| glob (100 files) | 64.85 µs | 71.06 µs |
|
|
70
|
-
| hash (1MB file) | **87 µs** | **393 µs** |
|
|
71
|
-
|
|
72
|
-
Bun adapter is **4.5x faster** for file hashing.
|
|
73
|
-
|
|
74
|
-
## License
|
|
75
|
-
|
|
76
|
-
MIT
|
package/package.json
CHANGED
package/src/adapters/node.js
CHANGED
|
@@ -2,12 +2,22 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import {
|
|
3
3
|
access as accessAsync,
|
|
4
4
|
cp as cpAsync,
|
|
5
|
+
glob as globAsync,
|
|
5
6
|
readFile as readFileAsync,
|
|
6
7
|
writeFile as writeFileAsync
|
|
7
8
|
} from "node:fs/promises";
|
|
8
9
|
import { dirname } from "node:path";
|
|
9
|
-
import fg from "fast-glob";
|
|
10
10
|
import { BaseFileSystem } from "../utils/common.js";
|
|
11
|
+
async function collectGlobMatches(pattern, options) {
|
|
12
|
+
const matches = [];
|
|
13
|
+
for await (const entry of globAsync(pattern, {
|
|
14
|
+
cwd: options.cwd ?? process.cwd(),
|
|
15
|
+
exclude: options.ignore
|
|
16
|
+
})) {
|
|
17
|
+
matches.push(entry);
|
|
18
|
+
}
|
|
19
|
+
return matches;
|
|
20
|
+
}
|
|
11
21
|
class NodeFileSystem extends BaseFileSystem {
|
|
12
22
|
async existsAsync(filePath) {
|
|
13
23
|
try {
|
|
@@ -39,11 +49,8 @@ class NodeFileSystem extends BaseFileSystem {
|
|
|
39
49
|
await cpAsync(source, destination);
|
|
40
50
|
}
|
|
41
51
|
async glob(patterns, options = {}) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
ignore: options.ignore,
|
|
45
|
-
...options
|
|
46
|
-
});
|
|
52
|
+
const files = await Promise.all(patterns.map((pattern) => collectGlobMatches(pattern, options)));
|
|
53
|
+
return Array.from(new Set(files.flat()));
|
|
47
54
|
}
|
|
48
55
|
hash(path) {
|
|
49
56
|
try {
|
package/src/adapters/node.ts
CHANGED
|
@@ -7,14 +7,27 @@ import crypto from 'node:crypto';
|
|
|
7
7
|
import {
|
|
8
8
|
access as accessAsync,
|
|
9
9
|
cp as cpAsync,
|
|
10
|
+
glob as globAsync,
|
|
10
11
|
readFile as readFileAsync,
|
|
11
12
|
writeFile as writeFileAsync,
|
|
12
13
|
} from 'node:fs/promises';
|
|
13
14
|
import { dirname } from 'node:path';
|
|
14
|
-
import fg from 'fast-glob';
|
|
15
15
|
import type { FileSystem, GlobOptions } from '../types.ts';
|
|
16
16
|
import { BaseFileSystem } from '../utils/common.ts';
|
|
17
17
|
|
|
18
|
+
async function collectGlobMatches(pattern: string, options: GlobOptions): Promise<string[]> {
|
|
19
|
+
const matches: string[] = [];
|
|
20
|
+
|
|
21
|
+
for await (const entry of globAsync(pattern, {
|
|
22
|
+
cwd: options.cwd ?? process.cwd(),
|
|
23
|
+
exclude: options.ignore,
|
|
24
|
+
})) {
|
|
25
|
+
matches.push(entry);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return matches;
|
|
29
|
+
}
|
|
30
|
+
|
|
18
31
|
/**
|
|
19
32
|
* Node.js implementation of the FileSystem interface.
|
|
20
33
|
*/
|
|
@@ -53,11 +66,8 @@ export class NodeFileSystem extends BaseFileSystem implements FileSystem {
|
|
|
53
66
|
}
|
|
54
67
|
|
|
55
68
|
async glob(patterns: string[], options: GlobOptions = {}): Promise<string[]> {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
ignore: options.ignore,
|
|
59
|
-
...options,
|
|
60
|
-
});
|
|
69
|
+
const files = await Promise.all(patterns.map((pattern) => collectGlobMatches(pattern, options)));
|
|
70
|
+
return Array.from(new Set(files.flat()));
|
|
61
71
|
}
|
|
62
72
|
|
|
63
73
|
hash(path: string): string {
|
package/src/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Automatically selects the optimal adapter based on runtime:
|
|
6
6
|
* - **Bun**: Uses `Bun.Glob`, `Bun.hash`, `Bun.file` for maximum performance
|
|
7
|
-
* - **Node.js**: Uses `
|
|
7
|
+
* - **Node.js**: Uses native `node:fs/promises.glob()` and `crypto`
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```typescript
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Automatically selects the optimal adapter based on runtime:
|
|
6
6
|
* - **Bun**: Uses `Bun.Glob`, `Bun.hash`, `Bun.file` for maximum performance
|
|
7
|
-
* - **Node.js**: Uses `
|
|
7
|
+
* - **Node.js**: Uses native `node:fs/promises.glob()` and `crypto`
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```typescript
|