-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgulpfile.js
193 lines (171 loc) · 5.67 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* gulpfile - Include gulp.js build tasks definitions
*
* @author Ciprian Popa (cyparu)
* @since 0.0.1
* @version 0.0.1
*/
"use strict";
var gulp = require("gulp"), // http://gulpjs.com
$ = require("gulp-load-plugins")(), // lazy loading plugins: https://github.com/jackfranklin/gulp-load-plugins
sequence = require("run-sequence"), // specify run order https://github.com/OverZealous/run-sequence
gutil = require("gulp-util"), // https://github.com/gulpjs/gulp
log = gutil.log,
colors = gutil.colors,
// watching flag
watching = false,
// project config
/* jshint -W015 */
project = {
js: ["*.js", "src/**/*.js", "test/**/*.js"], // include JavaScript files
specs: ["test/**/*.spec.js"], // include JavaScript files
md: ["*.md", "doc/*.md"], // include all markdown files
json: ["*.json", ".*rc"], // include all json and rc files
tmp: "target", // target directory
dist: "dist" // dist directory
};
// --------------------
// helper functions
// --------------------
/**
* Pipe a cache with the specified name just if the current task was triggered by the watcher
*/
function cache(name) {
return $.if(watching, $.cached(name));
}
/**
* Add error notification for the specified stream using the type as a title prefix
*/
function notifyError(stream, type) {
stream.on("error", function(err) {
var title = type + " error",
message = err.message;
log(colors.red(title) + ": " + colors.yellow(message));
$.notify({
title: title,
message: message
});
});
return stream;
}
/**
* Wrap src to automatically set the base to current working directory
*/
function src(glob, opt) {
opt = opt || {};
opt.base = opt.base || process.cwd();
return gulp.src(glob, opt);
}
/**
* Wrap gulp.task for more expressive usage
*/
function task(name, deps, fn) {
return gulp.task(name, deps, fn);
}
/**
* Wrap gulp.task usage for sequencial execution for more expressive usage
*/
function seqTask(name) {
var tasks = Array.prototype.slice.call(arguments, 1);
task(name, function(cb) { // run the tasks in the specified order
tasks.push(cb);
sequence.apply(undefined, tasks);
});
}
/**
* Wrap gulp.watch with file change reporting and watching flag setting for triggered tasks
*/
function watch(glob, opt, fn) {
var watcher = gulp.watch(glob, opt, fn);
// show change
watcher.on("change", function(event) {
// set watching flag
watching = true;
// log the change
log("The file " + colors.yellow(event.path) + " was " + colors.green(event.type));
});
return watcher;
}
// --------------------
// tasks
// --------------------
// define "clean" task for cleaning all the generated and temporary files
task("clean", function() {
log("Remove temporary files");
return src(project.tmp, { read: false }).pipe($.clean());
});
// define "jslint" task for JavaScript linting
task("jshint", function() {
log("Linting JavaScript files");
return src(project.js)
.pipe(cache("js")) // enable caching just when watching
.pipe($.jshint())
.pipe($.jshint.reporter("jshint-stylish"))
.pipe($.if(!watching, notifyError($.jshint.reporter("fail"), "JSHint"))); // fail if not running under watcher
});
// define "jscs" task for JavaScript code style constraints
task("jscs", function() {
log("Linting JavaScript files");
return src(project.js)
.pipe(cache("jscs")) // enable caching just when watching
.pipe(notifyError($.jscs(), "JSCS"));
});
// define "jsonlint" task for JSON linting
task("jsonlint", function() {
log("Linting JSON files");
return src(project.json)
.pipe(cache("json")) // enable caching just when watching
.pipe($.jsonlint())
.pipe($.jsonlint.reporter());
});
// define "md" task for HTML generation from .md files
task("md", function() {
log("Generating HTML documentation from Markdown files");
return src(project.md, { base: process.cwd() })
.pipe(cache("md")) // enable caching just when watching
.pipe($.markdown())
.pipe(gulp.dest(project.tmp + "/doc/html"));
});
// define "mdpdf" task for PDF generation from .md files
task("mdpdf", function() {
log("Generating PDF documentation from Markdown files");
return src(project.md, { base: process.cwd() })
.pipe(cache("md-pdf")) // enable caching just when watching
.pipe($.markdownPdf())
.pipe(gulp.dest(project.tmp + "/doc/pdf"));
});
// define "docco" task for annotated source code documentation
task("docco", function() {
log("Generating Docco annotated documentation for JavaScript files");
return src(project.js, { base: process.cwd() })
.pipe(cache("docco")) // enable caching just when watching
.pipe($.docco({ layout: "classic" }))
.pipe(gulp.dest(project.tmp + "/doc/js/source"));
});
// define "jsdoc" task for API source code documentation
task("jsdoc", function() {
log("Generating JSDoc3 API documentation for JavaScript files");
return src(project.js, { base: process.cwd() })
.pipe(cache("jdoc")) // enable caching just when watching
.pipe($.jsdoc(project.tmp + "/doc/js/api"));
});
// define "test" for Jasmine testing
task("test", function() {
log("Execute Jasmine tests/specs");
return src(project.specs)
.pipe(cache("specs")) // enable caching just when watching
.pipe(notifyError($.jasmine({ verbose: true }),"Jasmine"));
});
// define "watch" task to start watch/interactive development mode
task("watch", function() {
log("Watching project files...");
watch(project.js, ["jshint", "jscs"]);
watch(project.json, ["jsonlint"]);
watch(project.specs, ["test"]);
});
// define top tasks & aliases
task("lint", ["jsonlint", "jshint", "jscs"]);
task("doc", ["jsdoc", "docco", "md"]); // TODO: "mdpdf" removed, pdf generation will block
seqTask("build", "clean", "lint", "test", "doc"); // run sequentially
// define default task
task("default", ["build"]);