-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/core): add new hosts
SafeHost swallows all errors with values. EmptyHost has no files/directories.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 { Observable, of, throwError } from 'rxjs'; | ||
import { FileDoesNotExistException } from '../../exception'; | ||
import { Path, PathFragment } from '../path'; | ||
import { FileBuffer, HostCapabilities, ReadonlyHost, Stats } from './interface'; | ||
|
||
export class Empty implements ReadonlyHost { | ||
readonly capabilities: HostCapabilities = { | ||
synchronous: true, | ||
}; | ||
|
||
read(path: Path): Observable<FileBuffer> { | ||
return throwError(new FileDoesNotExistException(path)); | ||
} | ||
|
||
list(path: Path): Observable<PathFragment[]> { | ||
return of([]); | ||
} | ||
|
||
exists(path: Path): Observable<boolean> { | ||
return of(false); | ||
} | ||
|
||
isDirectory(path: Path): Observable<boolean> { | ||
return of(false); | ||
} | ||
|
||
isFile(path: Path): Observable<boolean> { | ||
return of(false); | ||
} | ||
|
||
stat(path: Path): Observable<Stats<{}> | null> { | ||
// We support stat() but have no file. | ||
return of(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 { Observable, of } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { Path, PathFragment } from '../path'; | ||
import { FileBuffer, HostCapabilities, ReadonlyHost, Stats } from './interface'; | ||
|
||
/** | ||
* A Host that filters out errors. The only exception is `read()` which will still error out if | ||
* the delegate returned an error (e.g. NodeJS will error out if the file doesn't exist). | ||
*/ | ||
export class SafeReadonlyHost<StatsT extends object = {}> implements ReadonlyHost<StatsT> { | ||
constructor(private _delegate: ReadonlyHost<StatsT>) {} | ||
|
||
get capabilities(): HostCapabilities { | ||
return this._delegate.capabilities; | ||
} | ||
|
||
read(path: Path): Observable<FileBuffer> { | ||
return this._delegate.read(path); | ||
} | ||
|
||
list(path: Path): Observable<PathFragment[]> { | ||
return this._delegate.list(path).pipe( | ||
catchError(() => of([])), | ||
); | ||
} | ||
|
||
exists(path: Path): Observable<boolean> { | ||
return this._delegate.exists(path); | ||
} | ||
isDirectory(path: Path): Observable<boolean> { | ||
return this._delegate.isDirectory(path).pipe( | ||
catchError(() => of(false)), | ||
); | ||
} | ||
isFile(path: Path): Observable<boolean> { | ||
return this._delegate.isFile(path).pipe( | ||
catchError(() => of(false)), | ||
); | ||
} | ||
|
||
// Some hosts may not support stats. | ||
stat(path: Path): Observable<Stats<StatsT> | null> | null { | ||
const maybeStat = this._delegate.stat(path); | ||
|
||
return maybeStat && maybeStat.pipe( | ||
catchError(() => of(null)), | ||
); | ||
} | ||
} |