Skip to content

Commit 98469a2

Browse files
committed
feat(@angular-devkit/core): add new hosts
SafeHost swallows all errors with values. EmptyHost has no files/directories.
1 parent 2631faa commit 98469a2

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Observable, of, throwError } from 'rxjs';
9+
import { FileDoesNotExistException } from '../../exception';
10+
import { Path, PathFragment } from '../path';
11+
import { FileBuffer, HostCapabilities, ReadonlyHost, Stats } from './interface';
12+
13+
export class Empty implements ReadonlyHost {
14+
readonly capabilities: HostCapabilities = {
15+
synchronous: true,
16+
};
17+
18+
read(path: Path): Observable<FileBuffer> {
19+
return throwError(new FileDoesNotExistException(path));
20+
}
21+
22+
list(path: Path): Observable<PathFragment[]> {
23+
return of([]);
24+
}
25+
26+
exists(path: Path): Observable<boolean> {
27+
return of(false);
28+
}
29+
30+
isDirectory(path: Path): Observable<boolean> {
31+
return of(false);
32+
}
33+
34+
isFile(path: Path): Observable<boolean> {
35+
return of(false);
36+
}
37+
38+
stat(path: Path): Observable<Stats<{}> | null> {
39+
// We support stat() but have no file.
40+
return of(null);
41+
}
42+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
export * from './alias';
1010
export * from './buffer';
11+
export * from './empty';
1112
export * from './interface';
1213
export * from './memory';
1314
export * from './pattern';
15+
export * from './record';
16+
export * from './safe';
1417
export * from './scoped';
1518
export * from './sync';
1619

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { Observable, of } from 'rxjs';
9+
import { catchError } from 'rxjs/operators';
10+
import { Path, PathFragment } from '../path';
11+
import { FileBuffer, HostCapabilities, ReadonlyHost, Stats } from './interface';
12+
13+
/**
14+
* A Host that filters out errors. The only exception is `read()` which will still error out if
15+
* the delegate returned an error (e.g. NodeJS will error out if the file doesn't exist).
16+
*/
17+
export class SafeReadonlyHost<StatsT extends object = {}> implements ReadonlyHost<StatsT> {
18+
constructor(private _delegate: ReadonlyHost<StatsT>) {}
19+
20+
get capabilities(): HostCapabilities {
21+
return this._delegate.capabilities;
22+
}
23+
24+
read(path: Path): Observable<FileBuffer> {
25+
return this._delegate.read(path);
26+
}
27+
28+
list(path: Path): Observable<PathFragment[]> {
29+
return this._delegate.list(path).pipe(
30+
catchError(() => of([])),
31+
);
32+
}
33+
34+
exists(path: Path): Observable<boolean> {
35+
return this._delegate.exists(path);
36+
}
37+
isDirectory(path: Path): Observable<boolean> {
38+
return this._delegate.isDirectory(path).pipe(
39+
catchError(() => of(false)),
40+
);
41+
}
42+
isFile(path: Path): Observable<boolean> {
43+
return this._delegate.isFile(path).pipe(
44+
catchError(() => of(false)),
45+
);
46+
}
47+
48+
// Some hosts may not support stats.
49+
stat(path: Path): Observable<Stats<StatsT> | null> | null {
50+
const maybeStat = this._delegate.stat(path);
51+
52+
return maybeStat && maybeStat.pipe(
53+
catchError(() => of(null)),
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)