From 7a6400a7d4b18de10b1c501955ed665f7af5a989 Mon Sep 17 00:00:00 2001 From: Tanguy Krotoff Date: Sat, 4 Jul 2015 17:18:50 +0200 Subject: [PATCH] Improve gulp.watch() --- gulp/gulp-tests.ts | 39 +++++++++++++++++++++++++++++++++++++++ gulp/gulp.d.ts | 44 ++++++++++---------------------------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/gulp/gulp-tests.ts b/gulp/gulp-tests.ts index 7e07f25f706d1b..07a92fc91945e4 100644 --- a/gulp/gulp-tests.ts +++ b/gulp/gulp-tests.ts @@ -1,6 +1,8 @@ /// +/// import gulp = require("gulp"); +import browserSync = require("browser-sync"); var typescript: IGulpPlugin = null; // this would be the TypeScript compiler var jasmine: IGulpPlugin = null; // this would be the jasmine test runner @@ -27,3 +29,40 @@ gulp.task('test', ['compile', 'compile2'], function() }); gulp.task('default', ['compile', 'test']); + + +var opts = {}; + +gulp.watch('*.html', 'compile'); +gulp.watch('*.html', ['compile', 'test']); +gulp.watch('*.html', () => {}); +gulp.watch('*.html', [() => {}, (event) => {}]); +gulp.watch('*.html', ['compile', () => {}]); + +gulp.watch('*.html', opts, 'compile'); +gulp.watch('*.html', opts, ['compile', 'test']); +gulp.watch('*.html', opts, () => {}); +gulp.watch('*.html', opts, [() => {}, (event) => {}]); +gulp.watch('*.html', opts, ['compile', () => {}]); + +gulp.watch(['*.html', '*.ts'], 'compile'); +gulp.watch(['*.html', '*.ts'], ['compile', 'test']); +gulp.watch(['*.html', '*.ts'], () => {}); +gulp.watch(['*.html', '*.ts'], [() => {}, (event) => {}]); +gulp.watch(['*.html', '*.ts'], ['compile', () => {}]); + +gulp.watch(['*.html', '*.ts'], opts, 'compile'); +gulp.watch(['*.html', '*.ts'], opts, ['compile', 'test']); +gulp.watch(['*.html', '*.ts'], opts, () => {}); +gulp.watch(['*.html', '*.ts'], opts, [() => {}, (event) => {}]); +gulp.watch(['*.html', '*.ts'], opts, ['compile', () => {}]); + +var watcher = gulp.watch('*.html', event => { + console.log('Event type: ' + event.type); + console.log('Event path: ' + event.path); +}); + +gulp.task('serve', ['compile'], () => { + var browser = browserSync.create(); + gulp.watch(['*.html', '*.ts'], ['compile', browser.reload]); +}); diff --git a/gulp/gulp.d.ts b/gulp/gulp.d.ts index 7ec11def7e7d7a..d7a0dba57d0ab3 100644 --- a/gulp/gulp.d.ts +++ b/gulp/gulp.d.ts @@ -236,45 +236,21 @@ declare module gulp { dest(outFolder:(file:string)=>string, opt?:IDestOptions): NodeJS.ReadWriteStream; - /** - * Watch files and do something when a file changes. This always returns an EventEmitter that emits change events. - * - * @param glob a single glob or array of globs that indicate which files to watch for changes. - * @param tasks names of task(s) to run when a file changes, added with gulp.task() - */ - watch(glob:string, tasks:string[]): EventEmitter; - watch(glob:string[], tasks:string[]): EventEmitter; - - /** - * Watch files and do something when a file changes. This always returns an EventEmitter that emits change events. - * - * @param glob a single glob or array of globs that indicate which files to watch for changes. - * @param opt options, that are passed to the gaze library. - * @param tasks names of task(s) to run when a file changes, added with gulp.task() - */ - watch(glob:string, opt:IWatchOptions, tasks:string[]): EventEmitter; - watch(glob:string[], opt:IWatchOptions, tasks:string[]): EventEmitter; - - /** - * Watch files and do something when a file changes. This always returns an EventEmitter that emits change events. - * - * @param glob a single glob or array of globs that indicate which files to watch for changes. - * @param fn a callback or array of callbacks to be called on each change. - */ - watch(glob:string, fn:IWatchCallback): EventEmitter; - watch(glob:string[], fn:IWatchCallback): EventEmitter; - watch(glob:string, fn:IWatchCallback[]): EventEmitter; - watch(glob:string[], fn:IWatchCallback[]): EventEmitter; - /** * Watch files and do something when a file changes. This always returns an EventEmitter that emits change events. * * @param glob a single glob or array of globs that indicate which files to watch for changes. * @param opt options, that are passed to the gaze library. - * @param fn a callback or array of callbacks to be called on each change. - */ - watch(glob:string, opt:IWatchOptions, fn:IWatchCallback): EventEmitter; - watch(glob:string, opt:IWatchOptions, fn:IWatchCallback[]): EventEmitter; + * @param fn a callback or array of callbacks to be called on each change, or names of task(s) to run when a file changes, added with gulp.task(). + */ + watch(glob:string, fn:(IWatchCallback|string)): EventEmitter; + watch(glob:string, fn:(IWatchCallback|string)[]): EventEmitter; + watch(glob:string, opt:IWatchOptions, fn:(IWatchCallback|string)): EventEmitter; + watch(glob:string, opt:IWatchOptions, fn:(IWatchCallback|string)[]): EventEmitter; + watch(glob:string[], fn:(IWatchCallback|string)): EventEmitter; + watch(glob:string[], fn:(IWatchCallback|string)[]): EventEmitter; + watch(glob:string[], opt:IWatchOptions, fn:(IWatchCallback|string)): EventEmitter; + watch(glob:string[], opt:IWatchOptions, fn:(IWatchCallback|string)[]): EventEmitter; } }