Skip to content

Commit 4b98448

Browse files
committed
fix(@angular-devkit/schematics): handle updating renamed files
With this change we fix an issue which caused a file not found error when trying to modify the file after it was renamed. Closes #14255 and closes #21083 (cherry picked from commit a30525b)
1 parent e09dc5c commit 4b98448

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

packages/angular_devkit/schematics/src/sink/host.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,22 @@ export class HostSink extends SimpleSinkBase {
3636
protected _validateFileExists(p: Path): Observable<boolean> {
3737
if (this._filesToCreate.has(p) || this._filesToUpdate.has(p)) {
3838
return observableOf(true);
39-
} else if (this._filesToDelete.has(p)) {
40-
return observableOf(false);
41-
} else if ([...this._filesToRename.values()].some(([from]) => from == p)) {
39+
}
40+
41+
if (this._filesToDelete.has(p)) {
4242
return observableOf(false);
43-
} else {
44-
return this._host.exists(p);
4543
}
44+
45+
for (const [from, to] of this._filesToRename.values()) {
46+
switch (p) {
47+
case from:
48+
return observableOf(false);
49+
case to:
50+
return observableOf(true);
51+
}
52+
}
53+
54+
return this._host.exists(p);
4655
}
4756

4857
protected _overwriteFile(path: Path, content: Buffer): Observable<void> {
@@ -82,12 +91,12 @@ export class HostSink extends SimpleSinkBase {
8291
),
8392
observableFrom([...this._filesToCreate.entries()]).pipe(
8493
concatMap(([path, buffer]) => {
85-
return this._host.write(path, (buffer.generate() as {}) as virtualFs.FileBuffer);
94+
return this._host.write(path, buffer.generate() as {} as virtualFs.FileBuffer);
8695
}),
8796
),
8897
observableFrom([...this._filesToUpdate.entries()]).pipe(
8998
concatMap(([path, buffer]) => {
90-
return this._host.write(path, (buffer.generate() as {}) as virtualFs.FileBuffer);
99+
return this._host.write(path, buffer.generate() as {} as virtualFs.FileBuffer);
91100
}),
92101
),
93102
).pipe(reduce(() => {}));

packages/angular_devkit/schematics/src/sink/host_spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,22 @@ describe('FileSystemSink', () => {
135135
})
136136
.then(done, done.fail);
137137
});
138+
139+
it('can rename then modify the same file', async () => {
140+
const host = new virtualFs.test.TestHost({
141+
'/file0': 'world',
142+
});
143+
const tree = new HostTree(host);
144+
145+
tree.rename('/file0', '/file1');
146+
expect(tree.exists('/file0')).toBeFalsy();
147+
expect(tree.exists('/file1')).toBeTruthy();
148+
149+
tree.overwrite('/file1', 'hello');
150+
151+
const sink = new HostSink(host);
152+
await sink.commit(tree).toPromise();
153+
expect(virtualFs.fileBufferToString(host.sync.read(normalize('/file1')))).toBe('hello');
154+
});
138155
});
139156
});

0 commit comments

Comments
 (0)