Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
71 changes: 50 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,65 @@ A gulp task that inserts a delay before calling the next function in a chain.

## Example

The following example will watch for changes to templates and delay the livereload server refresh until after the nodemon script is expected to have restarted.
### Time based delay

Waits a specified number of milliseconds before executing the next item in the chain.

```javascript
// Gulpfile.js
var gulp = require('gulp')
, r = require('tiny-lr')
, refresh = require('gulp-livereload')
, nodemon = require('gulp-nodemon')
, wait = require('../gulp-wait')
, server = lr();

gulp.task('dev', function () {
var wait = require('gulp-wait');

gulp.src('./index.js')
.pipe(nodemon());
gulp.task('wait', function() {
gulp.src('yourfile.js')
.pipe(wait(1500))
.pipe(gulp.dest('destfile.js'));
});

server.listen(35729, function (err) {
```

if (err) return console.log(err);
### Callback based delay

gulp.watch('./app/views/**/*.html', function (e) {
gulp.src(e.path)
.pipe(wait(1500))
.pipe(refresh(server));
});
Waits for a callback to be truthy before executing the next item in the chain.

});
```javascript

})
var wait = require('gulp-wait');
var fs = require('fs');

function doesFileExist(filePath) {
try {
fs.accessSync(filePath);
return true;
} catch () {
return false;
}
}

gulp.task('wait', function() {
gulp.src('yourfile.js')
.pipe(wait(doesFileExist('../path/to/some/file.js')))
.pipe(gulp.dest('destfile.js'));
});

```

Yes, ideally there would be an event from nodemon to trigger the livereload.
### Using an options object

You can pass in an options object as well.

```javascript

const options = {
time: 100, // same as wait(100)
callback: () => true, // same as wait(() => true) - defaults to callback if both time and callback are passed
before: () => {},
after: () => {}
};

gulp.task('wait', function() {
gulp.src('yourfile.js')
.pipe(wait(options))
.pipe(gulp.dest('destfile.js'));
});

```
69 changes: 69 additions & 0 deletions index.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const map = require('map-stream');
let log = console.log;

function waitByTime(opts) {
return (file, callback) => {
if (opts.before) {
opts.before(opts);
}

setTimeout(() => {
log(`Waiting for ${opts}ms`);
callback(null, file);

if (typeof opts.after !== 'undefined') {
opts.after(opts);
}
}, opts.time)
}
}

function waitByCallback(opts) {
return (file, callback) => {
if (opts.before) {
opts.before(opts);
}

let run_count = 0;

log('Waiting for callback to return truthy');

while (!opts.callback()) {
run_count++;
}

callback(null, file);

log(`Ran callback ${run_count} times`);

if (opts.after) {
opts.after(opts);
}
}
}

module.exports = (opts) => {
if (typeof opts === 'number' || typeof opts === 'undefined') {
opts = {
time: opts || 1000
};
}

if (typeof opts === 'function') {
opts = {
callback: opts
}
};

if (!opts.verbose) {
log = () => {};
}

if (opts.callback) {
return map(waitByCallback(opts));
} else {
return map(waitByTime(opts));
}
};
88 changes: 59 additions & 29 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
{
"name": "gulp-wait",
"version": "0.0.2",
"description": "A gulp task that inserts a delay before calling the next function in a chain.",
"main": "index.js",
"name": "gulp-wait2",
"version": "0.0.5",
"description": "A gulp task that inserts a delay (time or callback-based) before calling the next function in a chain. (Forked from bpartridge/gulp-wait)",
"main": "index-compiled.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "babel index.es6 --out-file index.js --source-maps inline",
"test": "npm run build && tape 'tests/**/*.js'"
},
"repository": {
"type": "git",
"url": "https://github.com/bpartridge83/gulp-wait.git"
"url": "https://github.com/cwramsey/gulp-wait.git"
},
"keywords": [
"gulp",
"wait",
"delay",
"gulpplugin"
],
"author": "Brian Partridge",
"author": "Chris Ramsey",
"contributors": [
"Brian Partridge"
],
"license": "BSD-2-Clause",
"bugs": {
"url": "https://github.com/bpartridge/gulp-wait/issues"
"url": "https://github.com/cwramsey/gulp-wait/issues"
},
"dependencies": {
"map-stream": "0.0.4"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-preset-es2015": "^6.9.0",
"tape": "^4.5.1"
}
}
Loading