qtools-library-dot-d 1.0.3 → 1.0.5

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
@@ -39,7 +39,7 @@ promptObjects.seal(); //make the library immutable
39
39
 
40
40
 
41
41
 
42
- NEW SYNCHROMOUS CODE SAMPLE:
42
+ SYNCHROMOUS CODE SAMPLE:
43
43
 
44
44
  const promptObjects = require('qtools-library-dot-d')({
45
45
  libraryName: 'promptObjects',
@@ -50,3 +50,159 @@ const passThroughParameters = {};
50
50
  promptObjects.loadModules({ passThroughParameters });
51
51
  promptObjects.seal(); //make the library immutable
52
52
 
53
+
54
+ COMPLETE WORKING CODE SAMPLE
55
+
56
+ // ================================================================================
57
+ // index.js for demonstration
58
+
59
+ #!/usr/bin/env node
60
+ 'use strict';
61
+ // Suppress punycode deprecation warning
62
+ process.noDeprecation = true;
63
+
64
+ const moduleName = __filename.replace(__dirname + '/', '').replace(/.js$/, ''); //this just seems to come in handy a lot
65
+
66
+ // const qt = require('qtools-functional-library'); //also exposes qtLog(); qt.help({printOutput:true, queryString:'.*', sendJson:false});
67
+
68
+ const os = require('os');
69
+ const path = require('path');
70
+ const fs = require('fs');
71
+
72
+ const qtLibDotD = require('qtools-library-dot-d');
73
+
74
+ //START OF moduleFunction() ============================================================
75
+ const moduleFunction =
76
+ ({ moduleName } = {}) =>
77
+ ({ unused } = {}) => {
78
+
79
+ // ========================================================================
80
+ // SYNCHRONOUS DEMO
81
+
82
+ const synchronousDemo = () => {
83
+ const demoDotD = qtLibDotD({
84
+ libraryName: 'demoDotD',
85
+ });
86
+
87
+ const libraryModulesPath = path.join(__dirname, 'demoModules_DotD');
88
+
89
+ const passThroughParameters = {
90
+ message: 'Hello from a dotDlib module',
91
+ add: (a, b) => a + b,
92
+ };
93
+
94
+ demoDotD.setLibraryPath(libraryModulesPath);
95
+ demoDotD.loadModules({ passThroughParameters });
96
+ demoDotD.seal();
97
+
98
+ // console.log(demoDotD.qtdProcessLog());
99
+ console.log(demoDotD.toString());
100
+
101
+ demoDotD.demo1();
102
+ demoDotD.demo2(10, 7);
103
+ };
104
+ console.log(`SYNCHRONOUS DEMO =====================================`);
105
+ synchronousDemo();
106
+
107
+ // ========================================================================
108
+ // ASYNCHRONOUS DEMO
109
+
110
+ const asynchronousDemo = (callback) => {
111
+ const localCallback = (err) => {
112
+ demoDotD.seal();
113
+ callback(err, demoDotD);
114
+ };
115
+
116
+ const demoDotD = qtLibDotD({
117
+ libraryName: 'demoDotD',
118
+ });
119
+
120
+ const libraryModulesPath = path.join(__dirname, 'demoModules_DotD');
121
+
122
+ const passThroughParameters = {
123
+ message: 'Hello from a dotDlib module',
124
+ add: (a, b) => a + b,
125
+ };
126
+
127
+ demoDotD.setLibraryPath(libraryModulesPath);
128
+ demoDotD.loadModules({ passThroughParameters }, localCallback);
129
+ };
130
+
131
+ console.log(`\n\nASYNCHRONOUS DEMO =====================================`);
132
+
133
+ const callback = (err, demoDotD) => {
134
+ console.log(demoDotD.toString());
135
+ demoDotD.demo1();
136
+ demoDotD.demo2(10, 7);
137
+ };
138
+
139
+ asynchronousDemo(callback);
140
+ };
141
+
142
+
143
+ // ================================================================================
144
+ // demo1.js
145
+
146
+ #!/usr/bin/env node
147
+ 'use strict';
148
+ // Suppress punycode deprecation warning
149
+ process.noDeprecation = true;
150
+
151
+ const moduleName = __filename.replace(__dirname + '/', '').replace(/.js$/, ''); //this just seems to come in handy a lot
152
+
153
+ //START OF moduleFunction() ============================================================
154
+ const moduleFunction = ({ moduleName } = {}) => ({ dotD, passThroughParameters } = {}) => {
155
+
156
+ const workingFunction = () => {
157
+ console.log(`${passThroughParameters.message} [${moduleName}]`);
158
+ return 'hello';
159
+ };
160
+
161
+
162
+ dotD.library.add(moduleName, workingFunction);
163
+ };
164
+
165
+ //END OF moduleFunction() ============================================================
166
+
167
+ module.exports = moduleFunction({ moduleName }); //runs it right now
168
+ //END OF moduleFunction() ============================================================
169
+
170
+ module.exports = moduleFunction({ moduleName })({});
171
+
172
+
173
+ // ================================================================================
174
+ //demo2.js
175
+
176
+ #!/usr/bin/env node
177
+ 'use strict';
178
+ // Suppress punycode deprecation warning
179
+ process.noDeprecation = true;
180
+
181
+ const moduleName = __filename.replace(__dirname + '/', '').replace(/.js$/, ''); //this just seems to come in handy a lot
182
+
183
+ //START OF moduleFunction() ============================================================
184
+ const moduleFunction = ({ moduleName } = {}) => ({ dotD, passThroughParameters } = {}) => {
185
+
186
+ const workingFunction = (arg1, arg2) => {
187
+ console.log(`Pass Through Function: ${arg1}+${arg2}=${passThroughParameters.add(arg1, arg2)} [${moduleName}]`);
188
+ return 'hello';
189
+ };
190
+
191
+
192
+ dotD.library.add(moduleName, workingFunction);
193
+ };
194
+
195
+ //END OF moduleFunction() ============================================================
196
+
197
+ module.exports = moduleFunction({ moduleName }); //runs it right now
198
+
199
+
200
+ HISTORY
201
+
202
+ v1.05 - cleaned up toString() report, returns exception list (or err if callback) if needed, added qtools-functional-library
203
+ v1.04 - Now tolerates (and reports) directories and files that are not modules.
204
+
205
+
206
+ HISTORY
207
+
208
+ 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.3",
3
+ "version": "1.0.5",
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": {
@@ -9,6 +9,7 @@
9
9
  "author": "TQ White II",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "qtools-asynchronous-pipe-plus": "^1.0.2"
12
+ "qtools-asynchronous-pipe-plus": "^1.0.2",
13
+ "qtools-functional-library": "^2.0.4"
13
14
  }
14
15
  }
