94

プロジェクトでGrunt (JavaScript プロジェクト用のタスクベースのコマンド ライン ビルド ツール) を使用しています。カスタム タグを作成しましたが、それにコマンドを実行できるかどうか疑問に思っています。

明確にするために、私はクロージャーテンプレートを使用しようとしています。「タスク」はjarファイルを呼び出して、Soyファイルをjavascriptファイルにプリコンパイルする必要があります。

このjarをコマンドラインから実行していますが、タスクとして設定したいです。

4

6 に答える 6

106

または、これを支援するために grunt プラグインをロードすることもできます。

うなり声の例:

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

または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
  }
}
于 2012-11-01T23:07:38.100 に答える
36

チェックアウトgrunt.util.spawn:

grunt.util.spawn({
  cmd: 'rm',
  args: ['-rf', '/tmp'],
}, function done() {
  grunt.log.ok('/tmp deleted');
});
于 2013-02-13T21:23:46.350 に答える
20

解決策を見つけたので、共有したいと思います。

ノードの下で grunt を使用しているため、端末コマンドを呼び出すには、「child_process」モジュールが必要です。

例えば、

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

myTerminal(commandToBeExecuted, function(error, stdout, stderr) {
    if (!error) {
         //do something
    }
});
于 2012-05-07T19:25:16.197 に答える
18

最新の grunt バージョン (この記事の執筆時点では 0.4.0rc7) を使用している場合、grunt-exec と grunt-shell の両方が失敗します (最新の grunt を処理するように更新されていないようです)。一方、child_process の exec は async であり、これは面倒です。

結局、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');
});
于 2013-02-03T20:00:41.030 に答える
14

男は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);
});
于 2015-04-22T14:18:17.357 に答える
2

Grunt 0.4.xで動作する非同期シェルコマンドについては、https: //github.com/rma4ok/grunt-bg-shellを使用してください。

于 2013-03-10T23:10:48.173 に答える