Skip to content

Commit

Permalink
handle renamed files in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 11, 2017
1 parent f4d9765 commit 2a231b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/watch/fileWatchers.js
Expand Up @@ -60,8 +60,8 @@ export default class FileWatcher {
const handleWatchEvent = event => {
if (event === 'rename' || event === 'unlink') {
this.fsWatcher.close();
dispose();
this.trigger();
dispose();
} else {
// this is necessary because we get duplicate events...
const contents = fs.readFileSync(id, 'utf-8');
Expand Down
42 changes: 28 additions & 14 deletions src/watch/index.js
Expand Up @@ -48,25 +48,27 @@ class Watcher extends EventEmitter {
code: 'START'
});

mapSequence(this.tasks, task => {
return task.run().catch(error => {
mapSequence(this.tasks, task => task.run())
.then(() => {
this.succeeded = true;

this.emit('event', {
code: this.succeeded ? 'ERROR' : 'FATAL',
error
code: 'END'
});
});
}).then(() => {
this.running = false;
if (this.dirty) {
this._run();
} else {
})
.catch(error => {
this.emit('event', {
code: 'END'
code: this.succeeded ? 'ERROR' : 'FATAL',
error
});
})
.then(() => {
this.running = false;

this.succeeded = true;
}
});
if (this.dirty) {
this._run();
}
});
}
}

Expand All @@ -76,6 +78,7 @@ class Task {
this.watcher = watcher;
this.options = options;

this.dirty = true;
this.closed = false;
this.watched = new Set();

Expand Down Expand Up @@ -120,6 +123,7 @@ class Task {
}

run() {
if (!this.dirty) return;
this.dirty = false;

const options = Object.assign(this.options, {
Expand Down Expand Up @@ -176,6 +180,16 @@ class Task {
output: this.dests,
duration: Date.now() - start
});
})
.catch(error => {
if (this.cache) {
this.cache.modules.forEach(module => {
// this is necessary to ensure that any 'renamed' files
// continue to be watched following an error
addTask(module.id, this, this.chokidarOptions, this.chokidarOptionsHash);
});
}
throw error;
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/test.js
Expand Up @@ -975,7 +975,7 @@ describe( 'rollup', function () {
else if ( typeof next === 'string' ) {
watcher.once( 'event', event => {
if ( event.code !== next ) {
reject( new Error( `Expected ${next} error, got ${event.code}` ) );
reject( new Error( `Expected ${next} event, got ${event.code}` ) );
} else {
go( event );
}
Expand Down Expand Up @@ -1057,7 +1057,6 @@ describe( 'rollup', function () {
'START',
'BUNDLE_START',
'ERROR',
'END',
() => {
sander.writeFileSync( 'test/_tmp/input/main.js', 'export default 43;' );
},
Expand Down

0 comments on commit 2a231b6

Please sign in to comment.