qtools-library-dot-d 1.0.0

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 ADDED
@@ -0,0 +1,31 @@
1
+ # qtools-library-dot-d
2
+
3
+ # NOT READY. STILL WORKING IT OUT.
4
+
5
+ NAME
6
+
7
+     qtools-library-dot-d
8
+
9
+ DESCRIPTION
10
+
11
+ Utility provides tools to scan a directory for NodeJS modules, require() them into existance with tools to connect them to a system.
12
+
13
+
14
+
15
+ First use case is adding endpoints to an expressJS system. When instantiated, qtDotD
16
+
17
+ scans a directory containing modules, dotLib.d. It requires it with two parameters,
18
+
19
+ dotD and passThroughArgs. For this case, passThrough args has two properties,
20
+
21
+ expressApp and routingPrefix. The module uses these to construct one or more
22
+
23
+ expressJS endpoitns.
24
+
25
+
26
+
27
+ the dotD object contains three parameters, add....
28
+
29
+
30
+
31
+
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "qtools-library-dot-d",
3
+ "version": "1.0.0",
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
+ "main": "qtools-library-dot-d.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "TQ White II",
10
+ "license": "ISC"
11
+ }
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const moduleName = __filename.replace(__dirname + '/', '').replace(/.js$/, ''); //this just seems to come in handy a lot
5
+
6
+ // const qt = require('qtools-functional-library'); //also exposes qtLog(); qt.help({printOutput:true, queryString:'.*', sendJson:false});
7
+
8
+ const os = require('os');
9
+ const path = require('path');
10
+ const fs = require('fs');
11
+
12
+ console.log(`HELLO FROM ${__dirname}/${moduleName}`);
13
+ //START OF moduleFunction() ============================================================
14
+ const moduleFunction = ({ moduleName } = {}) => ({ libraryName }) => {
15
+ // ======================================================================================
16
+ // SETTERS
17
+
18
+ // --------------------------------------------------------------------------------
19
+ // SET LIBRARY PATH
20
+
21
+ let libraryPath;
22
+
23
+ const setLibraryPath = (dirPath, callback) => {
24
+ try {
25
+ libraryPath = fs.realpathSync(dirPath);
26
+ } catch (err) {
27
+ callback(err.toString());
28
+ return;
29
+ }
30
+
31
+ callback('', libraryPath);
32
+ };
33
+
34
+ // --------------------------------------------------------------------------------
35
+ // SET CUSTOM MODULE ADDING FUNCTION
36
+
37
+ const defaultAdd = (item, callback) => {
38
+ require(item.endpointPath)(item.moduleArgs);
39
+ callback();
40
+ };
41
+
42
+ let add = defaultAdd;
43
+
44
+ const setCustomAddFunction = newFunction => (add = newFunction);
45
+
46
+
47
+ // ======================================================================================
48
+ // LOG LIST OBJECT
49
+
50
+ const logList = (() => {
51
+ const list = [];
52
+ const push = item => list.push(item);
53
+ const toString = () => {
54
+ return list
55
+ .reduce(
56
+ (result, item) => `${result}${item}\n\t`,
57
+ `\n${libraryName} Log\n\t`
58
+ )
59
+ .replace(/\n\t$/, '');
60
+ };
61
+ return { push, toString };
62
+ })();
63
+
64
+
65
+ // ======================================================================================
66
+ // LIBRARY OBJECT
67
+
68
+ const library = (() => {
69
+ const list = {};
70
+ const add = (name, item) => (list[name] = item);
71
+ const toString = () => {
72
+ return Object.keys(list)
73
+ .reduce(
74
+ (result, name) => `${result}${name}: ${(typeof(list[name])).match(/(string|number)/i)?list[name]:typeof(list[name])}\n\t`,
75
+ `\n${libraryName} Library\n\t`
76
+ )
77
+ .replace(/\n\t$/, '');
78
+ };
79
+ return { add, toString };
80
+ })();
81
+
82
+ // ======================================================================================
83
+ // LOAD MODULES (actual execution)
84
+
85
+ const loadModules = ({ passThroughParameters }, callback) => {
86
+ const asynchronousPipePlus = new require('qtools-asynchronous-pipe-plus')();
87
+ const pipeRunner = asynchronousPipePlus.pipeRunner;
88
+ const taskListPlus = asynchronousPipePlus.taskListPlus;
89
+
90
+ const taskList = new taskListPlus();
91
+
92
+ // --------------------------------------------------------------------------------
93
+ // INTERNAL UTILITIES
94
+
95
+ const mergeArgs = (args, next, propertyName) => (err, result) =>
96
+ next(err, {
97
+ ...args,
98
+ ...(propertyName ? { [propertyName]: result } : result)
99
+ });
100
+
101
+ const forwardArgs = ({ next, args }) => err => next(err, args);
102
+
103
+ // --------------------------------------------------------------------------------
104
+ // INITIALIZE COMMON ADDED MODULE PARAMETERS
105
+
106
+ const dotD = { logList, library };
107
+
108
+ const moduleArgs = {
109
+ dotD,
110
+ passThroughParameters
111
+ };
112
+
113
+ // --------------------------------------------------------------------------------
114
+ // BUILD TASKLIST
115
+
116
+ taskList.push((args, next) => {
117
+ const { libraryPath, passThroughParameters } = args;
118
+
119
+ const localCallback = (err, result) => {
120
+ next(err, { ...args, result });
121
+ };
122
+
123
+ fs
124
+ .readdirSync(libraryPath)
125
+ //.filter(file => path.extname(file) === '.js')
126
+ .forEach(file => {
127
+ // --------------------------------------------------------------------------------
128
+ // TASKLIST ITEM TEMPLATE
129
+ taskList.push((args, next) => {
130
+ const { libraryPath, passThroughParameters } = args;
131
+
132
+ const endpointPath = path.join(libraryPath, file);
133
+ add(
134
+ {
135
+ endpointPath,
136
+ moduleArgs
137
+ },
138
+ forwardArgs({ next, args })
139
+ );
140
+ });
141
+ });
142
+ localCallback('', args);
143
+ });
144
+
145
+ // --------------------------------------------------------------------------------
146
+ // INIT AND EXECUTE THE PIPELINE
147
+
148
+ const initialData = { libraryPath, passThroughParameters };
149
+ pipeRunner(taskList.getList(), initialData, (err, args) => {
150
+ const { libraryDotD } = args;
151
+
152
+ callback(err);
153
+ });
154
+ };
155
+
156
+ return {
157
+ setLibraryPath,
158
+ setCustomAddFunction,
159
+ logList,
160
+ library,
161
+ loadModules
162
+ };
163
+ };
164
+
165
+ //END OF moduleFunction() ============================================================
166
+
167
+ module.exports = moduleFunction({ moduleName });
168
+