Skip to content
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
23 changes: 23 additions & 0 deletions spec/pathwatcher-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ describe('PathWatcher', () => {
await wait(100);
});

describe('when a watched file is replaced via atomic save', () => {
it('fires the callback with a change event', async () => {
let eventType;
let eventPath;

PathWatcher.watch(tempFile, (type, path) => {
eventType = type;
eventPath = path;
});

await wait(20);

let tempFileCopy = path.join(tempDir, 'file-copy');
fs.writeFileSync(tempFileCopy, 'atomic save content');
fs.renameSync(tempFileCopy, tempFile);

await condition(() => !!eventType);

expect(eventType).toBe('change');
expect(eventPath).toBe('');
});
});

describe('getWatchedPaths', () => {
it('returns an array of all watched paths', () => {
let realTempFilePath = fs.realpathSync(tempFile);
Expand Down
7 changes: 6 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ class PathWatcher {
let isSameDirectory = !this.isWatchingParent &&
path.dirname(event.path) == path.dirname(event.oldPath);

if ((oldWatched && newWatched) || isSameDirectory) {
if (eventPathIsEqual && !eventOldPathIsEqual) {
// Atomic save: some other file was renamed into our watched path.
// The new path _is_ the watched file, but the old path was not.
newEvent.action = 'change';
newEvent.path = '';
} else if ((oldWatched && newWatched) || isSameDirectory) {
// We can keep tabs on both file paths from here, so this will
// be treated as a rename.
newEvent.action = 'rename';
Expand Down
Loading