qtools-library-dot-d 1.0.2 → 1.0.4

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,31 @@ 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
31
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
52
+
53
+
54
+ HISTORY
55
+
56
+ v1.04 - Now tolerates (and reports) directories and files that are not modules.
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.4",
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,218 @@ 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;
21
-
22
- const setLibraryPath = (dirPath, callback) => {
23
- try {
24
- libraryPath = fs.realpathSync(dirPath);
25
- } catch (err) {
26
- callback(err.toString());
27
- return;
28
- }
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
+ };
29
38
 
30
- callback('', libraryPath);
31
- };
39
+ // --------------------------------------------------------------------------------
40
+ // SET CUSTOM MODULE ADDING FUNCTION
41
+
42
+ const defaultAdd = (item, callback) => {
43
+ let error;
44
+ try {
45
+ require(item.endpointPath)(item.moduleArgs); //calls module with dotD object, including library.add()
46
+ } catch (err) {
47
+ item.exceptionList.push(
48
+ `${path.basename(item.endpointPath)}: ${err.toString().split('\n').qtFirst('n/a')}`,
49
+ );
50
+ }
51
+
52
+ if (typeof callback == 'function') {
53
+ callback(error);
54
+ } else {
55
+ return;
56
+ }
57
+ };
32
58
 
33
- // --------------------------------------------------------------------------------
34
- // SET CUSTOM MODULE ADDING FUNCTION
59
+ let add = defaultAdd;
35
60
 
36
- const defaultAdd = (item, callback) => {
37
- require(item.endpointPath)(item.moduleArgs);
38
- callback();
39
- };
61
+ const setCustomAddFunction = (newFunction) => (add = newFunction);
40
62
 
41
- let add = defaultAdd;
63
+ // ======================================================================================
64
+ // LOG LIST OBJECT
42
65
 
43
- const setCustomAddFunction = newFunction => (add = newFunction);
44
-
45
- // ======================================================================================
46
- // LOG LIST OBJECT
66
+ const logList = (() => {
67
+ const list = [];
68
+ const push = (item) => list.push(item);
69
+ const formatList = () => {
70
+ return list
71
+ .reduce(
72
+ (result, item) => `${result}${item}\n\t`,
73
+ `\n${libraryName} Log\n\t`,
74
+ )
75
+ .replace(/\n\t$/, '');
76
+ };
77
+ return { push, formatList };
78
+ })();
79
+
80
+ // ======================================================================================
81
+ // LIBRARY OBJECT
82
+
83
+ const libraryObject = {}; //used only for producing display listings
84
+ const exceptionList = [];
85
+ const library = (() => {
86
+ const add = (name, item) => {
87
+ libraryObject[name] = item; //used only for producing display listings
88
+ externalElements[name] = item;
89
+ };
90
+ return { add };
91
+ })();
92
+
93
+ // ======================================================================================
94
+ // LIBRARY OBJECT
95
+
96
+ const toString = () => {
97
+ const formatProperty = (item) => {
98
+ let outString = typeof item;
99
+ if (typeof item == 'function') {
100
+ const result = item.toString().match(/\((?<args>.*?\))/);
101
+ outString = `(${result.qtGetSurePath('groups.args', 'function')})`;
102
+ }
103
+ return outString;
104
+ };
47
105
 
48
- const logList = (() => {
49
- const list = [];
50
- const push = item => list.push(item);
51
- const formatList = () => {
52
- return list
106
+ const libString = Object.keys(libraryObject)
53
107
  .reduce(
54
- (result, item) => `${result}${item}\n\t`,
55
- `\n${libraryName} Log\n\t`
108
+ (result, name) =>
109
+ `${result}${name}: ${
110
+ (typeof libraryObject[name]).match(/(string|number)/i)
111
+ ? libraryObject[name]
112
+ : formatProperty(libraryObject[name])
113
+ }\n\t`,
114
+ `\n${libraryName} Library\n\t`,
56
115
  )
57
116
  .replace(/\n\t$/, '');
58
- };
59
- return { push, formatList };
60
- })();
61
117
 
62
- // ======================================================================================
63
- // LIBRARY OBJECT
118
+ const exceptionString = '\n\t' + exceptionList.join('\n\t');
64
119
 
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;
120
+ return `${libString}${exceptionString}`;
70
121
  };
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
-
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
- };
133
-
134
- // ======================================================================================
135
- // LOAD MODULES (actual execution)
136
122
 
