-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathhost.ts
266 lines (234 loc) · 7.5 KB
/
host.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
PathLike,
Stats,
constants,
existsSync,
promises as fsPromises,
mkdirSync,
readFileSync,
readdirSync,
renameSync,
rmSync,
statSync,
writeFileSync,
} from 'node:fs';
import { dirname as pathDirname } from 'node:path';
import { Observable, map, mergeMap, from as observableFrom, publish, refCount } from 'rxjs';
import { Path, PathFragment, dirname, fragment, getSystemPath, normalize, virtualFs } from '../src';
async function exists(path: PathLike): Promise<boolean> {
try {
await fsPromises.access(path, constants.F_OK);
return true;
} catch {
return false;
}
}
// This will only be initialized if the watch() method is called.
// Otherwise chokidar appears only in type positions, and shouldn't be referenced
// in the JavaScript output.
let FSWatcher: typeof import('chokidar').FSWatcher;
function loadFSWatcher() {
if (!FSWatcher) {
try {
FSWatcher = require('chokidar').FSWatcher;
} catch (e) {
if ((e as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
throw new Error(
'As of angular-devkit version 8.0, the "chokidar" package ' +
'must be installed in order to use watch() features.',
);
}
throw e;
}
}
}
/**
* An implementation of the Virtual FS using Node as the background. There are two versions; one
* synchronous and one asynchronous.
*/
export class NodeJsAsyncHost implements virtualFs.Host<Stats> {
get capabilities(): virtualFs.HostCapabilities {
return { synchronous: false };
}
write(path: Path, content: virtualFs.FileBuffer): Observable<void> {
return observableFrom(fsPromises.mkdir(getSystemPath(dirname(path)), { recursive: true })).pipe(
mergeMap(() => fsPromises.writeFile(getSystemPath(path), new Uint8Array(content))),
);
}
read(path: Path): Observable<virtualFs.FileBuffer> {
return observableFrom(fsPromises.readFile(getSystemPath(path))).pipe(
map((buffer) => new Uint8Array(buffer).buffer as virtualFs.FileBuffer),
);
}
delete(path: Path): Observable<void> {
return observableFrom(
fsPromises.rm(getSystemPath(path), { force: true, recursive: true, maxRetries: 3 }),
);
}
rename(from: Path, to: Path): Observable<void> {
return observableFrom(fsPromises.rename(getSystemPath(from), getSystemPath(to)));
}
list(path: Path): Observable<PathFragment[]> {
return observableFrom(fsPromises.readdir(getSystemPath(path))).pipe(
map((names) => names.map((name) => fragment(name))),
);
}
exists(path: Path): Observable<boolean> {
return observableFrom(exists(getSystemPath(path)));
}
isDirectory(path: Path): Observable<boolean> {
return this.stat(path).pipe(map((stat) => stat.isDirectory()));
}
isFile(path: Path): Observable<boolean> {
return this.stat(path).pipe(map((stat) => stat.isFile()));
}
// Some hosts may not support stat.
stat(path: Path): Observable<virtualFs.Stats<Stats>> {
return observableFrom(fsPromises.stat(getSystemPath(path)));
}
// Some hosts may not support watching.
watch(
path: Path,
_options?: virtualFs.HostWatchOptions,
): Observable<virtualFs.HostWatchEvent> | null {
return new Observable<virtualFs.HostWatchEvent>((obs) => {
loadFSWatcher();
const watcher = new FSWatcher({ persistent: true });
watcher.add(getSystemPath(path));
watcher
.on('change', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Changed,
});
})
.on('add', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Created,
});
})
.on('unlink', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Deleted,
});
});
return () => {
void watcher.close();
};
}).pipe(publish(), refCount());
}
}
/**
* An implementation of the Virtual FS using Node as the backend, synchronously.
*/
export class NodeJsSyncHost implements virtualFs.Host<Stats> {
get capabilities(): virtualFs.HostCapabilities {
return { synchronous: true };
}
write(path: Path, content: virtualFs.FileBuffer): Observable<void> {
return new Observable((obs) => {
mkdirSync(getSystemPath(dirname(path)), { recursive: true });
writeFileSync(getSystemPath(path), new Uint8Array(content));
obs.next();
obs.complete();
});
}
read(path: Path): Observable<virtualFs.FileBuffer> {
return new Observable((obs) => {
const buffer = readFileSync(getSystemPath(path));
obs.next(new Uint8Array(buffer).buffer as virtualFs.FileBuffer);
obs.complete();
});
}
delete(path: Path): Observable<void> {
return new Observable<void>((obs) => {
rmSync(getSystemPath(path), { force: true, recursive: true, maxRetries: 3 });
obs.complete();
});
}
rename(from: Path, to: Path): Observable<void> {
return new Observable((obs) => {
const toSystemPath = getSystemPath(to);
mkdirSync(pathDirname(toSystemPath), { recursive: true });
renameSync(getSystemPath(from), toSystemPath);
obs.next();
obs.complete();
});
}
list(path: Path): Observable<PathFragment[]> {
return new Observable((obs) => {
const names = readdirSync(getSystemPath(path));
obs.next(names.map((name) => fragment(name)));
obs.complete();
});
}
exists(path: Path): Observable<boolean> {
return new Observable((obs) => {
obs.next(existsSync(getSystemPath(path)));
obs.complete();
});
}
isDirectory(path: Path): Observable<boolean> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.stat(path)!.pipe(map((stat) => stat.isDirectory()));
}
isFile(path: Path): Observable<boolean> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.stat(path)!.pipe(map((stat) => stat.isFile()));
}
// Some hosts may not support stat.
stat(path: Path): Observable<virtualFs.Stats<Stats>> {
return new Observable((obs) => {
obs.next(statSync(getSystemPath(path)));
obs.complete();
});
}
// Some hosts may not support watching.
watch(
path: Path,
_options?: virtualFs.HostWatchOptions,
): Observable<virtualFs.HostWatchEvent> | null {
return new Observable<virtualFs.HostWatchEvent>((obs) => {
loadFSWatcher();
const watcher = new FSWatcher({ persistent: false });
watcher.add(getSystemPath(path));
watcher
.on('change', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Changed,
});
})
.on('add', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Created,
});
})
.on('unlink', (path) => {
obs.next({
path: normalize(path),
time: new Date(),
type: virtualFs.HostWatchEventType.Deleted,
});
});
return () => {
void watcher.close();
};
}).pipe(publish(), refCount());
}
}