Automatyzacja instalacji npm i altany za pomocą grunt

Mam projekt node / angular, który używa npm do zarządzania zależnościami backend i bower do zarządzania zależnościami frontend. Chciałbym użyć zadania grunt do wykonania obu komend instalacyjnych. Nie byłem w stanie wymyślić, jak to zrobić.

Próbowałem użyć exec, ale tak naprawdę nic nie instaluje.

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

};

Kiedy wejdę cd do frontendu, Otwórz node i uruchom ten kod z konsoli, to działa dobrze. Co robię źle w zadaniu grunt?

(ja również próbowałem użyć interfejsów API bower i npm, ale też nie mogłem tego zrobić.)

Author: Nick Heiner, 2013-01-05

5 answers

Musisz powiedzieć gruntowi, że używasz metody asynchronicznej (.exec) przez wywołanie metody this.async(), uzyskanie wywołania zwrotnego i wywołanie go po zakończeniu exec.

To powinno zadziałać:

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

Zobacz Dlaczego moje zadanie asynchroniczne nie jest zakończone?

 34
Author: Sindre Sorhus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-15 22:14:40

Aby zainstalować komponenty po stronie klienta podczas npm install w tym samym czasie, co biblioteki po stronie serwera, możesz dodać package.json

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

Wolę odróżnić install od test/build

 133
Author: jsan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-04 08:45:33

Dla twojej wiadomości, oto, gdzie teraz jestem.

Mogłeś też wziąć problem w inny sposób, np. niech npm zajmie się wykonaniem bowera, a ostatecznie niech grunt zajmie się npm. Zobacz Użyj altany z 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();
    });
});
 7
Author: xavier.seignard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-08 22:36:39

Zadanie, które wykonuje właśnie to zadanie (zgodnie z powyższym rozwiązaniem Sindre ' a):

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

 2
Author: nostopbutton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-12 13:15:46

Zadanie Grunt, które wykonuje polecenie bower install : https://github.com/yatskevich/grunt-bower-task

Ponadto możesz użyć https://github.com/stephenplusplus/grunt-bower-install

Aby automatycznie wprowadzić zależności do indeksu.plik html

 2
Author: Itsik Avidan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-27 10:04:27