-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathscoped.ts
209 lines (176 loc) · 5.24 KB
/
scoped.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @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 {
NormalizedRoot,
Path,
PathFragment,
join,
normalize,
relative,
} from '@angular-devkit/core';
import { Action } from './action';
import { DelegateTree } from './delegate';
import {
DirEntry,
FileEntry,
FileVisitor,
MergeStrategy,
Tree,
TreeSymbol,
UpdateRecorder,
} from './interface';
class ScopedFileEntry implements FileEntry {
constructor(private _base: FileEntry, private scope: Path) {}
get path(): Path {
return join(NormalizedRoot, relative(this.scope, this._base.path));
}
get content(): Buffer {
return this._base.content;
}
}
class ScopedDirEntry implements DirEntry {
constructor(private _base: DirEntry, readonly scope: Path) {}
get parent(): DirEntry | null {
if (!this._base.parent || this._base.path == this.scope) {
return null;
}
return new ScopedDirEntry(this._base.parent, this.scope);
}
get path(): Path {
return join(NormalizedRoot, relative(this.scope, this._base.path));
}
get subdirs(): PathFragment[] {
return this._base.subdirs;
}
get subfiles(): PathFragment[] {
return this._base.subfiles;
}
dir(name: PathFragment): DirEntry {
const entry = this._base.dir(name);
return entry && new ScopedDirEntry(entry, this.scope);
}
file(name: PathFragment): FileEntry | null {
const entry = this._base.file(name);
return entry && new ScopedFileEntry(entry, this.scope);
}
visit(visitor: FileVisitor): void {
return this._base.visit((path, entry) => {
visitor(
join(NormalizedRoot, relative(this.scope, path)),
entry && new ScopedFileEntry(entry, this.scope),
);
});
}
}
export class ScopedTree implements Tree {
readonly _root: ScopedDirEntry;
constructor(private _base: Tree, scope: string) {
const normalizedScope = normalize('/' + scope);
this._root = new ScopedDirEntry(this._base.getDir(normalizedScope), normalizedScope);
}
get root(): DirEntry {
return this._root;
}
branch(): Tree {
return new ScopedTree(this._base.branch(), this._root.scope);
}
merge(other: Tree, strategy?: MergeStrategy): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const delegate = new (class extends DelegateTree {
override get actions(): Action[] {
return other.actions.map((action) => self._fullPathAction(action));
}
})(other);
this._base.merge(delegate, strategy);
}
// Readonly.
read(path: string): Buffer | null {
return this._base.read(this._fullPath(path));
}
exists(path: string): boolean {
return this._base.exists(this._fullPath(path));
}
get(path: string): FileEntry | null {
const entry = this._base.get(this._fullPath(path));
return entry && new ScopedFileEntry(entry, this._root.scope);
}
getDir(path: string): DirEntry {
const entry = this._base.getDir(this._fullPath(path));
return entry && new ScopedDirEntry(entry, this._root.scope);
}
visit(visitor: FileVisitor): void {
return this._root.visit(visitor);
}
// Change content of host files.
overwrite(path: string, content: Buffer | string): void {
return this._base.overwrite(this._fullPath(path), content);
}
beginUpdate(path: string): UpdateRecorder {
return this._base.beginUpdate(this._fullPath(path));
}
commitUpdate(record: UpdateRecorder): void {
return this._base.commitUpdate(record);
}
// Structural methods.
create(path: string, content: Buffer | string): void {
return this._base.create(this._fullPath(path), content);
}
delete(path: string): void {
return this._base.delete(this._fullPath(path));
}
rename(from: string, to: string): void {
return this._base.rename(this._fullPath(from), this._fullPath(to));
}
apply(action: Action, strategy?: MergeStrategy): void {
return this._base.apply(this._fullPathAction(action), strategy);
}
get actions(): Action[] {
const scopedActions = [];
for (const action of this._base.actions) {
if (!action.path.startsWith(this._root.scope + '/')) {
continue;
}
if (action.kind !== 'r') {
scopedActions.push({
...action,
path: join(NormalizedRoot, relative(this._root.scope, action.path)),
});
} else if (action.to.startsWith(this._root.scope + '/')) {
scopedActions.push({
...action,
path: join(NormalizedRoot, relative(this._root.scope, action.path)),
to: join(NormalizedRoot, relative(this._root.scope, action.to)),
});
}
}
return scopedActions;
}
[TreeSymbol]() {
return this;
}
private _fullPath(path: string): Path {
return join(this._root.scope, normalize('/' + path));
}
private _fullPathAction(action: Action) {
let fullPathAction: Action;
if (action.kind === 'r') {
fullPathAction = {
...action,
path: this._fullPath(action.path),
to: this._fullPath(action.to),
};
} else {
fullPathAction = {
...action,
path: this._fullPath(action.path),
};
}
return fullPathAction;
}
}