Giter VIP home page Giter VIP logo

anseki / gulp-confirm Goto Github PK

View Code? Open in Web Editor NEW
9.0 4.0 1.0 27 KB

Abort or continue the flow of tasks according to an answer (with or without Enter key) to the specified question. The flow of tasks is paused, until the user responds and the next behavior is found by specified options.

License: MIT License

JavaScript 100.00%
gulpplugin confirm abort continue question pause wait prompt flow javascript

gulp-confirm's Introduction

gulp-confirm

npm GitHub issues David license

Abort or continue the flow of tasks according to an answer (with or without Enter key) to the specified question. The flow of tasks is paused, until the user responds and the next behavior is found by specified options.

sample

sample

Getting Started

npm install gulp-confirm --save-dev

Usage

gulpfile.js

var gulp = require('gulp'),
  confirm = require('gulp-confirm');

gulp.task('default', function() {
  return gulp.src('./develop/*.html')
    .pipe(confirm(options))
    // Other tasks...
    .pipe(gulp.dest('./public_html/'));
});

Options

question

Type: string, function or undefined

Display a specified string or a returned string by a specified function to the user.
If the function returns a falsy, this task is finished immediately. And the flow of tasks is continued.

If this option is not specified when '_key' is specified to the input option and the proceed option is not specified (i.e. you want to let the flow of tasks just pause), a message 'Continue... (Hit any key)' is displayed.

For example:

gulpfile.js

gulp.task('default', function() {
  return gulp.src('./develop/*.html')
    .pipe(confirm({
      // Static text.
      question: 'This processing requires about 10 minutes. Continue?',
      input: '_key:y'
    }))
    // Other tasks...
    .pipe(gulp.dest('./public_html/'));
});
var pattern = './src/*.html';
gulp.task('default', function() {
  return gulp.src(pattern)
    .pipe(confirm({
      question: function() {
        var files = require('glob').sync(pattern);
        return !files.length ? false : // No file. And do nothing.
          files.length + ' files are copied at next task. Are you sure?';
      },
      input: '_key:y'
    }))
    .pipe(gulp.dest('./public_html/'));
});

input

Type: string or undefined

Accept the user's input that is specified type.

  • If it is not specified (i.e. undefined), accept a text (with an Enter key), and let the proceed option decide whether to continue the flow of tasks.
  • If a comma-separated string like 'text1,text2' is specified, accept a text (with an Enter key), and continue the flow of tasks when the input text was found in that comma-separated string, otherwise abort it.
  • If '_key' is specified, get a pressed key (without an Enter key), and let the proceed option decide whether to continue the flow of tasks.
  • If a string '_key:charlist' is specified, get a pressed key (without an Enter key), and continue the flow of tasks when the pressed key was found in that charlist, otherwise abort it. The charlist is a string that includes characters as the keys. For example, if '_key:abc' is specified, continue the flow of tasks when the A, B or C key is pressed.

The string comparisons are case-insensitive (i.e. a equals A).

proceed

Type: function, boolean or undefined

Decide whether to continue the flow of tasks when the input option is not specified (i.e. undefined) or '_key' is specified to it.

If a function is specified, call the function, and continue the flow of tasks when that returns a truthy, otherwise abort it.
The function is passed an answer argument. It is a string that was input by the user, or a single character as a pressed key by the user.
The function can determine to abort or continue by using it.

If false is specified, the flow of tasks is aborted immediately. And the remaining tasks will not run.
Otherwise the flow of tasks is continued. That is, when any text was input or any key was pressed, it is continued. It is just paused until it.

For example:

gulpfile.js

var pattern = './src/*.*';
gulp.task('default', function() {
  return gulp.src(pattern)
    .pipe(confirm({
      question: 'How many files are required?',
      proceed: function(answer) {
        var files = require('glob').sync(pattern);
        if (files.length < +answer) {
          console.log('There are only ' + files.length + ' files.');
          return false;
        } else {
          return true;
        }
      }
    }))
    // Other tasks...
    .pipe(gulp.dest('./pack/'));
});
gulp.task('default', function() {
  return gulp.src('./src/*.*')
    .pipe(confirm({
      question: 'Do you want to build? :',
      input: '_key:y' // Continue the flow if `Y` key is pressed.
    }))
    // Other tasks...
    .pipe(gulp.dest('./pack/'));
});
gulp.task('default', function() {
  return gulp.src('./develop/*.html')
    // Show something. Pause until any key is pressed.
    .pipe(confirm({options: {input: '_key'}}))
    // Other tasks...
    .pipe(gulp.dest('./public_html/'));
});

gulp-confirm's People

Contributors

anseki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

textpress

gulp-confirm's Issues

How to set the process exit code ?

If the tasks where aborted the process exit with code 0. How can i change that ?
This can be a problem if you have a external tool calling the script, it can't know if the process exit successfully or was aborted.

Does not work on MINGW

i'm using this in a gulp task, it work on the windows powershell but skips on git bash for windows. No errors displayed. I guess the confirm method is not triggered at all.

gulp-run 1.7.1
windows 10
git bash v 2.12.0.1

Doesn't work in Intellij WebStrom

This segment:

gulp.src(srcPathsWithoutVideos)
.pipe(gulp_confirm({
question: 'This processing requires about 10 minutes. Continue?',
input: '_key:y'
}))

causes the following problem:

stty: /dev/tty: Device not configured
<...>/node_modules/readline-sync/lib/read.sh: line 49: /dev/tty: Device not configured
stty: /dev/tty: Device not configured
<...>/node_modules/readline-sync/lib/readline-sync.js:250
if (res.error) { throw res.error; }
^

Error: The current environment doesn't support interactive reading from TTY.
stty: /dev/tty: Device not configured
<...>/node_modules/readline-sync/lib/read.sh: line 49: /dev/tty: Device not configured
stty: /dev/tty: Device not configured
at readlineExt (<...>/node_modules/readline-sync/lib/readline-sync.js:212:19)
at tryExt (<...>/node_modules/readline-sync/lib/readline-sync.js:249:15)
at <...>/node_modules/readline-sync/lib/readline-sync.js:352:15
at _readlineSync (<...>/node_modules/readline-sync/lib/readline-sync.js:422:5)
at Object.exports.keyIn (<...>/node_modules/readline-sync/lib/readline-sync.js:877:17)
at Confirm.transform (<...>/node_modules/gulp-confirm/index.js:75:34)
at DestroyableTransform._transform (<...>/node_modules/gulp-confirm/index.js:33:57)
at DestroyableTransform.Transform._read (<...>/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:182:10)
at DestroyableTransform.Transform._write (<...>/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:170:83)
at doWrite (<...>/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:406:64)
at writeOrBuffer (<...>/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:395:5)
at DestroyableTransform.Writable.write (<...>/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:322:11)
at write (<...>/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)
at flow (<...>/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (<...>/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:664:5)
at emitNone (events.js:86:13)

Process finished with exit code 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.