binhend 2.0.4 → 2.0.6
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/package.json +1 -1
- package/src/web/component.method.js +3 -1
- package/src/web/index.js +2 -0
- package/src/web/binh.web.builder.js +0 -216
- package/src/z_api.js +0 -133
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const UglifyCSS = require('uglifycss');
|
|
4
5
|
const Component = require('./component');
|
|
6
|
+
const { isString } = require('../utils/typeOf');
|
|
5
7
|
|
|
6
8
|
const binh = {};
|
|
7
9
|
|
package/src/web/index.js
CHANGED
|
@@ -3,7 +3,9 @@ const path = require('path');
|
|
|
3
3
|
const { isEmptyString, isString } = require('../utils/typeOf');
|
|
4
4
|
const ComponentFormat = require('./component.format');
|
|
5
5
|
const ComponentBuild = require('./component.build');
|
|
6
|
+
const Component = require('./component');
|
|
6
7
|
const { server } = require('../server');
|
|
8
|
+
const express = require('express');
|
|
7
9
|
|
|
8
10
|
function WebBuilder(source, { output } = {}) {
|
|
9
11
|
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
|
|
5
|
-
const Component = require('./component');
|
|
6
|
-
const ComponentFormat = require('./component.format');
|
|
7
|
-
const ComponentBuild = require('./component.build');
|
|
8
|
-
|
|
9
|
-
const UglifyCSS = require('uglifycss');
|
|
10
|
-
|
|
11
|
-
function WebBuilder(binh, Binh) {
|
|
12
|
-
binh.context = function(module, defaultRequire) {
|
|
13
|
-
var context = { module, require: defaultRequire };
|
|
14
|
-
return {
|
|
15
|
-
context,
|
|
16
|
-
tag: tag.bind(context),
|
|
17
|
-
svg: svg.bind(context),
|
|
18
|
-
script: script.bind(context),
|
|
19
|
-
require: customRequire.bind(context),
|
|
20
|
-
css: css.bind(context)
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function tagnames(tagNames) {
|
|
25
|
-
return typeof tagNames === 'string' ? tagNames.split(/\s+/) : [];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function tag(tagNames) {
|
|
29
|
-
var htmltags = this.component.htmltags;
|
|
30
|
-
htmltags.push.apply(htmltags, tagnames(tagNames));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function svg(tagNames) {
|
|
34
|
-
var svgtags = this.component.svgtags;
|
|
35
|
-
svgtags.push.apply(svgtags, tagnames(tagNames));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function script() {
|
|
39
|
-
var scripts = Array.from(arguments);
|
|
40
|
-
var links = this.component.links;
|
|
41
|
-
links.push.apply(links, scripts);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function customRequire(filePath) {
|
|
45
|
-
filePath = filePath.endsWith('.css') ? (filePath+'.js') : filePath;
|
|
46
|
-
return this.require(filePath);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function css() {
|
|
50
|
-
var csses = Array.from(arguments);
|
|
51
|
-
var appliedStyles = this.component.options.csses;
|
|
52
|
-
appliedStyles.push.apply(appliedStyles, csses);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
binh.component = function(context, ui, service, style) {
|
|
56
|
-
var component = ui || service || style;
|
|
57
|
-
var type = ui && 'ui' || service && 'service' || style && 'style';
|
|
58
|
-
|
|
59
|
-
if (!(component instanceof Function)) return;
|
|
60
|
-
|
|
61
|
-
context.component = component;
|
|
62
|
-
|
|
63
|
-
var module = context.module;
|
|
64
|
-
module.exports = component;
|
|
65
|
-
|
|
66
|
-
component.module = module;
|
|
67
|
-
component.constructor = Component;
|
|
68
|
-
component.filename = module.filename;
|
|
69
|
-
component.type = type;
|
|
70
|
-
|
|
71
|
-
component.htmltags = [];
|
|
72
|
-
component.svgtags = [];
|
|
73
|
-
component.links = [];
|
|
74
|
-
component.options = { csses: [] };
|
|
75
|
-
component.vars = {};
|
|
76
|
-
component.varname = getVariableName(component.filename);
|
|
77
|
-
component.as = alias;
|
|
78
|
-
component.eachChild = loopComponentChildren;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
binh.final = function(module) {
|
|
82
|
-
var component = module.exports;
|
|
83
|
-
|
|
84
|
-
component.eachChild(childComponent => {
|
|
85
|
-
if (childComponent.alias) {
|
|
86
|
-
component.vars[childComponent.filename] = childComponent.alias;
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
function loopComponentChildren(callback) {
|
|
92
|
-
this.module.children.forEach(childModule => {
|
|
93
|
-
var childComponent = childModule.exports;
|
|
94
|
-
if (!(childComponent instanceof Function) || childComponent.constructor !== Component) return;
|
|
95
|
-
callback(childComponent);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
binh.ui = function(module, component, htmltags, links) {
|
|
100
|
-
binh.component({ module }, component);
|
|
101
|
-
component.htmltags = htmltags;
|
|
102
|
-
component.links = links;
|
|
103
|
-
binh.final(module);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
binh.service = function(module, component, links) {
|
|
107
|
-
binh.component({ module }, null, component, null);
|
|
108
|
-
component.links = links;
|
|
109
|
-
binh.final(module);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
binh.style = function(module, component) {
|
|
113
|
-
binh.component({ module }, null, null, component);
|
|
114
|
-
binh.final(module);
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
binh.css = function(module, cssFilename) {
|
|
118
|
-
var cssFilePath = cssFilename && path.join(module.path, cssFilename) || module.filename.replace(/.js$/, '');
|
|
119
|
-
var content = fs.readFileSync(cssFilePath, { encoding: 'utf8', flag: 'r' });
|
|
120
|
-
content = UglifyCSS.processString(content);
|
|
121
|
-
var component = new Function(`return function style() { return ${JSON.stringify(content)}; };`)();
|
|
122
|
-
component.cssFilePath = cssFilePath;
|
|
123
|
-
binh.component({ module }, null, null, component);
|
|
124
|
-
binh.final(module);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
function alias(name) {
|
|
128
|
-
if (!isString(name)) return this;
|
|
129
|
-
name = name.trim();
|
|
130
|
-
this.alias = name;
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function getVariableName(filePath) {
|
|
135
|
-
return path.parse(filePath).name.replace(/-/g, '').replace(/\W.*/, '').trim();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
Binh.minify = function(flag = true) {
|
|
139
|
-
Component.minification = flag;
|
|
140
|
-
return Binh;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
Binh.webStatic = function(webPath) {
|
|
144
|
-
Binh.web.value = getAbsolutePath(webPath);
|
|
145
|
-
return Binh;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
Binh.webModule = function({ source, module }, onDone) {
|
|
149
|
-
if (!(isString(source) && isString(module))) {
|
|
150
|
-
throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source and module. Current: { source: ${source}, module: ${module} }`);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
var { source, module } = getAbsolutePaths({ source, module });
|
|
154
|
-
|
|
155
|
-
ComponentFormat.generate(source, module, onDone);
|
|
156
|
-
|
|
157
|
-
return Binh;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
Binh.webModuleBundle = function({ source, module, web }) {
|
|
161
|
-
var { source, module, web } = processWebBuildPaths({ source, module, web });
|
|
162
|
-
ComponentBuild.bundle({ source, module, web });
|
|
163
|
-
return Binh;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
Binh.webModuleLazy = function({ source, module, web }) {
|
|
167
|
-
var { source, module, web } = processWebBuildPaths({ source, module, web });
|
|
168
|
-
ComponentBuild.lazyload({ source, module, web });
|
|
169
|
-
return Binh;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
Binh.webBundle = function({ source, module, web }) {
|
|
173
|
-
var absolutePaths = processWebBuildPaths({ source, module, web });
|
|
174
|
-
return Binh.webModule({ source, module }, () => ComponentBuild.bundle(absolutePaths));
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
Binh.webLazy = function({ source, module, web, external }) {
|
|
178
|
-
var absolutePaths = processWebBuildPaths({ source, module, web });
|
|
179
|
-
absolutePaths.external = external || 'NPM';
|
|
180
|
-
return Binh.webModule({ source, module }, () => ComponentBuild.lazyload(absolutePaths));
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
Binh.web = Binh.webBundle;
|
|
184
|
-
|
|
185
|
-
function getAbsolutePath(relativePath) {
|
|
186
|
-
return isString(relativePath) ? path.join(Binh.getRootpath(), relativePath) : null;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function getAbsolutePaths({ source, module, web }) {
|
|
190
|
-
return {
|
|
191
|
-
source: getAbsolutePath(source),
|
|
192
|
-
module: getAbsolutePath(module),
|
|
193
|
-
web: getAbsolutePath(web)
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function processWebBuildPaths({ source, module, web }) {
|
|
198
|
-
if (!(isString(source) && isString(module) && isString(web))) {
|
|
199
|
-
throw new Error(`[BINHEND][WEB-BUILDER] Require paths for source, module and web. Current: { source: ${source}, module: ${module}, web: ${web} }`);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
var absolutePaths = getAbsolutePaths({ source, module, web });
|
|
203
|
-
|
|
204
|
-
Binh.web.value = absolutePaths.web;
|
|
205
|
-
|
|
206
|
-
return absolutePaths;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function isString(input) {
|
|
210
|
-
return typeof input === 'string';
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
module.exports = {
|
|
215
|
-
WebBuilder
|
|
216
|
-
};
|
package/src/z_api.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
const { Module } = require('module');
|
|
2
|
-
const { readdirSync, statSync } = require('fs');
|
|
3
|
-
const { join, parse, resolve } = require('path');
|
|
4
|
-
const { isUndefined, isFunction } = require('./utils/typeOf');
|
|
5
|
-
const { ServerResponse } = require('http');
|
|
6
|
-
|
|
7
|
-
const express = require('express');
|
|
8
|
-
const { server } = require('./server');
|
|
9
|
-
const parseBasicAuthToken = require('./middleware/parseBasicAuthToken');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function mapRoutes(router, dirpath) {
|
|
13
|
-
readdirSync(dirpath).forEach(function(direntName) {
|
|
14
|
-
var path = join(dirpath, direntName),
|
|
15
|
-
route = parse(direntName).name;
|
|
16
|
-
|
|
17
|
-
var stat = statSync(path);
|
|
18
|
-
var childRouter;
|
|
19
|
-
|
|
20
|
-
if (stat.isFile()) {
|
|
21
|
-
childRouter = require(path);
|
|
22
|
-
}
|
|
23
|
-
else if (stat.isDirectory()) {
|
|
24
|
-
childRouter = Router().router;
|
|
25
|
-
mapRoutes(childRouter, path);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
router.use(`/${route}`, childRouter);
|
|
29
|
-
console.log('[BINHEND] Mapping routes from:', path);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function loadRoutes(dirPath) {
|
|
34
|
-
if (isUndefined(dirPath)) {
|
|
35
|
-
console.error(`[BINHEND] Error missing directory path: ${dirPath}.\n`);
|
|
36
|
-
throw new Error('Require directory path to load routes.');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const router = express.Router();
|
|
40
|
-
router.use(express.urlencoded({ extended: false }));
|
|
41
|
-
router.use(express.json());
|
|
42
|
-
router.use(parseBasicAuthToken);
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
mapRoutes(router, resolve(dirPath));
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
console.error(`[BINHEND] Error mapping routes from: ${resolve(dirPath)}.\n` + error);
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
server.use(router);
|
|
53
|
-
|
|
54
|
-
return router;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function Router(moduleInstance) {
|
|
58
|
-
var router = express.Router();
|
|
59
|
-
|
|
60
|
-
var output = { router };
|
|
61
|
-
|
|
62
|
-
for (var key in router) {
|
|
63
|
-
let method = router[key]; // use 'let' to not lose reference, 'var' will override 'method' reference in this function scope => error on run-time
|
|
64
|
-
|
|
65
|
-
if (!isFunction(method)) continue;
|
|
66
|
-
|
|
67
|
-
output[key] = function() {
|
|
68
|
-
let args = [];
|
|
69
|
-
|
|
70
|
-
Array.from(arguments).forEach((arg) => {
|
|
71
|
-
if (!isFunction(arg)) return args.push(arg);
|
|
72
|
-
args.push(createErrorHandler(router, arg));
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return method.apply(router, args);
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (moduleInstance instanceof Module) {
|
|
80
|
-
moduleInstance.exports = router;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
output.trycatch = trycatch;
|
|
84
|
-
|
|
85
|
-
return output;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function createErrorHandler(router, callback) {
|
|
89
|
-
if (callback.isAppliedTryCatch) return callback;
|
|
90
|
-
|
|
91
|
-
return async function(request, response, next) {
|
|
92
|
-
try {
|
|
93
|
-
return await callback.apply(router, arguments);
|
|
94
|
-
}
|
|
95
|
-
catch (error) {
|
|
96
|
-
if (arguments.length < 3 || !(response instanceof ServerResponse)) throw error;
|
|
97
|
-
responseErrorByDefault(error, response, next);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function trycatch(callback) {
|
|
103
|
-
var trycatchCallback = async function(request, response, next) {
|
|
104
|
-
try {
|
|
105
|
-
return await callback(request, response, next);
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
responseErrorByDefault(error, response, next);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
trycatchCallback.isAppliedTryCatch = true;
|
|
113
|
-
|
|
114
|
-
return trycatchCallback;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function responseErrorByDefault(error, response, next) {
|
|
118
|
-
if (!(error instanceof HttpError)) return next(error);
|
|
119
|
-
response.status(error.httpCode || 500).json({ error: error.message || 'Internal Server Error' });
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
class HttpError extends Error {
|
|
123
|
-
constructor(httpCode, message) {
|
|
124
|
-
super(message);
|
|
125
|
-
this.name = 'HttpError';
|
|
126
|
-
this.httpCode = httpCode;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
module.exports = {
|
|
131
|
-
loadRoutes, mapRoutes,
|
|
132
|
-
trycatch, HttpError, Router
|
|
133
|
-
};
|