Skip to content

Commit a7e0f83

Browse files
committed
feat(@angular-devkit/core): add a recording host
Doing changes to that host will create records for CREATE, OVERWRITE, RENAME and DELETE. Those records can then be applied to other hosts. This directly goes in sync with Tree, and will replace the backend for Trees.
1 parent 25e9da3 commit a7e0f83

File tree

5 files changed

+1040
-24
lines changed

5 files changed

+1040
-24
lines changed

packages/angular_devkit/core/src/exception/exception.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export class BaseException extends Error {
1313
}
1414

1515

16+
export class UnknownException extends Error {
17+
constructor(message: string) { super(message); }
18+
}
19+
20+
1621
// Exceptions
1722
export class FileDoesNotExistException extends BaseException {
1823
constructor(path: string) { super(`Path "${path}" does not exist.`); }

packages/angular_devkit/core/src/virtual-fs/host/memory.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ export interface SimpleMemoryHostStats {
3838
}
3939

4040
export class SimpleMemoryHost implements Host<{}> {
41-
private _cache = new Map<Path, Stats<SimpleMemoryHostStats>>();
41+
protected readonly _cache = new Map<Path, Stats<SimpleMemoryHostStats>>();
4242
private _watchers = new Map<Path, [HostWatchOptions, Subject<HostWatchEvent>][]>();
4343

4444
protected _newDirStats() {
4545
return {
46+
inspect() { return '<Directory>'; },
47+
4648
isFile() { return false; },
4749
isDirectory() { return true; },
4850
size: 0,
@@ -57,6 +59,8 @@ export class SimpleMemoryHost implements Host<{}> {
5759
}
5860
protected _newFileStats(content: FileBuffer, oldStats?: Stats<SimpleMemoryHostStats>) {
5961
return {
62+
inspect() { return `<File size(${content.byteLength})>`; },
63+
6064
isFile() { return true; },
6165
isDirectory() { return false; },
6266
size: content.byteLength,
@@ -158,7 +162,7 @@ export class SimpleMemoryHost implements Host<{}> {
158162
this._cache.set(path, stats);
159163
this._updateWatchers(path, old ? HostWatchEventType.Changed : HostWatchEventType.Created);
160164
}
161-
_read(path: Path): FileBuffer {
165+
protected _read(path: Path): FileBuffer {
162166
path = this._toAbsolute(path);
163167
const maybeStats = this._cache.get(path);
164168
if (!maybeStats) {
@@ -171,7 +175,7 @@ export class SimpleMemoryHost implements Host<{}> {
171175
return maybeStats.content;
172176
}
173177
}
174-
_delete(path: Path): void {
178+
protected _delete(path: Path): void {
175179
path = this._toAbsolute(path);
176180
if (this._isDirectory(path)) {
177181
for (const [cachePath, _] of this._cache.entries()) {
@@ -184,7 +188,7 @@ export class SimpleMemoryHost implements Host<{}> {
184188
}
185189
this._updateWatchers(path, HostWatchEventType.Deleted);
186190
}
187-
_rename(from: Path, to: Path): void {
191+
protected _rename(from: Path, to: Path): void {
188192
from = this._toAbsolute(from);
189193
to = this._toAbsolute(to);
190194
if (!this._cache.has(from)) {
@@ -214,7 +218,7 @@ export class SimpleMemoryHost implements Host<{}> {
214218
this._updateWatchers(from, HostWatchEventType.Renamed);
215219
}
216220

217-
_list(path: Path): PathFragment[] {
221+
protected _list(path: Path): PathFragment[] {
218222
path = this._toAbsolute(path);
219223
if (this._isFile(path)) {
220224
throw new PathIsFileException(path);
@@ -239,21 +243,21 @@ export class SimpleMemoryHost implements Host<{}> {
239243
return [...result];
240244
}
241245

242-
_exists(path: Path): boolean {
246+
protected _exists(path: Path): boolean {
243247
return !!this._cache.get(this._toAbsolute(path));
244248
}
245-
_isDirectory(path: Path): boolean {
249+
protected _isDirectory(path: Path): boolean {
246250
const maybeStats = this._cache.get(this._toAbsolute(path));
247251

248252
return maybeStats ? maybeStats.isDirectory() : false;
249253
}
250-
_isFile(path: Path): boolean {
254+
protected _isFile(path: Path): boolean {
251255
const maybeStats = this._cache.get(this._toAbsolute(path));
252256

253257
return maybeStats ? maybeStats.isFile() : false;
254258
}
255259

256-
_stat(path: Path): Stats<SimpleMemoryHostStats> {
260+
protected _stat(path: Path): Stats<SimpleMemoryHostStats> {
257261
const maybeStats = this._cache.get(this._toAbsolute(path));
258262

259263
if (!maybeStats) {
@@ -263,6 +267,21 @@ export class SimpleMemoryHost implements Host<{}> {
263267
}
264268
}
265269

270+
protected _watch(path: Path, options?: HostWatchOptions): Observable<HostWatchEvent> {
271+
path = this._toAbsolute(path);
272+
273+
const subject = new Subject<HostWatchEvent>();
274+
let maybeWatcherArray = this._watchers.get(path);
275+
if (!maybeWatcherArray) {
276+
maybeWatcherArray = [];
277+
this._watchers.set(path, maybeWatcherArray);
278+
}
279+
280+
maybeWatcherArray.push([options || {}, subject]);
281+
282+
return subject.asObservable();
283+
}
284+
266285
write(path: Path, content: FileBuffer): Observable<void> {
267286
return new Observable<void>(obs => {
268287
this._write(path, content);
@@ -332,17 +351,6 @@ export class SimpleMemoryHost implements Host<{}> {
332351
}
333352

334353
watch(path: Path, options?: HostWatchOptions): Observable<HostWatchEvent> | null {
335-
path = this._toAbsolute(path);
336-
337-
const subject = new Subject<HostWatchEvent>();
338-
let maybeWatcherArray = this._watchers.get(path);
339-
if (!maybeWatcherArray) {
340-
maybeWatcherArray = [];
341-
this._watchers.set(path, maybeWatcherArray);
342-
}
343-
344-
maybeWatcherArray.push([options || {}, subject]);
345-
346-
return subject.asObservable();
354+
return this._watch(path, options);
347355
}
348356
}

0 commit comments

Comments
 (0)