使用grunt自动化npm和bower安装 [英] Automate npm and bower install with grunt

查看:134
本文介绍了使用grunt自动化npm和bower安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个node / angular项目,它使用npm作为后端依赖管理,而bower作为前端依赖管理。我想使用一个咕噜任务来执行两个安装命令。我一直未能弄清楚如何做到这一点。



我尝试使用 exec ,但它实际上并没有安装任何东西。

  module.exports = function(grunt){

grunt .registerTask('install','安装后端和前端依赖项',function(){
// //改编自http://www.dzone.com/snippets/execute-unix-command-nodejs
exec exec = require('child_process')。exec,
sys = require('sys');

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

//假设这个命令从repo的根目录运行
exec('bower install',{cwd:'./frontend'}, puts);
});

};

当我将 cd 放入前端时, node ,并从控制台运行此代码,这工作正常。我在做什么错误的任务?



(我也尝试使用bower和npm API,但是也无法做到这一点)

解决方案

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

  module.exports = function(grunt){
grunt.registerTask('install' ,'安装后端和前端依赖关系',function(){
var exec = require('child_process')。exec;
var cb = this.async();
exec(' ();
cb();
});
});
};

请参阅为什么我的异步任务没有完成?


I have a node / angular project that uses npm for backend dependency management and bower for frontend dependency management. I'd like to use a grunt task to do both install commands. I haven't been able to figure out how to do it.

I made an attempt using exec, but it doesn't actually install anything.

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);
    });

};

When I cd into frontend, open up node, and run this code from the console, this works fine. What am I doing wrong in the grunt task?

(I also tried to use the bower and npm APIs, but couldn't make that work either.)

解决方案

You need to tell grunt that you're using an async method (.exec) by calling the this.async() method, getting a callback, and calling that when exec is done.

This should work:

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();
        });
    });
};

See Why doesn't my asynchronous task complete?

这篇关于使用grunt自动化npm和bower安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