Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly output filename when watching files #907

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,42 @@ describe('volume', () => {
});
});
describe('.watch(path[, options], listener)', () => {
it('should handle watching a file correctly', () => {
const vol = Volume.fromJSON({ '/tmp/foo.js': 'hello test' });
vol.writeFileSync('/tmp/foo.js', 'hello test');

const mockCallback = jest.fn();
const writtenContent = 'hello world';
const watcher = vol.watch('/tmp/foo.js', mockCallback as any);

try {
vol.writeFileSync('/tmp/foo.js', writtenContent);

expect(mockCallback).toBeCalledTimes(2);
expect(mockCallback).toBeCalledWith('change', 'foo.js');
} finally {
watcher.close();
}
});

it('should handle watching a directory correctly', () => {
const vol = Volume.fromJSON({ '/tmp/foo.js': 'hello test' });
vol.mkdirSync('/tmp/foo-dir');

const mockCallback = jest.fn();
const writtenContent = 'hello world';
const watcher = vol.watch('/tmp/foo-dir', mockCallback as any);

try {
vol.writeFileSync('/tmp/foo-dir/foo.js', writtenContent);

expect(mockCallback).toBeCalledTimes(1);
expect(mockCallback).toBeCalledWith('rename', 'foo.js');
} finally {
watcher.close();
}
});

it('Calls listener on .watch when renaming with recursive=true', done => {
const vol = new Volume();
vol.mkdirSync('/test');
Expand Down
10 changes: 9 additions & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2759,7 +2759,15 @@ export class FSWatcher extends EventEmitter {
const watchLinkNodeChanged = (link: Link) => {
const filepath = link.getPath();
const node = link.getNode();
const onNodeChange = () => this.emit('change', 'change', relative(this._filename, filepath));
const onNodeChange = () => {
let filename = relative(this._filename, filepath);

if (!filename) {
filename = this._getName();
}

return this.emit('change', 'change', filename);
};
node.on('change', onNodeChange);

const removers = this._listenerRemovers.get(node.ino) ?? [];
Expand Down