2

次のようなカスタム grunt タスクがあります。

grunt.registerTask('list', 'test', function()
{
    var child;
    child = exec('touch skhjdfgkshjgdf',
      function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
          console.log('exec error: ' + error);
        }
    });
});

これは機能しますが、pwd コマンドを実行しようとすると、出力が得られません。これの最終目標は、sassファイルをgruntでコンパイルできるようにすることです。これを行う最善の方法は、コマンドラインコマンドを実行してsassをgruntでコンパイルすることだと思いますが、何らかの出力を画面に表示したい正常に動作します。このコードが grunt/nodejs を介して unix コマンドを実行した結果を出力しない理由はありますか?

4

1 に答える 1

3

exec() is async so you need to tell grunt that and execute the callback when it's done:

grunt.registerTask('list', 'test', function()
{
    // Tell grunt the task is async
    var cb = this.async();

    var child = exec('touch skhjdfgkshjgdf', function (error, stdout, stderr) {
        if (error !== null) {
          console.log('exec error: ' + error);
        }

        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);

        // Execute the callback when the async task is done
        cb();
    });
});

From grunt docs: Why doesn't my asynchronous task complete?

于 2012-08-19T14:55:06.777 に答える