forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.ts
106 lines (89 loc) · 2.82 KB
/
host.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
/**
* @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 { Path, virtualFs } from '@angular-devkit/core';
import {
EMPTY,
Observable,
concatMap,
concat as concatObservables,
from as observableFrom,
of as observableOf,
reduce,
} from 'rxjs';
import { CreateFileAction } from '../tree/action';
import { SimpleSinkBase } from './sink';
export class HostSink extends SimpleSinkBase {
protected _filesToDelete = new Set<Path>();
protected _filesToRename = new Set<[Path, Path]>();
protected _filesToCreate = new Map<Path, Buffer>();
protected _filesToUpdate = new Map<Path, Buffer>();
constructor(
protected _host: virtualFs.Host,
protected _force = false,
) {
super();
}
protected override _validateCreateAction(action: CreateFileAction): Observable<void> {
return this._force ? EMPTY : super._validateCreateAction(action);
}
protected _validateFileExists(p: Path): Observable<boolean> {
if (this._filesToCreate.has(p) || this._filesToUpdate.has(p)) {
return observableOf(true);
}
if (this._filesToDelete.has(p)) {
return observableOf(false);
}
for (const [from, to] of this._filesToRename.values()) {
switch (p) {
case from:
return observableOf(false);
case to:
return observableOf(true);
}
}
return this._host.exists(p);
}
protected _overwriteFile(path: Path, content: Buffer): Observable<void> {
this._filesToUpdate.set(path, content);
return EMPTY;
}
protected _createFile(path: Path, content: Buffer): Observable<void> {
this._filesToCreate.set(path, content);
return EMPTY;
}
protected _renameFile(from: Path, to: Path): Observable<void> {
this._filesToRename.add([from, to]);
return EMPTY;
}
protected _deleteFile(path: Path): Observable<void> {
if (this._filesToCreate.has(path)) {
this._filesToCreate.delete(path);
this._filesToUpdate.delete(path);
} else {
this._filesToDelete.add(path);
}
return EMPTY;
}
_done() {
// Really commit everything to the actual filesystem.
return concatObservables(
observableFrom([...this._filesToDelete.values()]).pipe(
concatMap((path) => this._host.delete(path)),
),
observableFrom([...this._filesToRename.entries()]).pipe(
concatMap(([_, [path, to]]) => this._host.rename(path, to)),
),
observableFrom([...this._filesToCreate.entries()]).pipe(
concatMap(([path, buffer]) => this._host.write(path, buffer)),
),
observableFrom([...this._filesToUpdate.entries()]).pipe(
concatMap(([path, buffer]) => this._host.write(path, buffer)),
),
).pipe(reduce(() => {}));
}
}