/** * @license * Copyright Google LLC 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.dev/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; } })(); }