Skip to content

Commit

Permalink
feat(@angular-devkit/core): add new hosts
Browse files Browse the repository at this point in the history
SafeHost swallows all errors with values.
EmptyHost has no files/directories.
  • Loading branch information
hansl committed Jun 6, 2018
1 parent 2631faa commit 98469a2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/host/empty.ts
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);
}
}
3 changes: 3 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/host/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

export * from './alias';
export * from './buffer';
export * from './empty';
export * from './interface';
export * from './memory';
export * from './pattern';
export * from './record';
export * from './safe';
export * from './scoped';
export * from './sync';

Expand Down
56 changes: 56 additions & 0 deletions packages/angular_devkit/core/src/virtual-fs/host/safe.ts
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)),
);
}
}

0 comments on commit 98469a2

Please sign in to comment.