Skip to content

Commit

Permalink
fix: readFile on windows (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviu-Marian authored and joshwiens committed Mar 15, 2017
1 parent 4216f13 commit cad9f8b
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function Plugin(
/* config.files */ files,
/* config.frameworks */ frameworks,
customFileHandlers,
emitter) {
emitter
) {
webpackOptions = _.clone(webpackOptions) || {}
webpackMiddlewareOptions = _.clone(webpackMiddlewareOptions || webpackServerOptions) || {}

Expand Down Expand Up @@ -193,7 +194,7 @@ Plugin.prototype.readFile = function(file, callback) {
var middleware = this.middleware
var optionsCount = this.optionsCount

function doRead() {
var doRead = function() {
if (optionsCount > 1) {
async.times(optionsCount, function(idx, callback) {
middleware.fileSystem.readFile('/_karma_webpack_/' + idx + '/' + file.replace(/\\/g, '/'), callback)
Expand All @@ -212,20 +213,27 @@ Plugin.prototype.readFile = function(file, callback) {
callback(null, Buffer.concat(contents))
})
} else {
middleware.fileSystem.readFile('/_karma_webpack_/' + file.replace(/\\/g, '/'), callback)
}
}
if (!this.waiting) {
try {
doRead()
} catch (e) {
// If this is an error from `readFileSync` method, wait for the next tick. Credit #69 @mewdriller
if (e.message.substring(0, 20) === "Path doesn't exist '") { // eslint-disable-line quotes
this.waiting = [process.nextTick.bind(process, this.readFile.bind(this, file, callback))]
} else {
throw e
try {
var fileContents = middleware.fileSystem.readFileSync('/_karma_webpack_/' + file.replace(/\\/g, '/'))

callback(undefined, fileContents)
} catch (e) {
// If this is an error from `readFileSync` method, wait for the next tick.
// Credit #69 @mewdriller
if (e.code === 'ENOENT') {
// eslint-disable-line quotes
this.waiting = [process.nextTick.bind(process, this.readFile.bind(this, file, callback))]

// throw otherwise
} else {
callback(e)
}
}
}
}.bind(this)

if (!this.waiting) {
doRead()
} else {
// Retry to read once a build is finished
// do it on process.nextTick to catch changes while building
Expand Down

0 comments on commit cad9f8b

Please sign in to comment.