Skip to content

Commit dab8a82

Browse files
pvdlglusarz
authored andcommittedOct 11, 2018
feat(server): Add public API to force a file refresh
Adds a public API similar to `server.refreshFiles()` that allows to refresh a specific file. This is useful for preprocessors that process files with dependencies (sass, js modules, postcss), so they can trigger an update on a file when a change is detected on a dependency. Contrary to `server.refreshFiles()` it allows to refresh only the file that need to be processed again.
1 parent 871a1c9 commit dab8a82

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed
 

‎docs/dev/04-public-api.md

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ Trigger a file list refresh. Returns a promise.
3333
server.refreshFiles()
3434
```
3535

36+
### **server.refreshFile(path)**
37+
38+
Trigger a file refresh. Returns a promise.
39+
40+
```javascript
41+
server.refreshFile('src/js/module-dep.js')
42+
```
43+
3644
### Events
3745

3846
The `server` object is an [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#events_class_events_eventemitter). You can simply listen to events like this:

‎lib/file-list.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class FileList {
213213
})
214214
}
215215

216-
changeFile (path) {
216+
changeFile (path, force) {
217217
const pattern = this._findIncluded(path)
218218
const file = this._findFile(path, pattern)
219219

@@ -226,14 +226,14 @@ class FileList {
226226
fs.statAsync(path),
227227
this._refreshing
228228
]).spread((stat) => {
229-
if (stat.mtime <= file.mtime) throw new Promise.CancellationError()
229+
if (!force && stat.mtime <= file.mtime) throw new Promise.CancellationError()
230230

231231
file.mtime = stat.mtime
232232
return this._preprocess(file)
233233
})
234234
.then(() => {
235235
log.info('Changed file "%s".', path)
236-
this._emitModified()
236+
this._emitModified(force)
237237
return this.files
238238
})
239239
.catch(Promise.CancellationError, () => this.files)

‎lib/server.js

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class Server extends KarmaEventEmitter {
133133
return this._fileList ? this._fileList.refresh() : Promise.resolve()
134134
}
135135

136+
refreshFile (path) {
137+
return this._fileList ? this._fileList.changeFile(path) : Promise.resolve()
138+
}
139+
136140
_start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
137141
if (config.detached) {
138142
this._detach(config, done)

‎test/unit/file-list.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,23 @@ describe('FileList', () => {
632632
})
633633
})
634634

635+
it('fire "file_list_modified" if force is true even if mtime has not changed', () => {
636+
// MATCH: /some/a.js, /some/b.js, /a.txt
637+
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
638+
639+
var modified = sinon.stub()
640+
emitter.on('file_list_modified', modified)
641+
642+
return list.refresh().then((files) => {
643+
// not touching the file, stat will return still the same
644+
modified.reset()
645+
646+
return list.changeFile('/some/b.js', true).then(() => {
647+
expect(modified).to.have.been.calledOnce
648+
})
649+
})
650+
})
651+
635652
it('does not fire "file_list_modified" if mtime has not changed', () => {
636653
// chokidar on fucking windows sometimes fires event multiple times
637654
// MATCH: /some/a.js, /some/b.js, /a.txt

0 commit comments

Comments
 (0)
Please sign in to comment.