2015-10-05 29 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