forked from pepfar-datim/datim-approvals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
346 lines (286 loc) · 9.02 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'use strict';
var dhis_directory = '/Users/markadm/Projects/dhis/DHIS2_HOME/apps/approvals/';
var build_directory = 'build';
var files = [
//Test libraries
'src/vendor/rxjs/dist/rx.all.js',
'src/vendor/lodash/dist/lodash.compat.js',
'src/vendor/jquery/dist/jquery.js',
'src/vendor/angular/angular.js',
'src/vendor/angular-mocks/angular-mocks.js',
'src/vendor/d2js/dist/js/d2.js',
'src/vendor/restangular/dist/restangular.js',
'src/vendor/angular-bootstrap/ui-bootstrap-tpls.js',
'src/vendor/angular-ui-select/dist/select.js',
'src/vendor/rx-angular/dist/rx.angular.js',
//Test files
'src/test/utils/*.js',
'src/test/matchers/*.js',
'src/test/fixtures/*.js',
'src/test/mocks/*.js',
//App files
'src/main/**/*.js',
'src/main/**/*.html',
//App mocks
'src/test/mocks/pepfar/*.js',
'src/test/specs/**/*.js'
];
//General requires
var gulp = require('gulp');
var runSequence = require('run-sequence');
var rimraf = require('rimraf');
//Test
var karma = require('gulp-karma');
var jshint = require('gulp-jshint');
var jscs = require('gulp-jscs');
//Build
var sass = require('gulp-sass');
var zip = require('gulp-zip');
var ngAnnotate = require('gulp-ng-annotate');
function run_karma(watch) {
var config = {
configFile: 'src/test/karma.conf.js'
};
if (watch === true) {
config.action = 'watch';
}
return karma(config);
}
gulp.task('clean', function(cb){
rimraf('./build', cb);
});
//Run the tests in the test directory
gulp.task('test', function () {
return gulp.src(files).pipe(run_karma());
});
//Run a watch on karma to rerun tests when the files change
gulp.task('watch', function() {
return gulp.src(files).pipe(run_karma(true));
});
//Run jshint on the code in src/main
gulp.task('lint', function () {
return gulp.src('./src/main/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
//Run jscs on the code in src/main
gulp.task('jscs', function () {
return gulp.src('src/main/**/*.js').pipe(jscs('./.jscsrc'));
});
gulp.task('js', /*['lint', 'jscs'],*/ function () {
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
return gulp.src('src/main/**/*.js')
.pipe(ngAnnotate({
add: true,
remove: true,
single_quotes: true,
stats: true
}))
.pipe(concat('app.js'))
//.pipe(uglify())
.pipe(gulp.dest(
[build_directory, 'js'].join('/')
));
});
gulp.task('sass', function () {
var minifyCSS = require('gulp-minify-css');
return gulp.src('src/main/app.sass')
.pipe(sass())
.pipe(minifyCSS())
.pipe(gulp.dest(
[build_directory, 'css'].join('/')
));
});
gulp.task('html', function () {
var minifyHTML = require('gulp-minify-html');
return gulp.src('src/main/**/*.html')
.pipe(minifyHTML({ empty: true, quotes: true }))
.pipe(gulp.dest(build_directory));
});
gulp.task('i18n', function () {
return gulp.src('./src/main/common/i18n/**/*.json', { base: './src/main/' }).pipe(gulp.dest(
build_directory
));
});
gulp.task('images', function () {
return gulp.src('./src/main/img/icons/**/*', { base: './src/main/' }).pipe(gulp.dest(
build_directory
));
});
gulp.task('manifest', function () {
return gulp.src('./src/main/*.webapp', { base: './src/main/' }).pipe(gulp.dest(
build_directory
));
});
gulp.task('dependencies', function () {
var path = require('canonical-path');
var _ = require('lodash');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
function get_main_script(dependency) {
var bower_directory = 'src/vendor';
var main_scripts;
try {
main_scripts = require(path.resolve([
bower_directory,
dependency,
'bower.json'
].join('/'))).main;
} catch (e) {
main_scripts = require(path.resolve([
bower_directory,
dependency,
'.bower.json'
].join('/'))).main;
}
var replace_leading_dot = function (script) {
if (/^\.\/.*/.test(script)) {
return script.replace(/^\.\//, '');
}
return script;
}
if (_.isArray(main_scripts)) {
main_scripts = _.map(main_scripts, function (script) {
return [ bower_directory, dependency, replace_leading_dot(script) ].join('/');
});
}
if (_.isString(main_scripts)) {
main_scripts = [ bower_directory, dependency, replace_leading_dot(main_scripts) ].join('/')
}
return main_scripts;
}
var dependencies = require(path.resolve('bower.json')).dependencies;
var depFiles = []
_.forEach(dependencies, function (version, dependency) {
if (dependency === 'd2js') return;
depFiles = depFiles.concat(get_main_script(dependency));
});
var jsFiles = _.filter(depFiles, function (fileName) {
var regex = /^.+\.js$/;
return regex.test(fileName);
});
var nonjsFiles = _.filter(depFiles, function (fileName) {
var regex = /^.+\.js$/;
return !regex.test(fileName);
});
gulp.src(jsFiles)
.pipe(concat('vendor.js'))
.pipe(uglify())
.pipe(gulp.dest([
build_directory,
'vendor'
].join('/')));
gulp.src(nonjsFiles).pipe(gulp.dest([
build_directory,
'vendor'
].join('/')));
//TODO: fix this
// Redo d2js seperately
return gulp.src([
'src/vendor/d2js/dist/**/*'
]).pipe(gulp.dest([
build_directory,
'vendor',
'd2js'
].join('/')));
});
gulp.task('vendor', function (cb) {
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var stuff = gulp.src([
'src/vendor/jquery/**/jquery.js',
'src/vendor/lodash/**/lodash.compat.js',
'src/vendor/angular/angular.js',
'src/vendor/angular*/**/ui-bootstrap-tpls.js',
'src/vendor/angular*/**/select.js',
'src/vendor/angular*/**/angular-translate.js',
'src/vendor/restangular/**/restangular.js',
'src/vendor/rxjs/dist/rx.all.js'
])
.pipe(concat('vendor.js'))
.pipe(uglify())
.pipe(gulp.dest(
[build_directory, 'vendor'].join('/')
));
gulp.src([
'src/vendor/toastr/**/toastr.js',
'src/vendor/d2js/js/**/*'
])
.pipe(uglify())
.pipe(gulp.dest(
[build_directory, 'vendor'].join('/')
));
gulp.src([
'src/vendor/toastr/**/toastr.css',
'src/vendor/d2js/**/d2.css',
'src/vendor/d2js/**/*.html'
])
.pipe(gulp.dest(
[build_directory, 'vendor'].join('/')
));
return stuff;
});
gulp.task('font-awesome', function () {
return gulp.src([
'src/vendor/font-awesome/fonts/**'
])
.pipe(gulp.dest('build/fonts'));
});
gulp.task('do-rev-on-build', function () {
function doFileCheck(file) {
if (/\.html$/.test(file.path) || /\.js$/.test(file.path) || /\.css$/.test(file.path)) {
return true;
}
return false;
}
var rev = require('gulp-rev');
var revReplace = require('gulp-rev-replace');
var gulpif = require('gulp-if');
return gulp.src(['build/**/*.*'])
.pipe(gulpif('!**/index.html', gulpif('!**/vendor/**', gulpif(doFileCheck, rev()))))
.pipe(revReplace())
.pipe(gulp.dest('build-with-rev/'));
});
gulp.task('clean-rev', function(cb){
rimraf('./build-with-rev', cb);
});
gulp.task('build', function (cb) {
runSequence('clean', 'js', 'header-bar-dependency', 'sass', 'html', 'dependencies', 'font-awesome', 'i18n', 'images', 'manifest', cb);
});
gulp.task('build-prod', function (cb) {
runSequence('clean-rev', 'build', 'do-rev-on-build', 'package', cb);
});
gulp.task('header-bar-dependency', function () {
return gulp.src([
'node_modules/d2-ui/dist/header-bar.js',
'src/header-bar/**/*.js',
])
.pipe(gulp.dest('build'));
});
gulp.task('deploy', function () {
return gulp.src(['build-with-rev/**/*'])
.pipe(gulp.dest(dhis_directory));
});
gulp.task('default', function (cb) {
rimraf(dhis_directory, cb);
runSequence('build', 'deploy', cb);
});
gulp.task('git:pre-commit', function (cb) {
gulp.on('err', function(e){
console.log('Pre-commit validate failed');
process.exit(1);
});
runSequence('test', 'lint', 'jscs', cb);
});
gulp.task('copy-fake-api', function () {
return gulp.src([
'fake-api/**/*'
]).pipe(gulp.dest(dhis_directory + 'fake-api/'));
});
gulp.task('package', function () {
return gulp.src('build-with-rev/**/*', { base: './build-with-rev/' })
.pipe(zip('approvals.zip', { compress: false }))
.pipe(gulp.dest('.'));
});