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 +25 -0
- package/package.json +1 -1
- package/qtools-library-dot-d.js +180 -175
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.
|
|
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": {
|
package/qtools-library-dot-d.js
CHANGED
|
@@ -10,213 +10,218 @@ const path = require('path');
|
|
|
10
10
|
const fs = require('fs');
|
|
11
11
|
|
|
12
12
|
//START OF moduleFunction() ============================================================
|
|
13
|
-
const moduleFunction =
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const moduleFunction =
|
|
14
|
+
({ moduleName } = {}) =>
|
|
15
|
+
({ libraryName }) => {
|
|
16
|
+
// ======================================================================================
|
|
17
|
+
// SETTERS
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
require(item.endpointPath)(item.moduleArgs);
|
|
38
|
-
callback();
|
|
39
|
-
};
|
|
61
|
+
const setCustomAddFunction = (newFunction) => (add = newFunction);
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
// ======================================================================================
|
|
64
|
+
// LOG LIST OBJECT
|
|
42
65
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
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,
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
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
|
-
|
|
155
|
+
// ======================================================================================
|
|
156
|
+
// LOAD MODULES (actual execution)
|
|
143
157
|
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
148
|
-
next(err, {
|
|
149
|
-
...args,
|
|
150
|
-
...(propertyName ? { [propertyName]: result } : result)
|
|
151
|
-
});
|
|
165
|
+
const taskList = new taskListPlus();
|
|
152
166
|
|
|
153
|
-
|
|
167
|
+
// --------------------------------------------------------------------------------
|
|
168
|
+
// INTERNAL UTILITIES
|
|
154
169
|
|
|
155
|
-
|
|
156
|
-
|
|
170
|
+
const mergeArgs = (args, next, propertyName) => (err, result) =>
|
|
171
|
+
next(err, {
|
|
172
|
+
...args,
|
|
173
|
+
...(propertyName ? { [propertyName]: result } : result),
|
|
174
|
+
});
|
|
157
175
|
|
|
158
|
-
|
|
176
|
+
const forwardArgs =
|
|
177
|
+
({ next, args }) =>
|
|
178
|
+
(err) =>
|
|
179
|
+
next(err, args);
|
|
159
180
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
passThroughParameters
|
|
163
|
-
};
|
|
181
|
+
// --------------------------------------------------------------------------------
|
|
182
|
+
// INITIALIZE COMMON ADDED MODULE PARAMETERS
|
|
164
183
|
|
|
165
|
-
|
|
166
|
-
// BUILD TASKLIST
|
|
184
|
+
const dotD = { logList, library };
|
|
167
185
|
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
|
|
201
|
-
|
|
202
|
-
const { libraryDotD } = args;
|
|
203
|
+
// --------------------------------------------------------------------------------
|
|
204
|
+
// INIT AND EXECUTE THE PIPELINE
|
|
203
205
|
|
|
204
|
-
callback
|
|
205
|
-
|
|
206
|
-
|
|
206
|
+
if (typeof callback == 'function') {
|
|
207
|
+
callback();
|
|
208
|
+
} else {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
207
212
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
|