执行'节点应用程序'挂在吞咽任务 [英] exec 'node app' hangs inside gulp task

查看:107
本文介绍了执行'节点应用程序'挂在吞咽任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个gulp任务挂在 exec('node config / app')行。第一个 exec 可以正常工作,但第二个只是挂起。

  gulp.task ('test',function(cb){
var exec = require('child_process')。exec;

exec('echo 3',function(err,stdout){
console.log(stdout);
});

exec('node config / app',function(err,stdout,stderr){

console.log(stdout);

var testemOptions = {
file:'testem.json'
};

var t = new testem( );

return t.startCI(testemOptions,function(){
cb();
});
});

});

我可以看到输出 3 ,但没有输出显示为第二个 console.log



我试图在运行测试之前运行我的服务器testem。

我试过这个类似的解决方案,但它不起作用:

另外我最近问了一个悬挂的testem gulp任务问题: Testem gulp任务在完成后挂起。



编辑

解决方案是:

$ p $ gulp.task('test',/ * ['build'],* / function(cb){
var spawn = require('child_process')。spawn;

var proc = spawn('node',['config / app ]);

proc.stdout.on(可读,函数(){
VAR输出= proc.stdout.read();

if(output&& output.toString()。match('express listening')){

var testemOptions = {
file:'testem .json'
};

var t = new testem();
$ b $ t.startCI(testemOptions,function(){
proc.kill();
cb();
});
}

});

});


解决方案

如果您想使用testem来测试节点config / app服务器,你不能使用exec。



Exec应该在命令完成时回调,因此在你的情况下永远不会回调。 b
$ b

尝试

  gulp.task('test',function(cb){
var spawn = require('child_process')。spawn;

var proc = spawn('node',['config / app']);

var testStarted = false;
$ b proc.stdout.on('readable',function(){

if(testStarted)return;

testStarted = true;

var testemOptions = {
file:'testem.json'
};

var t = new testem();

t.startCI(testemOptions,function(){
proc.kill()
cb();
});

}
$ b));

请注意,我没有测试这个代码,它可能不会处理所有你可能遇到的角落案例(例如,如果服务器意外停止例如)

你可能还想检查插件 https://github.com/sargentsurg/gulp-testem


This gulp task hangs on exec('node config/app') line. first exec works fine but the second just hangs.

gulp.task('test', function(cb) {
    var exec = require('child_process').exec;

    exec('echo 3', function(err, stdout) {
        console.log(stdout);
    });

    exec('node config/app', function(err, stdout, stderr) {

        console.log(stdout);

        var testemOptions = {
            file: 'testem.json'
        };

        var t = new testem();

        return t.startCI(testemOptions, function() {
            cb();
        });
    });

});

I can see the output 3 but no output is shown for the second console.log.

I am trying to run my server before running the tests with testem.

I've tried this similar solution but it doesn't work: Exec not returning anything when trying to run git shortlog with nodejs.

Also I've recently asked a hanging testem gulp task question: Testem gulp task hangs after finished.

Edit:

My current solution is:

gulp.task('test', /*['build'],*/ function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    proc.stdout.on('readable', function() {
        var output = proc.stdout.read();

        if (output && output.toString().match('express listening')) {

            var testemOptions = {
                file: 'testem.json'
            };

            var t = new testem();

            t.startCI(testemOptions, function() {
                proc.kill();
                cb();
            });
        }

    });

});

解决方案

If you want to use testem to test the "node config/app" server, you cannot use exec.

Exec is supposed to callback when the command is finished so in your case it will never callback.

try with

gulp.task('test', function(cb) {
    var spawn = require('child_process').spawn;

    var proc = spawn('node', ['config/app']);

    var testStarted = false;

    proc.stdout.on('readable', function() {

        if (testStarted) return;

        testStarted = true;

        var testemOptions = {
           file: 'testem.json'
        };

        var t = new testem();

        t.startCI(testemOptions, function() {
            proc.kill()
            cb();
        });

    }
});

Note that I did not test this code and that it probably does not handle all the corner cases you might encounter (if the server stops unexpectedly for example)

you may also want to check the plugin https://github.com/sargentsurg/gulp-testem

这篇关于执行'节点应用程序'挂在吞咽任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