• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    TypeScript
  • Created about 9 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Make a beautiful class-based gulpfiles with TypeScript and Gulpclass.

Make a beautiful class-based gulpfiles with Typescript and Gulpclass

Allows to create a gulp files in classes, each method of which can be a gulp task.

Installation

  1. Install module:

    npm install gulpclass --save-dev

  2. Use typings to install all required definition dependencies.

    typings install

Usage

  1. Create a gulpfile.ts and describe your tasks

    import {Gulpclass, Task, SequenceTask} from "gulpclass";
    import * as gulp from "gulp";
    import * as del from "del";
    
    @Gulpclass()
    export class Gulpfile {
    
        @Task() // return promise to indicate your task completion
        clean() {
            // del module returns promise for you automatically
            return del("./dist/**");
        }
        
        @Task() // or use provided callback instead
        clean(cb: Function) {
            return del("./dist/**", cb);
        }
    
        @Task()
        copyFiles() { // don't forget to return stream from your task function
            return gulp.src(["./README.md"])
                .pipe(gulp.dest("./dist"));
        }
    
        @Task("copy-source-files") // you can specify custom task name if you need
        copySourceFiles() {
            return gulp.src(["./src/**.js"])
                .pipe(gulp.dest("./dist/src"));
        }
    
        @SequenceTask() // this special annotation using "run-sequence" module to run returned tasks in sequence
        build() {
            return ["copyFiles", "copy-source-files"];
        }
    
        // list of dependencies could be specified as a second argument to trigger other tasks
        // the example below is equivalent to `gulp.task("default", ["build"]);`
        @Task("default", ["build"])
        defaultTask() {
            // using "defaultTask", because "default" is a reserved keyword in ES2015
        }
    
    }
  2. How to run

    First option. Use ts-node

    Install ts-node, it supports running gulp tasks directly using gulp command

    Second option. If you don't want to use ts-node

    There is a caveat of using gulp and typescript together. The problem is that when you run your gulp commands in console, gulp cannot read your tasks from your typescript code - it can read only from gulpfile.js. But there is a simple workaround - you can create a gulpfile.js, compile and execute typescript on-the-fly.

    Create a gulpfile.js and put there this piece of code:

    eval(require("typescript").transpile(require("fs").readFileSync("./gulpfile.ts").toString()));

    This piece of code reads your gulpfile.ts contents, and asks typescript to transpile it on-the-fly and executes transpiled result as javascript.

    (you need to run npm install typescript --save-dev if you dont have typescript package installed)

    Please note that if you are NOT using outDir in typescript compile options, you may have problems if your gulpclass file is named gulpfile.ts typescript compiler will try to compile it to gulpfile.js, and will override code you added to gulpfile.js. Solution is simple - rename your gulpfile.ts. You can call it as you wish, for example you can call it gulpclass.ts.

    Alternative approaches

    Alternative approaches depend on which tsconfig configuration you use. These examples assume that you are using "outDir": "build" as a directory to where files are compiled:

    • create gulpfile.js and put there require("build/gulpfile")
    • or run gulp in cmd with extra parameters: gulp --gulpfile build/gulpfile.js --cwd .

    Benefits of this apporach is that you can use debug your gulp classes in IDEs.

FAQ

  • How to load tasks from multiple files?

Edit your gulpfile.js and change it following way:

const files = [
    "./othertask.ts",
    "./gulpfile.ts"
];
files.forEach(function(file) { eval(require("typescript").transpile(require("fs").readFileSync(file).toString())) });
  • How to use task dependencies?

Simple use second argument of the @Task decorator:

@Task("someTask", ["clean", "compile"])
someTask() {
    return doSomething();
}
  • Why my task gulp.task("default", ["clean", "compile", "build"]) is not working?

I have such a task and its not working:

@Task("default")
default() {
    return ["clean", "compile", "build"];
}

Why? Because the array you are returning is what task is doing, not a task dependencies as you wish:

@Task("default", ["clean", "compile", "build"])
default() {
}

Samples

This project itself using gulpfile.ts. Take a look on it as an example.

More Repositories

1

ngx-popover