137
- const loadModules = ({ passThroughParameters }, callback) => {
138
- const asynchronousPipePlus = new require('qtools-asynchronous-pipe-plus')();
139
- const pipeRunner = asynchronousPipePlus.pipeRunner;
140
- const taskListPlus = asynchronousPipePlus.taskListPlus;
123
+ // ======================================================================================
124
+ // SEAL OBJECT (make immutable)
125
+
126
+ const sealActual = (externalElements) => () => {
127
+ Object.assign(
128
+ externalElements.library.qtSelectProperties(['add'], {
129
+ excludeMode: true,
130
+ }),
131
+ );
132
+ Object.assign(
133
+ externalElements.logList.qtSelectProperties(['push'], {
134
+ excludeMode: true,
135
+ }),
136
+ );
137
+
138
+ Object.assign(
139
+ externalElements.qtSelectProperties(
140
+ [
141
+ 'setLibraryPath',
142
+ 'loadModules',
143
+ 'seal',
144
+ 'setCustomAddFunction',
145
+ 'library',
146
+ 'logList',
147
+ ],
148
+ { excludeMode: true },
149
+ ),
150
+ );
151
+ Object.freeze(externalElements);
152
+ Object.seal(externalElements);
153
+ };
141
154
 
142
- const taskList = new taskListPlus();
155
+ // ======================================================================================
156
+ // LOAD MODULES (actual execution)
143
157
 
144
- // --------------------------------------------------------------------------------
145
- // INTERNAL UTILITIES
158
+ const loadModules = ({ passThroughParameters }, callback) => {
159
+ const asynchronousPipePlus = new require(
160
+ 'qtools-asynchronous-pipe-plus',
161
+ )();
162
+ const pipeRunner = asynchronousPipePlus.pipeRunner;
163
+ const taskListPlus = asynchronousPipePlus.taskListPlus;
146
164
 
147
- const mergeArgs = (args, next, propertyName) => (err, result) =>
148
- next(err, {
149
- ...args,
150
- ...(propertyName ? { [propertyName]: result } : result)
151
- });
165
+ const taskList = new taskListPlus();
152
166
 
153
- const forwardArgs = ({ next, args }) => err => next(err, args);
167
+ // --------------------------------------------------------------------------------
168
+ // INTERNAL UTILITIES
154
169
 
155
- // --------------------------------------------------------------------------------
156
- // INITIALIZE COMMON ADDED MODULE PARAMETERS
170
+ const mergeArgs = (args, next, propertyName) => (err, result) =>
171
+ next(err, {
172
+ ...args,
173
+ ...(propertyName ? { [propertyName]: result } : result),
174
+ });
157
175
 
158
- const dotD = { logList, library };
176
+ const forwardArgs =
177
+ ({ next, args }) =>
178
+ (err) =>
179
+ next(err, args);
159
180
 
160
- const moduleArgs = {
161
- dotD,
162
- passThroughParameters
163
- };
181
+ // --------------------------------------------------------------------------------
182
+ // INITIALIZE COMMON ADDED MODULE PARAMETERS
164
183
 
165
- // --------------------------------------------------------------------------------
166
- // BUILD TASKLIST
184
+ const dotD = { logList, library };
167
185
 
168
- taskList.push((args, next) => {
169
- const { libraryPath, passThroughParameters } = args;
170
-
171
- const localCallback = (err, result) => {
172
- next(err, { ...args, result });
186
+ const moduleArgs = {
187
+ dotD,
188
+ passThroughParameters,
173
189
  };
174
190
 
175
- fs
176
- .readdirSync(libraryPath)
191
+ fs.readdirSync(libraryPath)
177
192
  //.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
- );
193
+ .forEach((file) => {
194
+ const endpointPath = path.join(libraryPath, file);
195
+
196
+ add({
197
+ endpointPath,
198
+ moduleArgs,
199
+ exceptionList,
192
200
  });
193
201
  });
194
- localCallback('', args);
195
- });
196
-
197
- // --------------------------------------------------------------------------------
198
- // INIT AND EXECUTE THE PIPELINE
199
202
 
200
- const initialData = { libraryPath, passThroughParameters };
201
- pipeRunner(taskList.getList(), initialData, (err, args) => {
202
- const { libraryDotD } = args;
203
+ // --------------------------------------------------------------------------------
204
+ // INIT AND EXECUTE THE PIPELINE
203
205
 
204
- callback(err);
205
- });
206
- };
206
+ if (typeof callback == 'function') {
207
+ callback();
208
+ } else {
209
+ return;
210
+ }
211
+ };
207
212
 
208
- const externalElements = {
209
- setLibraryPath,
210
- setCustomAddFunction,
211
- logList,
212
- library,
213
- loadModules,
214
- toString,
215
- qtdProcessLog: logList.formatList
213
+ const externalElements = {
214
+ setLibraryPath,
215
+ setCustomAddFunction,
216
+ logList,
217
+ library,
218
+ loadModules,
219
+ toString,
220
+ qtdProcessLog: logList.formatList,
221
+ };
222
+ externalElements.seal = sealActual(externalElements);
223
+ return externalElements;
216
224
  };
217
- externalElements.seal = sealActual(externalElements);
218
- return externalElements;
219
- };
220
225
 
221
226
  //END OF moduleFunction() ============================================================
222
227