1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const _ = require('lodash');
|
18 | const paths = require('./paths');
|
19 | const config = require('./config');
|
20 | const 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 |
|
39 | var 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 |
|
115 | function 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 |
|
127 | function 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 |
|
136 | module.exports = start;
|