Last week I thought I'd move a few hundred lines of our build process from our custom build tool to grunt.
I ran into a problem with grunt.task.run
queuing a task but not
waiting for the task to complete before continuing or ensuring
execution order
(relevant testcase).
Maybe it was problem with the child processes not executing synchronously. I tried a few ways to run a synchronous child process in node:
However, the docs and source say the job is queued but not run (even
though the method is grunt.task.run
). For example with the following
grunt file:
module.exports = (grunt) ->
grunt.registerTask 'pre-foo', ->
console.log 'pre-foo'
grunt.registerTask 'foo', ->
grunt.task.run 'pre-foo'
console.log 'foo'
grunt.task.run 'post-foo'
grunt.registerTask 'post-foo', ->
console.log 'post-foo'
We get:
» grunt foo
Running "foo" task
foo
Running "pre-foo" task
pre-foo
Running "post-foo" task
post-foo
Done, without errors.
Grunt runs foo
then drains the task queue running pre-foo
then
'post-foo`. I refactored one task using the auto method from Async.js:
grunt.registerTask 'tag-patch-version', ->
done = @async()
async.auto(
# Don't need array if one callback
bumpVersion: (callback) ->
console.log 'bumpVersion'
grunt.task._tasks['bump-version'].fn 'patch', null, callback
updateVersion: ['bumpVersion', (callback) ->
console.log 'UpdateVersion'
cmd = """git add package.json && \
git commit -m 'Update version' && \
git checkout package.json"""
grunt.helper 'grunt-exec-spawn', cmd, callback
]
writeVersion: ['updateVersion', (callback) ->
console.log "Write RELEASE-VERSION file"
grunt.task._tasks['bump-version'].fn null, null, callback
]
tagVersion: ['writeVersion', (callback) ->
console.log "tag version"
cmd = "git tag -a $(cat RELEASE-VERSION) -m $(cat RELEASE-VERSION)"
grunt.helper 'grunt-exec-spawn', cmd, -> callback(); done()
]
)
This gave me the nice Makefile format I wanted, but I had more code,
needed all subtasks to take a callback, and needed to call the
bump-version
subtask through the private grunt.tasks._tasks
( the
subtask wouldn't resolve it's dependencies if bump-version
called a
grunt clean task for instance).
Grunt.js does handle a lot of common JS build tasks well (the watchers are nice), but at this point I figured I'd wasted enough time trying to write a Makefile in grunt and switched to a plain Makefile.