使用grunt自动执行npm和bower安装

时间:2013-01-04 23:15:27

标签: javascript node.js npm bower gruntjs

我有一个节点/角度项目,它使用npm进行后端依赖关系管理,并使用bower进行前端依赖关系管理。我想使用grunt任务来执行两个安装命令。我一直无法弄清楚如何去做。

我尝试使用exec,但它实际上并未安装任何内容。

module.exports = function(grunt) {

    grunt.registerTask('install', 'install the backend and frontend dependencies', function() {
        // adapted from http://www.dzone.com/snippets/execute-unix-command-nodejs
        var exec = require('child_process').exec,
            sys  = require('sys');

        function puts(error, stdout, stderr) { console.log(stdout); sys.puts(stdout) }

        // assuming this command is run from the root of the repo
        exec('bower install', {cwd: './frontend'}, puts);
    });

};

当我cd进入前端时,打开node,然后从控制台运行此代码,这样可以正常工作。我在咕噜咕噜的任务中做错了什么?

(我也试过使用bower和npm API,但也无法使用。)

5 个答案:

答案 0 :(得分:133)

要在npm install期间同时安装客户端组件而不是服务器端库,您可以添加package.json

"dependencies": {
    ...
    "bower" : ""
},
"scripts": {
    ...
    "postinstall" : "bower install"
}

我更喜欢在安装和测试/构建之间做出区分

答案 1 :(得分:35)

你需要告诉grunt你正在使用异步方法(.exec),方法是调用this.async()方法,获得回调,并在exec完成时调用它。

这应该有效:

module.exports = function(grunt) {
    grunt.registerTask('install', 'install the backend and frontend dependencies', function() {
        var exec = require('child_process').exec;
        var cb = this.async();
        exec('bower install', {cwd: './frontend'}, function(err, stdout, stderr) {
            console.log(stdout);
            cb();
        });
    });
};

请参阅Why doesn't my asynchronous task complete?

答案 2 :(得分:7)

仅供参考,我现在就在这里。

您也可以采用另一种方式解决问题,即让npm处理bower的执行,并最终让grunt处理npm。请参阅Use bower with heroku

grunt.registerTask('install', 'install the backend and frontend dependencies', function() {
    var async = require('async');
    var exec = require('child_process').exec;
    var done = this.async();

    var runCmd = function(item, callback) {
        process.stdout.write('running "' + item + '"...\n');
        var cmd = exec(item);
        cmd.stdout.on('data', function (data) {
            grunt.log.writeln(data);
        });
        cmd.stderr.on('data', function (data) {
            grunt.log.errorlns(data);
        });
        cmd.on('exit', function (code) {
            if (code !== 0) throw new Error(item + ' failed');
            grunt.log.writeln('done\n');
            callback();
        });
    };

    async.series({
        npm: function(callback){
            runCmd('npm install', callback);
        },
        bower: function(callback){
            runCmd('bower install', callback);  
        }
    },
    function(err, results) {
        if (err) done(false);
        done();
    });
});

答案 3 :(得分:2)

完成这项工作的Grunt任务(根据Sindre上面的解决方案):

https://github.com/ahutchings/grunt-install-dependencies

答案 4 :(得分:2)

执行bower install命令的Grunt任务: https://github.com/yatskevich/grunt-bower-task

另外,你可以使用 https://github.com/stephenplusplus/grunt-bower-install

将您的依赖项自动注入index.html文件

相关问题