Skip to content

Commit 7dcd2ed

Browse files
clydinpull[bot]
authored andcommitted
refactor(@angular-devkit/core): allow creation of a host from a non-observable source
1 parent 48e7075 commit 7dcd2ed

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

etc/api/angular_devkit/core/src/_golden-api.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ export declare class CoreSchemaRegistry implements SchemaRegistry {
229229
useXDeprecatedProvider(onUsage: (message: string) => void): void;
230230
}
231231

232+
export declare function createSyncHost<StatsT extends object = {}>(handler: SyncHostHandler<StatsT>): Host<StatsT>;
233+
232234
export declare function createWorkspaceHost(host: virtualFs.Host): WorkspaceHost;
233235

234236
export interface CustomDimensionsAndMetricsOptions {
@@ -1048,6 +1050,18 @@ export declare class SyncDelegateHost<T extends object = {}> {
10481050
write(path: Path, content: FileBufferLike): void;
10491051
}
10501052

1053+
export interface SyncHostHandler<StatsT extends object = {}> {
1054+
delete(path: Path): void;
1055+
exists(path: Path): boolean;
1056+
isDirectory(path: Path): boolean;
1057+
isFile(path: Path): boolean;
1058+
list(path: Path): PathFragment[];
1059+
read(path: Path): FileBuffer;
1060+
rename(from: Path, to: Path): void;
1061+
stat(path: Path): Stats<StatsT> | null;
1062+
write(path: Path, content: FileBufferLike): void;
1063+
}
1064+
10511065
export declare class SynchronousDelegateExpectedException extends BaseException {
10521066
constructor();
10531067
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 } from 'rxjs';
9+
import { Path, PathFragment } from '../path';
10+
import { FileBuffer, FileBufferLike, Host, HostCapabilities, Stats } from './interface';
11+
12+
export interface SyncHostHandler<StatsT extends object = {}> {
13+
read(path: Path): FileBuffer;
14+
list(path: Path): PathFragment[];
15+
16+
exists(path: Path): boolean;
17+
isDirectory(path: Path): boolean;
18+
isFile(path: Path): boolean;
19+
20+
stat(path: Path): Stats<StatsT> | null;
21+
22+
write(path: Path, content: FileBufferLike): void;
23+
delete(path: Path): void;
24+
rename(from: Path, to: Path): void;
25+
}
26+
27+
function wrapAction<T>(action: () => T): Observable<T> {
28+
return new Observable((subscriber) => {
29+
subscriber.next(action());
30+
subscriber.complete();
31+
});
32+
}
33+
34+
export function createSyncHost<StatsT extends object = {}>(
35+
handler: SyncHostHandler<StatsT>,
36+
): Host<StatsT> {
37+
return new (class {
38+
get capabilities(): HostCapabilities {
39+
return { synchronous: true };
40+
}
41+
42+
read(path: Path): Observable<FileBuffer> {
43+
return wrapAction(() => handler.read(path));
44+
}
45+
46+
list(path: Path): Observable<PathFragment[]> {
47+
return wrapAction(() => handler.list(path));
48+
}
49+
50+
exists(path: Path): Observable<boolean> {
51+
return wrapAction(() => handler.exists(path));
52+
}
53+
54+
isDirectory(path: Path): Observable<boolean> {
55+
return wrapAction(() => handler.isDirectory(path));
56+
}
57+
58+
isFile(path: Path): Observable<boolean> {
59+
return wrapAction(() => handler.isFile(path));
60+
}
61+
62+
stat(path: Path): Observable<Stats<StatsT> | null> {
63+
return wrapAction(() => handler.stat(path));
64+
}
65+
66+
write(path: Path, content: FileBufferLike): Observable<void> {
67+
return wrapAction(() => handler.write(path, content));
68+
}
69+
70+
delete(path: Path): Observable<void> {
71+
return wrapAction(() => handler.delete(path));
72+
}
73+
74+
rename(from: Path, to: Path): Observable<void> {
75+
return wrapAction(() => handler.rename(from, to));
76+
}
77+
78+
watch(): null {
79+
return null;
80+
}
81+
})();
82+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
export * from './alias';
1010
export * from './buffer';
11+
export * from './create';
1112
export * from './empty';
1213
export * from './interface';
1314
export * from './memory';

0 commit comments

Comments
 (0)