mastercontroller 1.1.15 → 1.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/MasterAction.js CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- // version 1.0.16
2
+ // version 1.0.17
3
3
 
4
4
  var master = require('./MasterControl');
5
5
  var fileserver = require('fs');
@@ -24,7 +24,7 @@ class MasterAction{
24
24
 
25
25
  // location starts from the view folder. Ex: partialViews/fileName.html
26
26
  returnPartialView(location, data){
27
- var actionUrl = master.router.currentRoute.root + "app/views/" + location;
27
+ var actionUrl = master.router.currentRoute.root + "/app/views/" + location;
28
28
  var getAction = fileserver.readFileSync(actionUrl, 'utf8');
29
29
  if(typeof(templateFunc) === "function"){
30
30
  return templateFunc(getAction, data);
@@ -96,7 +96,7 @@ class MasterAction{
96
96
  this.params = tools.combineObjects(data, this.params);
97
97
  var func = master.viewList;
98
98
  this.params = tools.combineObjects(this.params, func);
99
- var actionUrl = (location === undefined) ? master.router.currentRoute.root + "app/views/" + master.router.currentRoute.toController + "/" + master.router.currentRoute.toAction + ".html" : master.router.currentRoute.root + location;
99
+ var actionUrl = (location === undefined) ? master.router.currentRoute.root + "/app/views/" + master.router.currentRoute.toController + "/" + master.router.currentRoute.toAction + ".html" : master.router.currentRoute.root + location;
100
100
  var actionView = fileserver.readFileSync(actionUrl, 'utf8');
101
101
  if(typeof(templateFunc) === "function"){
102
102
  masterView = templateFunc(actionView, data);
@@ -127,7 +127,7 @@ class MasterAction{
127
127
  this.params = tools.combineObjects(data, this.params);
128
128
  var func = master.viewList;
129
129
  this.params = tools.combineObjects(this.params, func);
130
- var viewUrl = (location === undefined || location === "" || location === null) ? master.router.currentRoute.root + "app/views/" + master.router.currentRoute.toController + "/" + master.router.currentRoute.toAction + ".html": master.router.currentRoute.root + "app/" + location;
130
+ var viewUrl = (location === undefined || location === "" || location === null) ? master.router.currentRoute.root + "/app/views/" + master.router.currentRoute.toController + "/" + master.router.currentRoute.toAction + ".html": master.router.currentRoute.root + "/app/" + location;
131
131
  var viewFile = fileserver.readFileSync(viewUrl,'utf8');
132
132
  if(typeof(templateFunc) === "function"){
133
133
  childView = templateFunc(viewFile, this.params);
@@ -137,7 +137,7 @@ class MasterAction{
137
137
  }
138
138
 
139
139
  this.params.yield = childView;
140
- var masterFile = fileserver.readFileSync(master.router.currentRoute.root + "app/views/layouts/master.html", 'utf8');
140
+ var masterFile = fileserver.readFileSync(master.router.currentRoute.root + "/app/views/layouts/master.html", 'utf8');
141
141
  if(typeof(templateFunc) === "function"){
142
142
  masterView = templateFunc(masterFile, this.params);
143
143
  }
package/MasterControl.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // MasterControl - by Alexander rich
2
- // version 1.0.18
2
+ // version 1.0.19
3
3
 
4
4
  var url = require('url');
5
5
  var fileserver = require('fs');
@@ -8,6 +8,8 @@ var https = require('https');
8
8
  var fs = require('fs');
9
9
  var url = require('url');
10
10
  var path = require('path');
11
+ var globSearch = require("glob");
12
+
11
13
 
12
14
  class MasterControl {
13
15
  controllerList = {}
@@ -16,6 +18,15 @@ class MasterControl {
16
18
  _root = null
17
19
  _environmentType = null
18
20
  _serverProtocol = null
21
+ _scopedList = []
22
+
23
+ #loadTransientListClasses(name, params){
24
+ Object.defineProperty(this.requestList, name, {
25
+ get: function() {
26
+ return new params();
27
+ }
28
+ });
29
+ }
19
30
 
20
31
  get env(){
21
32
  return require(`${this.root}/config/environments/env.${this.environmentType}.json`);
@@ -96,13 +107,64 @@ class MasterControl {
96
107
  };
97
108
  }
98
109
 
99
- // adds your class to the --------- todo
100
- register(name, param){
101
- if(name && param){
102
- this.requestList[name] = param;
110
+ /*
111
+ Services are created each time they are requested.
112
+ It gets a new instance of the injected object, on each request of this object.
113
+ For each time you inject this object is injected in the class, it will create a new instance.
114
+ */
115
+ addTransient(name, params){
116
+ if(name && params){
117
+ this.#loadTransientListClasses(name, params);
103
118
  }
119
+
104
120
  }
105
121
 
122
+ /*
123
+ Services are created on each request (once per request). This is most recommended for WEB applications.
124
+ So for example, if during a request you use the same dependency injection,
125
+ in many places, you will use the same instance of that object,
126
+ it will make reference to the same memory allocation
127
+ */
128
+ addScoped(name, params){
129
+ if(name && params){
130
+ this._scopedList[name] = params;
131
+ }
132
+ }
133
+
134
+ /*
135
+ Services are created once for the lifetime of the application. It uses the same instance for the whole application.
136
+ */
137
+ addSingleton(name, params){
138
+ if(name && params){
139
+ this.requestList[name] = new params();
140
+ }
141
+ }
142
+
143
+ // adds your class instance to the request object.
144
+ register(name, params){
145
+ if(name && params){
146
+ this.requestList[name] = params;
147
+ }
148
+ }
149
+
150
+ component(folderLocation, innerFolder){
151
+
152
+ var rootFolderLocation = `${this.root}/${folderLocation}/${innerFolder}`;
153
+ var search = `${rootFolderLocation}/**/*config.js`;
154
+ var files = globSearch.sync(search, rootFolderLocation);
155
+ require(files[0]);
156
+ var searchRoutes = `${rootFolderLocation}/**/*routes.js`;
157
+ var routeFiles = globSearch.sync(searchRoutes, rootFolderLocation);
158
+ var route = routeFiles[0];
159
+ var routeObject = {
160
+ isComponent : true,
161
+ root : rootFolderLocation
162
+ }
163
+ this.router.setup(routeObject);
164
+ require(route);
165
+ }
166
+
167
+
106
168
  // adds all the server settings needed
107
169
  serverSettings(settings){
108
170
 
@@ -195,6 +257,18 @@ class MasterControl {
195
257
  start(server){
196
258
  this.server = server;
197
259
  }
260
+
261
+ startMVC(foldername){
262
+ var rootFolderLocation = `${this.root}/${foldername}`;
263
+ var search = `${rootFolderLocation}/**/*routes.js`;
264
+ var files = globSearch.sync(search, rootFolderLocation);
265
+ var route = {
266
+ isComponent : false,
267
+ root : `${this.root}`
268
+ }
269
+ this.router.setup(route);
270
+ require(files[0]);
271
+ }
198
272
 
199
273
 
200
274
  // builds and calls all the required tools to have master running completely
package/MasterRouter.js CHANGED
@@ -1,12 +1,11 @@
1
-
2
-
3
- // version 1.0.17
1
+ // version 1.0.20
4
2
 
5
3
  var master = require('./MasterControl');
6
4
  var tools = require('./MasterTools');
7
5
  const EventEmitter = require("events");
8
6
  var currentRoute = {};
9
- const path = require('path')
7
+ const path = require('path');
8
+ const { root } = require('./MasterControl');
10
9
 
11
10
  var normalizePaths = function(requestPath, routePath, requestParams){
12
11
  var obj = {
@@ -86,41 +85,20 @@ const path = require('path')
86
85
  }
87
86
  };
88
87
 
88
+ var loadScopedListClasses = function(){
89
+ for (var key in master._scopedList) {
90
+ var className = master._scopedList[key];
91
+ master.requestList[key] = new className();
92
+ };
93
+ };
94
+
95
+
89
96
  class MasterRouter {
90
97
  currentRouteName = null
91
98
  _routes = {}
92
- __rootLocation = "";
93
-
94
- loadRoutes(mimeList){
95
- this.init(mimeList);
96
- }
97
-
98
- addMimeList(mimeList){
99
- this._addMimeList(mimeList);
100
- }
101
-
102
- init(root){
103
- var rootPath = path.join(root, '../');
104
- this.__rootLocation = rootPath;
105
- require( rootPath + "/routes");
106
- }
107
-
99
+
108
100
  start(){
109
101
  var $that = this;
110
- var rootLocation = path.join(this.__rootLocation, '../');
111
- var componentExist = false;
112
- if(rootLocation.includes("components")){
113
- componentExist = true;
114
- }
115
- this.currentRouteName = tools.makeWordId(4);
116
-
117
- if(this._routes[this.currentRouteName] === undefined){
118
- this._routes[this.currentRouteName] = {
119
- root : rootLocation,
120
- isComponent : componentExist,
121
- routes : []
122
- };
123
- }
124
102
  return {
125
103
  route : function(path, toPath, type, constraint){ // function to add to list of routes
126
104
 
@@ -201,6 +179,26 @@ class MasterRouter {
201
179
  }
202
180
  }
203
181
 
182
+ loadRoutes(mimeList){
183
+ this.init(mimeList);
184
+ }
185
+
186
+ addMimeList(mimeList){
187
+ this._addMimeList(mimeList);
188
+ }
189
+
190
+ setup(route){
191
+ this.currentRouteName = tools.makeWordId(4);
192
+
193
+ if(this._routes[this.currentRouteName] === undefined){
194
+ this._routes[this.currentRouteName] = {
195
+ root : route.root,
196
+ isComponent : route.isComponent,
197
+ routes : []
198
+ };
199
+ }
200
+ }
201
+
204
202
  get currentRoute(){
205
203
  return currentRoute;
206
204
  }
@@ -236,7 +234,8 @@ class MasterRouter {
236
234
 
237
235
  _call(requestObject){
238
236
 
239
- tools.combineObjects(requestObject, master.requestList);
237
+ tools.combineObjects(master.requestList, requestObject);
238
+ requestObject = master.requestList;
240
239
  var Control = require(`${currentRoute.root}/app/controllers/${tools.firstLetterlowercase(requestObject.toController)}Controller`);
241
240
  if(Control === null){
242
241
  Control = require(`${currentRoute.root}/app/controllers/${tools.firstLetterUppercase(requestObject.toController)}Controller`);
@@ -263,6 +262,8 @@ class MasterRouter {
263
262
  }
264
263
 
265
264
  load(rr){ // load the the router
265
+
266
+ loadScopedListClasses();
266
267
  var $that = this;
267
268
  var requestObject = Object.create(rr);
268
269
  requestObject.pathName = requestObject.pathName.replace(/^\/|\/$/g, '').toLowerCase();
package/package.json CHANGED
@@ -3,7 +3,8 @@
3
3
  "qs" : "^6.11.0",
4
4
  "formidable": "^2.0.1",
5
5
  "cookie": "^0.5.0",
6
- "winston": "^3.8.2"
6
+ "winston": "^3.8.2",
7
+ "glob" :"^8.0.3"
7
8
  },
8
9
  "description": "A class library that makes using the Master Framework a breeze",
9
10
  "homepage": "https://github.com/Tailor/MasterController#readme",
@@ -17,5 +18,5 @@
17
18
  "scripts": {
18
19
  "test": "echo \"Error: no test specified\" && exit 1"
19
20
  },
20
- "version": "1.1.15"
21
+ "version": "1.1.17"
21
22
  }