Skip to content

Commit

Permalink
fix(task): prevent spawn ENAMETOOLONG on Windows
Browse files Browse the repository at this point in the history
As grunt.util.spawn is deprecated (see https://github.com/gruntjs/grunt-legacy-util/blob/621563dd8f6966af39546991bb8d710146430b79/README.md), we might as well switch to native node api. I've chosen child_process.fork() because it avoids looking for the correct node executable on windows (compare https://github.com/gruntjs/grunt-legacy-util/blob/621563dd8f6966af39546991bb8d710146430b79/index.js#L168-L173)

#Closes 161
  • Loading branch information
NicBright committed Sep 9, 2015
1 parent 6968361 commit 2b5e643
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Server = require('karma').Server
var data = JSON.parse(process.argv[2])
var server = new Server(data)

server.start(data)
process.stdin.on('readable', function () {
var data = JSON.parse(process.stdin.read())
var server = new Server(data)
server.start(data)
})
18 changes: 10 additions & 8 deletions tasks/grunt-karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,20 @@ module.exports = function (grunt) {

// allow karma to be run in the background so it doesn't block grunt
if (data.background) {
var backgroundArgs = {
cmd: 'node',
args: process.execArgv.concat([

var backgroundProcess = require('child_process').fork(
path.join(__dirname, '..', 'lib', 'background.js'),
JSON.stringify(data)
])
}
var backgroundProcess = grunt.util.spawn(backgroundArgs, function (error) {
{ silent: true }
)
backgroundProcess.stdin.write(JSON.stringify(data))

backgroundProcess.on('close', function (code) {
var error = code
if (error) {
grunt.log.error(error)
grunt.log.error('background karma process exited with error (code: ' + code + ')')
}
})

process.on('exit', function () {
backgroundProcess.kill()
})
Expand Down

0 comments on commit 2b5e643

Please sign in to comment.