forked from TerriaJS/nationalmap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
230 lines (184 loc) · 7.18 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
"use strict";
/*global require*/
var fs = require('fs');
var spawnSync = require('spawn-sync');
var glob = require('glob-all');
var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var jshint = require('gulp-jshint');
var jsdoc = require('gulp-jsdoc');
var less = require('gulp-less');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var exorcist = require('exorcist');
var buffer = require('vinyl-buffer');
var transform = require('vinyl-transform');
var source = require('vinyl-source-stream');
var watchify = require('watchify');
var NpmImportPlugin = require('less-plugin-npm-import');
var jsoncombine = require('gulp-jsoncombine');
var appJSName = 'nationalmap.js';
var appCssName = 'nationalmap.css';
var specJSName = 'nationalmap-tests.js';
var appEntryJSName = './index.js';
var testGlob = './test/**/*.js';
// Create the build directory, because browserify flips out if the directory that might
// contain an existing source map doesn't exist.
if (!fs.existsSync('wwwroot/build')) {
fs.mkdirSync('wwwroot/build');
}
gulp.task('build-app', ['prepare-terriajs'], function() {
return build(appJSName, appEntryJSName, false);
});
gulp.task('build-specs', ['prepare-terriajs'], function() {
return build(specJSName, glob.sync(testGlob), false);
});
gulp.task('build-css', function() {
return gulp.src('./index.less')
.pipe(less({
plugins: [
new NpmImportPlugin()
]
}))
.pipe(rename(appCssName))
.pipe(gulp.dest('./wwwroot/build/'));
});
gulp.task('build', ['build-css', 'merge-datasources', 'build-app', 'build-specs']);
gulp.task('release-app', ['prepare'], function() {
return build(appJSName, appEntryJSName, true);
});
gulp.task('release-specs', ['prepare'], function() {
return build(specJSName, glob.sync(testGlob), true);
});
gulp.task('release', ['build-css', 'merge-datasources', 'release-app', 'release-specs']);
gulp.task('watch-app', ['prepare'], function() {
return watch(appJSName, appEntryJSName, false);
});
gulp.task('watch-specs', ['prepare'], function() {
return watch(specJSName, glob.sync(testGlob), false);
});
gulp.task('watch-css', ['build-css'], function() {
return gulp.watch(['./index.less', './node_modules/terriajs/lib/Styles/*.less'], ['build-css']);
});
gulp.task('watch-datasource-groups', ['merge-groups'], function() {
return gulp.watch('datasources/00_National_Data_Sets/*.json', [ 'merge-groups', 'merge-catalog' ]);
});
gulp.task('watch-datasource-catalog', ['merge-catalog'], function() {
return gulp.watch('datasources/*.json', [ 'merge-catalog' ]);
});
gulp.task('watch-datasources', ['watch-datasource-groups','watch-datasource-catalog']);
gulp.task('watch', ['watch-app', 'watch-specs', 'watch-css', 'watch-datasources']);
gulp.task('lint', function(){
return gulp.src(['lib/**/*.js', 'test/**/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
});
gulp.task('docs', function(){
return gulp.src('lib/**/*.js')
.pipe(jsdoc('./wwwroot/doc', undefined, {
plugins : ['plugins/markdown']
}));
});
gulp.task('prepare', ['prepare-terriajs']);
gulp.task('prepare-terriajs', function() {
return gulp.src([
'node_modules/terriajs/wwwroot/**'
], { base: 'node_modules/terriajs/wwwroot' })
.pipe(gulp.dest('wwwroot/build/TerriaJS'));
});
gulp.task('merge-groups', function() {
var jsonspacing=0;
return gulp.src("./datasources/00_National_Data_Sets/*.json")
.pipe(jsoncombine("00_National_Data_Sets.json", function(data) {
// be absolutely sure we have the files in alphabetical order
var keys = Object.keys(data).slice().sort();
for (var i = 1; i < keys.length; i++) {
data[keys[0]].catalog[0].items.push(data[keys[i]].catalog[0].items[0]);
}
return new Buffer(JSON.stringify(data[keys[0]], null, jsonspacing));
}))
.pipe(gulp.dest("./datasources"));
});
gulp.task('merge-catalog', ['merge-groups'], function() {
var jsonspacing=0;
return gulp.src("./datasources/*.json")
.pipe(jsoncombine("nm.json", function(data) {
// be absolutely sure we have the files in alphabetical order, with 000_settings first.
var keys = Object.keys(data).slice().sort();
data[keys[0]].catalog = [];
for (var i = 1; i < keys.length; i++) {
data[keys[0]].catalog.push(data[keys[i]].catalog[0]);
}
return new Buffer(JSON.stringify(data[keys[0]], null, jsonspacing));
}))
.pipe(gulp.dest("./wwwroot/init"));
});
gulp.task('merge-datasources', ['merge-catalog', 'merge-groups']);
gulp.task('default', ['lint', 'build']);
function bundle(name, bundler, minify, catchErrors) {
// Get a version string from "git describe".
var version = spawnSync('git', ['describe']).stdout.toString().trim();
var isClean = spawnSync('git', ['status', '--porcelain']).stdout.toString().length === 0;
if (!isClean) {
version += ' (plus local modifications)';
}
fs.writeFileSync('version.js', 'module.exports = \'' + version + '\';');
// Combine main.js and its dependencies into a single file.
// The poorly-named "debug: true" causes Browserify to generate a source map.
var result = bundler.bundle();
if (catchErrors) {
// Display errors to the user, and don't let them propagate.
result = result.on('error', function(e) {
gutil.log('Browserify Error', e.message);
});
}
result = result
.pipe(source(name))
.pipe(buffer());
if (minify) {
// Minify the combined source.
// sourcemaps.init/write maintains a working source map after minification.
// "preserveComments: 'some'" preserves JSDoc-style comments tagged with @license or @preserve.
result = result
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify({preserveComments: 'some', mangle: true, compress: true}))
.pipe(sourcemaps.write());
}
result = result
// Extract the embedded source map to a separate file.
.pipe(transform(function () { return exorcist('wwwroot/build/' + name + '.map'); }))
// Write the finished product.
.pipe(gulp.dest('wwwroot/build'));
return result;
}
function build(name, files, minify) {
return bundle(name, browserify({
entries: files,
debug: true
}), minify, false);
}
function watch(name, files, minify) {
var bundler = watchify(browserify({
entries: files,
debug: true,
cache: {},
packageCache: {}
}));
function rebundle(ids) {
// Don't rebundle if only the version changed.
if (ids && ids.length === 1 && /\/version\.js$/.test(ids[0])) {
return;
}
var start = new Date();
var result = bundle(name, bundler, minify, true);
result.on('end', function() {
console.log('Rebuilt ' + name + ' in ' + (new Date() - start) + ' milliseconds.');
});
return result;
}
bundler.on('update', rebundle);
return rebundle();
}