|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import { Path, PathFragment, fragment, getSystemPath, virtualFs } from '@angular-devkit/core'; |
| 8 | +import { PathFragment, fragment, getSystemPath, virtualFs } from '@angular-devkit/core'; |
9 | 9 | import { Stats } from 'fs';
|
10 |
| -import { Observable, throwError } from 'rxjs'; |
11 |
| -import { map } from 'rxjs/operators'; |
12 | 10 | import { InputFileSystem } from 'webpack';
|
13 | 11 |
|
14 | 12 | // Host is used instead of ReadonlyHost due to most decorators only supporting Hosts
|
15 |
| -export class WebpackInputHost implements virtualFs.Host<Stats> { |
| 13 | +export function createWebpackInputHost(inputFileSystem: InputFileSystem) { |
| 14 | + return virtualFs.createSyncHost<Stats>({ |
| 15 | + write() { |
| 16 | + throw new Error('Not supported.'); |
| 17 | + }, |
16 | 18 |
|
17 |
| - constructor(public readonly inputFileSystem: InputFileSystem) { } |
| 19 | + delete() { |
| 20 | + throw new Error('Not supported.'); |
| 21 | + }, |
18 | 22 |
|
19 |
| - get capabilities(): virtualFs.HostCapabilities { |
20 |
| - return { synchronous: true }; |
21 |
| - } |
| 23 | + rename() { |
| 24 | + throw new Error('Not supported.'); |
| 25 | + }, |
22 | 26 |
|
23 |
| - write(_path: Path, _content: virtualFs.FileBufferLike) { |
24 |
| - return throwError(new Error('Not supported.')); |
25 |
| - } |
| 27 | + read(path): virtualFs.FileBuffer { |
| 28 | + const data = inputFileSystem.readFileSync(getSystemPath(path)); |
26 | 29 |
|
27 |
| - delete(_path: Path) { |
28 |
| - return throwError(new Error('Not supported.')); |
29 |
| - } |
| 30 | + return new Uint8Array(data).buffer as ArrayBuffer; |
| 31 | + }, |
30 | 32 |
|
31 |
| - rename(_from: Path, _to: Path) { |
32 |
| - return throwError(new Error('Not supported.')); |
33 |
| - } |
| 33 | + list(path): PathFragment[] { |
| 34 | + // readdirSync exists but is not in the Webpack typings |
| 35 | + const names: string[] = ((inputFileSystem as unknown) as { |
| 36 | + readdirSync: (path: string) => string[]; |
| 37 | + }).readdirSync(getSystemPath(path)); |
34 | 38 |
|
35 |
| - read(path: Path): Observable<virtualFs.FileBuffer> { |
36 |
| - return new Observable(obs => { |
37 |
| - // TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is |
38 |
| - // fixed. |
39 |
| - try { |
40 |
| - const data = this.inputFileSystem.readFileSync(getSystemPath(path)); |
41 |
| - obs.next(new Uint8Array(data).buffer as ArrayBuffer); |
42 |
| - obs.complete(); |
43 |
| - } catch (e) { |
44 |
| - obs.error(e); |
45 |
| - } |
46 |
| - }); |
47 |
| - } |
| 39 | + return names.map((name) => fragment(name)); |
| 40 | + }, |
48 | 41 |
|
49 |
| - list(path: Path): Observable<PathFragment[]> { |
50 |
| - return new Observable(obs => { |
51 |
| - // TODO: remove this try+catch when issue https://github.com/ReactiveX/rxjs/issues/3740 is |
52 |
| - // fixed. |
53 |
| - try { |
54 |
| - // tslint:disable-next-line:no-any |
55 |
| - const names: string[] = (this.inputFileSystem as any).readdirSync(getSystemPath(path)); |
56 |
| - obs.next(names.map(name => fragment(name))); |
57 |
| - obs.complete(); |
58 |
| - } catch (err) { |
59 |
| - obs.error(err); |
60 |
| - } |
61 |
| - }); |
62 |
| - } |
63 |
| - |
64 |
| - exists(path: Path): Observable<boolean> { |
65 |
| - return this.stat(path).pipe(map(stats => stats != null)); |
66 |
| - } |
| 42 | + exists(path): boolean { |
| 43 | + return !!this.stat(path); |
| 44 | + }, |
67 | 45 |
|
68 |
| - isDirectory(path: Path): Observable<boolean> { |
69 |
| - return this.stat(path).pipe(map(stats => stats != null && stats.isDirectory())); |
70 |
| - } |
| 46 | + isDirectory(path): boolean { |
| 47 | + return this.stat(path)?.isDirectory() ?? false; |
| 48 | + }, |
71 | 49 |
|
72 |
| - isFile(path: Path): Observable<boolean> { |
73 |
| - return this.stat(path).pipe(map(stats => stats != null && stats.isFile())); |
74 |
| - } |
| 50 | + isFile(path): boolean { |
| 51 | + return this.stat(path)?.isFile() ?? false; |
| 52 | + }, |
75 | 53 |
|
76 |
| - stat(path: Path): Observable<Stats | null> { |
77 |
| - return new Observable(obs => { |
| 54 | + stat(path): Stats | null { |
78 | 55 | try {
|
79 |
| - const stats = this.inputFileSystem.statSync(getSystemPath(path)); |
80 |
| - obs.next(stats); |
81 |
| - obs.complete(); |
| 56 | + return inputFileSystem.statSync(getSystemPath(path)); |
82 | 57 | } catch (e) {
|
83 | 58 | if (e.code === 'ENOENT') {
|
84 |
| - obs.next(null); |
85 |
| - obs.complete(); |
86 |
| - } else { |
87 |
| - obs.error(e); |
| 59 | + return null; |
88 | 60 | }
|
| 61 | + throw e; |
89 | 62 | }
|
90 |
| - }); |
91 |
| - } |
92 |
| - |
93 |
| - watch(_path: Path, _options?: virtualFs.HostWatchOptions): null { |
94 |
| - return null; |
95 |
| - } |
| 63 | + }, |
| 64 | + }); |
96 | 65 | }
|
0 commit comments