forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsink.ts
More file actions
177 lines (160 loc) · 5.23 KB
/
Copy pathsink.ts
File metadata and controls
177 lines (160 loc) · 5.23 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @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.io/license
*/
import {
Observable,
concat,
defer as deferObservable,
isObservable,
from as observableFrom,
of as observableOf,
} from 'rxjs';
import { concatMap, ignoreElements, map, mergeMap } from 'rxjs/operators';
import { FileAlreadyExistException, FileDoesNotExistException } from '../exception/exception';
import {
Action,
CreateFileAction,
DeleteFileAction,
OverwriteFileAction,
RenameFileAction,
UnknownActionException,
} from '../tree/action';
import { Tree } from '../tree/interface';
export interface Sink {
commit(tree: Tree): Observable<void>;
}
const Noop = function () {};
export abstract class SimpleSinkBase implements Sink {
preCommitAction: (
action: Action,
) => void | Action | PromiseLike<Action> | Observable<Action> = Noop;
postCommitAction: (action: Action) => void | Observable<void> = Noop;
preCommit: () => void | Observable<void> = Noop;
postCommit: () => void | Observable<void> = Noop;
protected abstract _validateFileExists(p: string): Observable<boolean>;
protected abstract _overwriteFile(path: string, content: Buffer): Observable<void>;
protected abstract _createFile(path: string, content: Buffer): Observable<void>;
protected abstract _renameFile(path: string, to: string): Observable<void>;
protected abstract _deleteFile(path: string): Observable<void>;
protected abstract _done(): Observable<void>;
protected _fileAlreadyExistException(path: string): void {
throw new FileAlreadyExistException(path);
}
protected _fileDoesNotExistException(path: string): void {
throw new FileDoesNotExistException(path);
}
protected _validateOverwriteAction(action: OverwriteFileAction): Observable<void> {
return this._validateFileExists(action.path).pipe(
map((b) => {
if (!b) {
this._fileDoesNotExistException(action.path);
}
}),
);
}
protected _validateCreateAction(action: CreateFileAction): Observable<void> {
return this._validateFileExists(action.path).pipe(
map((b) => {
if (b) {
this._fileAlreadyExistException(action.path);
}
}),
);
}
protected _validateRenameAction(action: RenameFileAction): Observable<void> {
return this._validateFileExists(action.path).pipe(
map((b) => {
if (!b) {
this._fileDoesNotExistException(action.path);
}
}),
mergeMap(() => this._validateFileExists(action.to)),
map((b) => {
if (b) {
this._fileAlreadyExistException(action.to);
}
}),
);
}
protected _validateDeleteAction(action: DeleteFileAction): Observable<void> {
return this._validateFileExists(action.path).pipe(
map((b) => {
if (!b) {
this._fileDoesNotExistException(action.path);
}
}),
);
}
validateSingleAction(action: Action): Observable<void> {
switch (action.kind) {
case 'o':
return this._validateOverwriteAction(action);
case 'c':
return this._validateCreateAction(action);
case 'r':
return this._validateRenameAction(action);
case 'd':
return this._validateDeleteAction(action);
default:
throw new UnknownActionException(action);
}
}
commitSingleAction(action: Action): Observable<void> {
return concat(
this.validateSingleAction(action),
new Observable<void>((observer) => {
let committed = null;
switch (action.kind) {
case 'o':
committed = this._overwriteFile(action.path, action.content);
break;
case 'c':
committed = this._createFile(action.path, action.content);
break;
case 'r':
committed = this._renameFile(action.path, action.to);
break;
case 'd':
committed = this._deleteFile(action.path);
break;
}
if (committed) {
committed.subscribe(observer);
} else {
observer.complete();
}
}),
).pipe(ignoreElements());
}
commit(tree: Tree): Observable<void> {
const actions = observableFrom(tree.actions);
return concat(
this.preCommit() || observableOf(null),
deferObservable(() => actions).pipe(
concatMap((action) => {
const maybeAction = this.preCommitAction(action);
if (isObservable(maybeAction) || isPromiseLike(maybeAction)) {
return maybeAction;
}
return observableOf(maybeAction || action);
}),
concatMap((action) => {
return concat(
this.commitSingleAction(action).pipe(ignoreElements()),
observableOf(action),
);
}),
concatMap((action) => this.postCommitAction(action) || observableOf(null)),
),
deferObservable(() => this._done()),
deferObservable(() => this.postCommit() || observableOf(null)),
).pipe(ignoreElements());
}
}
function isPromiseLike<T, U = unknown>(value: U | PromiseLike<T>): value is PromiseLike<T> {
return !!value && typeof (value as PromiseLike<T>).then === 'function';
}