-
Notifications
You must be signed in to change notification settings - Fork 10
/
gulpfile.js
694 lines (628 loc) · 20.7 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/// <binding />
var gulp = require('gulp'),
del = require('del'),
eslint = require('gulp-eslint-new'),
//convertEncoding = require('gulp-convert-encoding'),
minify = require('gulp-minify'),
CleanCSS = require('clean-css'),
JSZip = require('jszip'),
jsonlint = require("gulp-jsonlint"),
casperJs = require('gulp-casperjs'),
filter = require('gulp-filter'),
run = require('gulp-run'),
webpack = require('webpack'),
WebpackDevServer = require('webpack-dev-server'),
fs = require('fs'),
fse = require('fs-extra'),
os = require('os'),
{ exec, spawn } = require('child_process'),
path = require('path'),
cheerio = require("cheerio"),
lunr=require('lunr');
////////// Gestión de errores ////////
//var plumber = require('gulp-plumber');
//var gutil = require('gulp-util');
//var gulp_src = gulp.src;
//gulp.src = function () {
// return gulp_src.apply(gulp, arguments)
// .pipe(plumber(function (error) {
// // Output an error message
// gutil.log(gutil.colors.red('Error (' + error.plugin + '): ' + error.message));
// // emit the end event, to properly end the task
// this.emit('end');
// })
// );
//};
////////////////////////////////
var sitnaBuild = {
targetPath: 'dist/',
targetUrl: 'https://sitna.navarra.es/api/dev'
};
function buildCsprojFilter(cb) {
fs.readFile('./API.csproj', 'utf8', (err, data) => {
if (err) throw err;
sitnaBuild.projectFiles = data
.split('\n')
.filter(l => l.includes('<Content '))
.map(l => l.match(/Include="(.+)"/)[1])
.map(l => l.replace(/\\/g, '/'));
cb();
});
}
const walk = function (dir, done) {
let results = [];
fs.readdir(dir, function (err, list) {
if (err) return done(err);
var i = 0;
(function next() {
let file = list[i++];
if (!file) return done(null, results);
file = path.resolve(dir, file);
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
})();
});
};
function copyOnlineLibraries(cb) {
const currentDir = path.resolve('.');
const copyDirSyncRecursive = function (src, dest, callback) {
try {
fse.copySync(src, dest);
walk(src, function (err, list) {
if (err) {
console.error(err);
}
else {
sitnaBuild.projectFiles = sitnaBuild.projectFiles
.concat(list.map(r => '.' + r.replace(currentDir, '').replace(/\\/g, '/')));
if (callback) {
callback();
}
}
});
} catch (err) {
console.error(err);
}
};
copyDirSyncRecursive('node_modules/cesium/Build/Cesium/Workers', sitnaBuild.targetPath + 'lib/cesium/build/Workers', () => {
copyDirSyncRecursive('node_modules/cesium/Build/Cesium/ThirdParty', sitnaBuild.targetPath + 'lib/cesium/build/ThirdParty', () => {
copyDirSyncRecursive('node_modules/cesium/Build/Cesium/Assets', sitnaBuild.targetPath + 'lib/cesium/build/Assets', () => {
copyDirSyncRecursive('node_modules/cesium/Build/Cesium/Widgets', sitnaBuild.targetPath + 'lib/cesium/build/Widgets', cb);
});
});
});
}
//function copyOfflineLibraries(cb) {
// fs.mkdirSync(sitnaBuild.targetPath + 'lib/wkx', { recursive: true });
// fs.copyFile('node_modules/wkx/dist/wkx.min.js', sitnaBuild.targetPath + 'lib/wkx/wkx.min.js', cb);
//}
const spawnProcess = function (cmd, args, cb) {
const ls = spawn(path.resolve('./node_modules/.bin/' + cmd), args);
ls.stdout.on('data', data => console.log(`stdout: ${data}`));
ls.stderr.on('data', data => {
throw data;
});
ls.on('error', err => console.error(`Failed to start subprocess: ${err}`));
ls.on('close', code => {
if (code === 0) {
cb();
}
else {
throw new Error(`child process exited with code ${code}`);
}
});
};
function webpackApi() {
return run('npm run wp_pro').exec();
}
function webpackApiDebug() {
return run('npm run wp_dbg').exec();
}
function webpackSandbox() {
return run('npm run wp_snd').exec();
}
//function bundleCesiumDebug(cb) {
// sitnaBuild.projectFiles.push('lib/cesium/build/cesium-sitna.js');
// spawnProcess('webpack.cmd', ['--config', './webpack/cesium.webpack.config.debug.js'], cb);
//}
//function bundleCesiumRelease(cb) {
// sitnaBuild.projectFiles.push('lib/cesium/build/cesium-sitna.min.js');
// spawnProcess('webpack.cmd', ['--config', './webpack/cesium.webpack.config.js'], cb);
//}
function npmUpdate(cb) {
exec('npm update', {}, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
cb();
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
cb();
});
}
function resources() {
return gulp.src([
'**/*',
'!App_Start/**/*',
'!batch/**/*',
'!dist/**/*',
'!doc/**/*',
'!examples/**/*.html',
'!examples/**/*.appcache',
'!examples/tc-cb-service-worker.js',
'!kml/**/*',
'!images/**/*',
'!screenshots/**/*',
'!node_modules/**/*',
'!obj/**/*',
'!Properties/**/*',
'!TC/**/*.js',
'!SITNA/**/*.js',
'!TC/templates/**/*',
'!workers/**/*.js',
'!webpack/**/*.js',
'!lib/**/*.js',
'!lib/ol/**/*',
'!css/**/*.css',
'!css/compiler.js',
'!test/**/*',
'!**/*.cs',
'!*',
'!bin/*'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(sitnaBuild.targetPath));
}
function offlineScripts(cb) {
let target = sitnaBuild.targetPath + 'config/';
gulp.src([
'config/*.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(target));
target = sitnaBuild.targetPath + 'layout/';
gulp.src([
'layout/**/*.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(target));
target = sitnaBuild.targetPath + 'lib/';
gulp.src([
'lib/**/*.min.js',
'!lib/ol/**/*.js',
'!lib/geopackagejs/**/*.js',
'!lib/handlebars/helpers.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(target));
cb();
}
function onlineScripts() {
const minifyConfig = {
ext: {
min: '.min.js'
},
noSource: true,
compress: { sequences: false },
output: { ascii_only: true }
};
let target = sitnaBuild.targetPath + 'TC/cesium/';
gulp.src([
'TC/cesium/**/*.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(target))
.pipe(minify(minifyConfig))
.pipe(gulp.dest(target));
target = sitnaBuild.targetPath + 'lib/';
gulp.src([
'lib/**/*.js',
'!lib/**/*.min.js',
'!lib/ol/**/*.js',
'!lib/handlebars/helpers.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(target));
return gulp.src([
'workers/*-service-worker.js'
])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(sitnaBuild.targetPath + 'workers/'));
}
function compileTemplates(cb) {
require('./TC/templates/compiler.js');
cb();
}
function encapsulateWebWorkers(cb) {
require('./workers/module-creator.js');
cb();
}
function prefetchXsd(cb) {
require('./batch/xsd-prefetch-module-creator.js').then(cb);
}
function examples() {
return gulp.src([
'examples/**/*.html',
'examples/**/*.appcache'
], { removeBOM: false })
.pipe(filter(sitnaBuild.projectFiles))
.pipe(gulp.dest(sitnaBuild.targetPath + 'examples/'));
}
function exampleSW() {
return gulp.src([
'workers/tc-cb-service-worker.js'
])
.pipe(gulp.dest(sitnaBuild.targetPath + 'examples/'));
}
function zipLayout(cb) {
const zip = new JSZip();
const addFile = function (fileName) {
const file = fs.readFileSync('layout/responsive/' + fileName);
zip.file(fileName, file);
};
const addDir = function (dirName) {
const files = fs.readdirSync('layout/responsive/' + dirName);
for (var i = 0, ii = files.length; i < ii; i++) {
addFile(dirName + '/' + files[i]);
}
};
addFile('config.json');
addFile('markup.html');
addFile('markup.html');
addFile('script.js');
addFile('style.css');
addDir('fonts');
addDir('img');
addDir('resources');
zip.generateAsync({ type: "nodebuffer", compression: "DEFLATE" }).then(buffer => {
fs.writeFile(sitnaBuild.targetPath + 'layout/responsive/responsive.zip', buffer, cb);
});
}
function copyLegacyCss(cb) { // compatibilidad hacia atrás
fs.mkdirSync(sitnaBuild.targetPath + 'TC/css', { recursive: true });
fs.copyFileSync(sitnaBuild.targetPath + 'css/bootstrap-adapter.css', sitnaBuild.targetPath + 'TC/css/bootstrap-adapter.css');
fs.copyFile('css/sitna.css', sitnaBuild.targetPath + 'TC/css/tcmap.css', cb);
}
function copyLegacyLayout(cb) { // compatibilidad hacia atrás
try {
fse.copySync(sitnaBuild.targetPath + 'layout/responsive', sitnaBuild.targetPath + 'TC/layout/responsive');
} catch (err) {
console.error(err);
}
cb();
}
function copyLegacyStyleAssets(cb) { // compatibilidad hacia atrás
try {
fse.copySync(sitnaBuild.targetPath + 'css/fonts', sitnaBuild.targetPath + 'TC/css/fonts');
fse.copySync(sitnaBuild.targetPath + 'css/img', sitnaBuild.targetPath + 'TC/css/img');
} catch (err) {
console.error(err);
}
cb();
}
function baseCss(cb) {
require('./css/compiler.js');
fs.mkdirSync(sitnaBuild.targetPath + 'css', { recursive: true });
fs.copyFile('css/sitna.css', sitnaBuild.targetPath + 'css/sitna.css', cb);
}
function cleanCss(file, cb) {
const output = new CleanCSS({
level: 0/*,
format: {
breaks: {
afterBlockBegins: true,
afterRuleEnds: true
}
}*/
}).minify([file]);
fs.writeFile(sitnaBuild.targetPath + file, output.styles, cb);
}
function adapterCss(cb) {
cleanCss('css/bootstrap-adapter.css', cb);
}
function printCss(cb) {
cleanCss('css/print.css', cb);
}
async function componentCss(cb) {
fs.readdir('css/ui', (err, files) => {
if (err) {
console.error(err);
}
else {
fs.mkdirSync(sitnaBuild.targetPath + 'css/ui', { recursive: true });
const cleaner = new CleanCSS({
level: 0/*,
format: {
breaks: {
afterBlockBegins: true,
afterRuleEnds: true
}
}*/
});
files.forEach(f => {
const output = cleaner.minify(['css/ui/' + f]);
fs.writeFileSync(sitnaBuild.targetPath + 'css/ui/' + f, output.styles);
});
cb();
}
});
}
function layoutCss(cb) {
const dirs = fs.readdirSync('layout', { withFileTypes: true });
const cleaner = new CleanCSS({
level: 0/*,
format: {
breaks: {
afterBlockBegins: true,
afterRuleEnds: true
}
}*/
});
dirs.forEach(dir => {
if (dir.isDirectory()) {
fs.mkdirSync(sitnaBuild.targetPath + 'layout/' + dir.name, { recursive: true });
const path = 'layout/' + dir.name + '/style.css';
if (fs.existsSync(path)) {
const output = cleaner.minify([path]);
fs.writeFileSync(sitnaBuild.targetPath + path, output.styles);
}
}
});
cb();
}
function jsonValidate() {
return gulp.src(['TC/**/*.json', 'layout/**/*.json', 'resources/**/*.json', 'config/**/*.json'])
.pipe(filter(sitnaBuild.projectFiles))
.pipe(jsonlint())
.pipe(jsonlint.reporter())
.pipe(jsonlint.failOnError());
}
function textsValidate(cb) {
const es = JSON.parse(fs.readFileSync('resources/es-ES.json', { encoding: 'utf8' }));
const eu = JSON.parse(fs.readFileSync('resources/eu-ES.json', { encoding: 'utf8' }));
const en = JSON.parse(fs.readFileSync('resources/en-US.json', { encoding: 'utf8' }));
for (var esKey in es) {
if (!Object.prototype.hasOwnProperty.call(eu, esKey)) {
throw 'eu-ES no tiene la clave ' + esKey;
}
if (!Object.prototype.hasOwnProperty.call(en, esKey)) {
throw 'en-US no tiene la clave ' + esKey;
}
}
for (var euKey in eu) {
if (!Object.prototype.hasOwnProperty.call(es, euKey)) {
throw 'es-ES no tiene la clave ' + euKey;
}
if (!Object.prototype.hasOwnProperty.call(en, euKey)) {
throw 'en-US no tiene la clave ' + euKey;
}
}
for (var enKey in en) {
if (!Object.prototype.hasOwnProperty.call(es, enKey)) {
throw 'es-ES no tiene la clave ' + enKey;
}
if (!Object.prototype.hasOwnProperty.call(eu, enKey)) {
throw 'eu-ES no tiene la clave ' + enKey;
}
}
cb();
}
function lint() {
return gulp.
src([
'sitna.js',
'TC.js',
'TC/**/*.js',
'SITNA/**/*.js',
'workers/**/*.js'
])
.pipe(eslint())
.pipe(eslint.formatEach());
}
function clean(cb) {
del([
sitnaBuild.targetPath + '**/*',
'!' + sitnaBuild.targetPath
], cb);
}
function unitTests(cb) {
exec(path.resolve('test/unit/browser/runner.html'), cb);
}
function e2eTests() {
return gulp.src('test/endToEnd/test.js').pipe(casperJs());
}
//gulp.task('rasterJSTest', function () {
// const reportDir = 'test/unit/testResults';
// //return gulp.src(['test/unit/browser/layer/Raster.js'], { read: false })
// // .pipe(mocha({
// // reporter: 'mochawesome',
// // reporterOptions: 'reportDir=' + reportDir + ',reportFilename=nodeTestResults'
// // }));
// return del(reportDir + '/**/*', function () {
// var browserStream = mochaPhantomJS({
// reporter: 'spec',
// dump: reportDir + '/browserTestResults.txt'
// })
// browserStream.write({ path: 'http://localhost:56187/test/unit/browser/runner.html' });
// browserStream.end();
// return browserStream;
// });
//});
function docsite(cb) {
spawnProcess('jsdoc.cmd', ['-c', './batch/jsdoc/conf.json'], cb);
}
function docfiles() {
return gulp.src(['./batch/jsdoc/img/*'])
.pipe(gulp.dest(sitnaBuild.targetPath + 'doc/img'));
}
function docCSS() {
return gulp.src(['./batch/jsdoc/css/*'])
.pipe(gulp.dest(sitnaBuild.targetPath + 'doc/css'));
}
function docJS() {
return gulp.src(['./batch/jsdoc/js/*'])
.pipe(gulp.dest(sitnaBuild.targetPath + 'doc/js'));
}
function docExamples(cb) {
const dirs = fs.readdirSync('examples', { withFileTypes: false });
const index=lunr(function () {
this.field('title', { boost: 1000 });
this.field('summary', { boost: 500 });
this.ref('id');
});
const store = {};
dirs.forEach(file => {
if (file.endsWith(".html")) {
try {
const data = fs.readFileSync('examples/' + file, 'utf8');
if (!data) return;
const $ = cheerio.load(data, null, true);
if ($("html title").length && $(".instructions").length) {
const id = file;
store[id] = {
"id": id,
"title": $("html title").text().replace("SITNA - Ejemplo de ", ""),
"summary": $(".instructions:first-child").text()
};
index.add(Object.assign({}, store[id], { "title": store[id]["title"].replace(/\.|\~/gm, " ") }));
}
else
console.log(file + " no es un ejemplo");
}
catch (err) {
console.error(err);
return;
}
}
});
fs.writeFileSync(sitnaBuild.targetPath + "doc/lunr-data.json", JSON.stringify({ index: index, store: store }), "utf8");
cb();
}
const buildCss = gulp.series(
buildCsprojFilter,
baseCss,
adapterCss,
printCss,
componentCss,
layoutCss
);
function generateManifest(cb) {
const basePath = path.resolve(sitnaBuild.targetPath);
walk(sitnaBuild.targetPath, function (err, results) {
if (err) throw err;
const lines = results
.map(r => r.replace(basePath, sitnaBuild.targetUrl).replace(/\\/g, '/'))
.filter(r => {
return !r.startsWith(sitnaBuild.targetUrl + 'doc') &&
!r.startsWith(sitnaBuild.targetUrl + 'errors') &&
!r.startsWith(sitnaBuild.targetUrl + 'examples') &&
!r.startsWith(sitnaBuild.targetUrl + 'maps') &&
!r.startsWith('./TC') &&
!r.startsWith(sitnaBuild.targetUrl + 'workers') &&
!r.endsWith('.map') &&
(!r.startsWith(sitnaBuild.targetUrl + 'layout') || r.startsWith(sitnaBuild.targetUrl + 'layout/responsive'));
});
lines.unshift('');
lines.unshift('CACHE:');
lines.unshift('');
lines.unshift('#' + new Date().toISOString());
lines.unshift('CACHE MANIFEST');
fs.writeFile(sitnaBuild.targetPath + 'manifest.appcache', lines.join(os.EOL), cb);
});
}
let webpackServer;
function startDevServer() {
// Tareas previas para servir correctamente
require('./css/compiler.js');
compileTemplates(() => true);
encapsulateWebWorkers(() => true);
prefetchXsd(() => true);
///////////////////////////////////////////
const webpackConfig = require('./webpack/webpack.config.debug.js');
const compiler = webpack(webpackConfig);
const devServerOptions = { ...webpackConfig.devServer, open: true };
webpackServer = new WebpackDevServer(devServerOptions, compiler);
console.log('Starting server...');
webpackServer.start();
gulp.watch(['./TC/templates/*.hbs'], compileTemplates);
}
const doc = gulp.series(
docsite,
docfiles,
docCSS,
docJS,
docExamples
);
const bundleApi = gulp.parallel(
webpackApi,
webpackApiDebug,
webpackSandbox
);
const parallelTasks = gulp.parallel(
doc,
zipLayout,
examples,
exampleSW
);
const noTests = gulp.series(
clean,
compileTemplates,
encapsulateWebWorkers,
prefetchXsd,
buildCsprojFilter,
//bundleOLDebug,
//bundleOLRelease,
jsonValidate,
textsValidate,
//bundleCesiumDebug,
//bundleCesiumRelease,
baseCss,
adapterCss,
printCss,
componentCss,
layoutCss,
resources,
//copyOfflineLibraries,
offlineScripts,
webpackApi,
generateManifest,
onlineScripts,
copyOnlineLibraries,
copyLegacyCss,
copyLegacyLayout,
copyLegacyStyleAssets,
parallelTasks,
webpackApiDebug,
webpackSandbox
);
exports.startDevServer = startDevServer;
exports.bundleApi = bundleApi;
exports.buildCss = buildCss;
exports.compileTemplates = compileTemplates;
exports.lint = lint;
exports.unitTests = unitTests;
exports.e2eTests = e2eTests;
exports.doc = doc;
exports.copyResources = gulp.series(
buildCsprojFilter,
resources
);
exports.encapsulateWebWorkers = encapsulateWebWorkers;
exports.prefetchXsd = prefetchXsd;
exports.updateDependencies = npmUpdate;
exports.noTests = noTests;
exports.default = gulp.series(
noTests,
//e2eTests,
unitTests
);