-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
183 lines (161 loc) · 5.51 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
/* eslint-env node */
var browserify = require('browserify');
var CleanCSS = require('clean-css');
var gulp = require('gulp');
var buffer = require('gulp-buffer');
var Handlebars = require('handlebars');
const { ESLint } = require("eslint");
var changed = require('gulp-changed');
var _ = require('lodash');
var merge = require('merge2');
var source = require('vinyl-source-stream');
var stream = require('stream');
var streamToPromise = require('stream-to-promise');
var fs = require('fs');
// Requires dev dependencies to be installed
gulp.task('download-charts', function () {
var download = require('gulp-download-stream');
var unzip = require('gulp-unzip');
var url = 'https://www.ngdc.noaa.gov/mgg/shorelines/data/gshhg/latest/gshhg-shp-2.3.7.zip';
return download(url)
.pipe(unzip())
.pipe(gulp.dest('data_src/'));
});
// Requires dev dependencies to be installed,
// and download-charts task to be run
gulp.task('generate-charts', function () {
var source = require('vinyl-source-stream');
var mapJsonStream = require('./src/map-json-stream');
var geoJsonStream = require('geojson-stream');
var outputDir = 'data';
// Land
var mostOfWorldStream = (new mapJsonStream({}, 'data_src/GSHHS_shp/c/GSHHS_c_L1.shp'))
.pipe(geoJsonStream.stringify());
// Antarctica
var antarticaStream = (new mapJsonStream({}, 'data_src/GSHHS_shp/c/GSHHS_c_L5.shp'))
.pipe(geoJsonStream.stringify());
// Lackes
var lakesStream = (new mapJsonStream({}, 'data_src/GSHHS_shp/c/GSHHS_c_L2.shp'))
.pipe(geoJsonStream.stringify());
return Promise.all([
streamToPromise(mostOfWorldStream),
streamToPromise(antarticaStream),
streamToPromise(lakesStream),
]).then(function(collections) {
var mostOfWorldFeatures = JSON.parse(collections[0]).features;
var mostOfWorld = _(mostOfWorldFeatures)
.map(function(feature) {
return feature.geometry.coordinates[0];
})
.value();
var antarticaFeatures = JSON.parse(collections[1]).features;
var antarticaIslands = _(antarticaFeatures)
.filter(function(feature) {
return feature.id > 2;
})
.map(function(feature) {
return feature.geometry.coordinates[0];
})
.value();
var antarticaProper = _(antarticaFeatures)
.filter(function(feature) {
return feature.id <= 2;
})
.map(function(feature) {
return feature.geometry.coordinates[0];
})
.map(function(featureCoords) {
// Annoyingly land masses stradding 180 longtidue are split up.
// This is most obvious in Antartica, and done by adding in points
// at the South Pole and after
var seenSouthPole = false;
return _.filter(featureCoords, function(coords) {
seenSouthPole = seenSouthPole || coords[1] == -90;
return !seenSouthPole;
});
})
.reverse()
.flatten()
.value();
var lakesFeatures = JSON.parse(collections[2]).features;
var lakes = _(lakesFeatures)
.map(function(feature) {
// Reverse so lakaes are holes in the shape
return _.reverse(feature.geometry.coordinates[0]);
})
.value();
charts = _.flatten([mostOfWorld, antarticaIslands, [antarticaProper], lakes]).map(function(chart) {
return chart.map(function(points) {
return points.map(function(point) {
return parseInt(parseFloat(point.toFixed(2)) * 100);
});
})
});
chartsString = JSON.stringify(charts);
var saveStream = new stream.Readable()
saveStream.pipe(source('data-v2.json'))
.pipe(gulp.dest(outputDir));
saveStream.push(chartsString)
saveStream.push(null)
return streamToPromise(saveStream);
});
});
gulp.task('lint', async function () {
const eslint = new ESLint();
return await eslint.lintFiles(['gulpfile.js', 'src/*.js']);
});
gulp.task('default', function() {
var build = 'build';
var dest = build;
var dataSrc = 'data/*.json';
var dataDest = build + '/data';
var data = gulp.src(dataSrc)
.pipe(changed(dataDest))
.pipe(gulp.dest(dataDest));
var javascriptStream = browserify('src/app.js')
.plugin('tinyify', {})
.bundle();
var javascript = streamToPromise(javascriptStream).then(function(jsBuffers) {
return Buffer.concat(jsBuffers);
});
var cssSrc = [
'src/style.css'
];
var cssStream = gulp.src(cssSrc)
.pipe(changed(dest))
var css = streamToPromise(cssStream).then(function(css) {
var concatedCss = css.map(function(file) {
return file.contents.toString('utf-8');
}).join('\n');
return new CleanCSS({}).minify(concatedCss).styles;
});
var handlebarOpts = {
helpers: {
javascript: function(context) {
return new Handlebars.SafeString(context.data.root.javascript);
},
css: function(context) {
return new Handlebars.SafeString(context.data.root.css);
},
data: function(context) {
return new Handlebars.SafeString(context.data.root.data);
}
}
};
var htmlSrc = [
'src/index.html'
];
var html = Promise.all([javascript, css]).then(function(results) {
var javascriptContents = results[0];
var cssContents = results[1];
var template = Handlebars.compile(fs.readFileSync('src/index.html', 'utf8'));
return template({
javascript: javascriptContents,
css: cssContents,
data: fs.readFileSync('data/data-v2.json', 'utf8'),
}, handlebarOpts);
}).then(function(html) {
fs.writeFileSync('build/index.html', html)
});
return Promise.all([streamToPromise(css), html])
});