forked from gulpjs/gulp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add recipe for handling the deleted event - closes gulpjs#732
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Handling the Delete Event on Watch | ||
|
||
You can listen for `'change'` events to fire on the watcher returned from `gulp.watch`. | ||
|
||
Each change event has a `type` property. If `type` is `'deleted'`, you can delete the file | ||
from your destination directory, using something like: | ||
|
||
```js | ||
'use strict'; | ||
|
||
var del = require('del'); | ||
var path = require('path'); | ||
var gulp = require('gulp'); | ||
var header = require('gulp-header'); | ||
var footer = require('gulp-footer'); | ||
|
||
gulp.task('scripts', function() { | ||
return gulp.src('src/**/*.js', {base: 'src'}) | ||
.pipe(header('(function () {\r\n\t\'use strict\'\r\n')) | ||
.pipe(footer('\r\n})();')) | ||
.pipe(gulp.dest('build')); | ||
}); | ||
|
||
gulp.task('watch', function () { | ||
var watcher = gulp.watch('src/**/*.js', ['scripts']); | ||
|
||
watcher.on('change', function (event) { | ||
if (event.type === 'deleted') { | ||
// Simulating the {base: 'src'} used with gulp.src in the scripts task | ||
var filePathFromSrc = path.relative(path.resolve('src'), event.path); | ||
|
||
// Concatenating the 'build' absolute path used by gulp.dest in the scripts task | ||
var destFilePath = path.resolve('build', filePathFromSrc); | ||
|
||
del.sync(destFilePath); | ||
} | ||
}); | ||
}); | ||
``` |