Simple popover control for your angular2 applications using bootstrap3. Does not depend of jquery.
TypeScript
114
star
2

ngx-modal

Open modal window (dialog box) for your angular2 applications using bootstrap3.
TypeScript
107
star
3

ngx-tooltip

Simple tooltip control for your angular2 applications using bootstrap3. Does not depend of jquery.
TypeScript
85
star
4

ngx-dropdown

Simple dropdown for your angular2 applications using bootstrap3.
TypeScript
65
star
5

ngx-rating

Simple rating control for your angular2 applications using bootstrap3
TypeScript
55
star
6

ngx-select-controls

Checkbox group and radio group control for your angular2 applications using bootstrap3.
TypeScript
45
star
7

event-dispatch

Dispatching and listening for application events in Typescript
TypeScript
44
star
8

routing-controllers-express-demo

Example of express + typescript project using routing-controllers
TypeScript
41
star
9

ngx-accordion

Simple accordion control for your angular2 applications using bootstrap3.
TypeScript
39
star
10

ngx-tabs

Simple tabs control for your angular2 applications using bootstrap3
TypeScript
36
star
11

routing-controllers-koa-demo

Example of koa + typescript project using routing-controllers
TypeScript
36
star
12

ngx-paginator

Simple pagination control for your angular2 applications using bootstrap3
TypeScript
30
star
13

angular-disable-all

Angular directive that allows to disable any element (for example DIV) contents and disable clicks on the given element
JavaScript
21
star
14

ngx-progress-bar

Simple progress bar control for your angular2 applications using bootstrap3.
TypeScript
21
star
15

framework

TypeScript
16
star
16

frameworkx

TypeScript
14
star
17

ngx-loading-bar

Angular 2 loading bar on the top of the page to indicate page loading process.
TypeScript
13
star
18

typeodm

MongoDB ODM for Typescript
TypeScript
11
star
19

routing-controllers-angular2-demo

Example of angular2 + express + typescript project using routing-controllers
TypeScript
7
star
20

ngx-prevent-parent-scroll

Prevents scrolling in the parent container when child container already has a scroll.
TypeScript
4
star
21

configuration-manager

Allows to manage configuration files in your project
TypeScript
3
star
22

class-transformer-demo

Example how to use class-transformer with Angular 2
TypeScript
3
star
23

ngx-notification-center

Allows to globally dispatch notification messages.
TypeScript
3
star
24

microframework-express

Express.js integration with microframework
TypeScript
2
star
25

no-need-in-di

An example why there is no DI need in JavaScript/TypeScript.
TypeScript
2
star
26

gulp-parameters-generator

Asks user to input paramaters from your configuration file
JavaScript
2
star
27

angular-select-options

A special angular directive that works like angular's ng-options but can be used in along with any element and has several specific abilities
JavaScript
2
star
28

angular-auto-grow-input

This directive allows your inputs to grow as soon as user types. The input's width always fit the text user typed in the input.
JavaScript
2
star
29

microframework-winston

Winston logging tool to use it with microframework
TypeScript
1
star
30

angular-open-dropdown

A directive that allows to open dropdown when user clicks on button or anything else
JavaScript
1
star
31

angular-select-autocomplete

Angular autocomplete
JavaScript
1
star
32

rabbit.ts

Wrapper over rabbit.js library that provides some extra convinent functionality to work with rabbit.js in Typescript
TypeScript
1
star
33

select-items

This component allows to use ng-model with multiple checkboxes and radio boxes in an angular-way.
JavaScript
1
star
34

angular-select-dropdown

A special angular directive that allows to select items from the dropdown
JavaScript
1
star
35

microframework-redis

Redis integration with microframework
TypeScript
1
star
36

angular-select-items

A special angular directive used with input to make input to grow as soon as user types text.
JavaScript
1
star
37

ng2-days-slider

Simple days calendar slider control for your angular2 applications using bootstrap3.
TypeScript
1
star
38

angular-tags-input

Angular directive that allows to type into input small tags and store their values in ng-model.
JavaScript
1
star
39

angular-select-tags

A directive that allows user to add a tokens in the input box and select items from the dropdown
JavaScript
1
star