Javascript 运行任务前吞咽提示

Javascript 运行任务前吞咽提示,javascript,gulp,Javascript,Gulp,我想在运行底部的主build任务之前提示用户。我把gulp prompt作为解决方案,但在任何任务运行之前,我都不知道如何实现它。我希望用户能够在运行任何操作之前取消整个build任务。可能吗?如有任何建议,将不胜感激 不确定如何实现.pipe(prompt.confirm()) 这是整个吞咽文件 'use strict'; var gulp = require('gulp'); var config = require('../config'); var concat

我想在运行底部的主
build
任务之前提示用户。我把gulp prompt作为解决方案,但在任何任务运行之前,我都不知道如何实现它。我希望用户能够在运行任何操作之前取消整个
build
任务。可能吗?如有任何建议,将不胜感激

不确定如何实现
.pipe(prompt.confirm())

这是整个吞咽文件

'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
看一看。我相信其他人也会不同意,但当同步使用时,提示通常会更好地工作

编辑:

以下是基于gulp 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'
  )
));
有吞咽版本

我自己还没有测试过,但文档表明,您可以向用户提示确认消息,如“继续”,然后选择是否继续任务:

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

我想这正是我想要的,但是它看起来像是并联和串联在Gulp3中没有被识别出来。我还没有升级到4级。获取此错误
TypeError:gulp.parallel不是一个函数
Gotcha。您可以使用v4之前的版本,而不是
gulp.series
。我会更新我的答案。你太棒了,我的朋友工作得很好!感谢readline的同步提示,我以前没有使用过这个插件,看起来非常有用!没问题。如果答案对你有用,别忘了接受,干杯!谢谢Jake是的,这对于一个正常的任务是有效的,但是我不知道如何在我的构建任务中实现它。我读到prompt不能很好地处理streams,我的任务是按一定的顺序运行的。
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
  );
});
gulp.src('test.js')
    .pipe(prompt.confirm({
        message: 'Continue?',
        default: true
    }))
    .pipe(gulp.dest('dest'));