1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.FilterHostTree = exports.HostCreateTree = exports.HostTree = exports.HostDirEntry = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const jsonc_parser_1 = require("jsonc-parser");
|
13 | const rxjs_1 = require("rxjs");
|
14 | const operators_1 = require("rxjs/operators");
|
15 | const util_1 = require("util");
|
16 | const exception_1 = require("../exception/exception");
|
17 | const delegate_1 = require("./delegate");
|
18 | const entry_1 = require("./entry");
|
19 | const interface_1 = require("./interface");
|
20 | const recorder_1 = require("./recorder");
|
21 | const scoped_1 = require("./scoped");
|
22 | let _uniqueId = 0;
|
23 | class HostDirEntry {
|
24 | constructor(parent, path, _host, _tree) {
|
25 | this.parent = parent;
|
26 | this.path = path;
|
27 | this._host = _host;
|
28 | this._tree = _tree;
|
29 | }
|
30 | get subdirs() {
|
31 | return this._host
|
32 | .list(this.path)
|
33 | .filter((fragment) => this._host.isDirectory((0, core_1.join)(this.path, fragment)));
|
34 | }
|
35 | get subfiles() {
|
36 | return this._host
|
37 | .list(this.path)
|
38 | .filter((fragment) => this._host.isFile((0, core_1.join)(this.path, fragment)));
|
39 | }
|
40 | dir(name) {
|
41 | return this._tree.getDir((0, core_1.join)(this.path, name));
|
42 | }
|
43 | file(name) {
|
44 | return this._tree.get((0, core_1.join)(this.path, name));
|
45 | }
|
46 | visit(visitor) {
|
47 | try {
|
48 | this.getSubfilesRecursively().forEach((file) => visitor(file.path, file));
|
49 | }
|
50 | catch (e) {
|
51 | if (e !== interface_1.FileVisitorCancelToken) {
|
52 | throw e;
|
53 | }
|
54 | }
|
55 | }
|
56 | getSubfilesRecursively() {
|
57 | function _recurse(entry) {
|
58 | return entry.subdirs.reduce((files, subdir) => [...files, ..._recurse(entry.dir(subdir))], entry.subfiles.map((subfile) => entry.file(subfile)));
|
59 | }
|
60 | return _recurse(this);
|
61 | }
|
62 | }
|
63 | exports.HostDirEntry = HostDirEntry;
|
64 | class HostTree {
|
65 | constructor(_backend = new core_1.virtualFs.Empty()) {
|
66 | this._backend = _backend;
|
67 | this._id = --_uniqueId;
|
68 | this._ancestry = new Set();
|
69 | this._dirCache = new Map();
|
70 | this._record = new core_1.virtualFs.CordHost(new core_1.virtualFs.SafeReadonlyHost(_backend));
|
71 | this._recordSync = new core_1.virtualFs.SyncDelegateHost(this._record);
|
72 | }
|
73 | [interface_1.TreeSymbol]() {
|
74 | return this;
|
75 | }
|
76 | static isHostTree(tree) {
|
77 | if (tree instanceof HostTree) {
|
78 | return true;
|
79 | }
|
80 | if (typeof tree === 'object' && typeof tree._ancestry === 'object') {
|
81 | return true;
|
82 | }
|
83 | return false;
|
84 | }
|
85 | _normalizePath(path) {
|
86 | return (0, core_1.normalize)('/' + path);
|
87 | }
|
88 | _willCreate(path) {
|
89 | return this._record.willCreate(path);
|
90 | }
|
91 | _willOverwrite(path) {
|
92 | return this._record.willOverwrite(path);
|
93 | }
|
94 | _willDelete(path) {
|
95 | return this._record.willDelete(path);
|
96 | }
|
97 | _willRename(path) {
|
98 | return this._record.willRename(path);
|
99 | }
|
100 | branch() {
|
101 | const branchedTree = new HostTree(this._backend);
|
102 | branchedTree._record = this._record.clone();
|
103 | branchedTree._recordSync = new core_1.virtualFs.SyncDelegateHost(branchedTree._record);
|
104 | branchedTree._ancestry = new Set(this._ancestry).add(this._id);
|
105 | return branchedTree;
|
106 | }
|
107 | isAncestorOf(tree) {
|
108 | if (tree instanceof HostTree) {
|
109 | return tree._ancestry.has(this._id);
|
110 | }
|
111 | if (tree instanceof delegate_1.DelegateTree) {
|
112 | return this.isAncestorOf(tree._other);
|
113 | }
|
114 | if (tree instanceof scoped_1.ScopedTree) {
|
115 | return this.isAncestorOf(tree._base);
|
116 | }
|
117 | return false;
|
118 | }
|
119 | merge(other, strategy = interface_1.MergeStrategy.Default) {
|
120 | if (other === this) {
|
121 |
|
122 | return;
|
123 | }
|
124 | if (this.isAncestorOf(other)) {
|
125 |
|
126 |
|
127 | strategy |= interface_1.MergeStrategy.Overwrite;
|
128 | }
|
129 | const creationConflictAllowed = (strategy & interface_1.MergeStrategy.AllowCreationConflict) == interface_1.MergeStrategy.AllowCreationConflict;
|
130 | const overwriteConflictAllowed = (strategy & interface_1.MergeStrategy.AllowOverwriteConflict) == interface_1.MergeStrategy.AllowOverwriteConflict;
|
131 | const deleteConflictAllowed = (strategy & interface_1.MergeStrategy.AllowDeleteConflict) == interface_1.MergeStrategy.AllowDeleteConflict;
|
132 | other.actions.forEach((action) => {
|
133 | switch (action.kind) {
|
134 | case 'c': {
|
135 | const { path, content } = action;
|
136 | if (this._willCreate(path) || this._willOverwrite(path) || this.exists(path)) {
|
137 | const existingContent = this.read(path);
|
138 | if (existingContent && content.equals(existingContent)) {
|
139 |
|
140 | return;
|
141 | }
|
142 | if (!creationConflictAllowed) {
|
143 | throw new exception_1.MergeConflictException(path);
|
144 | }
|
145 | this._record.overwrite(path, content).subscribe();
|
146 | }
|
147 | else {
|
148 | this._record.create(path, content).subscribe();
|
149 | }
|
150 | return;
|
151 | }
|
152 | case 'o': {
|
153 | const { path, content } = action;
|
154 | if (this._willDelete(path) && !overwriteConflictAllowed) {
|
155 | throw new exception_1.MergeConflictException(path);
|
156 | }
|
157 |
|
158 | if (this._willOverwrite(path)) {
|
159 | const existingContent = this.read(path);
|
160 | if (existingContent && content.equals(existingContent)) {
|
161 |
|
162 | return;
|
163 | }
|
164 | if (!overwriteConflictAllowed) {
|
165 | throw new exception_1.MergeConflictException(path);
|
166 | }
|
167 | }
|
168 |
|
169 |
|
170 | this._record.write(path, content).subscribe();
|
171 | return;
|
172 | }
|
173 | case 'r': {
|
174 | const { path, to } = action;
|
175 | if (this._willDelete(path)) {
|
176 | throw new exception_1.MergeConflictException(path);
|
177 | }
|
178 | if (this._willRename(path)) {
|
179 | if (this._record.willRenameTo(path, to)) {
|
180 |
|
181 | return;
|
182 | }
|
183 |
|
184 | throw new exception_1.MergeConflictException(path);
|
185 | }
|
186 | this.rename(path, to);
|
187 | return;
|
188 | }
|
189 | case 'd': {
|
190 | const { path } = action;
|
191 | if (this._willDelete(path)) {
|
192 |
|
193 |
|
194 | return;
|
195 | }
|
196 | if (!this.exists(path) && !deleteConflictAllowed) {
|
197 | throw new exception_1.MergeConflictException(path);
|
198 | }
|
199 | this._recordSync.delete(path);
|
200 | return;
|
201 | }
|
202 | }
|
203 | });
|
204 | }
|
205 | get root() {
|
206 | return this.getDir('/');
|
207 | }
|
208 |
|
209 | read(path) {
|
210 | const entry = this.get(path);
|
211 | return entry ? entry.content : null;
|
212 | }
|
213 | readText(path) {
|
214 | const data = this.read(path);
|
215 | if (data === null) {
|
216 | throw new exception_1.FileDoesNotExistException(path);
|
217 | }
|
218 | const decoder = new util_1.TextDecoder('utf-8', { fatal: true });
|
219 | try {
|
220 |
|
221 | return decoder.decode(data);
|
222 | }
|
223 | catch (e) {
|
224 | if (e instanceof TypeError) {
|
225 | throw new Error(`Failed to decode "${path}" as UTF-8 text.`);
|
226 | }
|
227 | throw e;
|
228 | }
|
229 | }
|
230 | readJson(path) {
|
231 | const content = this.readText(path);
|
232 | const errors = [];
|
233 | const result = (0, jsonc_parser_1.parse)(content, errors, { allowTrailingComma: true });
|
234 |
|
235 | if (errors[0]) {
|
236 | const { error, offset } = errors[0];
|
237 | throw new Error(`Failed to parse "${path}" as JSON. ${(0, jsonc_parser_1.printParseErrorCode)(error)} at offset: ${offset}.`);
|
238 | }
|
239 | return result;
|
240 | }
|
241 | exists(path) {
|
242 | return this._recordSync.isFile(this._normalizePath(path));
|
243 | }
|
244 | get(path) {
|
245 | const p = this._normalizePath(path);
|
246 | if (this._recordSync.isDirectory(p)) {
|
247 | throw new core_1.PathIsDirectoryException(p);
|
248 | }
|
249 | if (!this._recordSync.exists(p)) {
|
250 | return null;
|
251 | }
|
252 | return new entry_1.LazyFileEntry(p, () => Buffer.from(this._recordSync.read(p)));
|
253 | }
|
254 | getDir(path) {
|
255 | const p = this._normalizePath(path);
|
256 | if (this._recordSync.isFile(p)) {
|
257 | throw new core_1.PathIsFileException(p);
|
258 | }
|
259 | let maybeCache = this._dirCache.get(p);
|
260 | if (!maybeCache) {
|
261 | let parent = (0, core_1.dirname)(p);
|
262 | if (p === parent) {
|
263 | parent = null;
|
264 | }
|
265 | maybeCache = new HostDirEntry(parent && this.getDir(parent), p, this._recordSync, this);
|
266 | this._dirCache.set(p, maybeCache);
|
267 | }
|
268 | return maybeCache;
|
269 | }
|
270 | visit(visitor) {
|
271 | this.root.visit((path, entry) => {
|
272 | visitor(path, entry);
|
273 | });
|
274 | }
|
275 |
|
276 | overwrite(path, content) {
|
277 | const p = this._normalizePath(path);
|
278 | if (!this._recordSync.exists(p)) {
|
279 | throw new exception_1.FileDoesNotExistException(p);
|
280 | }
|
281 | const c = typeof content == 'string' ? Buffer.from(content) : content;
|
282 | this._record.overwrite(p, c).subscribe();
|
283 | }
|
284 | beginUpdate(path) {
|
285 | const entry = this.get(path);
|
286 | if (!entry) {
|
287 | throw new exception_1.FileDoesNotExistException(path);
|
288 | }
|
289 | return recorder_1.UpdateRecorderBase.createFromFileEntry(entry);
|
290 | }
|
291 | commitUpdate(record) {
|
292 | if (record instanceof recorder_1.UpdateRecorderBase) {
|
293 | const path = record.path;
|
294 | const entry = this.get(path);
|
295 | if (!entry) {
|
296 | throw new exception_1.ContentHasMutatedException(path);
|
297 | }
|
298 | else {
|
299 | const newContent = record.apply(entry.content);
|
300 | if (!newContent.equals(entry.content)) {
|
301 | this.overwrite(path, newContent);
|
302 | }
|
303 | }
|
304 | }
|
305 | else {
|
306 | throw new exception_1.InvalidUpdateRecordException();
|
307 | }
|
308 | }
|
309 |
|
310 | create(path, content) {
|
311 | const p = this._normalizePath(path);
|
312 | if (this._recordSync.exists(p)) {
|
313 | throw new exception_1.FileAlreadyExistException(p);
|
314 | }
|
315 | const c = typeof content == 'string' ? Buffer.from(content) : content;
|
316 | this._record.create(p, c).subscribe();
|
317 | }
|
318 | delete(path) {
|
319 | this._recordSync.delete(this._normalizePath(path));
|
320 | }
|
321 | rename(from, to) {
|
322 | this._recordSync.rename(this._normalizePath(from), this._normalizePath(to));
|
323 | }
|
324 | apply(action, strategy) {
|
325 | throw new exception_1.SchematicsException('Apply not implemented on host trees.');
|
326 | }
|
327 | *generateActions() {
|
328 | for (const record of this._record.records()) {
|
329 | switch (record.kind) {
|
330 | case 'create':
|
331 | yield {
|
332 | id: this._id,
|
333 | parent: 0,
|
334 | kind: 'c',
|
335 | path: record.path,
|
336 | content: Buffer.from(record.content),
|
337 | };
|
338 | break;
|
339 | case 'overwrite':
|
340 | yield {
|
341 | id: this._id,
|
342 | parent: 0,
|
343 | kind: 'o',
|
344 | path: record.path,
|
345 | content: Buffer.from(record.content),
|
346 | };
|
347 | break;
|
348 | case 'rename':
|
349 | yield {
|
350 | id: this._id,
|
351 | parent: 0,
|
352 | kind: 'r',
|
353 | path: record.from,
|
354 | to: record.to,
|
355 | };
|
356 | break;
|
357 | case 'delete':
|
358 | yield {
|
359 | id: this._id,
|
360 | parent: 0,
|
361 | kind: 'd',
|
362 | path: record.path,
|
363 | };
|
364 | break;
|
365 | }
|
366 | }
|
367 | }
|
368 | get actions() {
|
369 |
|
370 |
|
371 | return Array.from(this.generateActions());
|
372 | }
|
373 | }
|
374 | exports.HostTree = HostTree;
|
375 | class HostCreateTree extends HostTree {
|
376 | constructor(host) {
|
377 | super();
|
378 | const tempHost = new HostTree(host);
|
379 | tempHost.visit((path) => {
|
380 | const content = tempHost.read(path);
|
381 | if (content) {
|
382 | this.create(path, content);
|
383 | }
|
384 | });
|
385 | }
|
386 | }
|
387 | exports.HostCreateTree = HostCreateTree;
|
388 | class FilterHostTree extends HostTree {
|
389 | constructor(tree, filter = () => true) {
|
390 | const newBackend = new core_1.virtualFs.SimpleMemoryHost();
|
391 |
|
392 | const originalBackend = tree._backend;
|
393 | const recurse = (base) => {
|
394 | return originalBackend.list(base).pipe((0, operators_1.mergeMap)((x) => x), (0, operators_1.map)((path) => (0, core_1.join)(base, path)), (0, operators_1.concatMap)((path) => {
|
395 | let isDirectory = false;
|
396 | originalBackend.isDirectory(path).subscribe((val) => (isDirectory = val));
|
397 | if (isDirectory) {
|
398 | return recurse(path);
|
399 | }
|
400 | let isFile = false;
|
401 | originalBackend.isFile(path).subscribe((val) => (isFile = val));
|
402 | if (!isFile || !filter(path)) {
|
403 | return rxjs_1.EMPTY;
|
404 | }
|
405 | let content = null;
|
406 | originalBackend.read(path).subscribe((val) => (content = val));
|
407 | if (!content) {
|
408 | return rxjs_1.EMPTY;
|
409 | }
|
410 | return newBackend.write(path, content);
|
411 | }));
|
412 | };
|
413 | recurse((0, core_1.normalize)('/')).subscribe();
|
414 | super(newBackend);
|
415 | for (const action of tree.actions) {
|
416 | if (!filter(action.path)) {
|
417 | continue;
|
418 | }
|
419 | switch (action.kind) {
|
420 | case 'c':
|
421 | this.create(action.path, action.content);
|
422 | break;
|
423 | case 'd':
|
424 | this.delete(action.path);
|
425 | break;
|
426 | case 'o':
|
427 | this.overwrite(action.path, action.content);
|
428 | break;
|
429 | case 'r':
|
430 | this.rename(action.path, action.to);
|
431 | break;
|
432 | }
|
433 | }
|
434 | }
|
435 | }
|
436 | exports.FilterHostTree = FilterHostTree;
|