Automate npm and bower install with grunt

前端 未结 5 1863
耶瑟儿~
耶瑟儿~ 2021-01-29 21:38

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 co

5条回答
  •  半阙折子戏
    2021-01-29 22:00

    FYI, here is where i am for now.

    You could also have taken the problem another way, i.e. let npm handle the execution of bower, and ultimately let grunt handle npm. See 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();
        });
    });
    

提交回复
热议问题