This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
357 lines (290 loc) · 14.5 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
347
348
349
350
351
352
353
354
355
356
357
/*©agpl*************************************************************************
* *
* Napkin Globe – Map-data interaction solution for the Napkin platform *
* Copyright (C) 2020 Napkin AS *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
*****************************************************************************©*/
/*eslint-env node*/
/*eslint no-sync: 0*/
/*eslint no-process-exit: 0*/
'use strict';
/*global require*/
// Every module required-in here must be a `dependency` in package.json, not just a `devDependency`,
// This matters if ever we have gulp tasks run from npm, especially post-install ones.
var fs = require('fs');
var gulp = require('gulp');
var path = require('path');
var PluginError = require('plugin-error');
var watchOptions = {
interval: 1000
};
gulp.task('check-terriajs-dependencies', function(done) {
var appPackageJson = require('./package.json');
var terriaPackageJson = require('terriajs/package.json');
syncDependencies(appPackageJson.dependencies, terriaPackageJson, true);
syncDependencies(appPackageJson.devDependencies, terriaPackageJson, true);
done();
});
gulp.task('write-version', function(done) {
var fs = require('fs');
var spawnSync = require('child_process').spawnSync;
// 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 + '\';');
done();
});
gulp.task('build-app', gulp.series('check-terriajs-dependencies', 'write-version', function buildApp(done) {
var runWebpack = require('terriajs/buildprocess/runWebpack.js');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js')(true);
checkForDuplicateCesium();
runWebpack(webpack, webpackConfig, done);
}));
gulp.task('release-app', gulp.series('check-terriajs-dependencies', 'write-version', function releaseApp(done) {
var runWebpack = require('terriajs/buildprocess/runWebpack.js');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js')(false);
checkForDuplicateCesium();
runWebpack(webpack, Object.assign({}, webpackConfig, {
plugins: webpackConfig.plugins || []
}), done);
}));
gulp.task('watch-app', gulp.series('check-terriajs-dependencies', function watchApp(done) {
var fs = require('fs');
var watchWebpack = require('terriajs/buildprocess/watchWebpack');
var webpack = require('webpack');
var webpackConfig = require('./buildprocess/webpack.config.js')(true, false);
checkForDuplicateCesium();
fs.writeFileSync('version.js', 'module.exports = \'Development Build\';');
watchWebpack(webpack, webpackConfig, done);
}));
gulp.task('copy-terriajs-assets', function() {
var terriaWebRoot = path.join(getPackageRoot('terriajs'), 'wwwroot');
var sourceGlob = path.join(terriaWebRoot, '**');
var destPath = path.resolve(__dirname, 'wwwroot', 'build', 'TerriaJS');
return gulp
.src([ sourceGlob ], { base: terriaWebRoot })
.pipe(gulp.dest(destPath));
});
gulp.task('watch-terriajs-assets', gulp.series('copy-terriajs-assets', function waitForTerriaJsAssetChanges() {
var terriaWebRoot = path.join(getPackageRoot('terriajs'), 'wwwroot');
var sourceGlob = path.join(terriaWebRoot, '**');
// gulp.watch as of gulp v4.0.0 doesn't work with backslashes (the task is never triggered).
// But Windows is ok with forward slashes, so use those instead.
if (path.sep === '\\') {
sourceGlob = sourceGlob.replace(/\\/g, '/');
}
return gulp.watch(sourceGlob, watchOptions, gulp.series('copy-terriajs-assets'));
}));
gulp.task('copy-editor', function() {
var glob = path.join(getPackageRoot('terriajs-catalog-editor'), '**');
return gulp.src(glob)
.pipe(gulp.dest('./wwwroot/editor'));
});
// Generate new schema for editor, and copy it over whatever version came with editor.
gulp.task('make-editor-schema', gulp.series('copy-editor', function makeEditorSchema() {
var generateSchema = require('generate-terriajs-schema');
var schemaSourceGlob = require('terriajs/buildprocess/schemaSourceGlob');
return generateSchema({
sourceGlob: schemaSourceGlob,
dest: 'wwwroot/editor',
noversionsubdir: true,
editor: true,
quiet: true
});
}));
gulp.task('lint', function(done) {
var runExternalModule = require('terriajs/buildprocess/runExternalModule');
runExternalModule('eslint/bin/eslint.js', [
'-c', path.join(getPackageRoot('terriajs'), '.eslintrc'),
'--ignore-pattern', 'lib/ThirdParty',
'--max-warnings', '0',
'index.js',
'lib'
]);
done();
});
function getPackageRoot(packageName) {
return path.dirname(require.resolve(packageName + '/package.json'));
}
gulp.task('make-package', function(done) {
var argv = require('yargs').argv;
var fs = require('fs-extra');
var spawnSync = require('child_process').spawnSync;
var json5 = require('json5');
var packageName = argv.packageName || (process.env.npm_package_name + '-' + spawnSync('git', ['describe']).stdout.toString().trim());
var packagesDir = path.join('.', 'deploy', 'packages');
if (!fs.existsSync(packagesDir)) {
fs.mkdirSync(packagesDir);
}
var workingDir = path.join('.', 'deploy', 'work');
if (fs.existsSync(workingDir)) {
fs.removeSync(workingDir);
}
fs.mkdirSync(workingDir);
fs.copySync('wwwroot', path.join(workingDir, 'wwwroot'));
fs.copySync('node_modules', path.join(workingDir, 'node_modules'));
if (fs.existsSync('proxyauth.json')) {
fs.copySync('proxyauth.json', path.join(workingDir, 'proxyauth.json'));
}
fs.copySync('deploy/varnish', path.join(workingDir, 'varnish'));
fs.copySync('ecosystem.config.js', path.join(workingDir, 'ecosystem.config.js'));
fs.copySync('ecosystem-production.config.js', path.join(workingDir, 'ecosystem-production.config.js'));
if (argv.serverConfigOverride) {
var serverConfig = json5.parse(fs.readFileSync('devserverconfig.json', 'utf8'));
var serverConfigOverride = json5.parse(fs.readFileSync(argv.serverConfigOverride, 'utf8'));
var productionServerConfig = mergeConfigs(serverConfig, serverConfigOverride);
fs.writeFileSync(path.join(workingDir, 'productionserverconfig.json'), JSON.stringify(productionServerConfig, undefined, ' '));
} else {
fs.writeFileSync(path.join(workingDir, 'productionserverconfig.json'), fs.readFileSync('devserverconfig.json', 'utf8'));
}
if (argv.clientConfigOverride) {
var clientConfig = json5.parse(fs.readFileSync(path.join('wwwroot', 'config.json'), 'utf8'));
var clientConfigOverride = json5.parse(fs.readFileSync(argv.clientConfigOverride, 'utf8'));
var productionClientConfig = mergeConfigs(clientConfig, clientConfigOverride);
fs.writeFileSync(path.join(workingDir, 'wwwroot', 'config.json'), JSON.stringify(productionClientConfig, undefined, ' '));
}
// if we are on OSX make sure to use gtar for compatibility with Linux
// otherwise we see lots of error message when extracting with GNU tar
var tar = /^darwin/.test(process.platform) ? 'gtar' : 'tar';
var tarResult = spawnSync(tar, [
'czf',
path.join('..', 'packages', packageName + '.tar.gz')
].concat(fs.readdirSync(workingDir)), {
cwd: workingDir,
stdio: 'inherit',
shell: false
});
if (tarResult.status !== 0) {
throw new PluginError('tar', 'External module exited with an error.', { showStack: false });
}
done();
});
gulp.task('clean', function(done) {
var fs = require('fs-extra');
// // Remove build products
fs.removeSync(path.join('wwwroot', 'build'));
done();
});
function mergeConfigs(original, override) {
var result = Object.assign({}, original);
if (typeof original === 'undefined') {
original = {};
}
for (var name in override) {
if (!override.hasOwnProperty(name)) {
continue;
}
if (Array.isArray(override[name])) {
result[name] = override[name];
} else if (typeof override[name] === 'object') {
result[name] = mergeConfigs(original[name], override[name]);
} else {
result[name] = override[name];
}
}
return result;
}
/*
Use EJS to render "datasources/foo.ejs" to "wwwroot/init/foo.json". Include files should be
stored in "datasources/includes/blah.ejs". You can refer to an include file as:
<%- include includes/foo %>
If you want to pass parameters to the included file, do this instead:
<%- include('includes/foo', { name: 'Cool layer' } %>
and in includes/foo:
"name": "<%= name %>"
*/
gulp.task('render-datasource-templates', function(done) {
var ejs = require('ejs');
var JSON5 = require('json5');
var templateDir = 'datasources';
try {
fs.accessSync(templateDir);
} catch (e) {
// Datasources directory doesn't exist? No problem.
done();
return;
}
fs.readdirSync(templateDir).forEach(function(filename) {
if (filename.match(/\.ejs$/)) {
var templateFilename = path.join(templateDir, filename);
var template = fs.readFileSync(templateFilename,'utf8');
var result = ejs.render(template, null, {filename: templateFilename});
// Remove all new lines. This means you can add newlines to help keep source files manageable, without breaking your JSON.
// If you want actual new lines displayed somewhere, you should probably use <br/> if it's HTML, or \n\n if it's Markdown.
//result = result.replace(/(?:\r\n|\r|\n)/g, '');
var outFilename = filename.replace('.ejs', '.json');
try {
// Replace "2" here with "0" to minify.
result = JSON.stringify(JSON5.parse(result), null, 2);
console.log('Rendered template ' + outFilename);
} catch (e) {
console.warn('Warning: Rendered template ' + outFilename + ' is not valid JSON');
}
fs.writeFileSync(path.join('wwwroot/init', outFilename), new Buffer(result));
}
});
done();
});
gulp.task('watch-datasource-templates', gulp.series('render-datasource-templates', function watchDatasourceTemplates() {
return gulp.watch(['datasources/**/*.ejs','datasources/*.json'], watchOptions, gulp.series('render-datasource-templates'));
}));
gulp.task('sync-terriajs-dependencies', function(done) {
var appPackageJson = require('./package.json');
var terriaPackageJson = require('terriajs/package.json');
syncDependencies(appPackageJson.dependencies, terriaPackageJson);
syncDependencies(appPackageJson.devDependencies, terriaPackageJson);
fs.writeFileSync('./package.json', JSON.stringify(appPackageJson, undefined, ' '));
console.log('TerriaMap\'s package.json has been updated. Now run yarn install.');
done();
});
function syncDependencies(dependencies, targetJson, justWarn) {
for (var dependency in dependencies) {
if (dependencies.hasOwnProperty(dependency)) {
var version = targetJson.dependencies[dependency] || targetJson.devDependencies[dependency];
if (version && version !== dependencies[dependency]) {
if (justWarn) {
console.warn('Warning: There is a version mismatch for ' + dependency + '. This build may fail or hang. You should run `gulp sync-terriajs-dependencies`, then re-run `npm install`, then run gulp again.');
} else {
console.log('Updating ' + dependency + ' from ' + dependencies[dependency] + ' to ' + version + '.');
dependencies[dependency] = version;
}
}
}
}
}
function checkForDuplicateCesium() {
var fse = require('fs-extra');
if (fse.existsSync('node_modules/terriajs-cesium') && fse.existsSync('node_modules/terriajs/node_modules/terriajs-cesium')) {
console.log('You have two copies of terriajs-cesium, one in this application\'s node_modules\n' +
'directory and the other in node_modules/terriajs/node_modules/terriajs-cesium.\n' +
'This leads to strange problems, such as knockout observables not working.\n' +
'Please verify that node_modules/terriajs-cesium is the correct version and\n' +
' rm -rf node_modules/terriajs/node_modules/terriajs-cesium\n' +
'Also consider running:\n' +
' npm run gulp sync-terriajs-dependencies\n' +
'to prevent this problem from recurring the next time you `npm install`.');
throw new PluginError('checkForDuplicateCesium', 'You have two copies of Cesium.', { showStack: false });
}
}
gulp.task('build', gulp.series('render-datasource-templates', 'copy-terriajs-assets', 'build-app'));
gulp.task('release', gulp.series('render-datasource-templates', 'copy-terriajs-assets', 'release-app', 'make-editor-schema'));
gulp.task('watch', gulp.parallel('watch-datasource-templates', 'watch-terriajs-assets', 'watch-app'));
gulp.task('default', gulp.series('lint', 'build'));