This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
301 lines (261 loc) · 7.26 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
295
296
297
298
299
300
301
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-clean-css');
var rename = require('gulp-rename');
var jshint = require('gulp-jshint');
var del = require('del');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
var buffer = require('vinyl-buffer');
var gulpif = require('gulp-if');
var runSequence = require('run-sequence');
var templateCache = require('gulp-angular-templatecache');
var template = require('gulp-template');
var argv = require('yargs').argv;
var replace = require('gulp-replace-task');
// Config
var now = new Date();
var config = {
buildLabel: '' + now.getFullYear() + now.getMonth() + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds(),
buildMode: null,
version: process.env.BUILD_NUMBER || 'dev'
};
// Karma Server instance
var KarmaServer = require('karma').Server;
/*****************************TASKS********************************/
/**************************COMMON TASKS****************************/
// Config DEV
gulp.task('config:dev', function() {
config.buildMode = 'dev';
});
// Config PROD
gulp.task('config:prod', function() {
config.buildMode = 'prod';
});
// Clean CSS
gulp.task('clean:css', function() {
return del([
'./dist/css/**/*',
'./dist/fonts/**/*'
]);
});
// Clean JS
gulp.task('clean:js', function() {
return del([
'./dist/js/**/*'
]);
});
// Clean Everything
gulp.task('clean', ['clean:js', 'clean:css'], function() {
return true;
});
gulp.task('copy:res', function() {
gulp.src(['./img/**/*'], {
base: './img/'
}).pipe(gulp.dest('./dist/img/'));
gulp.src(['./favicon.ico'], {
base: './'
}).pipe(gulp.dest('./dist/'));
// We can copy the font files into the dist folder, otherwise we could use the CDN
// and override the variable $fa-font-path: "//netdna.bootstrapcdn.com/font-awesome/4.5.0/fonts" !default;
// Copy font files
return gulp.src(['./node_modules/font-awesome/fonts/**/*'], {
base: './node_modules/font-awesome/fonts/'
}).pipe(gulp.dest('./dist/fonts/'));
});
gulp.task('compile:css', function() {
// myAccount.scss should have: @import "../node_modules/font-awesome/scss/font-awesome.scss";
// compiling the style
return gulp.src('./style/main.scss')
// The onerror handler prevents Gulp from crashing when you make a mistake in your SASS
.pipe(sass({
onError: function(e) {
console.log(e);
gutil.log(e);
}
}))
// Optionally add autoprefixer
.pipe(autoprefixer('last 2 versions', '> 1%', 'ie 8'))
.pipe(rename('style.css'))
.pipe(gulpif(config.buildMode === 'prod', minifyCSS()))
.pipe(gulp.dest('./dist/style/'));
});
gulp.task('build:style', function() {
runSequence(['copy:res', 'compile:css']);
});
// create module for templates
gulp.task('create:templates', function() {
gulp.src('./js/src/**/*.html')
.pipe(templateCache({
transformUrl: function(url) {
return './js/' + url;
}
}))
.pipe(gulp.dest('./js/src/templates'));
});
// JSHint task
gulp.task('lint:js', function() {
return gulp.src(['./js/src/**/*.js', '!./js/src/templates/**/*.js'])
.pipe(jshint())
// You can look into pretty reporters as well, but that's another story
.pipe(jshint.reporter('default'));
});
gulp.task('build:indexfile', function() {
return gulp.src('./index.html')
// And put it in the dist folder
.pipe(template({
version: config.version,
buildLabel: config.buildLabel
}))
// .pipe(preprocess({
// context: {
// ENV: 'prod',
// DEBUG: true
// }
// }))
.pipe(gulp.dest('./dist/'));
});
gulp.task('build:del:tempfiles', function() {
// to clean temp files
});
// build for development
gulp.task('build:dev', function() {
runSequence(
['clean', 'config:dev'],
['build:style', 'lint:js'],
['build:angular', 'build:indexfile'],
['build:del:tempfiles']
);
});
// build for production
gulp.task('build:prod', function() {
runSequence(
['clean', 'config:prod'],
['build:style', 'lint:js'],
['build:angular', 'build:indexfile'],
['build:del:tempfiles']
);
});
// watchers
gulp.task('watch', ['build:dev'], function() {
// JS watcher
gulp.watch([
'./index.html',
'./js/src/**/*.html',
'./js/src/**/*.js'
], [
'lint:js',
'build:angular:connect'
]);
// SCSS watcher
gulp.watch([
'./style/**/*.scss'
], ['compile:css']);
});
/*************************APP*************************/
gulp.task('build:js', ['create:templates'], function() {
console.log('AngJS build version: ' + config.buildMode);
// set up the browserify instance on a task basis
var b = browserify({
entries: './js/src/main.js',
debug: true
});
return b.bundle()
.pipe(source('angularjs.all.js'))
.pipe(buffer())
// .pipe(cachebust.resources())
.pipe(sourcemaps.init({loadMaps: true}))
// Add transformation tasks to the pipeline here.
// only if in production UGLIFY
.pipe(gulpif(config.buildMode === 'prod', uglify()))
.on('error', function(error) {
console.log(error);
gutil.log(error);
})
.pipe(sourcemaps.write('./maps/'))
.pipe(gulp.dest('./dist/js/'));
});
gulp.task('build:angular', function() {
runSequence(
['build:js']
);
});
// Test task, single run and exit
gulp.task('test', function(done) {
del(['./coverage']);
new KarmaServer({
configFile: __dirname + '/karma.conf.unit.js',
singleRun: true
}, done).start();
});
// Test task, continuous testing
gulp.task('tdd', function(done) {
del(['./coverage']);
new KarmaServer({
configFile: __dirname + '/karma.conf.unit.js'
}, done).start();
});
// Run unit tests in Chrome and enable debug mode
gulp.task('test:debug', function(done) {
del(['./coverage']);
new KarmaServer({
configFile: __dirname + '/karma.conf.unit.debug.js'
}, done).start();
});
// Generators
// Component Generation -- Template in Base -- Will abstract to gulp plugin
// to use: gulp g:component --name <insert name here>
gulp.task('g:component', function(){
if (!argv.name) {
throw new Error('Command usage is: use: "gulp g:component --name <insert name here>"');
}
// Gets structure from base
// Copy base template file
gulp.src('./base/**/*.html')
.pipe(rename(argv.name+'.template.html'))
.pipe(gulp.dest('./js/src/' + argv.name + '/'));
// Copy component and rename
gulp.src('./base/**/base.component.js')
.pipe(replace({
patterns: [
{
match: /base/g,
replacement: function () {
return argv.name;
}
},
{
match: /Base/g,
replacement: function () {
return argv.name.charAt(0).toUpperCase() + argv.name.slice(1);
}
}
]
}))
.pipe(rename(argv.name+'.component.js'))
.pipe(gulp.dest('./js/src/'+argv.name+'/'));
// Copy module and rename
gulp.src('./base/**/module.js')
.pipe(replace({
patterns: [
{
match: /base/g,
replacement: function () {
return argv.name;
}
},
{
match: /Base/g,
replacement: function () {
return argv.name.charAt(0).toUpperCase() + argv.name.slice(1);
}
}
]
}))
.pipe(gulp.dest('./js/src/'+argv.name+'/'));
console.log(argv.name+' component created');
})