-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
172 lines (154 loc) · 5.98 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
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var less = require('gulp-less');
var templateCache = require('gulp-angular-templatecache');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var jasmine = require('gulp-jasmine');
var istanbul = require('gulp-istanbul');
var karma = require('karma').Server;
var minifyCSS = require('gulp-minify-css');
var merge = require('merge-stream');
var del = require('del');
var sourcemaps = require('gulp-sourcemaps');
// Remove any cached distribution files
gulp.task('clean', function () {
del(['client/javascript/dist/**/*', 'client/stylesheets/dist/**/*']);
});
// Lint (JSHint) Task
gulp.task('lint', function () {
return gulp.src([
'./server/controllers/**/*.js',
'./server/models/**/*.js',
'./server/routes/**/*.js',
'./server/services/**/*.js',
'./server/utils/**/*.js',
'./app.js',
'./client/javascript/**/*.js',
// don't lint packaged or minified code
// 9 out of 10 times this is optimized (e.g. no semicolons)
'!./client/javascript/dist/*'
]).pipe(jshint()).pipe(jshint.reporter('default'));
});
// Compile Stylesheets
gulp.task('style', function () {
var lessStream = gulp.src(['./client/stylesheets/less/*.less',
'./client/stylesheets/less/**/*.less'])
.pipe(less())
.pipe(concat('styles.less'));
var cssStream = gulp.src(['./client/stylesheets/css/*.css',
'./client/stylesheets/css/**/*.css'])
.pipe(concat('styles.css'));
// Combine all style sheet files into one styles.css
var mergedStream = merge(lessStream, cssStream)
.pipe(concat('styles.css'))
.pipe(gulp.dest('./client/stylesheets/dist'));
return mergedStream;
});
// Convert Angular templates into a cache file
gulp.task('templates', function () {
return gulp.src('./client/templates/*.html')
.pipe(templateCache('templates.js', {
module: 'meanstacktutorials'
}))
.pipe(gulp.dest('./client/javascript/dist'));
});
// Concatenate JavaScripts
gulp.task('concat', ['templates'], function () {
return gulp.src([
'./client/javascript/**/*.js',
'!./client/javascript/vendor/**/*.js'
])
.pipe(sourcemaps.init())
.pipe(concat('all.js'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./client/javascript/dist'));
});
// Minify JavaScripts
gulp.task('minify', ['concat'], function () {
return gulp.src('./dist/all.js', {cwd: './client/javascript'})
.pipe(rename('all.min.js'))
.pipe(uglify())
.pipe(gulp.dest('./client/javascript/dist'));
});
// Karma
gulp.task('karma', function (done) {
return karma.start({
configFile: __dirname + '/karma.conf.js'
}, done);
});
// Server side unit tests with gulp-jasmine and code coverage. This only works for server side code.
// See: https://github.com/sindresorhus/gulp-jasmine/issues/46
gulp.task('coverage', ['templates'], function (cb) {
// All source files that should be included in the code coverage report
return gulp.src([
'./server/controllers/**/*.js',
'./server/models/**/*.js',
'./server/routes/**/*.js',
'./server/services/**/*.js',
'./server/utils/**/*.js',
'./client/javascript/**/*.js',
'./app.js'
])
.pipe(istanbul({includeUntested: true})) // Covering files
.pipe(istanbul.hookRequire()) // Force 'require' to return covered filed
.on('finish', function () {
var coverageThreshold = 90; // 90% code coverage
// Source to test
gulp.src(['./testing/tests/frontend/**/*Spec.js'])
// gulp-jasmine works on filepaths so you can't have any plugins before it
.pipe(jasmine()) // run all unit tests in source
.pipe(istanbul.writeReports())
.pipe(istanbul.enforceThresholds({thresholds: {global: coverageThreshold}}))
.on('end', function () {
// On end have call back (function) pop off stack
console.log('Tests and code coverage complete. See reports.');
this.emit('end'); //instead of erroring the stream, end it
});
});
});
// Run Node.js tests and create LCOV-format reports with Istanbul
gulp.task('test-server', function () {
return gulp.src([
'./server/controllers/**/*.js',
'./server/models/**/*.js',
'./server/routes/**/*.js',
'./server/services/**/*.js',
'./server/utils/**/*.js',
'./app.js'
])
.pipe(KarmaServer({
configFile: __dirname + '/testing/karma.conf.js',
action: 'run'
}))
.pipe(istanbul({includeUntested: true})) // Covering files
.pipe(istanbul.hookRequire()) // Force 'require' to return covered files
.pipe(istanbul()) // Node.js source coverage
.on('end', function () {
gulp.src(['./testing/tests/frontend/**/*Spec.js'])
.pipe(jasmine())
.on('error', function (err) {
throw err;
})
.pipe(istanbul.writeReports('reports')); // Creating reports
});
});
// Watch Files For Changes
gulp.task('watch', ['dev'], function () {
// Let the html templates compile first that way there aren't any load conflicts with the JS
gulp.watch('./client/templates/**/*.html', ['concat']);
gulp.watch('./client/javascript/**/*.js', ['concat']);
gulp.watch('./server/models/*.js', ['concat']);
gulp.watch('./server/**/*.js', ['concat']);
gulp.watch('./client/stylesheets/less/**/*.less', ['style']);
gulp.watch('./client/stylesheets/less/*.less', ['style']);
gulp.watch('./client/stylesheets/css/**/*.css', ['style']);
gulp.watch('./client/stylesheets/css/*.css', ['style']);
});
// Used for development 'gulp dev'
gulp.task('dev', ['clean', 'style', 'concat']);
// Runs unit tests
gulp.task('test', ['karma', 'jasmine']);
// Used for production 'gulp'
gulp.task('default', ['lint', 'style', 'minify']);