-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
276 lines (231 loc) · 7.87 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
'use strict';
var gulp = require('gulp');
var runSequence = require('run-sequence');
var del = require('del');
var path = require('path');
var dhisDirectory;
var buildDirectory = 'build';
var files = [
//Vendor dependency files
'vendor/jquery/dist/jquery.js',
'vendor/toastr/toastr.js',
'vendor/angular/angular.js',
'vendor/angular-animate/angular-animate.js',
'vendor/ui-router/release/angular-ui-router.js',
'vendor/lodash/dist/lodash.js',
'vendor/restangular/dist/restangular.js',
'vendor/angular-ui-select/dist/select.js',
'vendor/angular-bootstrap/ui-bootstrap-tpls.js',
'vendor/angular-messages/angular-messages.js',
'vendor/angular-ui-utils/validate.js',
//Test specific includes
'test/fixtures/fixtures.js',
'test/utils/*.js',
'test/matchers/*.js',
'vendor/angular-mocks/angular-mocks.js',
//Source files
'src/ngBootstrapper.js',
'src/**/*.js',
'src/**/*.html',
//Testmocks
'test/mocks/*_mock.js',
//Jasmine spec files
'test/specs/**/*_spec.js'
];
/**************************************************************************************************
* Utility functions
*/
function runKarma(watch) {
var karma = require('gulp-karma');
var config = {
configFile: 'test/karma.conf.js'
};
if (!watch) {
watch = false;
}
if (watch === true) {
config.action = 'watch';
}
return karma(config);
}
function printHelp() {
var task;
var taskDescriptions = {
default: 'Display this help',
test: 'Run the unit tests once',
watch: 'Run the unit tests on change detection',
jshint: 'Run jshint on the sourcecode',
jscs: 'Run jscs on the sourcecode',
sass: 'Run sass on the sass files and save the output to temp directory',
i18n: 'Copy the language files to the build directory',
manifest: 'Copy the manifest to the build directory',
images: 'Copy the images to the build directory',
package: 'Package the build directory in a zip file',
min: 'Minify and concat the html and javascript files'
};
console.log('\nGulp has the following available tasks.');
for (task in gulp.tasks) {
if (gulp.tasks.hasOwnProperty(task)) {
console.log(' ' + gulp.tasks[task].name + (taskDescriptions[task] ? ' -- ' + taskDescriptions[task] : ''));
}
}
console.log('');
}
/**
* Checks if the dhis.json file is present in the root of the project. This will be required for
* tasks that interact with a running dhis2 instance (for example to circumvent the install process)
*/
function checkForDHIS2ConfigFile() {
var dhisConfig = require(path.resolve('./dhis.json'));
if (!dhisConfig.dhisDeployDirectory) {
console.log('');
console.log('Dhis 2 deploy directory not set, please add a dhis.json to your project that looks like');
console.log(JSON.stringify({ dhisDeployDirectory: '<YOUR DHIS2 DIRECTORY>' }, undefined, 2));
console.log('');
throw new Error('DHIS deploy location not found');
}
dhisDirectory = dhisConfig.dhisDeployDirectory;
return dhisConfig;
}
function stringSrc(filename, string) {
var src = require('stream').Readable({ objectMode: true })
src._read = function () {
this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) }))
this.push(null)
}
return src
}
/**************************************************************************************************
* Gulp tasks
*/
gulp.task('default', function () {
printHelp();
});
gulp.task('test', function () {
return gulp.src(files).pipe(runKarma());
});
gulp.task('watch', function () {
return gulp.src(files).pipe(runKarma(true));
});
gulp.task('jshint', function () {
var jshint = require('gulp-jshint');
return gulp.src([
'test/specs/**/*.js',
'src/**/*.js'
])
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
gulp.task('jscs', function () {
var jscs = require('gulp-jscs');
return gulp.src([
'test/specs/**/*.js',
'src/**/*.js'
]).pipe(jscs('./.jscsrc'));
});
gulp.task('sass', function () {
var sass = require('gulp-ruby-sass');
return gulp.src('src/app/app.sass', { base: './src/' })
.pipe(sass())
.pipe(gulp.dest(
['temp', 'css'].join('/')
));
});
gulp.task('i18n', function () {
return gulp.src('src/i18n/**/*.json', { base: './src/' }).pipe(gulp.dest(
buildDirectory
));
});
gulp.task('manifest', function () {
return gulp.src('src/**/*.webapp', { base: './src/' }).pipe(gulp.dest(
buildDirectory
));
});
gulp.task('images', function () {
return gulp.src('src/**/icons/**/*', { base: './src/' }).pipe(gulp.dest(
buildDirectory
));
});
gulp.task('package', function () {
var zip = require('gulp-zip');
return gulp.src('build/**/*', { base: './build/' })
.pipe(zip('user-management.zip', { compress: false }))
.pipe(gulp.dest('.'));
});
gulp.task('min', ['sass'], function () {
var mangleJS = false;
var useref = require('gulp-useref');
var gulpif = require('gulp-if');
var ngAnnotate = require('gulp-ng-annotate');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');
var rev = require('gulp-rev');
var replaceRev = require('gulp-rev-replace');
var assets = useref.assets();
return gulp.src('src/**/*.html')
.pipe(assets)
.pipe(assets.restore())
.pipe(useref())
.pipe(gulpif('*.css', minifyCss()))
.pipe(gulpif('**/app.js', ngAnnotate({
add: true,
remove: true,
single_quotes: true,
stats: true
})))
.pipe(gulpif('*.js', uglify({
mangle: mangleJS
})))
.pipe(gulpif('!**/index.html', rev()))
.pipe(replaceRev())
.pipe(gulp.dest(buildDirectory));
});
gulp.task('copy-files', function () {
//TODO: Copy templates
});
gulp.task('copy-fonts', function () {
return gulp.src(['vendor/font-awesome/fonts/**/*.*'], {base: './vendor/font-awesome/'})
.pipe(gulp.dest(buildDirectory));
});
gulp.task('build', function (cb) {
runSequence('clean', 'test', 'i18n', 'manifest', 'images', 'jshint', 'jscs', 'min', 'copy-files', 'copy-fonts', cb);
});
gulp.task('build-prod', function (cb) {
runSequence('build', 'package', cb);
});
gulp.task('clean', function (cb) {
del(buildDirectory, cb);
});
gulp.task('modify-manifest', function (cb) {
var fs = require('fs');
var dhisConfig = checkForDHIS2ConfigFile();
fs.readFile('build/manifest.webapp', 'utf8', function (err, data) {
var manifest;
if (err) {
throw new Error('Failed to load manifest from build directory');
}
manifest = JSON.parse(data);
if (!(manifest && manifest.activities && manifest.activities.dhis && manifest.activities.dhis.href)) {
throw new Error('Incorrect manifest "manifest.activities.dhis.href" is not available');
}
manifest.activities.dhis.href = dhisConfig.dhisBaseUrl || '*';
fs.writeFile('build/manifest.webapp', JSON.stringify(manifest, undefined, 2), function (err) {
if (err) {
throw new Error('Failed to save modified manifest');
}
cb();
});
});
});
gulp.task('copy-app', function () {
checkForDHIS2ConfigFile();
return gulp.src('build/**/*.*', { base: './build/' })
.pipe(gulp.dest(dhisDirectory));
});
gulp.task('copy-to-dev', function () {
return runSequence('clean', 'i18n', 'manifest', 'images', 'jshint', 'jscs', 'min', 'copy-files', 'copy-fonts', 'modify-manifest', 'copy-app');
});
gulp.task('travis', function () {
return runSequence('test', 'jshint', 'jscs');
});