-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
280 lines (232 loc) · 6.79 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
var gulp = require('gulp');
var del = require('del');
var args = require('yargs').argv;
var browserSync = require('browser-sync');
var es = require('event-stream');
var series = require('stream-series');
var fs = require('fs');
var config = require('./gulp.config')();
var styleConfig = {};
// load gulp-* plugins. Cleaner Code is better - Thanks uncle Bob
var $ = require('gulp-load-plugins')({lazy:true});
// Default task
gulp.task('help', $.taskListing);
gulp.task('default', ['help'], function() {
log($.util.colors.green('Available build targets: --output dev [default], --output prod, --output asis'));
});
// Main dev task without actually hosting the site
gulp.task('build', ['setBuildTarget','cleanAll','loadStyleConfig','inject'], function(doneCallBack) {
if (args.output === 'asis') {
addASISXslt(doneCallBack);
return;
}
doneCallBack();
});
gulp.task('setBuildTarget', function(doneCallBack) {
if (args.output) {
var output = args.output;
if (output === 'prod') {
config.changeToProd();
}
else if (output === 'asis') {
config.changeToASIS();
}
else {
config.changeToDev();
}
}
else {
config.changeToDev();
}
doneCallBack();
});
// Serve up development and testing
gulp.task('serve', ['build'], function() {
log('Starting browserSync');
config.serving = true;
var sync = browserSync({
server: config.dest,
index: 'index.htm'
});
gulp.watch(config.sassToCompile, ['sass']);
gulp.watch([].concat(
config.jsToVet,config.views,
config.src + '*.htm',
config.styleConfigFile),
['reloadBrowser']);
});
gulp.task('vet', function(doneCallBack) {
log('Analysing Javascript for code issues and code style');
return gulp
.src(config.jsToVet)
.pipe($.if(args.verbose,$.print()))
.pipe($.jshint())
.pipe($.jscs())
.pipe($.jscsStylish.combineWithHintResults())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.jshint.reporter('fail'));
});
gulp.task('inject', ['sass', 'processJs', 'moveToBuild', 'createViewsInCache'], function() {
log('Injecting the css, processData and Javascript');
return injectProcessedSources();
});
gulp.task('createViewsInCache', function() {
//console.log($);
return gulp
.src(config.views)
.pipe($.angularTemplatecache({
module:'viewCache',
root: 'views',
standalone: true
}))
.pipe(gulp.dest(config.dest));
});
gulp.task('sass', ['loadStyleConfig','sassConfig'], function() {
log('Processing SASS');
return gulp
.src(config.sassToCompile)
.pipe($.jsonSass())
.pipe($.sass({
includePaths: config.bootstrapPath
}).on('error',$.sass.logError))
.pipe($.concat('mainStyles.css'))
.pipe(
$.if(doMinify(), $.minifyCss({
compatibility: 'ie8'
})))
.pipe(gulp.dest(config.temp + 'css'))
.pipe($.if(config.serving, gulp.dest(config.dest)))
.pipe($.if(config.serving, browserSync.stream({match: '**/*.css'})));
});
gulp.task('processJs', ['resourcJs','vendorJs', 'appJs'],function (doneCallBack) {
doneCallBack();
});
gulp.task('resourcJs', function() {
log('Processed resources scripts');
return gulp
.src(config.resourcesJs)
.pipe($.print())
.pipe($.concat('resources.js'))
.pipe(gulp.dest(config.temp + 'js'));
});
gulp.task('vendorJs', function() {
log('Processed vendor scripts');
return gulp
.src(config.vendorJs)
.pipe($.print())
.pipe($.concat('lib.js'))
.pipe(gulp.dest(config.temp + 'js'));
});
gulp.task('appJs', function() {
var sassConfig = 'var sassConfig = ' + JSON.stringify(styleConfig) + ';';
log('Output is prod: ' + args.output === 'prod');
return gulp
.src(config.angularJs)
.pipe($.file('sassConfig.js', sassConfig))
.pipe($.concat('main.js'))
.pipe($.if(doMinify(), $.uglify({mangle:false})))
.pipe(gulp.dest(config.temp + 'js'));
});
gulp.task('cleanAll', function(doneCallBack) {
try {
del.sync([config.dest + '**',config.temp + '**']);
}
catch (e) {
// Failed to clean, happens if files are open
log($.util.colors.red('CleanAll Error: ' + e.message));
}
doneCallBack();
});
gulp.task('moveImages', function() {
log('Moving and compressing the images to the destination');
return gulp
.src(config.images)
.pipe($.imagemin({optimizationLevel: 4}))
.pipe(gulp.dest(config.dest));
});
gulp.task('moveAssets', function() {
log('Moving Assets');
return gulp
.src(config.assets)
.pipe(gulp.dest(config.dest));
});
gulp.task('sassConfig', function() {
var sassConfig = JSON.stringify(styleConfig);
return gulp
.src(config.src + '*.js')
.pipe($.file('_sass_config.json', sassConfig))
.pipe($.jsonSass())
.pipe(gulp.dest(config.temp + 'config/'));
});
gulp.task('loadStyleConfig', function(doneCallBack) {
var jsonString = fs.readFileSync(config.styleConfigFile, 'utf8');
styleConfig = JSON.parse(jsonString);
doneCallBack();
});
gulp.task('moveToBuild', ['sass','processJs','moveCss',
'moveProcessData', 'moveJs', 'moveImages', 'moveFonts','moveAssets'], function() {
return moveToDestination(config.tempSources,'Moving from temp to destination');
});
gulp.task('moveFonts', function() {
return moveToDestination(config.fonts, 'Moving the fonts to the destination');
});
gulp.task('moveCss', function() {
return moveToDestination(config.temp + 'css/**/*.css', 'Moving CSS');
});
gulp.task('moveProcessData', function() {
return moveToDestination(config.src + 'data/*.js', 'Moving ProcessedData');
});
gulp.task('moveJs', function() {
return moveToDestination(config.temp + 'js/*.js', 'Moving Javascript');
});
gulp.task('reloadBrowser', ['build'],function () {
log('Reloading the browser ... ');
return browserSync.reload();
});
////////////////////////////////////////////////////////////////////
function injectProcessedSources() {
var sources = gulp.src(config.injectSources);
return gulp
.src(config.src + '*.htm')
.pipe($.inject(sources, {ignorePath:'builds/develop/',addRootSlash: false}))
.pipe(gulp.dest(config.dest));
}
function moveToDestination(sources, message) {
log(message);
return gulp
.src(sources)
.pipe(gulp.dest(config.dest));
}
function addASISXslt(doneCallBack)
{
var indexHtml = fs.readFileSync(config.dest + 'index.htm', 'utf8');
var xsltTop = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' +
'\n<xsl:template match="recipients/recipient">' +
'\n<xsl:text disable-output-escaping="yes">' +
'\n<![CDATA[\n';
var xsltBottom = '\n]]></xsl:text></xsl:template></xsl:stylesheet>';
fs.writeFileSync(config.dest + 'index.xslt', xsltTop + indexHtml + xsltBottom);
doneCallBack();
}
function doMinify()
{
if (args.output === 'prod' || args.output === 'asis') {
return true;
}
return false;
}
function log(message)
{
'use strict';
var item;
if (typeof (message) === 'object') {
for (item in message) {
if (message.hasOwnProperty(item)) {
$.util.log($.util.colors.blue(message[item]));
}
}
}
else {
$.util.log($.util.colors.blue(message));
}
}