Ejecutar un comando en una tarea Grunt


Estoy usando Grunt (herramienta de compilación de línea de comandos basada en tareas para proyectos JavaScript) en mi proyecto. He creado una etiqueta personalizada y me pregunto si es posible ejecutar un comando en él.

Para aclarar, estoy tratando de usar Plantillas de cierre y "la tarea" debería llamar al archivo jar para precompilar el archivo Soy a un archivo javascript.

Estoy ejecutando este jar desde la línea de comandos, pero quiero configurarlo como una tarea.

Author: Rob Evans, 2012-05-05

6 answers

Alternativamente, puede cargar complementos de grunt para ayudar a esto:

Grunt-shell ejemplo:

shell: {
  make_directory: {
    command: 'mkdir test'
  }
}

O grunt-exec ejemplo:

exec: {
  remove_logs: {
    command: 'rm -f *.log'
  },
  list_files: {
    command: 'ls -l **',
    stdout: true
  },
  echo_grunt_version: {
    command: function(grunt) { return 'echo ' + grunt.version; },
    stdout: true
  }
}
 101
Author: papercowboy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-01 23:07:38

Echa un vistazo grunt.util.spawn:

grunt.util.spawn({
  cmd: 'rm',
  args: ['-rf', '/tmp'],
}, function done() {
  grunt.log.ok('/tmp deleted');
});
 34
Author: Nick Heiner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-13 16:43:09

He encontrado una solución, así que me gustaría compartir con ustedes.

Estoy usando grunt bajo node, por lo que para llamar a comandos de terminal necesita requerir el módulo 'child_process'.

Por ejemplo,

var myTerminal = require("child_process").exec,
    commandToBeExecuted = "sh myCommand.sh";

myTerminal(commandToBeExecuted, function(error, stdout, stderr) {
    if (!error) {
         //do something
    }
});
 19
Author: blackdragon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-01 01:54:40

Si está utilizando la última versión de grunt (0.4.0rc7 en el momento de escribir este artículo), tanto grunt-exec como grunt-shell fallan (no parecen estar actualizados para manejar la última versión de grunt). Por otro lado, el exec de child_process es async, lo cual es una molestia.

Terminé usando La solución de Jake Trent , y agregando shelljs como una dependencia de desarrollo en mi proyecto para que pudiera ejecutar pruebas de manera fácil y sincrónica:

var shell = require('shelljs');

...

grunt.registerTask('jquery', "download jquery bundle", function() {
  shell.exec('wget http://jqueryui.com/download/jquery-ui-1.7.3.custom.zip');
});
 18
Author: kikito,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-03 20:00:41

Los chicos están apuntando child_process, pero intenta usar execSync para ver la salida..

grunt.registerTask('test', '', function () {
        var exec = require('child_process').execSync;
        var result = exec("phpunit -c phpunit.xml", { encoding: 'utf8' });
        grunt.log.writeln(result);
});
 13
Author: Artjom Kurapov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-17 12:00:22

Para comandos de shell asíncronos que trabajan con Grunt 0.4.x use https://github.com/rma4ok/grunt-bg-shell .

 2
Author: Daniel Steigerwald,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-10 23:10:48