-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
299 lines (265 loc) · 9.44 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
'use strict';
/**
* Module dependencies.
*/
var _ = require('lodash'),
fs = require('fs'),
defaultAssets = require('./config/assets/default'),
glob = require('glob'),
gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
runSequence = require('run-sequence'),
plugins = gulpLoadPlugins({
rename: {
'gulp-angular-templatecache': 'templateCache'
}
}),
pngquant = require('imagemin-pngquant'),
wiredep = require('wiredep').stream,
path = require('path'),
endOfLine = require('os').EOL,
protractor = require('gulp-protractor').protractor,
webdriver_update = require('gulp-protractor').webdriver_update,
webdriver_standalone = require('gulp-protractor').webdriver_standalone,
del = require('del'),
KarmaServer = require('karma').Server,
semver = require('semver');
// Set NODE_ENV to 'development'
gulp.task('env:dev', function () {
process.env.NODE_ENV = 'development';
process.env.BLUEBIRD_WARNINGS = 0;
});
// Set NODE_ENV to 'production'
gulp.task('env:prod', function () {
process.env.NODE_ENV = 'production';
process.env.BLUEBIRD_WARNINGS = 0;
});
// Nodemon task
gulp.task('nodemon', function () {
// Node.js v7 and newer use different debug argument
var debugArgument = semver.satisfies(process.versions.node, '>=7.0.0') ? '--inspect' : '--debug';
return plugins.nodemon({
script: 'server.js',
nodeArgs: [debugArgument],
ext: 'js,html',
verbose: true,
watch: _.union(defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
});
});
// Nodemon task without verbosity or debugging
gulp.task('nodemon-nodebug', function () {
return plugins.nodemon({
script: 'server.js',
ext: 'js,html',
watch: _.union(defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
});
});
// Watch Files For Changes
gulp.task('watch', function () {
// Start livereload
plugins.refresh.listen();
// Add watch rules
gulp.watch(defaultAssets.server.views).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.server.allJS, ['eslint']).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.client.js, ['eslint']).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.client.css, ['csslint']).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.client.sass, ['sass', 'csslint']).on('change', plugins.refresh.changed);
gulp.watch(defaultAssets.client.less, ['less', 'csslint']).on('change', plugins.refresh.changed);
if (process.env.NODE_ENV === 'production') {
gulp.watch(defaultAssets.server.gulpConfig, ['templatecache', 'eslint']);
gulp.watch(defaultAssets.client.views, ['templatecache']).on('change', plugins.refresh.changed);
} else {
gulp.watch(defaultAssets.server.gulpConfig, ['eslint']);
gulp.watch(defaultAssets.client.views).on('change', plugins.refresh.changed);
}
});
// CSS linting task
gulp.task('csslint', function () {
return gulp.src(defaultAssets.client.css)
.pipe(plugins.csslint('.csslintrc'))
.pipe(plugins.csslint.formatter());
// Don't fail CSS issues yet
// .pipe(plugins.csslint.failFormatter());
});
// ESLint JS linting task
gulp.task('eslint', function () {
var assets = _.union(
defaultAssets.server.gulpConfig,
defaultAssets.server.allJS,
defaultAssets.client.js
);
return gulp.src(assets)
.pipe(plugins.eslint())
.pipe(plugins.eslint.format());
});
// JS minifying task
gulp.task('uglify', function () {
var assets = _.union(
defaultAssets.client.js,
defaultAssets.client.templates
);
del(['public/dist/*']);
return gulp.src(assets)
.pipe(plugins.ngAnnotate())
.pipe(plugins.uglify({
mangle: true
}))
.pipe(plugins.concat('application.min.js'))
.pipe(plugins.rev())
.pipe(gulp.dest('public/dist'));
});
// CSS minifying task
gulp.task('cssmin', function () {
return gulp.src(defaultAssets.client.css)
.pipe(plugins.csso())
.pipe(plugins.concat('application.min.css'))
.pipe(plugins.rev())
.pipe(gulp.dest('public/dist'));
});
// Sass task
gulp.task('sass', function () {
return gulp.src(defaultAssets.client.sass)
.pipe(plugins.sass())
.pipe(plugins.autoprefixer())
.pipe(gulp.dest('./modules/'));
});
// Less task
gulp.task('less', function () {
return gulp.src(defaultAssets.client.less)
.pipe(plugins.less())
.pipe(plugins.autoprefixer())
.pipe(gulp.dest('./modules/'));
});
// Imagemin task
gulp.task('imagemin', function () {
return gulp.src(defaultAssets.client.img)
.pipe(plugins.imagemin({
progressive: true,
svgoPlugins: [{ removeViewBox: false }],
use: [pngquant()]
}))
.pipe(gulp.dest('public/dist/img'));
});
// wiredep task to default
gulp.task('wiredep', function () {
return gulp.src('config/assets/default.js')
.pipe(wiredep({
ignorePath: '../../'
}))
.pipe(gulp.dest('config/assets/'));
});
// wiredep task to production
gulp.task('wiredep:prod', function () {
return gulp.src('config/assets/production.js')
.pipe(wiredep({
ignorePath: '../../',
fileTypes: {
js: {
replace: {
css: function (filePath) {
var minFilePath = filePath.replace('.css', '.min.css');
var fullPath = path.join(process.cwd(), minFilePath);
if (!fs.existsSync(fullPath)) {
return '\'' + filePath + '\',';
} else {
return '\'' + minFilePath + '\',';
}
},
js: function (filePath) {
var minFilePath = filePath.replace('.js', '.min.js');
var fullPath = path.join(process.cwd(), minFilePath);
if (!fs.existsSync(fullPath)) {
return '\'' + filePath + '\',';
} else {
return '\'' + minFilePath + '\',';
}
}
}
}
}
}))
.pipe(gulp.dest('config/assets/'));
});
// Copy local development environment config example
gulp.task('copyLocalEnvConfig', function () {
var src = [];
var renameTo = 'local-development.js';
// only add the copy source if our destination file doesn't already exist
if (!fs.existsSync('config/env/' + renameTo)) {
src.push('config/env/local.example.js');
}
return gulp.src(src)
.pipe(plugins.rename(renameTo))
.pipe(gulp.dest('config/env'));
});
// Make sure upload directory exists
gulp.task('makeUploadsDir', function () {
return fs.mkdir('modules/users/client/img/profile/uploads', function (err) {
if (err && err.code !== 'EEXIST') {
console.error(err);
}
});
});
// Angular template cache task
gulp.task('templatecache', function () {
return gulp.src(defaultAssets.client.views)
.pipe(plugins.templateCache('templates.js', {
root: '/modules/',
module: 'core',
templateHeader: '(function () {' + endOfLine + ' \'use strict\';' + endOfLine + endOfLine + ' angular' + endOfLine + ' .module(\'<%= module %>\'<%= standalone %>)' + endOfLine + ' .run(templates);' + endOfLine + endOfLine + ' templates.$inject = [\'$templateCache\'];' + endOfLine + endOfLine + ' function templates($templateCache) {' + endOfLine,
templateBody: ' $templateCache.put(\'<%= url %>\', \'<%= contents %>\');',
templateFooter: ' }' + endOfLine + '})();' + endOfLine
}))
.pipe(gulp.dest('build'));
});
// Run karma with coverage options set and write report
gulp.task('karma:coverage', function(done) {
new KarmaServer({
configFile: __dirname + '/karma.conf.js',
preprocessors: {
'modules/*/client/views/**/*.html': ['ng-html2js'],
'modules/core/client/app/config.js': ['coverage'],
'modules/core/client/app/init.js': ['coverage'],
'modules/*/client/*.js': ['coverage'],
'modules/*/client/config/*.js': ['coverage'],
'modules/*/client/controllers/*.js': ['coverage'],
'modules/*/client/directives/*.js': ['coverage'],
'modules/*/client/services/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
coverageReporter: {
dir: 'coverage/client',
reporters: [
{ type: 'lcov', subdir: '.' }
// printing summary to console currently weirdly causes gulp to hang so disabled for now
// https://github.com/karma-runner/karma-coverage/issues/209
// { type: 'text-summary' }
]
}
}, done).start();
});
// Downloads the selenium webdriver if protractor version is compatible
gulp.task('webdriver_update', webdriver_update);
gulp.task('webdriver_prep', function(done) {
runSequence('protractor_prep', 'webdriver_update', done);
});
// Start the standalone selenium server
// NOTE: This is not needed if you reference the
// seleniumServerJar in your protractor.conf.js
gulp.task('webdriver_standalone', webdriver_standalone);
// Lint CSS and JavaScript files.
gulp.task('lint', function (done) {
runSequence('less', 'sass', ['csslint', 'eslint'], done);
});
// Lint project files and minify them into two production files.
gulp.task('build', function (done) {
runSequence('env:dev', 'wiredep:prod', 'lint', ['uglify', 'cssmin'], done);
});
// Run the project in development mode with node debugger enabled
gulp.task('default', function (done) {
runSequence('env:dev', ['copyLocalEnvConfig', 'makeUploadsDir'], 'lint', ['nodemon', 'watch'], done);
});
// Run the project in production mode
gulp.task('prod', function (done) {
runSequence(['copyLocalEnvConfig', 'makeUploadsDir', 'templatecache'], 'build', 'env:prod', 'lint', ['nodemon-nodebug', 'watch'], done);
});