@statewalker/webrun-files-node 0.7.0
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/LICENSE +21 -0
- package/README.md +152 -0
- package/dist/cjs/index.cjs +233 -0
- package/dist/esm/index.js +205 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/node-files-api.d.ts +27 -0
- package/dist/node-files-api.d.ts.map +1 -0
- package/package.json +51 -0
- package/src/index.ts +1 -0
- package/src/node-files-api.ts +233 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 statewalker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @statewalker/webrun-files-node
|
|
2
|
+
|
|
3
|
+
Node.js filesystem implementation of the `FilesApi` interface from `@statewalker/webrun-files`.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides a `FilesApi` implementation that works with the real filesystem using Node.js `fs/promises`. It maps virtual paths (starting with `/`) to a root directory on disk.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @statewalker/webrun-files-node @statewalker/webrun-files
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { NodeFilesApi } from '@statewalker/webrun-files-node';
|
|
21
|
+
import { readText, writeText } from '@statewalker/webrun-files';
|
|
22
|
+
|
|
23
|
+
// Create API rooted at a specific directory
|
|
24
|
+
const files = new NodeFilesApi({ rootDir: '/var/app/data' });
|
|
25
|
+
|
|
26
|
+
// Write a file - creates /var/app/data/config.json
|
|
27
|
+
await writeText(files, '/config.json', '{"debug": true}');
|
|
28
|
+
|
|
29
|
+
// Read it back
|
|
30
|
+
const content = await readText(files, '/config.json');
|
|
31
|
+
console.log(content); // {"debug": true}
|
|
32
|
+
|
|
33
|
+
// List files
|
|
34
|
+
for await (const entry of files.list('/')) {
|
|
35
|
+
console.log(entry.name, entry.kind, entry.size);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Default Root Directory
|
|
40
|
+
|
|
41
|
+
If no `rootDir` is specified, the current working directory is used:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { NodeFilesApi } from '@statewalker/webrun-files-node';
|
|
45
|
+
|
|
46
|
+
const files = new NodeFilesApi();
|
|
47
|
+
// Virtual path /data/file.txt maps to ./data/file.txt
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Path Mapping
|
|
51
|
+
|
|
52
|
+
Virtual paths are mapped directly to the filesystem:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
rootDir: "/var/app/data"
|
|
56
|
+
virtual path: "/users/alice.json"
|
|
57
|
+
real path: "/var/app/data/users/alice.json"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API Reference
|
|
61
|
+
|
|
62
|
+
### NodeFilesApi
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
interface NodeFilesApiOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Root directory for file operations.
|
|
68
|
+
* All paths are resolved relative to this directory.
|
|
69
|
+
* Defaults to current working directory if not specified.
|
|
70
|
+
*/
|
|
71
|
+
rootDir?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
class NodeFilesApi implements FilesApi {
|
|
75
|
+
constructor(options?: NodeFilesApiOptions);
|
|
76
|
+
|
|
77
|
+
// All FilesApi methods
|
|
78
|
+
read(path: string, options?: ReadOptions): AsyncIterable<Uint8Array>;
|
|
79
|
+
write(path: string, content: Iterable<Uint8Array> | AsyncIterable<Uint8Array>): Promise<void>;
|
|
80
|
+
mkdir(path: string): Promise<void>;
|
|
81
|
+
list(path: string, options?: ListOptions): AsyncIterable<FileInfo>;
|
|
82
|
+
stats(path: string): Promise<FileStats | undefined>;
|
|
83
|
+
exists(path: string): Promise<boolean>;
|
|
84
|
+
remove(path: string): Promise<boolean>;
|
|
85
|
+
move(source: string, target: string): Promise<boolean>;
|
|
86
|
+
copy(source: string, target: string): Promise<boolean>;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Features
|
|
91
|
+
|
|
92
|
+
### Efficient Streaming
|
|
93
|
+
|
|
94
|
+
Large files are read in chunks without loading entirely into memory:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Stream a large file
|
|
98
|
+
for await (const chunk of files.read('/large-video.mp4')) {
|
|
99
|
+
// Process each chunk (default 8KB chunks)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Read a specific range
|
|
103
|
+
for await (const chunk of files.read('/large-file.bin', { start: 1000, length: 500 })) {
|
|
104
|
+
// Only bytes 1000-1499
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Automatic Directory Creation
|
|
109
|
+
|
|
110
|
+
Parent directories are created automatically when writing files:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// Creates /var/app/data/deep/nested/path/ automatically
|
|
114
|
+
await writeText(files, '/deep/nested/path/file.txt', 'content');
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Recursive Operations
|
|
118
|
+
|
|
119
|
+
Copy and remove work recursively on directories:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// Copy entire directory tree
|
|
123
|
+
await files.copy('/source', '/destination');
|
|
124
|
+
|
|
125
|
+
// Remove directory and all contents
|
|
126
|
+
await files.remove('/old-data');
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Testing with Temporary Directories
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
133
|
+
import { tmpdir } from 'node:os';
|
|
134
|
+
import { join } from 'node:path';
|
|
135
|
+
import { NodeFilesApi } from '@statewalker/webrun-files-node';
|
|
136
|
+
|
|
137
|
+
// Create a temp directory for testing
|
|
138
|
+
const tempDir = await mkdtemp(join(tmpdir(), 'test-'));
|
|
139
|
+
const files = new NodeFilesApi({ rootDir: tempDir });
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
// Run tests...
|
|
143
|
+
await writeText(files, '/test.txt', 'test content');
|
|
144
|
+
} finally {
|
|
145
|
+
// Cleanup
|
|
146
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
let node_fs_promises = require("node:fs/promises");
|
|
29
|
+
node_fs_promises = __toESM(node_fs_promises);
|
|
30
|
+
|
|
31
|
+
//#region ../webrun-files/dist/esm/index.js
|
|
32
|
+
/**
|
|
33
|
+
* Path manipulation utilities for virtual filesystem paths.
|
|
34
|
+
* All paths use forward slashes and start with "/".
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Normalizes a file path to a consistent format.
|
|
38
|
+
* - Adds leading slash if missing
|
|
39
|
+
* - Removes trailing slash
|
|
40
|
+
* - Removes `.` segments
|
|
41
|
+
* - Collapses multiple slashes
|
|
42
|
+
*/
|
|
43
|
+
function normalizePath(filePath) {
|
|
44
|
+
const segments = filePath.split("/").filter((s) => !!s && s !== ".");
|
|
45
|
+
if (segments.length === 0) return "/";
|
|
46
|
+
return `/${segments.join("/")}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Joins path segments with proper normalization.
|
|
50
|
+
*/
|
|
51
|
+
function joinPath(...segments) {
|
|
52
|
+
return normalizePath(segments.join("/"));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns the directory portion of a path.
|
|
56
|
+
*/
|
|
57
|
+
function dirname(path) {
|
|
58
|
+
const normalized = normalizePath(path);
|
|
59
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
60
|
+
if (lastSlash <= 0) return "/";
|
|
61
|
+
return normalized.substring(0, lastSlash);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/node-files-api.ts
|
|
66
|
+
/**
|
|
67
|
+
* Node.js FilesApi implementation using fs/promises.
|
|
68
|
+
* Maps virtual paths (starting with /) to the filesystem rooted at rootDir.
|
|
69
|
+
*/
|
|
70
|
+
var NodeFilesApi = class {
|
|
71
|
+
rootDir;
|
|
72
|
+
constructor(options) {
|
|
73
|
+
this.rootDir = options?.rootDir ?? process.cwd();
|
|
74
|
+
}
|
|
75
|
+
resolvePath(virtualPath) {
|
|
76
|
+
const normalized = normalizePath(virtualPath);
|
|
77
|
+
return this.rootDir + normalized;
|
|
78
|
+
}
|
|
79
|
+
async *read(path, options) {
|
|
80
|
+
const realPath = this.resolvePath(path);
|
|
81
|
+
try {
|
|
82
|
+
const stat = await node_fs_promises.stat(realPath);
|
|
83
|
+
if (!stat.isFile()) return;
|
|
84
|
+
const handle = await node_fs_promises.open(realPath, "r");
|
|
85
|
+
try {
|
|
86
|
+
const start = options?.start ?? 0;
|
|
87
|
+
const length = options?.length;
|
|
88
|
+
const end = length !== void 0 ? start + length : stat.size;
|
|
89
|
+
if (start >= stat.size) return;
|
|
90
|
+
const bufferSize = 8192;
|
|
91
|
+
let position = start;
|
|
92
|
+
const actualEnd = Math.min(end, stat.size);
|
|
93
|
+
while (position < actualEnd) {
|
|
94
|
+
const remaining = actualEnd - position;
|
|
95
|
+
const toRead = Math.min(bufferSize, remaining);
|
|
96
|
+
const buffer = new Uint8Array(toRead);
|
|
97
|
+
const { bytesRead } = await handle.read(buffer, 0, toRead, position);
|
|
98
|
+
if (bytesRead === 0) break;
|
|
99
|
+
yield buffer.subarray(0, bytesRead);
|
|
100
|
+
position += bytesRead;
|
|
101
|
+
}
|
|
102
|
+
} finally {
|
|
103
|
+
await handle.close();
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async write(path, content) {
|
|
110
|
+
const realPath = this.resolvePath(path);
|
|
111
|
+
const normalized = normalizePath(path);
|
|
112
|
+
const dir = this.rootDir + dirname(normalized);
|
|
113
|
+
await node_fs_promises.mkdir(dir, { recursive: true });
|
|
114
|
+
const chunks = [];
|
|
115
|
+
let totalLength = 0;
|
|
116
|
+
for await (const chunk of content) {
|
|
117
|
+
chunks.push(chunk);
|
|
118
|
+
totalLength += chunk.length;
|
|
119
|
+
}
|
|
120
|
+
const result = new Uint8Array(totalLength);
|
|
121
|
+
let offset = 0;
|
|
122
|
+
for (const chunk of chunks) {
|
|
123
|
+
result.set(chunk, offset);
|
|
124
|
+
offset += chunk.length;
|
|
125
|
+
}
|
|
126
|
+
await node_fs_promises.writeFile(realPath, result);
|
|
127
|
+
}
|
|
128
|
+
async mkdir(path) {
|
|
129
|
+
const realPath = this.resolvePath(path);
|
|
130
|
+
await node_fs_promises.mkdir(realPath, { recursive: true });
|
|
131
|
+
}
|
|
132
|
+
async *list(path, options) {
|
|
133
|
+
const realPath = this.resolvePath(path);
|
|
134
|
+
const normalizedPath = normalizePath(path);
|
|
135
|
+
try {
|
|
136
|
+
const entries = await node_fs_promises.readdir(realPath, { withFileTypes: true });
|
|
137
|
+
for (const entry of entries) {
|
|
138
|
+
const entryPath = joinPath(normalizedPath, entry.name);
|
|
139
|
+
const fullPath = this.resolvePath(entryPath);
|
|
140
|
+
let stat;
|
|
141
|
+
try {
|
|
142
|
+
stat = await node_fs_promises.stat(fullPath);
|
|
143
|
+
} catch {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
yield {
|
|
147
|
+
name: entry.name,
|
|
148
|
+
path: entryPath,
|
|
149
|
+
kind: entry.isDirectory() ? "directory" : "file",
|
|
150
|
+
size: stat.size,
|
|
151
|
+
lastModified: stat.mtimeMs
|
|
152
|
+
};
|
|
153
|
+
if (options?.recursive && entry.isDirectory()) yield* this.list(entryPath, options);
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async stats(path) {
|
|
160
|
+
const realPath = this.resolvePath(path);
|
|
161
|
+
try {
|
|
162
|
+
const stat = await node_fs_promises.stat(realPath);
|
|
163
|
+
return {
|
|
164
|
+
kind: stat.isDirectory() ? "directory" : "file",
|
|
165
|
+
size: stat.size,
|
|
166
|
+
lastModified: stat.mtimeMs
|
|
167
|
+
};
|
|
168
|
+
} catch {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async exists(path) {
|
|
173
|
+
const realPath = this.resolvePath(path);
|
|
174
|
+
try {
|
|
175
|
+
await node_fs_promises.access(realPath);
|
|
176
|
+
return true;
|
|
177
|
+
} catch {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async remove(path) {
|
|
182
|
+
const realPath = this.resolvePath(path);
|
|
183
|
+
try {
|
|
184
|
+
if ((await node_fs_promises.stat(realPath)).isDirectory()) await node_fs_promises.rm(realPath, {
|
|
185
|
+
recursive: true,
|
|
186
|
+
force: true
|
|
187
|
+
});
|
|
188
|
+
else await node_fs_promises.unlink(realPath);
|
|
189
|
+
return true;
|
|
190
|
+
} catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
async move(source, target) {
|
|
195
|
+
const sourcePath = this.resolvePath(source);
|
|
196
|
+
const targetPath = this.resolvePath(target);
|
|
197
|
+
const normalizedTarget = normalizePath(target);
|
|
198
|
+
try {
|
|
199
|
+
await node_fs_promises.access(sourcePath);
|
|
200
|
+
} catch {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
205
|
+
await node_fs_promises.mkdir(targetDir, { recursive: true });
|
|
206
|
+
await node_fs_promises.rename(sourcePath, targetPath);
|
|
207
|
+
return true;
|
|
208
|
+
} catch {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async copy(source, target) {
|
|
213
|
+
const sourcePath = this.resolvePath(source);
|
|
214
|
+
const targetPath = this.resolvePath(target);
|
|
215
|
+
const normalizedTarget = normalizePath(target);
|
|
216
|
+
try {
|
|
217
|
+
await node_fs_promises.access(sourcePath);
|
|
218
|
+
} catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
223
|
+
await node_fs_promises.mkdir(targetDir, { recursive: true });
|
|
224
|
+
await node_fs_promises.cp(sourcePath, targetPath, { recursive: true });
|
|
225
|
+
return true;
|
|
226
|
+
} catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
//#endregion
|
|
233
|
+
exports.NodeFilesApi = NodeFilesApi;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
//#region ../webrun-files/dist/esm/index.js
|
|
4
|
+
/**
|
|
5
|
+
* Path manipulation utilities for virtual filesystem paths.
|
|
6
|
+
* All paths use forward slashes and start with "/".
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes a file path to a consistent format.
|
|
10
|
+
* - Adds leading slash if missing
|
|
11
|
+
* - Removes trailing slash
|
|
12
|
+
* - Removes `.` segments
|
|
13
|
+
* - Collapses multiple slashes
|
|
14
|
+
*/
|
|
15
|
+
function normalizePath(filePath) {
|
|
16
|
+
const segments = filePath.split("/").filter((s) => !!s && s !== ".");
|
|
17
|
+
if (segments.length === 0) return "/";
|
|
18
|
+
return `/${segments.join("/")}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Joins path segments with proper normalization.
|
|
22
|
+
*/
|
|
23
|
+
function joinPath(...segments) {
|
|
24
|
+
return normalizePath(segments.join("/"));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns the directory portion of a path.
|
|
28
|
+
*/
|
|
29
|
+
function dirname(path) {
|
|
30
|
+
const normalized = normalizePath(path);
|
|
31
|
+
const lastSlash = normalized.lastIndexOf("/");
|
|
32
|
+
if (lastSlash <= 0) return "/";
|
|
33
|
+
return normalized.substring(0, lastSlash);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/node-files-api.ts
|
|
38
|
+
/**
|
|
39
|
+
* Node.js FilesApi implementation using fs/promises.
|
|
40
|
+
* Maps virtual paths (starting with /) to the filesystem rooted at rootDir.
|
|
41
|
+
*/
|
|
42
|
+
var NodeFilesApi = class {
|
|
43
|
+
rootDir;
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.rootDir = options?.rootDir ?? process.cwd();
|
|
46
|
+
}
|
|
47
|
+
resolvePath(virtualPath) {
|
|
48
|
+
const normalized = normalizePath(virtualPath);
|
|
49
|
+
return this.rootDir + normalized;
|
|
50
|
+
}
|
|
51
|
+
async *read(path, options) {
|
|
52
|
+
const realPath = this.resolvePath(path);
|
|
53
|
+
try {
|
|
54
|
+
const stat = await fs.stat(realPath);
|
|
55
|
+
if (!stat.isFile()) return;
|
|
56
|
+
const handle = await fs.open(realPath, "r");
|
|
57
|
+
try {
|
|
58
|
+
const start = options?.start ?? 0;
|
|
59
|
+
const length = options?.length;
|
|
60
|
+
const end = length !== void 0 ? start + length : stat.size;
|
|
61
|
+
if (start >= stat.size) return;
|
|
62
|
+
const bufferSize = 8192;
|
|
63
|
+
let position = start;
|
|
64
|
+
const actualEnd = Math.min(end, stat.size);
|
|
65
|
+
while (position < actualEnd) {
|
|
66
|
+
const remaining = actualEnd - position;
|
|
67
|
+
const toRead = Math.min(bufferSize, remaining);
|
|
68
|
+
const buffer = new Uint8Array(toRead);
|
|
69
|
+
const { bytesRead } = await handle.read(buffer, 0, toRead, position);
|
|
70
|
+
if (bytesRead === 0) break;
|
|
71
|
+
yield buffer.subarray(0, bytesRead);
|
|
72
|
+
position += bytesRead;
|
|
73
|
+
}
|
|
74
|
+
} finally {
|
|
75
|
+
await handle.close();
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async write(path, content) {
|
|
82
|
+
const realPath = this.resolvePath(path);
|
|
83
|
+
const normalized = normalizePath(path);
|
|
84
|
+
const dir = this.rootDir + dirname(normalized);
|
|
85
|
+
await fs.mkdir(dir, { recursive: true });
|
|
86
|
+
const chunks = [];
|
|
87
|
+
let totalLength = 0;
|
|
88
|
+
for await (const chunk of content) {
|
|
89
|
+
chunks.push(chunk);
|
|
90
|
+
totalLength += chunk.length;
|
|
91
|
+
}
|
|
92
|
+
const result = new Uint8Array(totalLength);
|
|
93
|
+
let offset = 0;
|
|
94
|
+
for (const chunk of chunks) {
|
|
95
|
+
result.set(chunk, offset);
|
|
96
|
+
offset += chunk.length;
|
|
97
|
+
}
|
|
98
|
+
await fs.writeFile(realPath, result);
|
|
99
|
+
}
|
|
100
|
+
async mkdir(path) {
|
|
101
|
+
const realPath = this.resolvePath(path);
|
|
102
|
+
await fs.mkdir(realPath, { recursive: true });
|
|
103
|
+
}
|
|
104
|
+
async *list(path, options) {
|
|
105
|
+
const realPath = this.resolvePath(path);
|
|
106
|
+
const normalizedPath = normalizePath(path);
|
|
107
|
+
try {
|
|
108
|
+
const entries = await fs.readdir(realPath, { withFileTypes: true });
|
|
109
|
+
for (const entry of entries) {
|
|
110
|
+
const entryPath = joinPath(normalizedPath, entry.name);
|
|
111
|
+
const fullPath = this.resolvePath(entryPath);
|
|
112
|
+
let stat;
|
|
113
|
+
try {
|
|
114
|
+
stat = await fs.stat(fullPath);
|
|
115
|
+
} catch {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
yield {
|
|
119
|
+
name: entry.name,
|
|
120
|
+
path: entryPath,
|
|
121
|
+
kind: entry.isDirectory() ? "directory" : "file",
|
|
122
|
+
size: stat.size,
|
|
123
|
+
lastModified: stat.mtimeMs
|
|
124
|
+
};
|
|
125
|
+
if (options?.recursive && entry.isDirectory()) yield* this.list(entryPath, options);
|
|
126
|
+
}
|
|
127
|
+
} catch {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async stats(path) {
|
|
132
|
+
const realPath = this.resolvePath(path);
|
|
133
|
+
try {
|
|
134
|
+
const stat = await fs.stat(realPath);
|
|
135
|
+
return {
|
|
136
|
+
kind: stat.isDirectory() ? "directory" : "file",
|
|
137
|
+
size: stat.size,
|
|
138
|
+
lastModified: stat.mtimeMs
|
|
139
|
+
};
|
|
140
|
+
} catch {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async exists(path) {
|
|
145
|
+
const realPath = this.resolvePath(path);
|
|
146
|
+
try {
|
|
147
|
+
await fs.access(realPath);
|
|
148
|
+
return true;
|
|
149
|
+
} catch {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async remove(path) {
|
|
154
|
+
const realPath = this.resolvePath(path);
|
|
155
|
+
try {
|
|
156
|
+
if ((await fs.stat(realPath)).isDirectory()) await fs.rm(realPath, {
|
|
157
|
+
recursive: true,
|
|
158
|
+
force: true
|
|
159
|
+
});
|
|
160
|
+
else await fs.unlink(realPath);
|
|
161
|
+
return true;
|
|
162
|
+
} catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async move(source, target) {
|
|
167
|
+
const sourcePath = this.resolvePath(source);
|
|
168
|
+
const targetPath = this.resolvePath(target);
|
|
169
|
+
const normalizedTarget = normalizePath(target);
|
|
170
|
+
try {
|
|
171
|
+
await fs.access(sourcePath);
|
|
172
|
+
} catch {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
177
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
178
|
+
await fs.rename(sourcePath, targetPath);
|
|
179
|
+
return true;
|
|
180
|
+
} catch {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async copy(source, target) {
|
|
185
|
+
const sourcePath = this.resolvePath(source);
|
|
186
|
+
const targetPath = this.resolvePath(target);
|
|
187
|
+
const normalizedTarget = normalizePath(target);
|
|
188
|
+
try {
|
|
189
|
+
await fs.access(sourcePath);
|
|
190
|
+
} catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
195
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
196
|
+
await fs.cp(sourcePath, targetPath, { recursive: true });
|
|
197
|
+
return true;
|
|
198
|
+
} catch {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
//#endregion
|
|
205
|
+
export { NodeFilesApi };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { FileInfo, FileStats, FilesApi, ListOptions, ReadOptions } from "@statewalker/webrun-files";
|
|
2
|
+
export interface NodeFilesApiOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Root directory for file operations. All paths are resolved relative to this.
|
|
5
|
+
* Defaults to current working directory if not specified.
|
|
6
|
+
*/
|
|
7
|
+
rootDir?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Node.js FilesApi implementation using fs/promises.
|
|
11
|
+
* Maps virtual paths (starting with /) to the filesystem rooted at rootDir.
|
|
12
|
+
*/
|
|
13
|
+
export declare class NodeFilesApi implements FilesApi {
|
|
14
|
+
private rootDir;
|
|
15
|
+
constructor(options?: NodeFilesApiOptions);
|
|
16
|
+
private resolvePath;
|
|
17
|
+
read(path: string, options?: ReadOptions): AsyncIterable<Uint8Array>;
|
|
18
|
+
write(path: string, content: Iterable<Uint8Array> | AsyncIterable<Uint8Array>): Promise<void>;
|
|
19
|
+
mkdir(path: string): Promise<void>;
|
|
20
|
+
list(path: string, options?: ListOptions): AsyncIterable<FileInfo>;
|
|
21
|
+
stats(path: string): Promise<FileStats | undefined>;
|
|
22
|
+
exists(path: string): Promise<boolean>;
|
|
23
|
+
remove(path: string): Promise<boolean>;
|
|
24
|
+
move(source: string, target: string): Promise<boolean>;
|
|
25
|
+
copy(source: string, target: string): Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=node-files-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node-files-api.d.ts","sourceRoot":"","sources":["../src/node-files-api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,WAAW,EACX,WAAW,EACZ,MAAM,2BAA2B,CAAC;AAGnC,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,qBAAa,YAAa,YAAW,QAAQ;IAC3C,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,CAAC,EAAE,mBAAmB;IAIzC,OAAO,CAAC,WAAW;IAKZ,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,aAAa,CAAC,UAAU,CAAC;IA2CrE,KAAK,CACT,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,QAAQ,CAAC,UAAU,CAAC,GAAG,aAAa,CAAC,UAAU,CAAC,GACxD,OAAO,CAAC,IAAI,CAAC;IAyBV,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKjC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,aAAa,CAAC,QAAQ,CAAC;IAsCnE,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAenD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAWtC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBtC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAqBtD,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAoB7D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statewalker/webrun-files-node",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Node.js FilesApi implementation",
|
|
7
|
+
"homepage": "https://github.com/statewalker/webrun-files",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Mikhail Kotelnikov",
|
|
10
|
+
"email": "mikhail.kotelnikov@gmail.com"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git@github.com:statewalker/webrun-files.git"
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist/cjs/index.cjs",
|
|
18
|
+
"module": "./dist/esm/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/esm/index.js",
|
|
24
|
+
"require": "./dist/cjs/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@statewalker/webrun-files": "0.7.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^24.10.1",
|
|
36
|
+
"rimraf": "^6.1.2",
|
|
37
|
+
"rolldown": "^1.0.0-beta.53",
|
|
38
|
+
"typescript": "^5.6.0",
|
|
39
|
+
"vitest": "^4.0.12",
|
|
40
|
+
"@statewalker/webrun-files-tests": "1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"sideEffects": false,
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "rimraf dist && rolldown -c && tsc --emitDeclarationOnly --declaration",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"lint": "biome lint src tests"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { NodeFilesApi, type NodeFilesApiOptions } from "./node-files-api.js";
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { Stats } from "node:fs";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import type {
|
|
4
|
+
FileInfo,
|
|
5
|
+
FileStats,
|
|
6
|
+
FilesApi,
|
|
7
|
+
ListOptions,
|
|
8
|
+
ReadOptions,
|
|
9
|
+
} from "@statewalker/webrun-files";
|
|
10
|
+
import { dirname, joinPath, normalizePath } from "@statewalker/webrun-files";
|
|
11
|
+
|
|
12
|
+
export interface NodeFilesApiOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Root directory for file operations. All paths are resolved relative to this.
|
|
15
|
+
* Defaults to current working directory if not specified.
|
|
16
|
+
*/
|
|
17
|
+
rootDir?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Node.js FilesApi implementation using fs/promises.
|
|
22
|
+
* Maps virtual paths (starting with /) to the filesystem rooted at rootDir.
|
|
23
|
+
*/
|
|
24
|
+
export class NodeFilesApi implements FilesApi {
|
|
25
|
+
private rootDir: string;
|
|
26
|
+
|
|
27
|
+
constructor(options?: NodeFilesApiOptions) {
|
|
28
|
+
this.rootDir = options?.rootDir ?? process.cwd();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private resolvePath(virtualPath: string): string {
|
|
32
|
+
const normalized = normalizePath(virtualPath);
|
|
33
|
+
return this.rootDir + normalized;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async *read(path: string, options?: ReadOptions): AsyncIterable<Uint8Array> {
|
|
37
|
+
const realPath = this.resolvePath(path);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const stat = await fs.stat(realPath);
|
|
41
|
+
if (!stat.isFile()) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const handle = await fs.open(realPath, "r");
|
|
46
|
+
try {
|
|
47
|
+
const start = options?.start ?? 0;
|
|
48
|
+
const length = options?.length;
|
|
49
|
+
const end = length !== undefined ? start + length : stat.size;
|
|
50
|
+
|
|
51
|
+
if (start >= stat.size) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const bufferSize = 8192;
|
|
56
|
+
let position = start;
|
|
57
|
+
const actualEnd = Math.min(end, stat.size);
|
|
58
|
+
|
|
59
|
+
while (position < actualEnd) {
|
|
60
|
+
const remaining = actualEnd - position;
|
|
61
|
+
const toRead = Math.min(bufferSize, remaining);
|
|
62
|
+
const buffer = new Uint8Array(toRead);
|
|
63
|
+
|
|
64
|
+
const { bytesRead } = await handle.read(buffer, 0, toRead, position);
|
|
65
|
+
if (bytesRead === 0) break;
|
|
66
|
+
|
|
67
|
+
yield buffer.subarray(0, bytesRead);
|
|
68
|
+
position += bytesRead;
|
|
69
|
+
}
|
|
70
|
+
} finally {
|
|
71
|
+
await handle.close();
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// File doesn't exist or can't be read
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async write(
|
|
80
|
+
path: string,
|
|
81
|
+
content: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
const realPath = this.resolvePath(path);
|
|
84
|
+
const normalized = normalizePath(path);
|
|
85
|
+
const dir = this.rootDir + dirname(normalized);
|
|
86
|
+
|
|
87
|
+
await fs.mkdir(dir, { recursive: true });
|
|
88
|
+
|
|
89
|
+
const chunks: Uint8Array[] = [];
|
|
90
|
+
let totalLength = 0;
|
|
91
|
+
|
|
92
|
+
for await (const chunk of content) {
|
|
93
|
+
chunks.push(chunk);
|
|
94
|
+
totalLength += chunk.length;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const result = new Uint8Array(totalLength);
|
|
98
|
+
let offset = 0;
|
|
99
|
+
for (const chunk of chunks) {
|
|
100
|
+
result.set(chunk, offset);
|
|
101
|
+
offset += chunk.length;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
await fs.writeFile(realPath, result);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async mkdir(path: string): Promise<void> {
|
|
108
|
+
const realPath = this.resolvePath(path);
|
|
109
|
+
await fs.mkdir(realPath, { recursive: true });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async *list(path: string, options?: ListOptions): AsyncIterable<FileInfo> {
|
|
113
|
+
const realPath = this.resolvePath(path);
|
|
114
|
+
const normalizedPath = normalizePath(path);
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const entries = await fs.readdir(realPath, { withFileTypes: true });
|
|
118
|
+
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
const entryPath = joinPath(normalizedPath, entry.name);
|
|
121
|
+
const fullPath = this.resolvePath(entryPath);
|
|
122
|
+
|
|
123
|
+
let stat: Stats;
|
|
124
|
+
try {
|
|
125
|
+
stat = await fs.stat(fullPath);
|
|
126
|
+
} catch {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const info: FileInfo = {
|
|
131
|
+
name: entry.name,
|
|
132
|
+
path: entryPath,
|
|
133
|
+
kind: entry.isDirectory() ? "directory" : "file",
|
|
134
|
+
size: stat.size,
|
|
135
|
+
lastModified: stat.mtimeMs,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
yield info;
|
|
139
|
+
|
|
140
|
+
if (options?.recursive && entry.isDirectory()) {
|
|
141
|
+
yield* this.list(entryPath, options);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
} catch {
|
|
145
|
+
// Directory doesn't exist or can't be read
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async stats(path: string): Promise<FileStats | undefined> {
|
|
151
|
+
const realPath = this.resolvePath(path);
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const stat = await fs.stat(realPath);
|
|
155
|
+
return {
|
|
156
|
+
kind: stat.isDirectory() ? "directory" : "file",
|
|
157
|
+
size: stat.size,
|
|
158
|
+
lastModified: stat.mtimeMs,
|
|
159
|
+
};
|
|
160
|
+
} catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async exists(path: string): Promise<boolean> {
|
|
166
|
+
const realPath = this.resolvePath(path);
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
await fs.access(realPath);
|
|
170
|
+
return true;
|
|
171
|
+
} catch {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async remove(path: string): Promise<boolean> {
|
|
177
|
+
const realPath = this.resolvePath(path);
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
const stat = await fs.stat(realPath);
|
|
181
|
+
if (stat.isDirectory()) {
|
|
182
|
+
await fs.rm(realPath, { recursive: true, force: true });
|
|
183
|
+
} else {
|
|
184
|
+
await fs.unlink(realPath);
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
} catch {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async move(source: string, target: string): Promise<boolean> {
|
|
193
|
+
const sourcePath = this.resolvePath(source);
|
|
194
|
+
const targetPath = this.resolvePath(target);
|
|
195
|
+
const normalizedTarget = normalizePath(target);
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
await fs.access(sourcePath);
|
|
199
|
+
} catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
205
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
206
|
+
await fs.rename(sourcePath, targetPath);
|
|
207
|
+
return true;
|
|
208
|
+
} catch {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async copy(source: string, target: string): Promise<boolean> {
|
|
214
|
+
const sourcePath = this.resolvePath(source);
|
|
215
|
+
const targetPath = this.resolvePath(target);
|
|
216
|
+
const normalizedTarget = normalizePath(target);
|
|
217
|
+
|
|
218
|
+
try {
|
|
219
|
+
await fs.access(sourcePath);
|
|
220
|
+
} catch {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const targetDir = this.rootDir + dirname(normalizedTarget);
|
|
226
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
227
|
+
await fs.cp(sourcePath, targetPath, { recursive: true });
|
|
228
|
+
return true;
|
|
229
|
+
} catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|