@ecopages/file-system 0.2.0-alpha.0 → 0.2.0-alpha.10

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.
@@ -1,223 +0,0 @@
1
- /**
2
- * @module @ecopages/file-system/utils/common
3
- * @description Shared utilities used by both Bun and Node adapters.
4
- */
5
-
6
- import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
7
- import { cp as cpAsync, mkdir as mkdirAsync, readdir, rm as rmAsync, stat as statAsync } from 'node:fs/promises';
8
- import { dirname, extname, join as pathJoin } from 'node:path';
9
- import zlib from 'node:zlib';
10
- import { FileNotFoundError, type GlobOptions } from '../types.ts';
11
-
12
- /**
13
- * Base implementation for file system operations.
14
- * Subclasses override specific methods with runtime-optimized versions.
15
- */
16
- export abstract class BaseFileSystem {
17
- /**
18
- * Check if a file or directory exists.
19
- */
20
- exists(filePath: string): boolean {
21
- return existsSync(filePath);
22
- }
23
-
24
- /**
25
- * Verify that a file exists, throw FileNotFoundError if not.
26
- */
27
- verifyFileExists(filePath: string): void {
28
- if (!this.exists(filePath)) {
29
- throw new FileNotFoundError(filePath);
30
- }
31
- }
32
-
33
- /**
34
- * Read a file as a string synchronously.
35
- */
36
- readFileSync(filePath: string): string {
37
- try {
38
- return readFileSync(filePath, 'utf-8');
39
- } catch (error) {
40
- const message = error instanceof Error ? error.message : String(error);
41
- throw new Error(`Error reading file: ${filePath}, ${message}`, { cause: error });
42
- }
43
- }
44
-
45
- /**
46
- * Read a file as a Buffer.
47
- */
48
- readFileAsBuffer(filePath: string): Buffer {
49
- try {
50
- return readFileSync(filePath);
51
- } catch (error) {
52
- const message = error instanceof Error ? error.message : String(error);
53
- throw new Error(`Error reading file: ${filePath}, ${message}`, { cause: error });
54
- }
55
- }
56
-
57
- /**
58
- * Write contents to a file synchronously.
59
- */
60
- write(filepath: string, contents: string | Buffer): void {
61
- try {
62
- this.ensureDir(dirname(filepath));
63
- writeFileSync(filepath, contents);
64
- } catch (error) {
65
- const message = error instanceof Error ? error.message : String(error);
66
- throw new Error(`Error writing file: ${filepath}. Cause: ${message}`);
67
- }
68
- }
69
-
70
- /**
71
- * Ensure a directory exists.
72
- */
73
- ensureDir(dirPath: string, forceCleanup?: boolean): void {
74
- if (forceCleanup && existsSync(dirPath)) {
75
- rmSync(dirPath, { recursive: true, force: true });
76
- }
77
- mkdirSync(dirPath, { recursive: true });
78
- }
79
-
80
- /**
81
- * Ensure a directory exists asynchronously.
82
- */
83
- async ensureDirAsync(dirPath: string, forceCleanup?: boolean): Promise<void> {
84
- if (forceCleanup && (await this.existsAsync(dirPath))) {
85
- await rmAsync(dirPath, { recursive: true, force: true });
86
- }
87
- await mkdirAsync(dirPath, { recursive: true });
88
- }
89
-
90
- /**
91
- * Copy a directory recursively.
92
- */
93
- copyDir(source: string, destination: string): void {
94
- cpSync(source, destination, { recursive: true });
95
- }
96
-
97
- /**
98
- * Copy a directory recursively asynchronously.
99
- */
100
- async copyDirAsync(source: string, destination: string): Promise<void> {
101
- await cpAsync(source, destination, { recursive: true });
102
- }
103
-
104
- /**
105
- * Copy a file.
106
- */
107
- copyFile(source: string, destination: string): void {
108
- cpSync(source, destination);
109
- }
110
-
111
- /**
112
- * Remove all contents of a directory.
113
- */
114
- emptyDir(dirPath: string): void {
115
- if (!this.isDirectory(dirPath)) {
116
- return;
117
- }
118
- const entries = readdirSync(dirPath);
119
- for (const entry of entries) {
120
- const fullPath = pathJoin(dirPath, entry);
121
- rmSync(fullPath, { recursive: true, force: true });
122
- }
123
- }
124
-
125
- /**
126
- * Remove a directory's contents asynchronously.
127
- */
128
- async emptyDirAsync(dirPath: string): Promise<void> {
129
- if (!(await this.isDirectoryAsync(dirPath))) {
130
- return;
131
- }
132
- const entries = await readdir(dirPath);
133
- await Promise.all(entries.map((entry) => rmAsync(pathJoin(dirPath, entry), { recursive: true, force: true })));
134
- }
135
-
136
- /**
137
- * Remove a file or directory synchronously.
138
- */
139
- remove(filePath: string): void {
140
- rmSync(filePath, { recursive: true, force: true });
141
- }
142
-
143
- /**
144
- * Remove a file or directory asynchronously.
145
- */
146
- async removeAsync(filePath: string): Promise<void> {
147
- await rmAsync(filePath, { recursive: true, force: true });
148
- }
149
-
150
- /**
151
- * Check if a path is a directory.
152
- */
153
- isDirectory(filePath: string): boolean {
154
- return existsSync(filePath) && statSync(filePath).isDirectory();
155
- }
156
-
157
- /**
158
- * Check if a path is a directory asynchronously.
159
- */
160
- async isDirectoryAsync(filePath: string): Promise<boolean> {
161
- try {
162
- const stats = await statAsync(filePath);
163
- return stats.isDirectory();
164
- } catch {
165
- return false;
166
- }
167
- }
168
-
169
- /**
170
- * Gzip a single file.
171
- */
172
- gzipFile(filePath: string): void {
173
- const data = this.readFileAsBuffer(filePath);
174
- const compressedData = zlib.gzipSync(Buffer.from(data));
175
- const gzipFile = `${filePath}.gz`;
176
- writeFileSync(gzipFile, compressedData);
177
- }
178
-
179
- /**
180
- * Gzip all files with specified extensions in a directory.
181
- */
182
- gzipDir(dirPath: string, extensionsToGzip: string[]): void {
183
- const entries = readdirSync(dirPath, { recursive: true, withFileTypes: true });
184
- for (const entry of entries) {
185
- if (entry.isFile()) {
186
- const ext = extname(entry.name).slice(1);
187
- if (extensionsToGzip.includes(ext)) {
188
- this.gzipFile(pathJoin(entry.parentPath, entry.name));
189
- }
190
- }
191
- }
192
- }
193
-
194
- /**
195
- * Glob patterns - must be implemented by runtime-specific adapters.
196
- */
197
- abstract glob(patterns: string[], options?: GlobOptions): Promise<string[]>;
198
-
199
- /**
200
- * Read file async - must be implemented by runtime-specific adapters.
201
- */
202
- abstract readFile(path: string): Promise<string>;
203
-
204
- /**
205
- * Write file async - must be implemented by runtime-specific adapters.
206
- */
207
- abstract writeAsync(filepath: string, contents: string | Buffer): Promise<void>;
208
-
209
- /**
210
- * Check if a file exists asynchronously - must be implemented by runtime-specific adapters.
211
- */
212
- abstract existsAsync(filePath: string): Promise<boolean>;
213
-
214
- /**
215
- * Copy a file asynchronously - must be implemented by runtime-specific adapters.
216
- */
217
- abstract copyFileAsync(source: string, destination: string): Promise<void>;
218
-
219
- /**
220
- * Hash file - must be implemented by runtime-specific adapters.
221
- */
222
- abstract hash(path: string): string;
223
- }