Last active
October 31, 2015 12:48
-
-
Save andrewconnell/90ef65fab495df47d429 to your computer and use it in GitHub Desktop.
Dynamically loading gulp tasks with TypeScript
This file contains hidden or 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
| 'use strict'; | |
| import * as fs from 'fs'; | |
| let gulp: any = require('gulp-help')(require('gulp')); | |
| let gulpTaskPath: string = './build/gulp/tasks'; | |
| // refer to the following file for referenced interfaces | |
| // https://github.com/andrewconnell/blog-gulpts-dynaload/blob/master/build/gulp/gulp.d.ts | |
| // load all gulp tasks (located in ./build/gulp/tasks) | |
| fs.readdirSync(gulpTaskPath) | |
| // filter out anything that's not a *.js file | |
| .filter((filename: any) => { | |
| return filename.match(/\.js$/i); | |
| }) | |
| // convert to an array of objects | |
| .map((filename: string) => { | |
| // each object contains the file name & loads | |
| // an instance of the gulp task object | |
| return <IGulpTaskFile>{ | |
| name: filename.substr(0, filename.length - 3), | |
| GulpTask: require(gulpTaskPath + '/' + filename).GulpTask | |
| }; | |
| }) | |
| // for each task... | |
| .forEach((file: IGulpTaskFile) => { | |
| // use gulp-help to register the task | |
| // the 4th parameter is the function that contains | |
| // the actual gulp task logic | |
| gulp.task( | |
| file.name, | |
| file.GulpTask.description, | |
| file.GulpTask.dependencies, | |
| file.GulpTask, | |
| { | |
| aliases: file.GulpTask.aliases, | |
| options: file.GulpTask.options | |
| } | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment