Javascript 在 Grunt 任务中运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10456865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:23:14  来源:igfitidea点击:

Running a command in a Grunt Task

javascripttemplatesgruntjscontinuous-integrationgoogle-closure-compiler

提问by JuanO

I'm using Grunt(task-based command line build tool for JavaScript projects) in my project. I've created a custom tag and I am wondering if it is possible to run a command into it.

我在我的项目中使用Grunt(用于 JavaScript 项目的基于任务的命令行构建工具)。我创建了一个自定义标签,我想知道是否可以在其中运行命令。

To clarify, I'm trying to use Closure Templates and "the task" should call the jar file to pre-compile the Soy file to a javascript file.

澄清一下,我正在尝试使用闭包模板,并且“任务”应该调用 jar 文件来将 Soy 文件预编译为 javascript 文件。

I'm running this jar from command line, but I want to set it as a task.

我正在从命令行运行这个 jar,但我想将它设置为一个任务。

采纳答案by JuanO

I've found a solution so I'd like to share with you.

我已经找到了解决方案,所以我想与您分享。

I'm using grunt under node so, to call terminal commands you need to require 'child_process' module.

我在 node 下使用 grunt,所以要调用需要“child_process”模块的终端命令。

For example,

例如,

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

myTerminal(commandToBeExecuted, function(error, stdout, stderr) {
    if (!error) {
         //do something
    }
});

回答by papercowboy

Alternatively you could load in grunt plugins to help this:

或者,您可以加载 grunt 插件来帮助实现这一点:

grunt-shellexample:

grunt-shell示例:

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

or grunt-execexample:

grunt-exec示例:

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

回答by Nick Heiner

Check out grunt.util.spawn:

退房grunt.util.spawn

grunt.util.spawn({
  cmd: 'rm',
  args: ['-rf', '/tmp'],
}, function done() {
  grunt.log.ok('/tmp deleted');
});

回答by kikito

If you are using the latest grunt version (0.4.0rc7 at the time of this writing) both grunt-exec and grunt-shell fail (they don't seem to be updated to handle the latest grunt). On the other hand, child_process's exec is async, which is a hassle.

如果您使用的是最新的 grunt 版本(在撰写本文时为 0.4.0rc7),则 grunt-exec 和 grunt-shell 都会失败(它们似乎没有更新以处理最新的 grunt)。另一方面,child_process 的exec 是async,比较麻烦。

I ended up using Jake Trent's solution, and adding shelljsas a dev dependency on my project so I could just run tests easily and synchronously:

我最终使用了Jake Trent 的解决方案,并将shelljs添加为我项目的开发依赖项,这样我就可以轻松同步地运行测试:

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

回答by Artjom Kurapov

Guys are pointing child_process, but try to use execSyncto see output..

伙计们指向 child_process,但尝试使用execSync来查看输出..

grunt.registerTask('test', '', function () {
        var exec = require('child_process').execSync;
        var result = exec("phpunit -c phpunit.xml", { encoding: 'utf8' });
        grunt.log.writeln(result);
});

回答by Daniel Steigerwald

For async shell commands working with Grunt 0.4.x use https://github.com/rma4ok/grunt-bg-shell.

对于使用 Grunt 0.4.x 的异步 shell 命令,请使用https://github.com/rma4ok/grunt-bg-shell