nodejs顺序执行shell

最近工作中需要用到nodejs编写脚本来顺序执行自动化测试用例,编写代码如下:

var runCommand = function (command){
        child_process.exec(command,function (error, stdout, stderr) {
        if (stdout !== null) {
          console.log('exec stdout: ' + stdout);
        }
        if (error !== null) {
          console.log('exec error: ' + error);
        }
    });
};

for (var i = 0; i < path.length; i++) {
                if (i !== 0) {
                        if (argv.report === 'true') {
                                runCommand('mocha ./');
                        };

                };
};

但是nodejs的child_process模块执行是异步的,多个命令同时执行会失败。但是自动化测试的服务

不支持同时执行,导致测试多个进程失败。

最后在网上找到了nodejs的shelljs模块,可以解决问题:

require('shelljs/global');

for (var i = 0; i < path.length; i++) {
                if (exec('mocha ./').code !== 0) {
                        echo('Error: Git commit failed');
                        exit(1);
                }
};