forked from nrenner/brouter-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
394 lines (357 loc) · 11.2 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
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 debug = false;
var paths = {
// see overrides in package.json
scriptsConfig: mainNpmFiles().filter(f =>
RegExp('url-search-params/.*\\.js', 'i').test(f)
),
scripts: [
'node_modules/jquery/dist/jquery.js',
'node_modules/tether/dist/js/tether.js',
'node_modules/async/lib/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)
)
)
.concat([
'js/Browser.js',
'js/Util.js',
'js/Map.js',
'js/LayersConfig.js',
'js/router/BRouter.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)
),
locales: 'locales/*.json',
layers: 'layers/**/*.geojson',
layersDestName: 'layers.js',
layersConfig: [
'layers/config/config.js',
'layers/config/tree.js',
'layers/config/overrides.js',
'layers/config/geometry.js'
],
layersConfigDestName: 'layersConf.js',
dest: 'dist',
destName: 'brouter-web'
};
gulp.task('clean', function(cb) {
del(paths.dest + '/**/*', cb);
});
// libs that require loading before config.js
gulp.task('scripts_config', function() {
// just copy for now
return gulp.src(paths.scriptsConfig).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, 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('watch', function() {
debug = true;
var watcher = gulp.watch(paths.scripts, gulp.series('scripts'));
watcher.on('change', function(event) {
if (event.type === 'deleted') {
delete cached.caches.scripts[event.path];
remember.forget('scripts', event.path);
}
});
gulp.watch(paths.styles, gulp.series('styles'));
gulp.watch(paths.layersConfig, gulp.series('layers_config'));
});
// Print paths to console, for manually debugging the gulp build
// (comment out corresponding line of paths to print)
gulp.task('log', function() {
//return gulp.src(paths.scripts)
//return gulp.src(paths.styles)
//return gulp.src(paths.images)
// return gulp.src(paths.locales)
return gulp
.src(
paths.scripts
.concat(paths.styles)
.concat(paths.images)
.concat(paths.locales)
)
.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 tags = { patch: 'patch', minor: 'minor', major: 'major' };
var nextVersion;
var ghToken;
gulp.task('release:init', function(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'));
}
ghToken = gutil.env.token;
if (!ghToken) {
return cb(
new Error('--token is required (github personnal access token')
);
}
if (ghToken.length != 40) {
return cb(new Error('--token length must be 40'));
}
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() {
return gulp
.src('./index.html')
.pipe(
replace(
/<sup class="version">(.*)<\/sup>/,
'<sup class="version">' + nextVersion + '</sup>'
)
)
.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',
'scripts',
'styles',
'images',
'fonts',
'locales'
)
);
gulp.task(
'debug',
gulp.series(function(cb) {
debug = true;
cb();
}, 'default')
);
gulp.task('release:zip', function() {
gutil.log(gutil.colors.green('Build brouter-web.' + nextVersion + '.zip'));
return gulp
.src(
['dist/**', 'index.html', 'config.template.js', 'keys.template.js'],
{
base: '.'
}
)
.pipe(zip('brouter-web.' + nextVersion + '.zip'))
.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'
)
);