Grunt Spawn 任务并获得结果

Grunt Spawn task and get result

我正在尝试创建一个生成命令 window 并从命令中获取结果的 grunt 任务。作为测试,我只是 运行 cd 获取当前目录,(我也试过 cwd)

grunt.registerTask('random', function(){
    var done = this.async();

    grunt.util.spawn({
        cmd: 'cd'
    }, function(error, result, exitCode){
        console.log("callback hit");
        console.log(result);
        done();
    });

我收到找不到 cd 的错误

callback hit
{ stdout: '',
stderr: 'Error: not found: cd',
code: 127,
toString: [Function: toString] }

如何在 grunt 中执行 shell window 并返回结果?

grunt.registerTask('random', function(){

    var exec = require('child_process').execSync;

    var result = exec('cd', { encoding: 'utf8' });
    meta.version = result;
    grunt.log.writeln('version: ' + result);
});