diff --git a/etc/api/angular_devkit/core/src/_golden-api.d.ts b/etc/api/angular_devkit/core/src/_golden-api.d.ts index c0010196a4..81b0279f6f 100644 --- a/etc/api/angular_devkit/core/src/_golden-api.d.ts +++ b/etc/api/angular_devkit/core/src/_golden-api.d.ts @@ -229,6 +229,8 @@ export declare class CoreSchemaRegistry implements SchemaRegistry { useXDeprecatedProvider(onUsage: (message: string) => void): void; } +export declare function createSyncHost(handler: SyncHostHandler): Host; + export declare function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost; export interface CustomDimensionsAndMetricsOptions { @@ -1048,6 +1050,18 @@ export declare class SyncDelegateHost { write(path: Path, content: FileBufferLike): void; } +export interface SyncHostHandler { + delete(path: Path): void; + exists(path: Path): boolean; + isDirectory(path: Path): boolean; + isFile(path: Path): boolean; + list(path: Path): PathFragment[]; + read(path: Path): FileBuffer; + rename(from: Path, to: Path): void; + stat(path: Path): Stats | null; + write(path: Path, content: FileBufferLike): void; +} + export declare class SynchronousDelegateExpectedException extends BaseException { constructor(); } diff --git a/packages/angular_devkit/core/src/virtual-fs/host/create.ts b/packages/angular_devkit/core/src/virtual-fs/host/create.ts new file mode 100644 index 0000000000..8002f1dea4 --- /dev/null +++ b/packages/angular_devkit/core/src/virtual-fs/host/create.ts @@ -0,0 +1,82 @@ +/** + * @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 } from 'rxjs'; +import { Path, PathFragment } from '../path'; +import { FileBuffer, FileBufferLike, Host, HostCapabilities, Stats } from './interface'; + +export interface SyncHostHandler { + read(path: Path): FileBuffer; + list(path: Path): PathFragment[]; + + exists(path: Path): boolean; + isDirectory(path: Path): boolean; + isFile(path: Path): boolean; + + stat(path: Path): Stats | null; + + write(path: Path, content: FileBufferLike): void; + delete(path: Path): void; + rename(from: Path, to: Path): void; +} + +function wrapAction(action: () => T): Observable { + return new Observable((subscriber) => { + subscriber.next(action()); + subscriber.complete(); + }); +} + +export function createSyncHost( + handler: SyncHostHandler, +): Host { + return new (class { + get capabilities(): HostCapabilities { + return { synchronous: true }; + } + + read(path: Path): Observable { + return wrapAction(() => handler.read(path)); + } + + list(path: Path): Observable { + return wrapAction(() => handler.list(path)); + } + + exists(path: Path): Observable { + return wrapAction(() => handler.exists(path)); + } + + isDirectory(path: Path): Observable { + return wrapAction(() => handler.isDirectory(path)); + } + + isFile(path: Path): Observable { + return wrapAction(() => handler.isFile(path)); + } + + stat(path: Path): Observable | null> { + return wrapAction(() => handler.stat(path)); + } + + write(path: Path, content: FileBufferLike): Observable { + return wrapAction(() => handler.write(path, content)); + } + + delete(path: Path): Observable { + return wrapAction(() => handler.delete(path)); + } + + rename(from: Path, to: Path): Observable { + return wrapAction(() => handler.rename(from, to)); + } + + watch(): null { + return null; + } + })(); +} diff --git a/packages/angular_devkit/core/src/virtual-fs/host/index.ts b/packages/angular_devkit/core/src/virtual-fs/host/index.ts index c17b0c50f9..a795f4209f 100644 --- a/packages/angular_devkit/core/src/virtual-fs/host/index.ts +++ b/packages/angular_devkit/core/src/virtual-fs/host/index.ts @@ -8,6 +8,7 @@ export * from './alias'; export * from './buffer'; +export * from './create'; export * from './empty'; export * from './interface'; export * from './memory';