mushroom-cli 1.2.0-alpha.9 → 1.2.1
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 +208 -161
- package/dist/check-update.js +6 -0
- package/dist/common.js +284 -0
- package/dist/config.js +394 -0
- package/dist/docs/bootstrap-theme.min.css +6 -0
- package/dist/docs/bootstrap.min.css +6 -0
- package/dist/docs/bootstrap.min.js +7 -0
- package/dist/docs/doc-mt.js +633 -0
- package/dist/docs/docs.html +5380 -0
- package/{docs → dist/docs}/docs.v2.html +5206 -5065
- package/dist/docs/docs.v2.js +2037 -0
- package/dist/docs/jquery.min.js +4 -0
- package/dist/docs/mt-dom-1.0.js +1398 -0
- package/dist/docs/mt.extension.js +199 -0
- package/dist/gen/gen-api-docs-v1.js +110 -0
- package/dist/gen/gen-api-docs.js +327 -0
- package/{gen → dist/gen}/gen-csharp.js +1256 -1399
- package/{gen → dist/gen}/gen-db-docs.js +204 -222
- package/{gen → dist/gen}/gen-js-driver-tarball.js +81 -101
- package/{gen → dist/gen}/gen-js-driver.js +116 -133
- package/dist/gen/gen-ts-driver.js +600 -0
- package/{gen.js → dist/gen.js} +109 -124
- package/{help.js → dist/help.js} +53 -55
- package/dist/init/init-json.js +92 -0
- package/dist/init/init-netcore.js +30 -0
- package/dist/init.js +23 -0
- package/dist/master-file/master-file.js +185 -0
- package/dist/merge.js +70 -0
- package/dist/mr.js +77 -0
- package/dist/types.js +1 -0
- package/dist/update/tsCode.js +82 -0
- package/dist/update.js +20 -0
- package/package.json +64 -29
- package/check-update.js +0 -9
- package/common.js +0 -273
- package/config.js +0 -445
- package/docs/doc-mt.js +0 -523
- package/docs/docs.html +0 -5190
- package/docs/docs.v2.js +0 -1558
- package/docs/mt-dom-1.0.js +0 -1268
- package/docs/mt.extension.js +0 -174
- package/gen/gen-api-docs-v1.js +0 -125
- package/gen/gen-api-docs.js +0 -298
- package/gen/gen-ts-driver.js +0 -471
- package/init/init-json.js +0 -106
- package/init/init-netcore.js +0 -32
- package/init.js +0 -28
- package/master-file/master-file.js +0 -234
- package/merge.js +0 -84
- package/mr.js +0 -82
- package/update/tsCode.js +0 -20
- package/update.js +0 -24
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
var urls = {};
|
|
3
|
+
function loadFile(url, successCallback, errorCallback) {
|
|
4
|
+
if (url in urls) {
|
|
5
|
+
var args = urls[url];
|
|
6
|
+
args.cached = true;
|
|
7
|
+
setTimeout(successCallback, 1, args);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
var xhr = new XMLHttpRequest();
|
|
11
|
+
xhr.onreadystatechange = function () {
|
|
12
|
+
if (this.readyState !== 4) return;
|
|
13
|
+
if (this.status < 200 || this.status >= 300) {
|
|
14
|
+
if (typeof this.debugMode === "function" && this.debugMode())
|
|
15
|
+
console.error("Unable to load: " + url);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
var args = { content: this.responseText };
|
|
19
|
+
urls[url] = args;
|
|
20
|
+
if (typeof successCallback === "function") successCallback(args);
|
|
21
|
+
};
|
|
22
|
+
xhr.onerror = function () {
|
|
23
|
+
if (typeof this.debugMode === "function" && this.debugMode())
|
|
24
|
+
console.error("Error occurred when loading: " + url);
|
|
25
|
+
if (typeof errorCallback === "function") errorCallback();
|
|
26
|
+
};
|
|
27
|
+
xhr.onabort = function () {
|
|
28
|
+
if (typeof this.debugMode === "function" && this.debugMode())
|
|
29
|
+
console.error("Connection aborted: " + url);
|
|
30
|
+
if (typeof errorCallback === "function") errorCallback();
|
|
31
|
+
};
|
|
32
|
+
xhr.ontimeout = function () {
|
|
33
|
+
if (typeof this.debugMode === "function" && this.debugMode())
|
|
34
|
+
console.error("Connection timeout: " + url);
|
|
35
|
+
if (typeof errorCallback === "function") errorCallback();
|
|
36
|
+
};
|
|
37
|
+
xhr.open("GET", url, true);
|
|
38
|
+
xhr.send(null);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
mt.modules = {};
|
|
42
|
+
mt.bindModule = function (module, dom, data, session) {
|
|
43
|
+
if (!module || !dom) return;
|
|
44
|
+
|
|
45
|
+
loadFile(
|
|
46
|
+
"/modules/" + module + "/" + module + "-view.html",
|
|
47
|
+
function (response) {
|
|
48
|
+
dom.innerHTML =
|
|
49
|
+
"<link rel='stylesheet' href='/modules/" +
|
|
50
|
+
module +
|
|
51
|
+
"/" +
|
|
52
|
+
module +
|
|
53
|
+
"-style.min.css' />" +
|
|
54
|
+
response.content;
|
|
55
|
+
loadFile(
|
|
56
|
+
"/modules/" + module + "/" + module + "-script.js",
|
|
57
|
+
function (resp) {
|
|
58
|
+
if (!resp.content) return;
|
|
59
|
+
|
|
60
|
+
if (!resp.cached) {
|
|
61
|
+
try {
|
|
62
|
+
eval(resp.content);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
throw "Invalid module script";
|
|
65
|
+
}
|
|
66
|
+
if (!mt.modules[module]) throw "Invalid module script";
|
|
67
|
+
if (typeof mt.modules[module].init === "function")
|
|
68
|
+
mt.modules[module].init();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
var config = mt.modules[module];
|
|
72
|
+
|
|
73
|
+
var args = {
|
|
74
|
+
dom: dom,
|
|
75
|
+
data: data,
|
|
76
|
+
fnPool: config.fn,
|
|
77
|
+
session: session,
|
|
78
|
+
};
|
|
79
|
+
if (typeof config.prebinding === "function") {
|
|
80
|
+
config.prebinding(args);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
mt.bindDom(dom, args.data, args.fnPool, session);
|
|
84
|
+
|
|
85
|
+
if (typeof config.load === "function") {
|
|
86
|
+
args.dom = dom;
|
|
87
|
+
args.session = session;
|
|
88
|
+
config.load(args);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
function () {
|
|
92
|
+
mt.bindDom(dom, data, null, session);
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#region mt-link
|
|
100
|
+
if (mt["default"] === undefined) mt["default"] = {};
|
|
101
|
+
if (mt["default"].mtLink === undefined) mt["default"].mtLink = {};
|
|
102
|
+
// cho phép mở rộng được về sau: ví dụ thay thế bằng custom dialog
|
|
103
|
+
mt["default"].mtLink.itemClickHandler = function (dom, message, callback) {
|
|
104
|
+
if (confirm(message)) callback(dom);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function mtLinkHandler(dom) {
|
|
108
|
+
mt.bindModule(
|
|
109
|
+
dom.__mtLinkConfig.module,
|
|
110
|
+
document.getElementById("page-wrapper"),
|
|
111
|
+
dom.__mtLinkConfig.params,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function mtLinkClicked(e) {
|
|
116
|
+
if (this.__mtLinkConfig.confirm)
|
|
117
|
+
mt["default"].mtLink.itemClickHandler(
|
|
118
|
+
this,
|
|
119
|
+
this.__mtLinkConfig.confirm,
|
|
120
|
+
mtLinkHandler,
|
|
121
|
+
);
|
|
122
|
+
else mtLinkHandler(this);
|
|
123
|
+
|
|
124
|
+
e = e || window.event;
|
|
125
|
+
e.stopPropagation();
|
|
126
|
+
e.preventDefault();
|
|
127
|
+
e.cancelBubble = true;
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
mt.registerDomFeature("mtLink", function (arg) {
|
|
132
|
+
if (arg.dom.nodeName !== "A") {
|
|
133
|
+
arg.dom.dataset.mtLinkError = "mtLink feature only affects <a> tag";
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!arg.config.module) {
|
|
138
|
+
arg.dom.dataset.mtLinkError = "mtLink feature: missing module";
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
arg.dom.__mtLinkConfig = mt.fnUtils.mergeData({
|
|
143
|
+
mask: arg.config,
|
|
144
|
+
data: arg.data,
|
|
145
|
+
session: arg.session,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (!arg.dom.href) arg.dom.href = "javascript:void(0)";
|
|
149
|
+
|
|
150
|
+
mt.fnUtils.addEventListener(arg.dom, "click", mtLinkClicked, false);
|
|
151
|
+
|
|
152
|
+
delete arg.dom.dataset.mtLink;
|
|
153
|
+
});
|
|
154
|
+
//#endregion
|
|
155
|
+
|
|
156
|
+
//#region mt-module
|
|
157
|
+
mt.registerDomFeature("mtModule", function (arg) {
|
|
158
|
+
if (!arg.config.module) {
|
|
159
|
+
arg.dom.dataset.mtModuleError = "mtModule feature: missing module";
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
var params = mt.fnUtils.mergeData({
|
|
164
|
+
mask: arg.config.params,
|
|
165
|
+
data: arg.data,
|
|
166
|
+
session: arg.session,
|
|
167
|
+
});
|
|
168
|
+
mt.bindModule(arg.config.module, arg.dom, params, arg.session);
|
|
169
|
+
arg.mediator.skipInner = true;
|
|
170
|
+
|
|
171
|
+
delete arg.dom.dataset.mtModule;
|
|
172
|
+
});
|
|
173
|
+
//#endregion
|
|
174
|
+
|
|
175
|
+
//#region mt-inline-module
|
|
176
|
+
mt.inlineModuleTemplates = {};
|
|
177
|
+
mt.registerDomFeature("mtInlineModule", function (arg) {
|
|
178
|
+
var config = mt.fnUtils.mergeData({
|
|
179
|
+
mask: arg.config,
|
|
180
|
+
data: arg.data,
|
|
181
|
+
session: arg.session,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
if (!config.module) {
|
|
185
|
+
arg.dom.dataset.mtInlineModuleError =
|
|
186
|
+
"mtInlineModule feature: missing module";
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
arg.dom.innerHTML = mt.inlineModuleTemplates[config.module] || "";
|
|
191
|
+
|
|
192
|
+
delete arg.dom.dataset.mtInlineModule;
|
|
193
|
+
|
|
194
|
+
mt.bindDom(arg.dom, config.params);
|
|
195
|
+
|
|
196
|
+
arg.mediator.skipInner = true;
|
|
197
|
+
});
|
|
198
|
+
//#endregion
|
|
199
|
+
})();
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import open from 'open';
|
|
3
|
+
import jsdom, { JSDOM } from 'jsdom';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import * as common from '../common.js';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
function processGenDocs(aliasProject, entities, source, html, args, projectName, alias) {
|
|
10
|
+
common.logInfo('Received html');
|
|
11
|
+
common.logInfo('Parsing html');
|
|
12
|
+
const roles = aliasProject.allowRoles?.includes('*')
|
|
13
|
+
? source.roles
|
|
14
|
+
: source.roles?.filter((r) => aliasProject.allowRoles?.includes(r.name));
|
|
15
|
+
html = html.replace(/^\uFEFF/, '');
|
|
16
|
+
html = html.replace(/<script src="([^"]+)" data-mt-cond='\{"lazy-load": 1\}'><\/script>/g, (scriptTag, src) => `<script data-mt-cond='{"x": 1}'>${fs.readFileSync(__dirname + '/../docs/' + src).toString()}</script>`);
|
|
17
|
+
html = html.replace('</body>', `<script data-mt-cond='{"x": 1}'>
|
|
18
|
+
window.data.name = ${JSON.stringify(source.name)};
|
|
19
|
+
window.data.description = ${JSON.stringify(source.description)};
|
|
20
|
+
window.data.rootUrl = ${JSON.stringify(common.getRootUrl(source))};
|
|
21
|
+
window.data.environmentVariables = ${JSON.stringify(source.environmentVariables)};
|
|
22
|
+
window.data.roles = ${JSON.stringify(roles)};
|
|
23
|
+
window.data.entities = ${JSON.stringify(entities)};
|
|
24
|
+
window.data.features = ${JSON.stringify(aliasProject.features)};
|
|
25
|
+
try {
|
|
26
|
+
console.info("MUSHROOM-CLI: START");
|
|
27
|
+
window.generateDocument();
|
|
28
|
+
console.info("MUSHROOM-CLI: FINISH");
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
console.info("MUSHROOM-CLI: FAIL", e);
|
|
32
|
+
}
|
|
33
|
+
window.close();
|
|
34
|
+
</script></body>`);
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const virtualConsole = new jsdom.VirtualConsole();
|
|
37
|
+
virtualConsole.on('info', function (msg, err) {
|
|
38
|
+
if (typeof msg != 'string')
|
|
39
|
+
return;
|
|
40
|
+
if (msg.indexOf('MUSHROOM-CLI:') === 0) {
|
|
41
|
+
common.logInfo('GenDocEngine:', msg.substring('MUSHROOM-CLI:'.length));
|
|
42
|
+
}
|
|
43
|
+
if (msg === 'MUSHROOM-CLI: FAIL') {
|
|
44
|
+
reject(err);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (msg !== 'MUSHROOM-CLI: FINISH')
|
|
48
|
+
return;
|
|
49
|
+
const code = '<!DOCTYPE html>' + dom.window.document.documentElement.outerHTML;
|
|
50
|
+
if (args[3] === 'tmp') {
|
|
51
|
+
const htmlFilePath = aliasProject.output + '_tmp.html';
|
|
52
|
+
common.logInfo('Writing to', htmlFilePath);
|
|
53
|
+
fs.writeFile(htmlFilePath, code, (err) => {
|
|
54
|
+
if (err) {
|
|
55
|
+
reject(err);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
common.logInfo('Opening file', htmlFilePath);
|
|
59
|
+
open(htmlFilePath)
|
|
60
|
+
.then(() => {
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
fs.unlink(htmlFilePath, (ex) => {
|
|
63
|
+
if (ex)
|
|
64
|
+
common.logError('Unable to delete file', htmlFilePath);
|
|
65
|
+
});
|
|
66
|
+
}, 1000);
|
|
67
|
+
})
|
|
68
|
+
.catch(() => common.logError('Unable to open', htmlFilePath));
|
|
69
|
+
resolve();
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
common.ensureFolder(aliasProject.output);
|
|
74
|
+
common.logInfo('Writing to', aliasProject.output);
|
|
75
|
+
fs.writeFile(aliasProject.output, code, (err) => {
|
|
76
|
+
if (err) {
|
|
77
|
+
reject(err);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
common.logInfo(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);
|
|
81
|
+
resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
const dom = new JSDOM(html, {
|
|
85
|
+
runScripts: 'dangerously',
|
|
86
|
+
resources: 'usable',
|
|
87
|
+
virtualConsole: virtualConsole,
|
|
88
|
+
});
|
|
89
|
+
common.logInfo('Rendering API document');
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export default async function genDocs(args, projectName, project, alias, source) {
|
|
93
|
+
const aliasProject = ((project.docs || {}).alias || {})[alias];
|
|
94
|
+
if (!aliasProject) {
|
|
95
|
+
throw new Error(`Not found gen docs configuration. Please run 'npx mushroom-cli config ${projectName} gen docs OUTPUT_FILE.html${alias === 'default' ? '' : ' alias ' + alias}' first`);
|
|
96
|
+
}
|
|
97
|
+
common.logWarning('[DEPRECATED] Use GenDocEngine v1');
|
|
98
|
+
const entities = common.getEntities(source, aliasProject.allowRoles);
|
|
99
|
+
if (Array.isArray(aliasProject.entities)) {
|
|
100
|
+
for (const entityName in entities) {
|
|
101
|
+
if (Object.hasOwnProperty.call(entities, entityName)) {
|
|
102
|
+
if (aliasProject.entities.every((re) => !new RegExp(re).test(entityName))) {
|
|
103
|
+
delete entities[entityName];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const html = fs.readFileSync(__dirname + '/../docs/docs.html').toString();
|
|
109
|
+
await processGenDocs(aliasProject, entities, source, html, args, projectName, alias);
|
|
110
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import open from 'open';
|
|
3
|
+
import { minify } from 'minify';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import * as common from '../common.js';
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
function reserveWhitelist(obj, whitelist) {
|
|
10
|
+
if (!obj && typeof obj !== 'object')
|
|
11
|
+
return null;
|
|
12
|
+
for (const m in obj) {
|
|
13
|
+
if (!whitelist.includes(m))
|
|
14
|
+
delete obj[m];
|
|
15
|
+
}
|
|
16
|
+
return obj;
|
|
17
|
+
}
|
|
18
|
+
const environmentVariableWhitelist = ['type', 'description'];
|
|
19
|
+
function filterEnvironmentVariables(environmentVariables, allowRoles) {
|
|
20
|
+
if (!environmentVariables)
|
|
21
|
+
return;
|
|
22
|
+
if (!allowRoles.includes('*')) {
|
|
23
|
+
Object.keys(environmentVariables).forEach((envName) => {
|
|
24
|
+
if (environmentVariables[envName].codeHint?.roles?.every((role) => !allowRoles.includes(role))) {
|
|
25
|
+
delete environmentVariables[envName];
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
Object.values(environmentVariables).forEach((env) => reserveWhitelist(env, environmentVariableWhitelist));
|
|
30
|
+
}
|
|
31
|
+
const entityWhitelist = ['name', 'description', 'actions', 'views', 'features', 'fields'];
|
|
32
|
+
const actionWhitelist = ['grant', 'deny', 'matches', 'description', 'exclude'];
|
|
33
|
+
const findByIdWhitelist = [...actionWhitelist, 'returnableFields', 'defaultFields'];
|
|
34
|
+
const findManyWhitelist = [
|
|
35
|
+
...findByIdWhitelist,
|
|
36
|
+
'allowFilteringFields',
|
|
37
|
+
'requiredFilteringFields',
|
|
38
|
+
'allowSortings',
|
|
39
|
+
'defaultSortings',
|
|
40
|
+
'pagingType',
|
|
41
|
+
'maxPageSize',
|
|
42
|
+
'pageSize',
|
|
43
|
+
'includeTotal',
|
|
44
|
+
'cache',
|
|
45
|
+
];
|
|
46
|
+
const generalWhitelist = [...actionWhitelist, 'codeHint'];
|
|
47
|
+
const entityFeatureWhitelist = ['vn_text'];
|
|
48
|
+
const vnTextWhitelist = ['fields', 'channels'];
|
|
49
|
+
const vnTextChannelWhitelist = ['fields', 'name'];
|
|
50
|
+
const actionCodeHintWhitelist = ['delegator'];
|
|
51
|
+
const actionCodeHintDelegatorWhitelist = ['param', 'requestBody', 'result'];
|
|
52
|
+
const fieldsOnlyWhitelist = ['fields'];
|
|
53
|
+
const viewWhitelist = ['grant', 'deny', 'description', 'parameters', 'emptyResult', 'codeHint'];
|
|
54
|
+
const viewCodeHintWhitelist = ['output'];
|
|
55
|
+
function filterEntities(entities) {
|
|
56
|
+
Object.values(entities).forEach((entity) => {
|
|
57
|
+
filterEntityFields(entity);
|
|
58
|
+
if (!reserveWhitelist(entity, entityWhitelist))
|
|
59
|
+
return;
|
|
60
|
+
const entityActions = entity.actions ?? {};
|
|
61
|
+
Object.keys(entityActions).forEach((actionName) => {
|
|
62
|
+
const actions = entityActions[actionName];
|
|
63
|
+
switch (actionName) {
|
|
64
|
+
case 'findMany':
|
|
65
|
+
actions.forEach((action) => reserveWhitelist(action, findManyWhitelist));
|
|
66
|
+
break;
|
|
67
|
+
case 'findById':
|
|
68
|
+
actions.forEach((action) => reserveWhitelist(action, findByIdWhitelist));
|
|
69
|
+
break;
|
|
70
|
+
case 'createOne':
|
|
71
|
+
case 'createMany':
|
|
72
|
+
case 'updateOne':
|
|
73
|
+
case 'updateMany':
|
|
74
|
+
case 'updatePartially':
|
|
75
|
+
case 'deleteOne':
|
|
76
|
+
case 'deleteMany':
|
|
77
|
+
actions.forEach((action) => reserveWhitelist(action, actionWhitelist));
|
|
78
|
+
break;
|
|
79
|
+
default:
|
|
80
|
+
actions.forEach((action) => {
|
|
81
|
+
reserveWhitelist(action, generalWhitelist);
|
|
82
|
+
if (!action.codeHint)
|
|
83
|
+
return;
|
|
84
|
+
if (!reserveWhitelist(action.codeHint, actionCodeHintWhitelist))
|
|
85
|
+
return;
|
|
86
|
+
if (!action.codeHint.delegator)
|
|
87
|
+
return;
|
|
88
|
+
if (!reserveWhitelist(action.codeHint.delegator, actionCodeHintDelegatorWhitelist))
|
|
89
|
+
return;
|
|
90
|
+
reserveWhitelist(action.codeHint.delegator.param, fieldsOnlyWhitelist);
|
|
91
|
+
reserveWhitelist(action.codeHint.delegator.requestBody, fieldsOnlyWhitelist);
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const entityViews = entity.views ?? {};
|
|
97
|
+
Object.keys(entityViews).forEach((viewName) => {
|
|
98
|
+
const views = entityViews[viewName];
|
|
99
|
+
views.forEach((view) => {
|
|
100
|
+
reserveWhitelist(view, viewWhitelist);
|
|
101
|
+
if (!view.codeHint)
|
|
102
|
+
return;
|
|
103
|
+
if (!reserveWhitelist(view.codeHint, viewCodeHintWhitelist))
|
|
104
|
+
return;
|
|
105
|
+
reserveWhitelist(view.codeHint.output, fieldsOnlyWhitelist);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
if (entity.features) {
|
|
109
|
+
reserveWhitelist(entity.features, entityFeatureWhitelist);
|
|
110
|
+
reserveWhitelist(entity.features.vn_text, vnTextWhitelist);
|
|
111
|
+
entity.features.vn_text?.channels?.forEach((channel) => reserveWhitelist(channel, vnTextChannelWhitelist));
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function flattenFields(fields, dict, prefix) {
|
|
116
|
+
dict ?? (dict = {});
|
|
117
|
+
prefix ?? (prefix = '');
|
|
118
|
+
for (const m in fields) {
|
|
119
|
+
const path = prefix ? prefix + '.' + m : m;
|
|
120
|
+
dict[path] = false;
|
|
121
|
+
const config = fields[m];
|
|
122
|
+
if (config.type == 'object') {
|
|
123
|
+
flattenFields(config.fields ?? {}, dict, path);
|
|
124
|
+
}
|
|
125
|
+
else if (config.type == 'array' && config.items && config.items.type == 'object') {
|
|
126
|
+
flattenFields(config.items.fields ?? {}, dict, path);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return dict;
|
|
130
|
+
}
|
|
131
|
+
function removeDisallowedFields(fields, dict) {
|
|
132
|
+
const allowFields = Object.keys(dict);
|
|
133
|
+
if (!allowFields.length)
|
|
134
|
+
return;
|
|
135
|
+
reserveWhitelist(fields, allowFields);
|
|
136
|
+
for (const m in fields) {
|
|
137
|
+
const config = fields[m];
|
|
138
|
+
if (config.type == 'object') {
|
|
139
|
+
removeDisallowedFields(config.fields ?? {}, dict[m]);
|
|
140
|
+
}
|
|
141
|
+
else if (config.type == 'array' && config.items && config.items.type == 'object') {
|
|
142
|
+
removeDisallowedFields(config.items.fields ?? {}, dict[m]);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function markAllowFields(allowFields, dict) {
|
|
147
|
+
if (!Array.isArray(allowFields))
|
|
148
|
+
return;
|
|
149
|
+
allowFields.forEach((field) => (dict[field] = true));
|
|
150
|
+
}
|
|
151
|
+
function markIncludedFields(excludedFields, dict) {
|
|
152
|
+
if (!Array.isArray(excludedFields))
|
|
153
|
+
return;
|
|
154
|
+
for (const m in dict) {
|
|
155
|
+
if (!dict[m] &&
|
|
156
|
+
!excludedFields.includes(m) &&
|
|
157
|
+
excludedFields.every((excludedField) => !m.startsWith(excludedField + '.')))
|
|
158
|
+
dict[m] = true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function filterEntityFields(entity) {
|
|
162
|
+
const dict = flattenFields(entity.fields);
|
|
163
|
+
const entityActions = entity.actions ?? {};
|
|
164
|
+
Object.keys(entityActions).forEach((actionName) => {
|
|
165
|
+
const actions = entityActions[actionName];
|
|
166
|
+
switch (actionName) {
|
|
167
|
+
case 'findMany':
|
|
168
|
+
case 'findById':
|
|
169
|
+
actions.forEach((action) => {
|
|
170
|
+
markAllowFields(action.returnableFields, dict);
|
|
171
|
+
markAllowFields(action.defaultFields, dict);
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
case 'createOne':
|
|
175
|
+
case 'createMany':
|
|
176
|
+
case 'updateOne':
|
|
177
|
+
case 'updateMany':
|
|
178
|
+
case 'updatePartially':
|
|
179
|
+
actions.forEach((action) => markIncludedFields(action.exclude, dict));
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const allowFields = Object.keys(dict).filter((m) => dict[m]);
|
|
184
|
+
[...allowFields].forEach((shortField) => {
|
|
185
|
+
shortField += '.';
|
|
186
|
+
allowFields
|
|
187
|
+
.map((longerField, i) => (longerField.includes(shortField) ? i : -1))
|
|
188
|
+
.filter((i) => i !== -1)
|
|
189
|
+
.reverse()
|
|
190
|
+
.forEach((i) => allowFields.splice(i, 1));
|
|
191
|
+
});
|
|
192
|
+
const whitelist = {};
|
|
193
|
+
allowFields.forEach((fields) => {
|
|
194
|
+
let obj = whitelist;
|
|
195
|
+
fields.split('.').forEach((name) => {
|
|
196
|
+
let child = obj[name];
|
|
197
|
+
if (!child)
|
|
198
|
+
child = obj[name] = {};
|
|
199
|
+
obj = child;
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
Object.keys(entityActions).forEach((actionName) => {
|
|
203
|
+
const actions = entityActions[actionName];
|
|
204
|
+
switch (actionName) {
|
|
205
|
+
case 'createOne':
|
|
206
|
+
case 'createMany':
|
|
207
|
+
case 'updateOne':
|
|
208
|
+
case 'updateMany':
|
|
209
|
+
case 'updatePartially':
|
|
210
|
+
actions.forEach((action) => {
|
|
211
|
+
action.exclude = action.exclude?.filter((field) => {
|
|
212
|
+
let idx = 0;
|
|
213
|
+
do {
|
|
214
|
+
idx = field.indexOf('.', idx + 1);
|
|
215
|
+
const prefix = idx === -1 ? field : field.substring(0, idx);
|
|
216
|
+
if (allowFields.includes(prefix))
|
|
217
|
+
return true;
|
|
218
|
+
} while (idx !== -1);
|
|
219
|
+
return false;
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
removeDisallowedFields(entity.fields, whitelist);
|
|
226
|
+
}
|
|
227
|
+
async function writeDocs(filePath, strTemplate, strData) {
|
|
228
|
+
const placeholder = '<script id="INJECT-JAVASCRIPT-HERE"></script>';
|
|
229
|
+
const idx = strTemplate.indexOf(placeholder);
|
|
230
|
+
fs.writeFileSync(filePath, strTemplate.substring(0, idx));
|
|
231
|
+
fs.appendFileSync(filePath, '<script>window.docs = ');
|
|
232
|
+
fs.appendFileSync(filePath, strData);
|
|
233
|
+
fs.appendFileSync(filePath, ';\n');
|
|
234
|
+
fs.appendFileSync(filePath, fs.readFileSync(path.join(__dirname, '../docs/docs.v2.js')));
|
|
235
|
+
fs.appendFileSync(filePath, '</script>');
|
|
236
|
+
fs.appendFileSync(filePath, strTemplate.substring(idx + placeholder.length));
|
|
237
|
+
fs.writeFileSync(filePath, await minify(filePath, {
|
|
238
|
+
html: {
|
|
239
|
+
removeComments: true,
|
|
240
|
+
collapseWhitespace: true,
|
|
241
|
+
collapseBooleanAttributes: true,
|
|
242
|
+
removeAttributeQuotes: true,
|
|
243
|
+
removeRedundantAttributes: true,
|
|
244
|
+
useShortDoctype: true,
|
|
245
|
+
removeEmptyAttributes: true,
|
|
246
|
+
removeEmptyElements: false,
|
|
247
|
+
removeOptionalTags: true,
|
|
248
|
+
removeScriptTypeAttributes: false,
|
|
249
|
+
removeStyleLinkTypeAttributes: true,
|
|
250
|
+
minifyJS: true,
|
|
251
|
+
minifyCSS: true,
|
|
252
|
+
html5: true,
|
|
253
|
+
},
|
|
254
|
+
}));
|
|
255
|
+
copyStaticFiles(path.dirname(filePath), [
|
|
256
|
+
'bootstrap-theme.min.css',
|
|
257
|
+
'bootstrap.min.css',
|
|
258
|
+
'bootstrap.min.js',
|
|
259
|
+
'jquery.min.js',
|
|
260
|
+
]);
|
|
261
|
+
}
|
|
262
|
+
const copyStaticFiles = (folderPath, files) => {
|
|
263
|
+
files.forEach((file) => {
|
|
264
|
+
fs.copyFileSync(path.join(__dirname, '../docs/', file), path.join(folderPath, file));
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
export default async function genDocs(args, projectName, project, alias, source) {
|
|
268
|
+
const aliasProject = ((project.docs || {}).alias || {})[alias];
|
|
269
|
+
if (!aliasProject) {
|
|
270
|
+
throw new Error(`Not found gen docs configuration. Please run 'npx mushroom-cli config ${projectName} gen docs OUTPUT_FILE.html${alias === 'default' ? '' : ' alias ' + alias}' first`);
|
|
271
|
+
}
|
|
272
|
+
common.logInfo('Use GenDocEngine v1.2');
|
|
273
|
+
const allowRoles = aliasProject.allowRoles ?? ['*'];
|
|
274
|
+
const entities = common.getEntities(source, allowRoles);
|
|
275
|
+
if (Array.isArray(aliasProject.entities)) {
|
|
276
|
+
for (const entityName in entities) {
|
|
277
|
+
if (Object.hasOwnProperty.call(entities, entityName)) {
|
|
278
|
+
if (aliasProject.entities.every((re) => !new RegExp(re).test(entityName))) {
|
|
279
|
+
delete entities[entityName];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
filterEnvironmentVariables(source.environmentVariables, allowRoles);
|
|
285
|
+
filterEntities(entities);
|
|
286
|
+
const data = JSON.stringify({
|
|
287
|
+
info: {
|
|
288
|
+
name: source.name,
|
|
289
|
+
rootUrl: source.codeHint?.rootUrl ?? source.rootUrl,
|
|
290
|
+
description: source.description,
|
|
291
|
+
security: source.security === undefined
|
|
292
|
+
? undefined
|
|
293
|
+
: {
|
|
294
|
+
crossDomain: source.security.crossDomain,
|
|
295
|
+
allowOrigins: source.security.allowOrigins,
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
features: aliasProject.features,
|
|
299
|
+
roles: common.filterRoles(source.roles ?? [], allowRoles),
|
|
300
|
+
environmentVariables: source.environmentVariables,
|
|
301
|
+
entities: entities,
|
|
302
|
+
});
|
|
303
|
+
const html = fs.readFileSync(__dirname + '/../docs/docs.v2.html').toString();
|
|
304
|
+
if (args[3] === 'tmp') {
|
|
305
|
+
const htmlFilePath = aliasProject.output + '_tmp.html';
|
|
306
|
+
common.logInfo('Writing to', htmlFilePath);
|
|
307
|
+
await writeDocs(htmlFilePath, html, data);
|
|
308
|
+
common.logInfo('Opening file ', htmlFilePath);
|
|
309
|
+
try {
|
|
310
|
+
await open(htmlFilePath);
|
|
311
|
+
setTimeout(() => {
|
|
312
|
+
fs.unlink(htmlFilePath, (ex) => {
|
|
313
|
+
if (ex)
|
|
314
|
+
common.logError('Unable to delete file', htmlFilePath);
|
|
315
|
+
});
|
|
316
|
+
}, 1000);
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
common.logError('Unable to open', htmlFilePath);
|
|
320
|
+
}
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
common.ensureFolder(aliasProject.output);
|
|
324
|
+
common.logInfo('Writing to', aliasProject.output);
|
|
325
|
+
await writeDocs(aliasProject.output, html, data);
|
|
326
|
+
common.logInfo(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);
|
|
327
|
+
}
|