Skip to content

Commit

Permalink
fix: Make background option work with grunt tasks written in CoffeeSc…
Browse files Browse the repository at this point in the history
…ript

Use node's built-in message channel for the background server, allowing
only the modulePath argument to be passed to child_process.fork. This works
around an issue in CoffeeScript's interceptor, which doesn't support treating
args as an optional parameter, passing options as args and omitting options.

Closes #174
  • Loading branch information
MikeDimmickMnetics committed May 25, 2016
1 parent ae37486 commit 52174ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
16 changes: 16 additions & 0 deletions gruntfile.js
Expand Up @@ -87,6 +87,16 @@ module.exports = function (grunt) {
}
]
},
background: {
background: true,
files: [
{
src: 'node_modules/expect.js/index.js'
}, {
src: 'test/**/*.js'
}
]
},
config: {
configFile: 'karma.conf.js',
singleRun: true
Expand Down Expand Up @@ -114,6 +124,11 @@ module.exports = function (grunt) {
tests: {
files: 'test/**/*.js',
tasks: ['karma:dev:run']
},
bgtest: {
// This is just to stop node exiting
files: 'test/**/*.js',
tasks: []
}
}
})
Expand All @@ -123,6 +138,7 @@ module.exports = function (grunt) {

grunt.registerTask('test', ['karma:single', 'karma:config', 'karma:merge'])
grunt.registerTask('default', ['eslint', 'test'])
grunt.registerTask('bgtest', ['karma:background', 'watch:bgtest'])

grunt.registerTask('release', 'Bump the version and publish to npm.', function (type) {
grunt.task.run([
Expand Down
8 changes: 4 additions & 4 deletions lib/background.js
@@ -1,6 +1,6 @@
var Server = require('karma').Server
process.stdin.on('readable', function () {
var data = JSON.parse(process.stdin.read())
var server = new Server(data)
server.start(data)

process.on('message', function (data) {
var server = new Server(data.config)
server.start()
})
5 changes: 2 additions & 3 deletions tasks/grunt-karma.js
Expand Up @@ -112,10 +112,8 @@ module.exports = function (grunt) {
// allow karma to be run in the background so it doesn't block grunt
if (data.background) {
var backgroundProcess = require('child_process').fork(
path.join(__dirname, '..', 'lib', 'background.js'),
{ silent: true }
path.join(__dirname, '..', 'lib', 'background.js')
)
backgroundProcess.stdin.write(JSON.stringify(data))

backgroundProcess.on('close', function (code) {
var error = code
Expand All @@ -128,6 +126,7 @@ module.exports = function (grunt) {
backgroundProcess.kill()
})

backgroundProcess.send({ config: data })
done()
} else {
var server = new Server(data, finished.bind(done))
Expand Down

0 comments on commit 52174ef

Please sign in to comment.