qtools-library-dot-d 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -26,6 +26,27 @@ expressJS endpoitns.
26
26
 
27
27
  the dotD object contains three parameters, add....
28
28
 
29
+ ASYNCHRONOUS CODE SAMPLE
29
30
 
31
+ const promptObjects = require('qtools-library-dot-d')({
32
+ libraryName: 'promptObjects',
33
+ }, callback);
34
+ promptObjects.setLibraryPath(path.join(__dirname, 'prompts.d'), callback);
30
35
 
36
+ const passThroughParameters = {};
37
+ promptObjects.loadModules({ passThroughParameters }, callback);
38
+ promptObjects.seal(); //make the library immutable
39
+
40
+
41
+
42
+ NEW SYNCHROMOUS CODE SAMPLE:
43
+
44
+ const promptObjects = require('qtools-library-dot-d')({
45
+ libraryName: 'promptObjects',
46
+ });
47
+ promptObjects.setLibraryPath(path.join(__dirname, 'prompts.d'));
48
+
49
+ const passThroughParameters = {};
50
+ promptObjects.loadModules({ passThroughParameters });
51
+ promptObjects.seal(); //make the library immutable
31
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qtools-library-dot-d",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Scan a directory for JS modules that can execute and addMe() that attaches it to some form of name/value list, eg, a Map() with functions as values or an xPressJS route/endpoint list.",
5
5
  "main": "qtools-library-dot-d.js",