@@ -9,6 +9,8 @@ const os = require('os');
9
9
  const path = require('path');
10
10
  const fs = require('fs');
11
11
 
12
+ const qt = require('qtools-functional-library');
13
+
12
14
  //START OF moduleFunction() ============================================================
13
15
  const moduleFunction =
14
16
  ({ moduleName } = {}) =>
@@ -40,9 +42,17 @@ const moduleFunction =
40
42
  // SET CUSTOM MODULE ADDING FUNCTION
41
43
 
42
44
  const defaultAdd = (item, callback) => {
43
- require(item.endpointPath)(item.moduleArgs); //calls module with dotD object, including library.add()
45
+ let error;
46
+ try {
47
+ require(item.endpointPath)(item.moduleArgs); //calls module with dotD object, including library.add()
48
+ } catch (err) {
49
+ item.exceptionList.push(
50
+ `${path.basename(item.endpointPath)}: ${err.toString().split('\n').qtFirst('n/a')}`,
51
+ );
52
+ }
53
+
44
54
  if (typeof callback == 'function') {
45
- callback();
55
+ callback(error);
46
56
  } else {
47
57
  return;
48
58
  }
@@ -73,6 +83,7 @@ const moduleFunction =
73
83
  // LIBRARY OBJECT
74
84
 
75
85
  const libraryObject = {}; //used only for producing display listings
86
+ const exceptionList = [];
76
87
  const library = (() => {
77
88
  const add = (name, item) => {
78
89
  libraryObject[name] = item; //used only for producing display listings
@@ -89,12 +100,13 @@ const moduleFunction =
89
100
  let outString = typeof item;
90
101
  if (typeof item == 'function') {
91
102
  const result = item.toString().match(/\((?<args>.*?\))/);
92
- outString = `(${result.qtGetSurePath('groups.args', 'function')})`;
103
+ const args = result.qtGetSurePath('groups.args', 'function');
104
+ outString = `(${args == ')' ? args + ' # function takes no arguments' : args}`;
93
105
  }
94
106
  return outString;
95
107
  };
96
108
 
97
- return Object.keys(libraryObject)
109
+ const libString = Object.keys(libraryObject)
98
110
  .reduce(
99
111
  (result, name) =>
100
112
  `${result}${name}: ${
@@ -102,9 +114,13 @@ const moduleFunction =
102
114
  ? libraryObject[name]
103
115
  : formatProperty(libraryObject[name])
104
116
  }\n\t`,
105
- `\n${libraryName} Library\n\t`,
117
+ `\n${libraryName} Library Elements\n\t`,
106
118
  )
107
119
  .replace(/\n\t$/, '');
120
+
121
+ const exceptionString = '\n\t' + exceptionList.join('\n\t');
122
+
123
+ return `${libString}${exceptionString}`;
108
124
  };
109
125
 
110
126
  // ======================================================================================
@@ -179,9 +195,11 @@ const moduleFunction =
179
195
  //.filter(file => path.extname(file) === '.js')
180
196
  .forEach((file) => {
181
197
  const endpointPath = path.join(libraryPath, file);
198
+
182
199
  add({
183
200
  endpointPath,
184
201
  moduleArgs,
202
+ exceptionList,
185
203
  });
186
204
  });
187
205
 
@@ -189,10 +207,17 @@ const moduleFunction =
189
207
  // INIT AND EXECUTE THE PIPELINE
190
208
 
191
209
  if (typeof callback == 'function') {
192
- callback();
210
+ if (exceptionList.length) {
211
+ callback(exceptionList);
212
+ } else {
213
+ callback();
214
+ }
193
215
  } else {
194
- return;
195
- }
216
+ if (exceptionList.length) {
217
+ return exceptionList;
218
+ } else {
219
+ return;
220
+ }}
196
221
  };
197
222
 
198
223
  const externalElements = {