Cordova Build Hooks + Grunt

依然范特西╮ 提交于 2019-12-11 07:53:11

问题


I am trying to link-up my cordova build with grunt, there are a tasks that we run when serving the project locally that I would like to reuse during our Cordova build. For this I create a simple before_build hook and placed it in hooks/before_build.

My build hook looks like:

#!/usr/bin/env node
var grunt = require('grunt');
grunt.tasks(['build']);
process.exit(0);

The initial part of the build works well, all of the files are copied as expected. However, within my build task, I make several additional calls such as:

grunt.task.run('wiredep');

This, and all other similar calls, fail to execute without an error or warning.

Why would I not be able to call additional grunt tasks?

All of my tasks are defined in the Gruntfile using either loadNpmTask or registerTask, is this an incorrect configuration?


回答1:


Well... grunt.task.run(...) simply adds a task to Grunt's "queue" (of sorts), it does not immediately run that Grunt task. I think you may want to actually set your build task as the default in your Gruntfile.js:

grunt.registerTask('default', ['build']);

Then just use a bash script (versus a Node shell script):

#!/bin/bash
grunt

Note that you will need to have the grunt-cli installed first:

~$ nmp install -g grunt-cli



回答2:


I received some feedback on the Grunt issues board:
https://github.com/gruntjs/grunt/issues/1265

Any time I need to call one of my Grunt tasks, I add a hook_task.sh file with the following:

#!/bin/sh
grunt build

This works on Windows/Linux/Mac without any issues.

I would still like to know why the node approach was not working. Part of me feels much more comfortable using that approach, but this is working well so no complaints.

(Note: My Gruntfile.js is in the root of my Cordova App)



来源:https://stackoverflow.com/questions/27478566/cordova-build-hooks-grunt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!