forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack-file-system-host-adapter.ts
160 lines (138 loc) · 4.46 KB
/
webpack-file-system-host-adapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @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 { FileDoesNotExistException, JsonObject, normalize, virtualFs } from '@angular-devkit/core';
import { Callback, InputFileSystem } from '@ngtools/webpack/src/webpack';
import { Stats } from 'fs';
import { Observable, of } from 'rxjs';
import { map, mergeMap, switchMap } from 'rxjs/operators';
export class WebpackFileSystemHostAdapter implements InputFileSystem {
protected _syncHost: virtualFs.SyncDelegateHost<Stats> | null = null;
constructor(protected _host: virtualFs.Host<Stats>) {}
private _doHostCall<T>(o: Observable<T>, callback: Callback<T>) {
const token = Symbol();
let value: T | typeof token = token;
let error = false;
try {
o.subscribe({
error(err) {
error = true;
callback(err);
},
next(v) {
value = v;
},
complete() {
if (value !== token) {
callback(null, value);
} else {
callback(new Error('Unknown error happened.'));
}
},
});
} catch (err) {
// In some occasions, the error handler above will be called, then an exception will be
// thrown (by design in observable constructors in RxJS 5). Don't call the callback
// twice.
if (!error) {
callback(err);
}
}
}
stat(path: string, callback: Callback<Stats>): void {
const p = normalize('/' + path);
const result = this._host.stat(p);
if (result === null) {
const o = this._host.exists(p).pipe(
switchMap(exists => {
if (!exists) {
throw new FileDoesNotExistException(p);
}
return this._host.isDirectory(p).pipe(
mergeMap(isDirectory => {
return (isDirectory ? of(0) : this._host.read(p).pipe(
map(content => content.byteLength),
)).pipe(
map(size => [isDirectory, size]),
);
}),
);
}),
map(([isDirectory, size]) => {
return {
isFile() { return !isDirectory; },
isDirectory() { return isDirectory; },
size,
atime: new Date(),
mtime: new Date(),
ctime: new Date(),
birthtime: new Date(),
};
}),
);
this._doHostCall(o, callback);
} else {
this._doHostCall(result, callback);
}
}
readdir(path: string, callback: Callback<string[]>): void {
return this._doHostCall(this._host.list(normalize('/' + path)), callback);
}
readFile(path: string, callback: Callback<Buffer>): void {
const o = this._host.read(normalize('/' + path)).pipe(
map(content => Buffer.from(content)),
);
return this._doHostCall(o, callback);
}
readJson(path: string, callback: Callback<JsonObject>): void {
const o = this._host.read(normalize('/' + path)).pipe(
map(content => JSON.parse(virtualFs.fileBufferToString(content))),
);
return this._doHostCall(o, callback);
}
readlink(path: string, callback: Callback<string>): void {
const err: NodeJS.ErrnoException = new Error('Not a symlink.');
err.code = 'EINVAL';
callback(err);
}
statSync(path: string): Stats {
if (!this._syncHost) {
this._syncHost = new virtualFs.SyncDelegateHost(this._host);
}
const result = this._syncHost.stat(normalize('/' + path));
if (result) {
return result;
} else {
return {} as Stats;
}
}
readdirSync(path: string): string[] {
if (!this._syncHost) {
this._syncHost = new virtualFs.SyncDelegateHost(this._host);
}
return this._syncHost.list(normalize('/' + path));
}
readFileSync(path: string): Buffer {
if (!this._syncHost) {
this._syncHost = new virtualFs.SyncDelegateHost(this._host);
}
return Buffer.from(this._syncHost.read(normalize('/' + path)));
}
readJsonSync(path: string): {} {
if (!this._syncHost) {
this._syncHost = new virtualFs.SyncDelegateHost(this._host);
}
const data = this._syncHost.read(normalize('/' + path));
return JSON.parse(virtualFs.fileBufferToString(data));
}
readlinkSync(path: string): string {
const err: NodeJS.ErrnoException = new Error('Not a symlink.');
err.code = 'EINVAL';
throw err;
}
purge(_changes?: string[] | string): void {}
}