forked from lcdporto/events-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·294 lines (273 loc) · 8.92 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
var gulp = require('gulp');
var config = require('./gulp.config.js');
// https://www.npmjs.com/package/gulp-webserver
var webserver = require('gulp-webserver');
// https://www.npmjs.com/package/gulp-inject
var inject = require('gulp-inject');
// https://www.npmjs.com/package/gulp-util
var util = require('gulp-util');
// https://github.com/spalger/gulp-jshint
var jshint = require('gulp-jshint');
// https://github.com/jscs-dev/gulp-jscs
var jscs = require('gulp-jscs');
// https://github.com/dominictarr/map-stream
var map = require('map-stream');
// https://github.com/robrich/gulp-if
var gulpif = require('gulp-if');
// https://www.npmjs.com/package/gulp-ng-annotate
var annotate = require('gulp-ng-annotate');
// https://github.com/terinjokes/gulp-uglify
var uglify = require('gulp-uglify');
// https://www.npmjs.com/package/gulp-useref
var useref = require('gulp-useref');
// https://www.npmjs.com/package/gulp-minify-css
var minify = require('gulp-minify-css');
// https://github.com/sindresorhus/gulp-imagemin
var imagemin = require('gulp-imagemin');
// https://www.npmjs.com/package/gulp-task-listing
var listing = require('gulp-task-listing');
// https://www.npmjs.com/package/gulp-angular-templatecache
var templatecache = require('gulp-angular-templatecache');
// https://github.com/jonschlinkert/gulp-htmlmin
var htmlmin = require('gulp-htmlmin');
// https://www.npmjs.com/package/run-sequence
var runsequence = require('run-sequence');
/**
* Prepares everything to serve the development build
*/
gulp.task('serve', ['inject', 'webserver', 'watchers'], function() {
util.log(util.colors.bgBlue('Serving Development'));
});
/**
* Starts a webserver to launch the development build
*/
gulp.task('webserver', ['dev-settings'], function() {
gulp.src(config.root)
.pipe(webserver({
// https://github.com/schickling/gulp-webserver
// http://stephenradford.me/gulp-angularjs-and-html5mode/
fallback: config.index,
livereload: true,
open: true
}));
});
/**
* Launch a webserver to serve the production build
*/
gulp.task('serve-production', ['build'], function() {
util.log(util.colors.bgBlue('Serving Production Build'));
gulp.src(config.build)
.pipe(webserver({
// https://github.com/schickling/gulp-webserver
// http://stephenradford.me/gulp-angularjs-and-html5mode/
fallback: config.index,
livereload: true,
open: true
}));
});
/**
* Watcher setup, watch for changes in js, css and html files
* ignore changes to the templates.js file since it is auto-generated
*
* @bug when adding folders gulp might crash, at least in linux
* the problem seems to be with gaze, and has no easy solution
*/
gulp.task('watchers', function() {
util.log(util.colors.bgBlue('Setting Up Watchers'));
gulp.watch([
config.jsfiles,
config.cssfiles,
config.htmlfiles,
'!/app/core/templates.js'
], ['html-inject']);
gulp.watch([config.checkfiles], ['check']);
// we could set up here a watcher for the bower files, but that means the task
// will run twice on install, and none on uninstall since there appears
// to be some issues with the watch task and also with the gaze dependency
// so we decided to go back and use bower hooks, unfortunately bower does not
// have a postuninstall hook, so we are f... well, i've discover that it supports preuninstall
// so we are going that route, see the .bowerrc file
});
/*
* Injects css and js files in index.html file
*/
gulp.task('inject', function() {
return runsequence('bower-html-inject', 'html-inject');
});
/**
* Read the index.html file and inject css and js files using gulp-inject
*/
gulp.task('html-inject', ['templatecache'], function() {
// gulp util uses chalk, see reference
// https://github.com/chalk/chalk
util.log(util.colors.bgBlue('Custom Code HTML Inject'));
return gulp
.src(config.index)
// gulp src options:
// https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options
// we do not need to read the file content, all we need here are the paths
// gulp inject options: https://github.com/klei/gulp-inject#optionsrelative
.pipe(inject(gulp.src(config.jsfiles.concat(config.cssfiles), {
read: false
}), {
relative: false,
addRootSlash: false
}))
.pipe(gulp.dest(config.root));
});
/**
* Uses Wiredep to read dependencies from bower.json file
* and inject them in the index.html file
* this task is also called by the hooks defined in .bowerrc
*/
gulp.task('bower-html-inject', function() {
util.log(util.colors.bgBlue('Bower Dependencies HTML Inject'));
var wiredep = require('wiredep').stream;
var options = {
bowerJson: require(config.bowerjson),
directory: config.libs
};
return gulp
.src(config.index)
.pipe(wiredep(options))
.pipe(gulp.dest(config.root));
});
/**
* Lints JavaScript code and enforces coding style. Rules are
* defined in .jshintrc and .jscsrc respectively
*/
gulp.task('check', function() {
runsequence('check-jshint', 'check-jscs');
});
/**
* Custom jshint reporterer
*
* This will beep to warn about jshint fails
*/
var JSHintBeepReporter = function() {
return map(function (file, cb) {
if (!file.jshint.success) {
util.beep();
}
cb(null, file);
});
};
/**
* Code linting using jshint
*/
gulp.task('check-jshint', function() {
util.log(util.colors.bgBlue('Code check using JSHint'));
return gulp
.src(config.checkfiles)
.pipe(jshint())
//stylish reporter https://github.com/sindresorhus/jshint-stylish
.pipe(jshint.reporter('jshint-stylish'), {verbose: true})
.pipe(new JSHintBeepReporter());
});
/**
* Custom jscs reporterer
*
* This will beep to warn about jscs fails
*/
var JSCSBeepReporter = function () {
return map(function (file) {
if (!file.jscs.success) {
util.beep();
}
});
};
/**
* Check code style using jscs
*/
gulp.task('check-jscs', function() {
util.log(util.colors.bgBlue('Code check using JSCS'));
return gulp
.src(config.checkfiles)
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(new JSCSBeepReporter());
});
/**
* Creates a build to prepare the app for production
* reads the index.html and picks the required css and js
* files using useref
*/
gulp.task('build', ['inject', 'images', 'icons', 'production-settings'], function() {
util.log(util.colors.bgBlue('Building App for Production'));
return gulp
.src(config.index)
.pipe(useref())
.pipe(gulpif('*.js', annotate()))
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minify()))
// the index.html minification step must be the last one since the useref plugin
// depends on the comments to know where to find and inject files
.pipe(gulpif('index.html', htmlmin({collapseWhitespace: true, removeComments: true})))
.pipe(gulp.dest(config.build));
});
/**
* Compresses images and copies them to the build folder
*/
gulp.task('images', function() {
util.log(util.colors.bgBlue('Compressing and Copying Images to Build Folder'));
return gulp
.src(config.images)
.pipe(imagemin())
.pipe(gulp.dest(config.build + 'images'));
});
/**
* Copies icons to build folder
*/
gulp.task('icons', function() {
var dest = config.build + 'content/icons';
util.log(util.colors.bgBlue('Copying Icons to Build Folder'));
return gulp
.src(config.icons)
.pipe(gulp.dest(dest));
});
/**
* Copies development settings file to app folder
*/
gulp.task('dev-settings',function() {
util.log(util.colors.bgBlue('Copying Development Settings File'));
return gulp
.src('dist/angular/development/settings.js')
.pipe(gulp.dest(config.core));
});
/**
* Copies production settings file to app folder
*/
gulp.task('production-settings',function() {
util.log(util.colors.bgBlue('Copying Production Settings File'));
return gulp
.src('dist/angular/production/settings.js')
.pipe(gulp.dest(config.core));
});
/**
* Creates $templateCache from the html templates
*/
gulp.task('templatecache', function() {
util.log(util.colors.bgBlue('Create an Angular Template Cache'));
return gulp
.src(config.htmlfiles)
// http://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(templatecache('templates.js', {
module: 'app.core',
standalone: false,
root: 'app/'
}))
.pipe(gulp.dest(config.core));
});
/**
* Lists all available tasks
* @todo customize list by overring filters
* By default, is is defined as the regular expression /[-_:]/
* which means that any task with a hyphen, underscore,
* or colon in it's name is assumed to be a subtask
*/
gulp.task('help', listing);
/**
* Sets up the default task, this just calls the help task
*/
gulp.task('default', ['help']);