5

grunt で実行される node exec コマンドの次の例を教えてください。

echoコマンドは実行中であり、hello-world.txt作成されていgrunt.log.writelnますが、コールバック関数内のコマンドが起動していません。

var exec = require('child_process').exec,
    child;

    child = exec('echo hello, world! > hello-world.txt', 
        function(error, stdout, stderr){
            grunt.log.writeln('stdout: ' + stdout);
            grunt.log.writeln('stderr: ' + stderr);
            if (error !== null) {
                grunt.log.writeln('exec error: ' + error);
          }
        }
    );

参考文献:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

ノードの子プロセスから値を取得する

4

1 に答える 1

10

ドー!これは FAQ にあります。

非同期タスクに Gruntjs を使用する場合、タスクの完了時期を手動で指定する必要があります。 https://github.com/gruntjs/grunt/wiki/
よくある質問 https://github.com/robdodson/async-grunt-tasks
https://github.com/rwldrn/dmv/blob/master/ node_modules/grunt/docs/api_task.md

後世のために、上記は次のようになります。

var exec = require('child_process').exec,
    child,
    done = grunt.task.current.async(); // Tells Grunt that an async task is complete

child = exec('echo hello, world! > hello-world.txt', 
    function(error, stdout, stderr){
        grunt.log.writeln('stdout: ' + stdout);
        grunt.log.writeln('stderr: ' + stderr);
        done(error); // Technique recommended on #grunt IRC channel. Tell Grunt asych function is finished. Pass error for logging; if operation completes successfully error will be null

      }
    }
);
于 2012-12-19T20:31:37.090 に答える