2015-10-05 34 views
0

当我运行没有node任务的gulp时,它工作正常,并按预期处理客户端文件,如果我运行gulp node,它会按预期处理服务器文件。但是,如果我同时运行了gulp,它将按预期方式处理客户端和服务器文件,但是,通过按'Ctrl + C'(尝试在Windows 10 & Mac El Capitan上),它不会让我退出。有什么我在这里做错了吗?使用gulp-nodemon&gulp.watch时,无法使用Ctrl + C停止Gulp

'use strict'; 
    var gulp = require('gulp'); 
    var connect = require('gulp-connect'); 
    var browserify = require('browserify'); 
    var source = require('vinyl-source-stream'); 
    var nodemon = require('gulp-nodemon'); 

    var config = { 
     port: 9005, 
     devBaseUrl: 'http://localhost', 
     paths: { 
      html: './src/*.html', 
      dist: './dist', 
      js: './src/**/*.js', 
      images: './src/images/*', 
      mainJs: './src/main.js', 
      css: [ 
       'node_modules/bootstrap/dist/css/bootstrap.min.css', 
       'node_modules/bootstrap/dist/css/bootstrap-theme.min.css' 
      ] 
     } 
    }; 

gulp.task('connect', function() { 
    connect.server({ 
     root: ['dist'], 
     port: config.port, 
     base: config.devBaseUrl, 
     livereload: true 
    }); 
}); 


gulp.task('html', function() { 
    gulp.src(config.paths.html) 
     .pipe(gulp.dest(config.paths.dist)) 
}); 

gulp.task('js', function() { 
    browserify(config.paths.mainJs) 
     .bundle() 
     .on('error', console.error.bind(console)) 
     .pipe(source('bundle.js')) 
     .pipe(gulp.dest(config.paths.dist + '/scripts')) 
     .pipe(connect.reload()) 

}); 

gulp.task('node', function() { 
    nodemon({ 
     script: 'server/index.js', 
     ext: 'js', 
     env: { 
      PORT: 8000 
     }, 
     ignore: ['node_modules/**','src/**','dist/**'] 
    }) 
    .on('restart', function() { 
     console.log('Restarting node server...'); 
    }) 
}); 

gulp.task('watch', function() { 
    gulp.watch(config.paths.js, ['js']); 
}); 

gulp.task('default', ['html', 'js', 'connect', 'node', 'watch']); 
+0

你可以把这个问题解释为展示你的问题的最小代码量吗? –

+0

我已经删除了不必要的部分 –

回答

3

就在顶部,你有

var monitorCtrlC = require('monitorctrlc'); 

和内部watch任务你有

monitorCtrlC(); 

这似乎是this library

此功能将防止发送SIGINT当按下Ctrl + C时,信号为 。相反,指定的(或默认)回调将被调用。

+0

这就是我在Windows上开发时所使用的东西。由于在Windows上它需要首先按'Ctrl + C'然后确认'YES/NO'来结束批处理,monitorCtrlC()只是帮助删除窗口中的额外步骤。不过,我已经尝试彻底删除它,因为我不需要它在Mac上,最终结果仍然是一样的。 –

+1

你可能不应该在这个问题中包含它,因为它确实是你在抱怨:) –

+1

对不起,我已经删除它,以避免混淆 –

0

以防万一它可以帮助别人,我删除了node_modules,做npm install其固定的问题对我来说...

2

我有一个类似的问题之前,这是你在找什么:

process.on('SIGINT', function() { 
    setTimeout(function() { 
    gutil.log(gutil.colors.red('Successfully closed ' + process.pid)); 
    process.exit(1); 
    }, 500); 
}); 

只需将此代码添加到您的gulp文件。它会监视ctrl + C并正确终止进程。如果需要,也可以在超时时间内放置其他代码。

+0

谢谢你解决了我的问题。 – dippas