-
Notifications
You must be signed in to change notification settings - Fork 75
/
gulpfile.js
480 lines (427 loc) · 15.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
var gulp = require('gulp');
var concat = require('gulp-concat');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var gulpDebug = require('gulp-debug');
var mainNpmFiles = require('npmfiles');
var del = require('del');
var path = require('path');
var cached = require('gulp-cached');
var remember = require('gulp-remember');
var inject = require('gulp-inject');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var zip = require('gulp-zip');
var bump = require('gulp-bump');
var semver = require('semver');
var git = require('gulp-git');
var replace = require('gulp-replace');
var release = require('gulp-github-release');
var cleanCSS = require('gulp-clean-css');
var modifyCssUrls = require('gulp-modify-css-urls');
var sort = require('gulp-sort');
var scanner = require('i18next-scanner');
var jsonConcat = require('gulp-json-concat');
var rename = require('gulp-rename');
var browserSync = require('browser-sync');
var merge = require('merge-stream');
var babel = require('gulp-babel');
var { marked } = require('marked');
var fs = require('fs');
const server = browserSync.create();
var debug = false;
var paths = {
// see overrides in package.json
scriptsConfig: mainNpmFiles()
.filter(
(f) =>
// index.html
RegExp('url-search-params/.*\\.js', 'i').test(f) ||
RegExp('core-js-bundle/.*\\.js', 'i').test(f) ||
RegExp('regenerator-runtime/.*\\.js', 'i').test(f) ||
// dynamic import in MaplibreGlLazyLoader
RegExp('maplibre-gl/.*\\.js', 'i').test(f) ||
RegExp('@maplibre/maplibre-gl-leaflet/.*\\.js', 'i').test(f)
)
.concat([
// large lib as extra file for faster parallel loading (*.min.js already excluded from bundle)
'node_modules/@turf/turf/turf.min.js',
]),
scripts: [
'node_modules/jquery/dist/jquery.js',
'node_modules/async/dist/async.js',
'node_modules/leaflet/dist/leaflet-src.js',
]
.concat(
mainNpmFiles().filter(
(f) =>
RegExp('.*\\.js', 'i').test(f) &&
!RegExp('.*\\.min\\.js', 'i').test(f) &&
!RegExp('url-search-params/.*\\.js', 'i').test(f) &&
!RegExp('core-js-bundle/.*\\.js', 'i').test(f) &&
!RegExp('regenerator-runtime/.*\\.js', 'i').test(f) &&
!RegExp('maplibre-gl/.*\\.js', 'i').test(f) &&
!RegExp('@maplibre/maplibre-gl-leaflet/.*\\.js', 'i').test(f)
)
)
.concat([
'js/Browser.js',
'js/WhatsNew.js',
'js/Util.js',
'js/Map.js',
'js/LayersConfig.js',
'js/router/BRouter.js',
'js/util/*.js',
'js/format/*.js',
'js/plugin/*.js',
'js/control/*.js',
'js/index.js',
]),
styles: mainNpmFiles()
.filter((f) => RegExp('.*\\.css', 'i').test(f) && !RegExp('.*\\.min\\.css', 'i').test(f))
.concat('css/*.css'),
images: mainNpmFiles().filter((f) => RegExp('.*.+(png|gif|svg)', 'i').test(f)),
fonts: mainNpmFiles().filter((f) => RegExp('font-awesome/fonts/.*', 'i').test(f)),
changelog: 'CHANGELOG.md',
locales: 'locales/*.json',
layers: ['layers/**/*.geojson', 'layers/**/*.json'],
layersDestName: 'layers.js',
layersConfig: [
'layers/config/config.js',
'layers/config/tree.js',
'layers/config/overrides.js',
'layers/config/geometry.js',
],
layersConfigDestName: 'layersConf.js',
boundaries: ['resources/boundaries/*.geojson', 'resources/boundaries/*.topo.json'],
zip: ['dist/**', 'index.html', 'config.template.js', 'keys.template.js'],
dest: 'dist',
destName: 'brouter-web',
};
gulp.task('clean', function () {
return del(paths.dest + '/**/*');
});
// libs that require loading before config.js
gulp.task('scripts_config', function () {
// just copy for now
return gulp
.src(paths.scriptsConfig)
.pipe(
rename(function (path) {
if (path.basename === 'minified') {
path.basename = 'core-js-bundle.min';
} else if (path.basename === 'runtime') {
path.basename = 'regenerator-runtime';
}
})
)
.pipe(replace('//# sourceMappingURL=minified.js.map', ''))
.pipe(gulp.dest(paths.dest));
});
gulp.task('scripts', function () {
if (debug) gutil.log(gutil.colors.yellow('Running in Debug mode'));
else gutil.log(gutil.colors.green('Running in Release mode'));
return gulp
.src(paths.scripts, { base: '.' })
.pipe(sourcemaps.init())
.pipe(cached('scripts'))
.pipe(gulpif(!debug, babel({ caller: { supportsDynamicImport: true } })))
.pipe(gulpif(!debug, uglify()))
.pipe(remember('scripts'))
.pipe(concat(paths.destName + '.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dest));
});
// separate, fallback task for debugging (switch manually in index.html)
gulp.task('concat', function () {
return gulp
.src(paths.scripts)
.pipe(concat(paths.destName + '.src.js'))
.pipe(gulp.dest(paths.dest));
});
gulp.task('styles', function () {
return gulp
.src(paths.styles)
.pipe(
modifyCssUrls({
modify(url, filePath) {
var distUrl = url;
var imageExt = ['.png', '.gif', '.svg'];
if (imageExt.indexOf(path.extname(url)) !== -1) {
distUrl = 'images/' + path.basename(url);
} else if (url.indexOf('font') !== -1) {
distUrl = 'fonts/' + path.basename(url);
}
return distUrl;
},
})
)
.pipe(concat(paths.destName + '.css'))
.pipe(
cleanCSS({
rebase: false,
})
)
.pipe(postcss([autoprefixer({ remove: false })]))
.pipe(gulp.dest(paths.dest));
});
gulp.task('images', function () {
return gulp.src(paths.images).pipe(gulp.dest(paths.dest + '/images'));
});
gulp.task('fonts', function () {
return gulp.src(paths.fonts).pipe(gulp.dest(paths.dest + '/fonts'));
});
gulp.task('locales', function () {
return gulp.src(paths.locales).pipe(gulp.dest(paths.dest + '/locales'));
});
gulp.task('boundaries', function () {
return gulp.src(paths.boundaries).pipe(gulp.dest(paths.dest + '/boundaries'));
});
gulp.task('changelog', function (cb) {
var content = 'BR.changelog = `' + marked(fs.readFileSync(paths.changelog, 'utf-8')) + '`';
content = content.replace(/<h1.*<\/h1>/i, '');
fs.writeFile(paths.dest + '/changelog.js', content, cb);
});
gulp.task('reload', function (done) {
server.reload();
done();
});
gulp.task('watch', function () {
debug = true;
var watcher = gulp.watch(paths.scripts, gulp.series('scripts', 'reload'));
watcher.on('change', function (event) {
if (event.type === 'deleted') {
delete cached.caches.scripts[event.path];
remember.forget('scripts', event.path);
}
});
gulp.watch(paths.changelog, gulp.series('changelog', 'reload'));
gulp.watch(paths.locales, gulp.series('locales', 'reload'));
gulp.watch(paths.styles, gulp.series('styles', 'reload'));
gulp.watch(paths.layers, gulp.series('layers', 'reload'));
gulp.watch(paths.layersConfig, gulp.series('layers_config', 'reload'));
gulp.watch(['./index.html'].concat(paths.images).concat(paths.fonts).concat(paths.locales), gulp.series('reload'));
});
// Print paths to console, for manually debugging the gulp build
// (comment out corresponding line of paths to print)
gulp.task('log', function () {
// var src = paths.scriptsConfig;
var src = paths.scripts.concat(paths.styles).concat(paths.images).concat(paths.locales);
return gulp.src(src).pipe(gulpDebug());
});
gulp.task('inject', function () {
var target = gulp.src('index.html');
var sources = gulp.src(paths.scripts.concat(paths.styles), {
base: '.',
read: false,
});
return target.pipe(inject(sources, { relative: true })).pipe(gulp.dest('.'));
});
var pkg = require('./package.json');
var nextVersion = pkg.version;
var ghToken = gutil.env.token;
gulp.task('release:init', function (cb) {
if (!ghToken) {
return cb(new Error('--token is required (github personal access token'));
}
if (ghToken.length != 40) {
return cb(new Error('--token length must be 40, not ' + ghToken.length));
}
if (gutil.env.skipnewtag) {
return cb();
}
var tag = gutil.env.tag;
if (!tag) {
return cb(new Error('--tag is required'));
}
if (['major', 'minor', 'patch'].indexOf(tag) < 0) {
return cb(new Error('--tag must be major, minor or patch'));
}
nextVersion = semver.inc(pkg.version, tag);
git.status({ args: '--porcelain', quiet: true }, function (err, stdout) {
if (err) return cb(err);
if (stdout.length > 0) {
return cb(new Error('Repository is not clean. Please commit or stash your pending modification'));
}
cb();
});
});
gulp.task('bump:json', function () {
gutil.log(gutil.colors.green('Bump to ' + nextVersion));
return gulp
.src(['./package.json'])
.pipe(bump({ version: nextVersion }))
.pipe(gulp.dest('./'));
});
gulp.task('bump:html', function () {
const version = nextVersion || pkg.version;
return gulp
.src('./index.html')
.pipe(replace(/<sup class="version">(.*)<\/sup>/, '<sup class="version">' + version + '</sup>'))
.pipe(replace(/BR.version = '(.*?)';/, "BR.version = '" + version + "';"))
.pipe(gulp.dest('.'));
});
gulp.task('bump', gulp.series('bump:json', 'bump:html'));
gulp.task('release:commit', function () {
return gulp.src(['./index.html', './package.json']).pipe(git.commit('release: ' + nextVersion));
});
gulp.task('release:tag', function (cb) {
return git.tag(nextVersion, '', cb);
});
gulp.task('release:push', function (cb) {
git.push('origin', 'master', { args: '--tags' }, cb);
});
gulp.task('i18next', function () {
return gulp
.src(['index.html', 'locales/keys.js', 'layers/config/overrides.js', 'js/**/*.js'])
.pipe(sort())
.pipe(
scanner({
lngs: ['en'], // we only generate English version, other languages are handled by transifex via yarn transifex-pull/push
removeUnusedKeys: true,
sort: true,
resource: {
// the source path is relative to current working directory
loadPath: 'locales/{{lng}}.json',
// the destination path is relative to your `gulp.dest()` path
savePath: 'locales/{{lng}}.json',
},
})
)
.pipe(gulp.dest('.'));
});
gulp.task('layers_config', function () {
return gulp.src(paths.layersConfig).pipe(concat(paths.layersConfigDestName)).pipe(gulp.dest(paths.dest));
});
// Bundles layer files. To download and extract run "yarn layers"
gulp.task('layers', function () {
return (
gulp
.src(paths.layers)
// Workaround to get file extension removed from the dictionary key
.pipe(rename({ extname: '.json' }))
.pipe(
jsonConcat(paths.layersDestName, function (data) {
var header =
'// Licensed under the MIT License (https://github.com/nrenner/brouter-web#license + Credits and Licenses),\n' +
'// except JOSM imagery database (dataSource=JOSM) is licensed under Creative Commons (CC-BY-SA),\n' +
'// see https://josm.openstreetmap.de/wiki/Maps#Otherimportantinformation\n';
return Buffer.from(header + 'BR.layerIndex = ' + JSON.stringify(data, null, 2) + ';');
})
)
.pipe(gulp.dest(paths.dest))
);
});
gulp.task(
'default',
gulp.series(
'clean',
'scripts_config',
'layers_config',
'layers',
'bump:html',
'scripts',
'styles',
'images',
'fonts',
'locales',
'boundaries',
'changelog'
)
);
gulp.task(
'debug',
gulp.series(function (cb) {
debug = true;
cb();
}, 'default')
);
gulp.task(
'serve',
gulp.series('debug', function (cb) {
server.init({
server: {
baseDir: './',
},
open: false,
});
cb();
})
);
gulp.task('release:zip', function () {
gutil.log(gutil.colors.green('Build brouter-web.' + nextVersion + '.zip'));
return gulp
.src(paths.zip, {
base: '.',
})
.pipe(zip('brouter-web.' + nextVersion + '.zip'))
.pipe(gulp.dest('.'));
});
gulp.task('release:zip_standalone', function () {
var version = pkg.version;
var destName = 'brouter-web-standalone.' + version + '.zip';
gutil.log(gutil.colors.green('Build ' + destName));
var brouterWeb = gulp
.src(paths.zip, {
base: '.',
})
.pipe(
rename(function (path) {
path.dirname = 'brouter-web/' + path.dirname;
})
);
var root = gulp.src(['resources/standalone/run.sh', 'resources/standalone/segments4']);
var serverRoot = gulp
.src(['docs/developers/profile_developers_guide.md', 'brouter-server/build/libs/brouter-*-all.jar'], {
cwd: path.join(process.cwd(), '../brouter'),
})
.pipe(
rename(function (path) {
if (path.basename.startsWith('brouter-') && path.basename.endsWith('-all')) {
path.basename = 'brouter';
}
})
);
var serverProfiles = gulp.src(
[
'profiles2/**',
'!profiles2/all.brf',
'!profiles2/car-eco-suspect_scan.brf',
'!profiles2/car-traffic_analysis.brf',
'!profiles2/softaccess.brf',
],
{
cwd: path.join(process.cwd(), '../brouter/misc/'),
base: '../brouter/misc/',
}
);
var serverScripts = gulp.src(['standalone/**'], {
cwd: path.join(process.cwd(), '../brouter/misc/scripts/'),
base: '../brouter/misc/scripts/',
});
return merge(brouterWeb, root, serverRoot, serverProfiles, serverScripts).pipe(zip(destName)).pipe(gulp.dest('.'));
});
gulp.task('release:publish', function () {
return gulp.src('./brouter-web.' + nextVersion + '.zip').pipe(
release({
tag: nextVersion,
token: ghToken,
manifest: pkg,
})
);
});
gulp.task(
'release',
gulp.series(
'release:init',
'bump',
'release:commit',
'release:tag',
'release:push',
'default',
'release:zip',
'release:publish'
)
);