简体   繁体   English

如何在Grunt中的循环内执行Shell命令?

[英]How to execute shell commands inside a loop in Grunt?

I have an app folder with multiple subfolders. 我有一个包含多个子文件夹的应用程序文件夹。 I'm looping through these folders and trying to run a shell command for each folder. 我正在遍历这些文件夹,并尝试为每个文件夹运行一个shell命令。 This is what i have so far: 这是我到目前为止所拥有的:

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

    var done = this.async();
    var exec = require('child_process').exec;

    // Start deployment
    grunt.log.write('Starting deployment...').ok();

    // Read all subdirectories from the app folder
    grunt.file.expand("./app/*").forEach(function (dir) {

        // Get folder name
        var functionName = dir.split('/')[2];

        exec('echo ' + functionName, {
            cwd: dir
        }, function(error, stdout, stderr) {
            grunt.log.ok(functionName+' deployed.');
            done();
        });

    });

});

What i would expect in my terminal is to print out each folder name, but all i got is the callback in the exec function, like this: 我希望在终端中打印出每个文件夹名称,但是我所得到的只是exec函数中的回调,如下所示:

Running "deploy" task
Starting deployment...OK
>> testA deployed.
>> testB deployed.

I'm not sure what I'm doing wrong. 我不确定自己在做什么错。

The output of the command can be found in the stdout variable passed to your callback. 该命令的输出可以在传递给回调的stdout变量中找到。 It's not automatically printed the terminal. 它不会自动打印终端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM