@@ -14,6 +14,7 @@ import {
14
14
relative ,
15
15
} from '@angular-devkit/core' ;
16
16
import { Action } from './action' ;
17
+ import { DelegateTree } from './delegate' ;
17
18
import {
18
19
DirEntry ,
19
20
FileEntry ,
@@ -89,7 +90,16 @@ export class ScopedTree implements Tree {
89
90
get root ( ) : DirEntry { return this . _root ; }
90
91
91
92
branch ( ) : Tree { return new ScopedTree ( this . _base . branch ( ) , this . _root . scope ) ; }
92
- merge ( other : Tree , strategy ?: MergeStrategy ) : void { this . _base . merge ( other , strategy ) ; }
93
+ merge ( other : Tree , strategy ?: MergeStrategy ) : void {
94
+ const self = this ;
95
+ const delegate = new class extends DelegateTree {
96
+ get actions ( ) : Action [ ] {
97
+ return other . actions . map ( action => self . _fullPathAction ( action ) ) ;
98
+ }
99
+ } ( other ) ;
100
+
101
+ this . _base . merge ( delegate , strategy ) ;
102
+ }
93
103
94
104
// Readonly.
95
105
read ( path : string ) : Buffer | null { return this . _base . read ( this . _fullPath ( path ) ) ; }
@@ -125,15 +135,57 @@ export class ScopedTree implements Tree {
125
135
}
126
136
127
137
apply ( action : Action , strategy ?: MergeStrategy ) : void {
128
- return this . _base . apply ( action , strategy ) ;
138
+ return this . _base . apply ( this . _fullPathAction ( action ) , strategy ) ;
139
+ }
140
+
141
+ get actions ( ) : Action [ ] {
142
+ const scopedActions = [ ] ;
143
+
144
+ for ( const action of this . _base . actions ) {
145
+ if ( ! action . path . startsWith ( this . _root . scope + '/' ) ) {
146
+ continue ;
147
+ }
148
+
149
+ if ( action . kind !== 'r' ) {
150
+ scopedActions . push ( {
151
+ ...action ,
152
+ path : join ( NormalizedRoot , relative ( this . _root . scope , action . path ) ) ,
153
+ } ) ;
154
+ } else if ( action . to . startsWith ( this . _root . scope + '/' ) ) {
155
+ scopedActions . push ( {
156
+ ...action ,
157
+ path : join ( NormalizedRoot , relative ( this . _root . scope , action . path ) ) ,
158
+ to : join ( NormalizedRoot , relative ( this . _root . scope , action . to ) ) ,
159
+ } ) ;
160
+ }
161
+ }
162
+
163
+ return scopedActions ;
129
164
}
130
- get actions ( ) : Action [ ] { return this . _base . actions ; }
131
165
132
166
[ TreeSymbol ] ( ) {
133
167
return this ;
134
168
}
135
169
136
- private _fullPath ( path : string ) {
170
+ private _fullPath ( path : string ) : Path {
137
171
return join ( this . _root . scope , normalize ( '/' + path ) ) ;
138
172
}
173
+
174
+ private _fullPathAction ( action : Action ) {
175
+ let fullPathAction : Action ;
176
+ if ( action . kind === 'r' ) {
177
+ fullPathAction = {
178
+ ...action ,
179
+ path : this . _fullPath ( action . path ) ,
180
+ to : this . _fullPath ( action . to ) ,
181
+ } ;
182
+ } else {
183
+ fullPathAction = {
184
+ ...action ,
185
+ path : this . _fullPath ( action . path ) ,
186
+ } ;
187
+ }
188
+
189
+ return fullPathAction ;
190
+ }
139
191
}
0 commit comments