UNPKG

5.65 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.ScopedTree = void 0;
11const core_1 = require("@angular-devkit/core");
12const delegate_1 = require("./delegate");
13const interface_1 = require("./interface");
14class ScopedFileEntry {
15 constructor(_base, scope) {
16 this._base = _base;
17 this.scope = scope;
18 }
19 get path() {
20 return (0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this.scope, this._base.path));
21 }
22 get content() {
23 return this._base.content;
24 }
25}
26class ScopedDirEntry {
27 constructor(_base, scope) {
28 this._base = _base;
29 this.scope = scope;
30 }
31 get parent() {
32 if (!this._base.parent || this._base.path == this.scope) {
33 return null;
34 }
35 return new ScopedDirEntry(this._base.parent, this.scope);
36 }
37 get path() {
38 return (0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this.scope, this._base.path));
39 }
40 get subdirs() {
41 return this._base.subdirs;
42 }
43 get subfiles() {
44 return this._base.subfiles;
45 }
46 dir(name) {
47 const entry = this._base.dir(name);
48 return entry && new ScopedDirEntry(entry, this.scope);
49 }
50 file(name) {
51 const entry = this._base.file(name);
52 return entry && new ScopedFileEntry(entry, this.scope);
53 }
54 visit(visitor) {
55 return this._base.visit((path, entry) => {
56 visitor((0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this.scope, path)), entry && new ScopedFileEntry(entry, this.scope));
57 });
58 }
59}
60class ScopedTree {
61 constructor(_base, scope) {
62 this._base = _base;
63 const normalizedScope = (0, core_1.normalize)('/' + scope);
64 this._root = new ScopedDirEntry(this._base.getDir(normalizedScope), normalizedScope);
65 }
66 get root() {
67 return this._root;
68 }
69 branch() {
70 return new ScopedTree(this._base.branch(), this._root.scope);
71 }
72 merge(other, strategy) {
73 // eslint-disable-next-line @typescript-eslint/no-this-alias
74 const self = this;
75 const delegate = new (class extends delegate_1.DelegateTree {
76 get actions() {
77 return other.actions.map((action) => self._fullPathAction(action));
78 }
79 })(other);
80 this._base.merge(delegate, strategy);
81 }
82 // Readonly.
83 read(path) {
84 return this._base.read(this._fullPath(path));
85 }
86 readText(path) {
87 return this._base.readText(this._fullPath(path));
88 }
89 readJson(path) {
90 return this._base.readJson(this._fullPath(path));
91 }
92 exists(path) {
93 return this._base.exists(this._fullPath(path));
94 }
95 get(path) {
96 const entry = this._base.get(this._fullPath(path));
97 return entry && new ScopedFileEntry(entry, this._root.scope);
98 }
99 getDir(path) {
100 const entry = this._base.getDir(this._fullPath(path));
101 return entry && new ScopedDirEntry(entry, this._root.scope);
102 }
103 visit(visitor) {
104 return this._root.visit(visitor);
105 }
106 // Change content of host files.
107 overwrite(path, content) {
108 return this._base.overwrite(this._fullPath(path), content);
109 }
110 beginUpdate(path) {
111 return this._base.beginUpdate(this._fullPath(path));
112 }
113 commitUpdate(record) {
114 return this._base.commitUpdate(record);
115 }
116 // Structural methods.
117 create(path, content) {
118 return this._base.create(this._fullPath(path), content);
119 }
120 delete(path) {
121 return this._base.delete(this._fullPath(path));
122 }
123 rename(from, to) {
124 return this._base.rename(this._fullPath(from), this._fullPath(to));
125 }
126 apply(action, strategy) {
127 return this._base.apply(this._fullPathAction(action), strategy);
128 }
129 get actions() {
130 const scopedActions = [];
131 for (const action of this._base.actions) {
132 if (!action.path.startsWith(this._root.scope + '/')) {
133 continue;
134 }
135 if (action.kind !== 'r') {
136 scopedActions.push({
137 ...action,
138 path: (0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this._root.scope, action.path)),
139 });
140 }
141 else if (action.to.startsWith(this._root.scope + '/')) {
142 scopedActions.push({
143 ...action,
144 path: (0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this._root.scope, action.path)),
145 to: (0, core_1.join)(core_1.NormalizedRoot, (0, core_1.relative)(this._root.scope, action.to)),
146 });
147 }
148 }
149 return scopedActions;
150 }
151 [interface_1.TreeSymbol]() {
152 return this;
153 }
154 _fullPath(path) {
155 return (0, core_1.join)(this._root.scope, (0, core_1.normalize)('/' + path));
156 }
157 _fullPathAction(action) {
158 let fullPathAction;
159 if (action.kind === 'r') {
160 fullPathAction = {
161 ...action,
162 path: this._fullPath(action.path),
163 to: this._fullPath(action.to),
164 };
165 }
166 else {
167 fullPathAction = {
168 ...action,
169 path: this._fullPath(action.path),
170 };
171 }
172 return fullPathAction;
173 }
174}
175exports.ScopedTree = ScopedTree;