简体   繁体   English

一起使用gulp-nodemon和gulp.watch时,无法使用Ctrl + C停止Gulp

[英]Cannot stop Gulp with Ctrl+C when using gulp-nodemon & gulp.watch together

When I run gulp without the node task, it works fine and processes client file as expected, If I run gulp node it processes server file as expected. 当我运行无node任务的gulp时,它可以正常工作并按预期方式处理客户端文件;如果运行gulp node则其按预期方式处理服务器文件。 However if I run both gulp it processes both client and server file as expected, however, it won't let me quit by pressing 'Ctrl + C' (Tried it on windows 10 & Mac El Capitan). 但是,如果我同时运行两个gulp程序,它将按预期处理客户端和服务器文件,但是,它不会让我通过按“ Ctrl + C”(在Windows 10和Mac El Capitan上尝试过)退出。 Is there something I'm doing wrong here? 我在这里做错什么了吗?

'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']);

Right at the top you have 就在顶部

var monitorCtrlC = require('monitorctrlc');

and inside of the watch task you have watch任务中

monitorCtrlC();

Which seems to be this library 好像是这个图书馆

This function will prevent sending of SIGINT signal when Ctrl+C is pressed. 按下Ctrl + C时,此功能将阻止发送SIGINT信号。 Instead, the specified (or default) callback will be invoked. 而是将调用指定的(或默认的)回调。

I had a similar issue before this is what you're looking for : 在您要寻找的东西之前,我遇到了类似的问题:

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

Just add this code to your gulp file. 只需将此代码添加到您的gulp文件。 It will watch the ctrl + C and properly terminate the process. 它将监视ctrl + C并正确终止该过程。 You can put some other code in the timeout also if desired. 如果需要,您还可以在超时中放入其他代码。

以防万一它对其他人有帮助,我删除了node_modules并做了npm install ,这为我解决了这个问题...

The SIGINT solution did not work for me, probably because I'm also using gulp-nodemon but this worked: SIGINT解决方案对我不起作用,可能是因为我也在使用gulp-nodemon,但这可行:

    var monitor = $.nodemon(...)
    // Make sure we can exit on Ctrl+C
    process.once('SIGINT', function() {
    monitor.once('exit', function() {
          console.log('Closing gulp');
          process.exit();
        });
    });
    monitor.once('quit', function() {
        console.log('Closing gulp');
        process.exit();
    });

Got it from here . 这里得到它。

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

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