UNPKG

3.19 kBJavaScriptView Raw
1/* Copyright 2017 Agneta Network Applications, LLC.
2 *
3 * Source file: main/start.js
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17const _ = require('lodash');
18const paths = require('./paths');
19const config = require('./config');
20const Promise = require('bluebird');
21
22_.mixin(require('lodash-deep'));
23_.omitDeep = function(collection, excludeKeys) {
24
25 function omitFn(value) {
26
27 if (value && typeof value === 'object') {
28 excludeKeys.forEach((key) => {
29 delete value[key];
30 });
31 }
32 }
33
34 return _.cloneDeepWith(collection, omitFn);
35
36};
37
38
39var start = {
40 init: function(subApps) {
41
42 return Promise.each(subApps, function(component) {
43 if (component.preInit) {
44 return component.preInit();
45 }
46 })
47 .then(function() {
48 return Promise.each(subApps, function(component) {
49 console.log('Initiating: ' + component.locals.app.get('name'));
50
51 if (component.init) {
52 return component.init();
53 }
54 });
55 })
56 .then(function() {
57 return Promise.each(subApps, function(component) {
58 console.log('Starting: ' + component.locals.app.get('name'));
59 if (component.start) {
60 return component.start();
61 }
62 return null;
63 });
64 });
65 },
66 default: function(options) {
67
68 var component = start.pages({
69 mode: 'default',
70 locals: options
71 });
72
73 return component;
74
75 },
76 portal: function(options) {
77
78 options.includeSources = [{
79 name: 'portal',
80 path: paths.appPortal.source
81 }];
82
83 var component = start.pages({
84 mode: 'preview',
85 dir: paths.portal.base,
86 locals: options
87 });
88
89 setName(component, 'pages_portal', options);
90 return component;
91 },
92 website: function(options) {
93
94 var component = start.pages({
95 mode: 'preview',
96 sync: true,
97 locals: options
98 });
99
100 setName(component, 'pages_website', options);
101 return component;
102 },
103 pages: function(options) {
104 options.paths = paths.loadApp(options);
105 return getComponent('pages', '../pages', options);
106 },
107 services: function(options) {
108
109 options.paths = paths.loadApp(options);
110 return getComponent('services', paths.core.services, options);
111
112 }
113};
114
115function getComponent(name, componentPath, options) {
116
117 _.extend(options, config);
118
119 var component = require(componentPath)(options);
120
121 setName(component, name, options);
122
123 return component;
124
125}
126
127function setName(component, name, options) {
128 options = options || {};
129 if (options.id) {
130 name += '_' + options.id;
131 }
132 component.locals.app.set('name', name);
133
134}
135
136module.exports = start;