2016-10-19 69 views
0

編號喜歡在底部運行主要的build任務之前提示用戶。我正在尋找gulp-prompt作爲解決方案,但無法確定如何在任何運行任務之前實施它。我希望用戶能夠在任何運行之前取消整個build任務。那可能嗎?任何建議將不勝感激。運行任務之前吞噬提示

不知道如何實現.pipe(prompt.confirm())

gulp.src('test.js') 
    .pipe(prompt.confirm()) 
    .pipe(gulp.dest('dest')); 

這裏是整個吞掉文件

'use strict'; 

var gulp  = require('gulp'); 
var config = require('../config'); 
var concat = require('gulp-concat'); 
var unzip  = require('gulp-unzip'); 
var minimatch = require('minimatch'); 
var prompt = require('gulp-prompt'); 
var notify = require('gulp-notify'); 
var gutil  = require('gulp-util'); 

/** 
* Build Initial Project File Structure 
* Docs: https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support 
* Order of Build: 
*  1. 'buildSass': Builds the ./sass directory in root 
*  2. 'buildFonts': Extracts the fonts from .zip and places them in ./fonts directory 
*  3. 'buildFontsCss': Extracts the fonts .css from .zip, concats all.css and places them in ./sass/typography/_fonts.scss directory 
*  4. 'buildJS': Builds the ./js directory in root 
*  5. 'moveCustomJSFiles': Places all js files (from wp _underscore theme) in proper directories 
*  6. run 'gulp build' in terminal to run all 5 tasks in the proper order and create the initial setup for your project 
*/ 

gulp.task('buildSass', function() { 
    var stream = gulp.src(config.build.sass.src) 
     .pipe(gulp.dest(config.build.sass.dest)) 
    return stream 
     .on('end', function(){ gutil.log(gutil.colors.bgGreen('buildSass Compiled Successfully!')); }); 
}); 

gulp.task('buildFonts', ['buildSass'], function() { 
    var stream = gulp.src(config.build.fonts.src) 
     .pipe(unzip({ 
      filter: function(entry) { 
       return minimatch(entry.path, config.build.fonts.include) 
      } 
     })) 
     .pipe(gulp.dest(config.build.fonts.dest)) 
    return stream 
     .on('end', function(){ gutil.log(gutil.colors.bgGreen('buildFonts Compiled Successfully!')); }); 
}); 

gulp.task('buildFontsCss', ['buildFonts'], function() { 
    var stream = gulp.src(config.build.fonts.css.src) 
     .pipe(unzip({ 
      filter: function(entry) { 
       return minimatch(entry.path, config.build.fonts.css.include) 
      } 
     })) 
     .pipe(concat(config.file.name.fontsSass)) 
     .pipe(gulp.dest(config.build.fonts.css.dest)) 
    return stream 
     .on('end', function(){ gutil.log(gutil.colors.bgGreen('buildFontsCss Compiled Successfully!')); }); 
}); 

gulp.task('buildJS', ['buildFontsCss'], function() { 
    var stream = gulp.src(config.build.js.src) 
     .pipe(gulp.dest(config.build.js.dest)) 
    return stream 
     .on('end', function(){ gutil.log(gutil.colors.bgGreen('buildJS Compiled Successfully!')); }); 
}); 

gulp.task('moveCustomJSFiles', ['buildJS'], function() { 
    var stream = gulp.src(config.build.copyJS.src) 
     .pipe(gulp.dest(config.build.copyJS.dest)) 
    return stream 
     .on('end', function(){ gutil.log(gutil.colors.bgGreen('moveCustomJSFiles Compiled Successfully!')); }) 
     .pipe(notify({ message: 'Project Build was successful! ✅', onLast: true })); 
}); 

gulp.task('build', ['buildSass', 'buildFonts', 'buildFontsCss', 'buildJS', 'moveCustomJSFiles']); 

// TODO: 
// put confirm before run 

回答

1

看看readline-sync。我相信其他人會不同意,但是提示是其中一種在同步使用IMO時常常更好的方法。

編輯:

這裏是你將如何基於與一飲而盡V4代碼做到這一點:

const readlineSync = require('readline-sync'); 

gulp.task('build', gulp.series(
    function(done) { 
    if (readlineSync.keyInYN('Do you want to build?')) { 
     return done(); 
    } 
    console.log('Ok, not building.'); 
    process.exit(1); 
    }, 
    gulp.parallel(
    'buildSass', 
    'buildFonts', 
    'buildFontsCss', 
    'buildJS', 
    'moveCustomJSFiles' 
) 
)); 

隨着一飲而盡版本< 4,你可以使用run-sequence模塊,而不是gulp.series的:

const readlineSync = require('readline-sync'); 
const runSequence = require('run-sequence'); 
gulp.task('build-prompt', function(done) { 
    if (readlineSync.keyInYN('Do you want to build?')) { 
    return done(); 
    } 
    console.log('Ok, not building.'); 
    process.exit(1); 
}); 

gulp.task('build', function(callback) { 
    runSequence(
    'build-prompt', 
    [ 
     'buildSass', 
     'buildFonts', 
     'buildFontsCss', 
     'buildJS', 
     'moveCustomJSFiles' 
    ], 
    callback 
); 
}); 
+0

,我認爲這正是進出口尋找但它看起來像並聯和串聯不在咕嘟咕嘟3.承認我還沒有升級到4呢。得到這個錯誤'TypeError:gulp.parallel不是一個函數' – MoxDev

+0

陷阱。在v4之前,你可以使用[run-sequence](https://www.npmjs.com/package/run-sequence)而不是'gulp.series'。我會更新我的答案。 – AndyPerlitch

+0

你真棒,我的朋友精美地工作!感謝readline-sync同步提醒,我之前沒有使用過這個插件,似乎非常有用! – MoxDev

0

https://www.npmjs.com/package/gulp-prompt

我自己沒有測試過,但文檔表明您可以提示用戶輸入確認信息,如「繼續?」。然後有選擇地繼續任務或不:

gulp.src('test.js') 
    .pipe(prompt.confirm({ 
     message: 'Continue?', 
     default: true 
    })) 
    .pipe(gulp.dest('dest')); 
+0

謝謝傑克是的,這是一個正常的任務,但我無法弄清楚如何在我的構建任務上實現它。我讀過提示並不能很好地處理流,並且我按照特定順序運行任務。 – MoxDev