6
6
  "scripts": {
@@ -10,213 +10,203 @@ const path = require('path');
10
10
  const fs = require('fs');
11
11
 
12
12
  //START OF moduleFunction() ============================================================
13
- const moduleFunction = ({ moduleName } = {}) => ({ libraryName }) => {
14
- // ======================================================================================
15
- // SETTERS
13
+ const moduleFunction =
14
+ ({ moduleName } = {}) =>
15
+ ({ libraryName }) => {
16
+ // ======================================================================================
17
+ // SETTERS
16
18
 
17
- // --------------------------------------------------------------------------------
18
- // SET LIBRARY PATH
19
-
20
- let libraryPath;
19
+ // --------------------------------------------------------------------------------
20
+ // SET LIBRARY PATH
21
+
22
+ let libraryPath;
23
+
24
+ const setLibraryPath = (dirPath, callback) => {
25
+ try {
26
+ libraryPath = fs.realpathSync(dirPath);
27
+ } catch (err) {
28
+ callback(err.toString());
29
+ return;
30
+ }
31
+
32
+ if (typeof callback == 'function') {
33
+ callback('', libraryPath);
34
+ } else {
35
+ return libraryPath;
36
+ }
37
+ };
21
38
 
22
- const setLibraryPath = (dirPath, callback) => {
23
- try {
24
- libraryPath = fs.realpathSync(dirPath);
25
- } catch (err) {
26
- callback(err.toString());
27
- return;
28
- }
39
+ // --------------------------------------------------------------------------------
40
+ // SET CUSTOM MODULE ADDING FUNCTION
41
+
42
+ const defaultAdd = (item, callback) => {
43
+ require(item.endpointPath)(item.moduleArgs); //calls module with dotD object, including library.add()
44
+ if (typeof callback == 'function') {
45
+ callback();
46
+ } else {
47
+ return;
48
+ }
49
+ };
29
50
 
30
- callback('', libraryPath);
31
- };
51
+ let add = defaultAdd;
32
52
 
33
- // --------------------------------------------------------------------------------
34
- // SET CUSTOM MODULE ADDING FUNCTION
53
+ const setCustomAddFunction = (newFunction) => (add = newFunction);
35
54
 
36
- const defaultAdd = (item, callback) => {
37
- require(item.endpointPath)(item.moduleArgs);
38
- callback();
39
- };
55
+ // ======================================================================================
56
+ // LOG LIST OBJECT
40
57
 
41
- let add = defaultAdd;
58
+ const logList = (() => {
59
+ const list = [];
60
+ const push = (item) => list.push(item);
61
+ const formatList = () => {
62
+ return list
63
+ .reduce(
64
+ (result, item) => `${result}${item}\n\t`,
65
+ `\n${libraryName} Log\n\t`,
66
+ )
67
+ .replace(/\n\t$/, '');
68
+ };
69
+ return { push, formatList };
70
+ })();
42
71
 
43
- const setCustomAddFunction = newFunction => (add = newFunction);
72
+ // ======================================================================================
73
+ // LIBRARY OBJECT
44
74
 
45
- // ======================================================================================
46
- // LOG LIST OBJECT
75
+ const libraryObject = {}; //used only for producing display listings
76
+ const library = (() => {
77
+ const add = (name, item) => {
78
+ libraryObject[name] = item; //used only for producing display listings
79
+ externalElements[name] = item;
80
+ };
81
+ return { add };
82
+ })();
83
+
84
+ // ======================================================================================
85
+ // LIBRARY OBJECT
86
+
87
+ const toString = () => {
88
+ const formatProperty = (item) => {
89
+ let outString = typeof item;
90
+ if (typeof item == 'function') {
91
+ const result = item.toString().match(/\((?<args>.*?\))/);
92
+ outString = `(${result.qtGetSurePath('groups.args', 'function')})`;
93
+ }
94
+ return outString;
95
+ };
47
96
 
48
- const logList = (() => {
49
- const list = [];
50
- const push = item => list.push(item);
51
- const formatList = () => {
52
- return list
97
+ return Object.keys(libraryObject)
53
98
  .reduce(
54
- (result, item) => `${result}${item}\n\t`,
55
- `\n${libraryName} Log\n\t`
99
+ (result, name) =>
100
+ `${result}${name}: ${
101
+ (typeof libraryObject[name]).match(/(string|number)/i)
102
+ ? libraryObject[name]
103
+ : formatProperty(libraryObject[name])
104
+ }\n\t`,
105
+ `\n${libraryName} Library\n\t`,
56
106
  )
57
107
  .replace(/\n\t$/, '');
58
108
  };
59
- return { push, formatList };
60
- })();
61
-
62
- // ======================================================================================
63
- // LIBRARY OBJECT
64
109
 
65
- const libraryObject = {}; //used only for producing display listings
66
- const library = (() => {
67
- const add = (name, item) => {
68
- libraryObject[name] = item; //used only for producing display listings
69
- externalElements[name] = item;
110
+ // ======================================================================================
111
+ // SEAL OBJECT (make immutable)
112
+
113
+ const sealActual = (externalElements) => () => {
114
+ Object.assign(
115
+ externalElements.library.qtSelectProperties(['add'], {
116
+ excludeMode: true,
117
+ }),
118
+ );
119
+ Object.assign(
120
+ externalElements.logList.qtSelectProperties(['push'], {
121
+ excludeMode: true,
122
+ }),
123
+ );
124
+
125
+ Object.assign(
126
+ externalElements.qtSelectProperties(
127
+ [
128
+ 'setLibraryPath',
129
+ 'loadModules',
130
+ 'seal',
131
+ 'setCustomAddFunction',
132
+ 'library',
133
+ 'logList',
134
+ ],
135
+ { excludeMode: true },
136
+ ),
137
+ );
138
+ Object.freeze(externalElements);
139
+ Object.seal(externalElements);
70
140
  };
71
- return { add };
72
- })();
73
-
74
- // ======================================================================================
75
- // LIBRARY OBJECT
76
-
77
- const toString = () => {
78
-
79
- const formatProperty=item=>{
80
- let outString=typeof(item);
81
- if (typeof(item)=='function'){
82
- const result=item.toString().match(/\((?<args>.*?\))/);
83
- outString=`(${result.qtGetSurePath('groups.args', 'function')})`
84
-
85
- }
86
- return outString;
87
- }
88
-
89
- return Object.keys(libraryObject)
90
- .reduce(
91
- (result, name) =>
92
- `${result}${name}: ${
93
- (typeof libraryObject[name]).match(/(string|number)/i)
94
- ? libraryObject[name]
95
- : formatProperty(libraryObject[name])
96
- }\n\t`,
97
- `\n${libraryName} Library\n\t`
98
- )
99
- .replace(/\n\t$/, '');
100
- };
101
141
 
102
- // ======================================================================================
103
- // SEAL OBJECT (make immutable)
104
-
105
- const sealActual = externalElements => () => {
106
- Object.assign(
107
- externalElements.library.qtSelectProperties(['add'], {
108
- excludeMode: true
109
- })
110
- );
111
- Object.assign(
112
- externalElements.logList.qtSelectProperties(['push'], {
113
- excludeMode: true
114
- })
115
- );
116
-
117
- Object.assign(
118
- externalElements.qtSelectProperties(
119
- [
120
- 'setLibraryPath',
121
- 'loadModules',
122
- 'seal',
123
- 'setCustomAddFunction',
124
- 'library',
125
- 'logList'
126
- ],
127
- { excludeMode: true }
128
- )
129
- );
130
- Object.freeze(externalElements);
131
- Object.seal(externalElements);
132
- };
142
+ // ======================================================================================
143
+ // LOAD MODULES (actual execution)
133
144
 
134
- // ======================================================================================
135
- // LOAD MODULES (actual execution)
145
+ const loadModules = ({ passThroughParameters }, callback) => {
146
+ const asynchronousPipePlus = new require(
147
+ 'qtools-asynchronous-pipe-plus',
148
+ )();
149
+ const pipeRunner = asynchronousPipePlus.pipeRunner;
150
+ const taskListPlus = asynchronousPipePlus.taskListPlus;
136
151
 
137
- const loadModules = ({ passThroughParameters }, callback) => {
138
- const asynchronousPipePlus = new require('qtools-asynchronous-pipe-plus')();
139
- const pipeRunner = asynchronousPipePlus.pipeRunner;
140
- const taskListPlus = asynchronousPipePlus.taskListPlus;
152
+ const taskList = new taskListPlus();
141
153
 
142
- const taskList = new taskListPlus();
154
+ // --------------------------------------------------------------------------------
155
+ // INTERNAL UTILITIES
143
156
 
144
- // --------------------------------------------------------------------------------
145
- // INTERNAL UTILITIES
146
-
147
- const mergeArgs = (args, next, propertyName) => (err, result) =>
148
- next(err, {
149
- ...args,
150
- ...(propertyName ? { [propertyName]: result } : result)
151
- });
152
-
153
- const forwardArgs = ({ next, args }) => err => next(err, args);
154
-
155
- // --------------------------------------------------------------------------------
156
- // INITIALIZE COMMON ADDED MODULE PARAMETERS
157
+ const mergeArgs = (args, next, propertyName) => (err, result) =>
158
+ next(err, {
159
+ ...args,
160
+ ...(propertyName ? { [propertyName]: result } : result),
161
+ });
157
162
 
158
- const dotD = { logList, library };
163
+ const forwardArgs =
164
+ ({ next, args }) =>
165
+ (err) =>
166
+ next(err, args);
159
167
 
160
- const moduleArgs = {
161
- dotD,
162
- passThroughParameters
163
- };
168
+ // --------------------------------------------------------------------------------
169
+ // INITIALIZE COMMON ADDED MODULE PARAMETERS
164
170
 
165
- // --------------------------------------------------------------------------------
166
- // BUILD TASKLIST
171
+ const dotD = { logList, library };
167
172
 
168
- taskList.push((args, next) => {
169
- const { libraryPath, passThroughParameters } = args;
170
-
171
- const localCallback = (err, result) => {
172
- next(err, { ...args, result });
173
+ const moduleArgs = {
174
+ dotD,
175
+ passThroughParameters,
173
176
  };
174
177
 
175
- fs
176
- .readdirSync(libraryPath)
178
+ fs.readdirSync(libraryPath)
177
179
  //.filter(file => path.extname(file) === '.js')
178
- .forEach(file => {
179
- // --------------------------------------------------------------------------------
180
- // TASKLIST ITEM TEMPLATE
181
- taskList.push((args, next) => {
182
- const { libraryPath, passThroughParameters } = args;
183
-
184
- const endpointPath = path.join(libraryPath, file);
185
- add(
186
- {
187
- endpointPath,
188
- moduleArgs
189
- },
190
- forwardArgs({ next, args })
191
- );
180
+ .forEach((file) => {
181
+ const endpointPath = path.join(libraryPath, file);
182
+ add({
183
+ endpointPath,
184
+ moduleArgs,
192
185
  });
193
186
  });
194
- localCallback('', args);
195
- });
196
-
197
- // --------------------------------------------------------------------------------
198
- // INIT AND EXECUTE THE PIPELINE
199
187
 
200
- const initialData = { libraryPath, passThroughParameters };
201
- pipeRunner(taskList.getList(), initialData, (err, args) => {
202
- const { libraryDotD } = args;
188
+ // --------------------------------------------------------------------------------
189
+ // INIT AND EXECUTE THE PIPELINE
203
190
 
204
- callback(err);
205
- });
206
- };
191
+ if (typeof callback == 'function') {
192
+ callback();
193
+ } else {
194
+ return;
195
+ }
196
+ };
207
197
 
208
- const externalElements = {
209
- setLibraryPath,
210
- setCustomAddFunction,
211
- logList,
212
- library,
213
- loadModules,
214
- toString,
215
- qtdProcessLog: logList.formatList
198
+ const externalElements = {
199
+ setLibraryPath,
200
+ setCustomAddFunction,
201
+ logList,
202
+ library,
203
+ loadModules,
204
+ toString,
205
+ qtdProcessLog: logList.formatList,
206
+ };
207
+ externalElements.seal = sealActual(externalElements);
208
+ return externalElements;
216
209
  };
217
- externalElements.seal = sealActual(externalElements);
218
- return externalElements;
219
- };
220
210
 
221
211
  //END OF moduleFunction() ============================================================
222
212