Skip to content

Commit

Permalink
fix(@angular-devkit/schematics): handle updating renamed files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 committed Jun 9, 2021
1 parent dc5a585 commit a30525b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/angular_devkit/schematics/src/sink/host.ts
Expand Up @@ -36,13 +36,22 @@ export class HostSink extends SimpleSinkBase {
protected _validateFileExists(p: Path): Observable<boolean> {
if (this._filesToCreate.has(p) || this._filesToUpdate.has(p)) {
return observableOf(true);
} else if (this._filesToDelete.has(p)) {
return observableOf(false);
} else if ([...this._filesToRename.values()].some(([from]) => from == p)) {
}

if (this._filesToDelete.has(p)) {
return observableOf(false);
} else {
return this._host.exists(p);
}

for (const [from, to] of this._filesToRename.values()) {
switch (p) {
case from:
return observableOf(false);
case to:
return observableOf(true);
}
}

return this._host.exists(p);
}

protected _overwriteFile(path: Path, content: Buffer): Observable<void> {
Expand Down Expand Up @@ -82,12 +91,12 @@ export class HostSink extends SimpleSinkBase {
),
observableFrom([...this._filesToCreate.entries()]).pipe(
concatMap(([path, buffer]) => {
return this._host.write(path, (buffer.generate() as {}) as virtualFs.FileBuffer);
return this._host.write(path, buffer.generate() as {} as virtualFs.FileBuffer);
}),
),
observableFrom([...this._filesToUpdate.entries()]).pipe(
concatMap(([path, buffer]) => {
return this._host.write(path, (buffer.generate() as {}) as virtualFs.FileBuffer);
return this._host.write(path, buffer.generate() as {} as virtualFs.FileBuffer);
}),
),
).pipe(reduce(() => {}));
Expand Down
17 changes: 17 additions & 0 deletions packages/angular_devkit/schematics/src/sink/host_spec.ts
Expand Up @@ -135,5 +135,22 @@ describe('FileSystemSink', () => {
})
.then(done, done.fail);
});

it('can rename then modify the same file', async () => {
const host = new virtualFs.test.TestHost({
'/file0': 'world',
});
const tree = new HostTree(host);

tree.rename('/file0', '/file1');
expect(tree.exists('/file0')).toBeFalsy();
expect(tree.exists('/file1')).toBeTruthy();

tree.overwrite('/file1', 'hello');

const sink = new HostSink(host);
await sink.commit(tree).toPromise();
expect(virtualFs.fileBufferToString(host.sync.read(normalize('/file1')))).toBe('hello');
});
});
});

0 comments on commit a30525b

Please sign in to comment.