UNPKG

6.71 kBJavaScriptView Raw
1const staticConfig = require('./staticConfig'),
2 localization = require('./localization'),
3 weExpress = require('./express');
4
5module.exports = {
6 checkDBConnection(we, next) {
7 we.db.checkDBConnection(we, next);
8 },
9 loadCoreFeatures(we, next) {
10 we.log.verbose('loadCoreFeatures step');
11
12 we.db.loadCoreModels( err => {
13 if(err) return next(err);
14
15 we.pluginManager.loadPluginsSettingsFromDB(we, err => {
16 if (err) return next(err);
17 // preload all plugins
18 we.pluginManager.loadPlugins(we, (err, plugins) => {
19 if (err) return next(err);
20 we.plugins = plugins;
21 next();
22 });
23 });
24 });
25 } ,
26 loadPluginFeatures(we, next) {
27 we.log.verbose('loadPluginFeatures step');
28
29 we.pluginNames = we.pluginManager.pluginNames;
30 // load plugin static configs, merge with old we.config and
31 // override the defalt config
32 we.config = staticConfig.loadPluginConfigs(we);
33 // set add ResponseFormat here for use we.js app
34 we.responses.addResponseFormater = (extension, formater, position)=> {
35 position = (position === 0 || position)? position: we.config.responseTypes.length;
36
37 we.config.responseTypes.splice(position, 0, extension);
38 we.responses.formaters[extension] = formater;
39 };
40
41 we.hooks.trigger('we:before:load:plugin:features', we, (err)=> {
42 if (err) return next(err);
43
44 we.utils.async.eachSeries(we.pluginNames, (pluginName, next)=> {
45 we.plugins[pluginName].loadFeatures(we, next);
46 }, (err) => {
47 if (err) return next(err);
48
49 we.events.emit('we:after:load:plugins', we);
50 we.hooks.trigger('we:after:load:plugins', we, next);
51 });
52 });
53 },
54 loadTemplateCache(we, next) {
55 // step to plug we-plugin-view
56 we.hooks.trigger('we-core:on:load:template:cache', we, next);
57 },
58 instantiateModels(we, next) {
59
60 // step to define all models with sequelize
61 we.log.verbose('instantiateModels step');
62 we.hooks.trigger('we:models:before:instance', we, (err) => {
63 if (err) return next(err);
64
65 for (let modelName in we.db.modelsConfigs) {
66 let mc = we.db.modelsConfigs[modelName];
67
68 // all models have a link permanent
69 mc.definition.linkPermanent = {
70 type: we.db.Sequelize.VIRTUAL,
71 formFieldType: null,
72 get() {
73 if (this.cachedLinkPermanent) return this.cachedLinkPermanent;
74 this.cachedLinkPermanent = this.getUrlPath();
75 return this.cachedLinkPermanent;
76 }
77 };
78
79 // set
80 mc.definition.metadata = {
81 type: we.db.Sequelize.VIRTUAL,
82 formFieldType: null
83 };
84
85 we.db.setModelClassMethods();
86 we.db.setModelInstanceMethods();
87
88 // save attrs list:
89 mc.attributeList = Object.keys(mc.definition);
90 // add created and updated at attrs
91 mc.attributeList.push('createdAt');
92 mc.attributeList.push('updatedAt');
93 // save assoc attr names list:
94 if (mc.associations)
95 mc.associationNames = Object.keys(mc.associations);
96
97 // define the model
98 we.db.models[modelName] = we.db.define(
99 modelName,
100 mc.definition,
101 mc.options
102 );
103 }
104
105 // set all associations
106 we.db.setModelAllJoins();
107 we.db.setModelHooks();
108
109 we.hooks.trigger('we:models:set:joins', we, next);
110 });
111 },
112 syncModels(we, next) {
113 we.db.defaultConnection.sync().nodeify( (err)=> {
114 if (err) return next(err);
115 we.hooks.trigger('we:models:ready', we, next);
116 });
117 },
118
119 loadControllers(we, next) {
120 we.log.verbose('loadControllers step');
121 we.events.emit('we:after:load:controllers', we);
122 next();
123 },
124 initI18n(we, next) {
125 we.log.verbose('initI18n step');
126 localization(we);
127 we.events.emit('we:after:init:i18n', we);
128 next();
129 },
130 installAndRegisterPlugins(we, next) {
131 if (we.config.skipInstall) return next();
132
133 we.log.verbose('installAndRegisterPluginsIfNeed step');
134 // dont have plugins to install
135 if (!we.pluginManager.pluginsToInstall) return next();
136 // get plugins to install names
137 var names = Object.keys(we.pluginManager.pluginsToInstall);
138 we.utils.async.eachSeries(names, function onEachPlugin (name, nextPlugin) {
139 // run install scripts
140 we.pluginManager.installPlugin(name, function afterInstallOnePlugin (err){
141 if (err) return nextPlugin(err);
142 // register it
143 we.pluginManager.registerPlugin(name, nextPlugin);
144 });
145 }, function afterInstallAllPlugins (err) {
146 if (err) return next(err);
147 next();
148 });
149 },
150 setExpressApp(we, next) {
151 // load express
152 we.express = weExpress(we);
153 we.events.emit('we:after:load:express', we);
154 next();
155 },
156 passport(we, next) {
157 // hook to set authentication.
158 // if we-plugin-auth is installed, load passport here
159 we.hooks.trigger('we-core:on:set:passport', we, next);
160 },
161 createDefaultFolders(we, next) {
162 we.log.verbose('createDefaultFolders step');
163 we.hooks.trigger('we:create:default:folders', we, next);
164 },
165 registerAllViewTemplates(we, next) {
166 // hook to plugin we-plugin-view template register
167 we.hooks.trigger('we-core:on:register:templates', we, next);
168 },
169 mergeRoutes(we, next) {
170 we.log.verbose('mergeRoutes step');
171 we.routes = {};
172 // merge plugin routes
173 for (let plugin in we.plugins) {
174 we.utils._.merge(we.routes, we.plugins[plugin].routes);
175 }
176 // merge project routes
177 we.utils._.merge(we.routes, we.config.routes);
178 next();
179 },
180 /**
181 * Bind all resources in App
182 *
183 * @param {Object} we
184 * @param {Function} next
185 */
186 bindResources(we, next) {
187 we.log.verbose('bindResources step');
188 try {
189 for (let resource in we.router.resources) {
190 we.router.bindResource(we.router.resources[resource]);
191 }
192 next();
193 } catch (e) {
194 next(e);
195 }
196 },
197 bindRoutes(we, next) {
198 we.log.verbose('bindRoutes step');
199 we.hooks.trigger('we:before:routes:bind', we, function beforeRouteBind(err) {
200 if (err) return next(err);
201
202 for (let route in we.routes) {
203 we.router.bindRoute(we, route, we.routes[route] );
204 }
205
206 we.hooks.trigger('we:after:routes:bind', we, function afterRouteBind(err) {
207 if (err) return next(err);
208
209 // bind after router handler for run responseMethod
210 we.express.use( (req, res, done)=> {
211 if (res.responseMethod) return res[res.responseMethod]();
212 done();
213 });
214
215 we.responses.sortResponses(we);
216
217 next();
218 });
219 });
220 }
221};
\No newline at end of file