forked from iondv/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
713 lines (661 loc) · 21.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
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*eslint "require-jsdoc": off, "no-console": off, "no-sync": off*/
const {series} = require('gulp');
const gulpSrc = require('gulp').src;
const gulpDest = require('gulp').dest;
const assert = require('assert');
const less = require('gulp-less');
const cssMin = require('gulp-clean-css');
const jsMin = require('gulp-jsmin');
const rename = require('gulp-rename');
const spawn = require('child_process').spawn;
const extend = require('extend');
const fs = require('fs');
const path = require('path');
const importer = require('lib/import-meta');
const deployer = require('lib/deploy');
const alias = require('core/scope-alias');
const aclImport = require('lib/aclImport');
const config = require('./config');
const di = require('core/di');
const IonLogger = require('core/impl/log/IonLogger');
const platformPath = path.normalize(__dirname);
const commandExtension = /^win/.test(process.platform) ? '.cmd' : '';
assert.ok(process.env.NODE_PATH,
'\x1b[93;41mThe NODE_PATH must be specified with the path to the application launch directory:\x1b[0m ' + __dirname.toLowerCase());
const nodePath = process.env.NODE_PATH.toLowerCase();
assert.notEqual(nodePath.indexOf(__dirname.toLowerCase()), -1,
'\x1b[93;41mNODE_PATH must contain the path to the application launch directory.\x1b[0m\nNow: ' +
nodePath + '\nMust contain: ' + __dirname.toLowerCase());
/*******************************
* Build and deploy tasks
********************************/
/**
* Initializing the primary application.
* First cleaned up folders and installed all modules.
*/
const build = series(buildBackendNpm, buildFrontend, buildBower, compileLessAll, minifyCssAll, minifyJsAll);
function deploy(done) {
console.log('Deploying and importing the application data.');
/**
* Build configuration setting
* @property {object} log - logging options
* @property {object} bootstrap - boot option
* @property {object} di - data interface settings
*/
let sysLog = new IonLogger(config.log || {});
let scope = null;
const appDir = path.join(platformPath, 'applications');
const applications = fs.readdirSync(appDir);
let apps = [];
let deps = [];
di('boot', config.bootstrap, {sysLog: sysLog}, null, ['rtEvents'])
.then(scope =>
di(
'app',
extend(true, config.di, scope.settings.get('plugins') || {}),
{},
'boot',
['auth', 'background', 'sessionHandler', 'scheduler', 'application']
)
)
.then(scope => alias(scope, scope.settings.get('di-alias')))
/**
* @param {Object} scp
* @param {Object} scp.dataSources
* @param {SettingsRepository} scp.settings
*/
.then((scp) => {
scope = scp;
let stage1 = Promise.resolve();
try {
if (!applications.length) {
console.log('No applications to install.');
return scp.dataSources.disconnect();
}
let appCount = 0;
let first = true;
applications.forEach((app) => {
let pth = path.join(appDir, app);
let stat = fs.statSync(pth);
if (stat.isDirectory()) {
appCount++;
stage1 = stage1.then(() =>
deployer(
pth,
first ?
{resetSettings: true, preserveModifiedSettings: true, settings: scp.settings} :
{settings: scp.settings}
)
.then((dep) => {
first = false;
console.log('The application is configured' + app);
apps.push(app);
deps.push(dep);
})
);
}
});
return stage1.then(() => {
console.log(appCount ? 'All applications where installed.' : 'No applications to install.');
return scp.dataSources.disconnect();
});
} catch (err) {
return Promise.reject(err);
}
})
.then(() =>
di('boot', config.bootstrap, {sysLog: sysLog}, null, ['rtEvents'])
.then(scope =>
di(
'app',
di.extract(
['dbSync', 'metaRepo', 'dataRepo', 'workflows', 'sequenceProvider', 'roleAccessManager', 'auth'],
extend(true, config.di, scope.settings.get('plugins') || {})
),
{},
'boot',
['application']
)
)
.then(scope => alias(scope, scope.settings.get('di-alias')))
)
.then((scp) => {
scope = scp;
let stage2 = Promise.resolve();
for (let i = 0; i < apps.length; i++) {
let stat = fs.statSync(path.join(appDir, apps[i]));
if (stat.isDirectory()) {
stage2 = stage2.then(appImporter(path.join(appDir, apps[i]), scope, sysLog, deps[i]));
}
}
return stage2.then(() => {
console.log('The meta application import is completed.');
});
})
.then(() => scope.dataSources.disconnect().catch(err => console.error(err)))
.then(() => done())
.catch(err => done(err));
}
// App build and deply
const assemble = series(build, deploy);
function compileLessAll (done) {
let themes = themeDirs();
let start = null;
for (let i = 0; i < themes.length; i++) {
if (start) {
start = start.then(compileLess(themes[i]));
} else {
start = compileLess(themes[i])();
}
}
if (!start) {
start = Promise.resolve();
}
start.then(function () {
done();
})
.catch(function (err) {
console.error(err);
done(err);
});
}
function minifyCssAll(done) {
let themes = themeDirs();
let start = null;
for (let i = 0; i < themes.length; i++) {
if (start) {
start = start.then(minifyCSS(themes[i]));
} else {
start = minifyCSS(themes[i])();
}
}
if (!start) {
start = Promise.resolve();
}
start
.then(done)
.catch(function (err) {
console.error(err);
done(err);
});
}
function minifyJsAll(done) {
let themes = themeDirs();
let start = null;
for (let i = 0; i < themes.length; i++) {
if (start) {
start = start.then(minifyJS(themes[i]));
} else {
start = minifyJS(themes[i])();
}
}
if (!start) {
start = Promise.resolve();
}
start
.then(done)
.catch(function (err) {
console.error(err);
done(err);
});
}
function buildBackendNpm(done) {
buildDir(buildDir(npm(platformPath)(), 'modules'), 'applications')
.then(done)
.catch((err) => {
console.error(err);
done(err);
});
}
function buildFrontend(done) {
const themes = themeDirs();
let start = Promise.resolve();
for (let i = 0; i < themes.length; i++)
start = start.then(frontendInstall(themes[i]));
start.then(done)
.catch((err) => {
console.error(err);
done(err);
});
}
function buildBower(done) {
const themes = themeDirs();
let start = Promise.resolve();
for (let i = 0; i < themes.length; i++)
start = start.then(bowerInstall(themes[i]));
start.then(done)
.catch((err) => {
console.error(err);
done(err);
});
}
/*******************************
* Service function
********************************/
function npm(pathDir) {
return function () {
return new Promise(function (resolve, reject) {
let npmArgs = ['install', '--no-save', '--prefer-offline']; // TODO '--only=prod' if use - delete gulp in devDependce
try {
fs.accessSync(path.join(pathDir, 'package-lock.json'));
console.log('Installing CI the backend packages for the path ' + pathDir); // TODO '--only=prod' if use - delete gulp in devDependce
npmArgs = ['ci', '--prefer-offline'];
} catch (error) {
console.log('Installing the backend packages for the path ' + pathDir);
}
run(pathDir, 'npm', npmArgs, resolve, reject);
});
};
}
function frontendInstall(pathDir) {
return function () {
return new Promise(function (resolve, reject) {
try {
fs.accessSync(path.join(pathDir, 'package.json'));
} catch (error) {
resolve();
return;
}
try {
/**
* Configuration parameters
* @property {String} vendorDir - package installation folder
*/
let packageJson = JSON.parse(fs.readFileSync(path.join(pathDir, 'package.json'), {encoding: 'utf-8'}));
if (!packageJson.vendorDir) {
console.warn('In the package.json the destination directory for vendor files is not specified in [vendorDir]!\nSet default ./static/vendor');
packageJson.vendorDir = './static/vendor';
}
let npmArgs = ['install', '--only=prod', '--no-save', '--prefer-offline'];
try {
fs.accessSync(path.join(pathDir, 'package-lock.json'));
console.log('Installing CI the frontend packages for the path ' + pathDir);
npmArgs = ['ci', '--only=prod', '--prefer-offline'];
} catch (error) {
console.log('Installing the frontend packages for the path ' + pathDir);
}
run(pathDir, 'npm', npmArgs, function () {
const srcDir = path.join(pathDir, 'node_modules');
try {
fs.accessSync(srcDir);
} catch (err) {
resolve();
return;
}
try {
const vendorModules = fs.readdirSync(srcDir);
let copyers = [];
let copyer;
for (let i = 0; i < vendorModules.length; i++) {
copyer = copyVendorResources(srcDir, path.join(pathDir, packageJson.vendorDir), vendorModules[i]);
if (copyer) {
copyers.push(copyer);
}
}
if (copyers.length) {
return Promise.all(copyers).then(()=>{resolve()}).catch(reject); // Gulp didn't wait array of promise result
}
} catch (error) {
return reject(error);
}
resolve();
}, reject);
} catch (error) {
reject(error);
}
});
};
}
function bowerInstall(pathDir) {
return function () {
return new Promise(function (resolve, reject) {
try {
fs.accessSync(path.join(pathDir, '.bowerrc'));
} catch (error) {
resolve();
return;
}
try {
/**
* Bower configuration parameters
* @property {String} vendorDir - package installation folder
*/
let bc = JSON.parse(fs.readFileSync(path.join(pathDir, '.bowerrc'), {encoding: 'utf-8'}));
console.warn('DEPRICATED installing the bower packages for the path ' + pathDir + ' use npm');
run(pathDir, 'bower', ['install', '--config.interactive=false', '--allow-root', '--quiet'], function () {
let srcDir = path.join(pathDir, bc.directory);
try {
fs.accessSync(srcDir);
} catch (err) {
resolve();
return;
}
try {
let vendorModules = fs.readdirSync(srcDir);
let copyers, copyer;
copyers = [];
if (bc.vendorDir) {
for (let i = 0; i < vendorModules.length; i++) {
copyer = copyVendorResources(srcDir, path.join(pathDir, bc.vendorDir), vendorModules[i]);
if (copyer) {
copyers.push(copyer);
}
}
} else {
console.warn('In the .bowerrc the destination directory for vendor files is not specified in!');
}
if (copyers.length) {
return Promise.all(copyers).then(()=>{resolve()}).catch(reject); // Gulp didn't wait array of promise result
}
} catch (error) {
return reject(error);
}
resolve();
}, reject);
} catch (error) {
reject(error);
}
});
};
}
function copyVendorResources(src, dst, module) {
return new Promise(function (resolve, reject) {
let dist = path.join(src, module, 'dist');
let min = path.join(src, module, 'min');
let dest = path.join(dst, module);
copyResources(
dist,
dest,
'Copied distribution files of vendor package ' + module
)(false).then(copyResources(
min,
dest,
'Copied minified files vendor package ' + module
)
).then(copyResources(
path.join(src, module),
dest,
'Copied vendor package files ' + module
)
).then(resolve).catch(reject);
});
}
function copyResources(srcPath, destPath, msg) {
return function (skip) {
return new Promise(function (resolve, reject) {
if (!skip) {
try {
fs.accessSync(srcPath);
} catch (err) {
resolve(false);
return;
}
gulpSrc([path.join(srcPath, '**', '*')])
.pipe(gulpDest(destPath))
.on('finish', function () {
console.log(msg);
resolve(true);
})
.on('error', reject);
} else {
resolve(true);
}
});
};
}
function compileLess(p) {
return function () {
return new Promise(function (resolve, reject) {
if (!fs.existsSync(path.join(p, 'less'))) {
return resolve();
}
console.log('Compiling less files for the path ' + p);
try {
gulpSrc([path.join(p, 'less', '*.less')])
.pipe(less({
paths: [path.join(p, 'less', '*.less')]
}))
.pipe(rename({
suffix: '.less'
}))
.pipe(gulpDest(path.join(p, 'static', 'css')))
.on('finish', resolve)
.on('error', reject);
} catch (error) {
reject(error);
}
});
};
}
function minifyCSS(p) {
return function () {
return new Promise(function (resolve, reject) {
if (!fs.existsSync(path.join(p, 'static', 'css'))) {
return resolve();
}
console.log('Minification of front-end style files for the path ' + p);
try {
gulpSrc([
path.join(p, 'static', 'css', '*.css'),
'!' + path.join(p, 'static', 'css', '*.min.css')
], {base: path.join(p, 'static', 'css')})
.pipe(cssMin({rebase: false}))
.pipe(rename({suffix: '.min'}))
.pipe(gulpDest(path.join(p, 'static', 'css')))
.on('finish', resolve)
.on('error', reject);
} catch (error) {
reject(error);
}
});
};
}
function minifyJS(p) {
return function () {
return new Promise(function (resolve, reject) {
if (!fs.existsSync(path.join(p, 'static', 'js'))) {
return resolve();
}
console.log('Minification frontend script files for the path ' + p);
try {
gulpSrc(
[
path.join(p, 'static', 'js', '*.js'),
'!' + path.join(p, 'static', 'js', '*.min.js')
], {base: path.join(p, 'static', 'js')})
.pipe(jsMin())
.pipe(rename({suffix: '.min'}))
.pipe(gulpDest(path.join(p, 'static', 'js')))
.on('finish', resolve)
.on('error', reject);
} catch (error) {
reject(error);
}
});
};
}
function run(path, command, args, resolve, reject) {
try {
let child = spawn(command + commandExtension,
args,
{
cwd: path,
env: process.env
}
);
child.stdout.on('data', function (data) {
console.log(data.toString('utf-8'));
});
child.stderr.on('data', function (data) {
console.error(data.toString('utf-8'));
});
child.on('close', function (code) {
if (code !== 0) {
return reject('child process failed with code ' + code);
}
return resolve();
});
} catch (error) {
reject(error);
}
}
function buildDir(start, dir) {
let modulesDir = path.join(platformPath, dir);
let modules = fs.readdirSync(modulesDir);
let stat;
let f = start;
for (let i = 0; i < modules.length; i++) {
stat = fs.statSync(path.join(modulesDir, modules[i]));
if (stat.isDirectory()) {
f = f.then(npm(path.join(modulesDir, modules[i])));
}
}
return f;
}
function themeDirs() {
let themes = _themeDirs(path.join(platformPath, 'view'));
let pth = path.join(platformPath, 'modules');
let tmp = fs.readdirSync(pth);
tmp.forEach(function (dir) {
let module = path.join(pth, dir);
let stat = fs.statSync(module);
if (stat.isDirectory()) {
themes.push(path.join(module, 'view'));
Array.prototype.push.apply(themes, _themeDirs(path.join(module, 'view')));
}
});
pth = path.join(platformPath, 'applications');
tmp = fs.readdirSync(pth);
tmp.forEach(function (dir) {
let module = path.join(pth, dir);
let stat = fs.statSync(module);
if (stat.isDirectory()) {
let themesDir = path.join(module, 'themes');
if (fs.existsSync(themesDir)) {
Array.prototype.push.apply(themes, _themeDirs(themesDir));
} else {
themes.push(module);
}
}
});
return themes;
}
function _themeDirs(basePath) {
let themes = [];
if (fs.existsSync(basePath)) {
let tmp = fs.readdirSync(basePath);
tmp.forEach(function (dir) {
let theme = path.join(basePath, dir);
let stat = fs.statSync(theme);
if (stat.isDirectory()) {
themes.push(theme);
}
});
}
return themes;
}
/**
* Import of the application data
* @param {string} appDir application catalog
* @param scope
* @param scope.roleAccessManager
* @param scope.auth
* @param scope.dataRepo
* @param scope.dbSync
* @param {Function} log - logger
* @param dep
* @returns {Function}
*/
function appImporter(appDir, scope, log, dep) {
return function () {
let ns = dep ? dep.namespace || '' : '';
console.log('Application meta from ' + appDir + ' is imported to ' +
(ns ? 'namespace ' + ns : 'global namespace'));
return aclImport(path.join(appDir, 'acl'), scope.roleAccessManager, log, scope.auth)
.catch(err => log.error(err))
.then(() => importer(appDir, {
sync: scope.dbSync,
metaRepo: scope.metaRepo,
// dataRepo: scope.dataRepo,
// workflows: scope.workflows,
// sequences: scope.sequenceProvider,
log: log,
namespace: ns //,
// Ignoring integrity monitoring, otherwise reference attributes are deleted, because objects are referenced,
// not yet imported
// ignoreIntegrityCheck: true
})).then(() => {
console.log('Meta from ' + appDir + ' was imported to database');
});
};
}
exports.build = build;
exports.deploy = deploy;
exports.assemble = assemble;
exports.default = assemble;
exports.buildBackendNpm = buildBackendNpm;
exports.buildFrontend = buildFrontend;
exports.buildBower = buildBower;
/*****************
* TODO check metada - gulp 3.9.1
********************/
// const jsonSchema = require('gulp-json-schema');
// const runSequence = require('run-sequence');
// const path = require('path');
// const join = path.join;
//
// const pathSchema = './test/meta-schema';
// const pathApplications = './applications/';
//
// const schemaOptions = {
// emitError: false,
// verbose: true,
// banUnknownProperties: true,
// checkRecursive: true,
// schemas: [join(pathSchema, 'command.schema.json'), join(pathSchema, 'view-properties.schema.json')],
// missing: 'error'};
// // validation parameters: https://www.npmjs.com/package/gulp-json-schema#options,
// // regarding schemas, perhaps it makes sense to drag a nested schemas in a separate folder, and then pull it entirely,
// // haven't searched for the options "how to do it" yet *
// gulp.task('validate:meta', function (done) { // Without runSequence, the output goes to the stack, consistently more convenient
// runSequence(
// ['validate:meta:class'],
// ['validate:meta:view-create&item'],
// ['validate:meta:view-list'],
// ['validate:meta:navigation-section'],
// ['validate:meta:navigation-unit'],
// function () {
// done();
// });
// });
//
// gulp.task('validate:meta:class', function () {
// console.log('Check the meta class.');
// return gulp.src([join(pathApplications, '*/meta/*.json')])
// .pipe(jsonSchema(join(pathSchema, 'class.main.schema.json'), schemaOptions));
// });
//
// gulp.task('validate:meta:view-create&item', function () {
// console.log('Check the create and edit views of the meta view.');
// return gulp.src([join(pathApplications, '*/views/*/create.json'),
// join(pathApplications, '*/views/*/item.json')])
// .pipe(jsonSchema(join(pathSchema, 'view-createnitem.main.schema.json'), schemaOptions));
// });
//
// gulp.task('validate:meta:view-list', function () {
// console.log('Check the list view of the meta view.');
// return gulp.src([join(pathApplications, '*/views/*/list.json')])
// .pipe(jsonSchema(join(pathSchema, 'view-list.main.schema.json'), schemaOptions));
// });
//
// gulp.task('validate:meta:navigation-section', function () {
// console.log('Check the meta navigation section.');
// return gulp.src([join(pathApplications, '*/navigation/*.section.json')])
// .pipe(jsonSchema(join(pathSchema, 'navigation-section.main.schema.json'), schemaOptions));
// });
//
// gulp.task('validate:meta:navigation-unit', function () {
// console.log('Check the meta navigation node.');
// return gulp.src([join(pathApplications, '*/navigation/*/*.json')])
// .pipe(jsonSchema(join(pathSchema, 'navigation-unit.main.schema.json'), schemaOptions));
// });