metro-memory-fs: Allow to also listen directly to files

Reviewed By: jeanlauliac

Differential Revision: D7584516

fbshipit-source-id: 4362b148a59f11b7cc1ccb27e4c1a9ebf042e52c
This commit is contained in:
Rafael Oleza 2018-04-11 09:41:31 -07:00 committed by Facebook Github Bot
parent 76a9a21369
commit bd5d776a2f
2 changed files with 16 additions and 2 deletions

View File

@ -420,6 +420,15 @@ describe('watch', () => {
watcher.close();
});
it('reports changed files when watching a file directly', () => {
const changedPaths = [];
fs.writeFileSync('/foo.txt', '');
const watcher = collectWatchEvents('/foo.txt', {}, changedPaths);
fs.writeFileSync('/foo.txt', 'test');
expect(changedPaths).toEqual([['change', 'foo.txt']]);
watcher.close();
});
function collectWatchEvents(entPath, options, events) {
return fs.watch(entPath, options, (eventName, filePath) => {
events.push([eventName, filePath]);

View File

@ -791,9 +791,14 @@ class MemoryFs {
nodePath: Array<[string, EntityNode]>,
options: {eventType: 'rename' | 'change'},
): void {
const node = nodePath.pop();
let filePath = node[0];
const fileNode = nodePath.pop();
let filePath = fileNode[0];
let recursive = false;
for (const watcher of fileNode[1].watchers) {
watcher.listener(options.eventType, filePath);
}
while (nodePath.length > 0) {
const dirNode = nodePath.pop();
for (const watcher of dirNode[1].watchers) {