mushroom-cli 1.1.2 → 1.2.0-alpha.10

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.
@@ -1,199 +1,174 @@
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
- })();
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)
13
+ return;
14
+ if (this.status < 200 || this.status >= 300) {
15
+ if (typeof this.debugMode === "function" && this.debugMode())
16
+ console.error("Unable to load: " + url);
17
+ return;
18
+ }
19
+ var args = { content: this.responseText };
20
+ urls[url] = args;
21
+ if (typeof successCallback === "function")
22
+ successCallback(args);
23
+ };
24
+ xhr.onerror = function () {
25
+ if (typeof this.debugMode === "function" && this.debugMode())
26
+ console.error("Error occurred when loading: " + url);
27
+ if (typeof errorCallback === "function")
28
+ errorCallback();
29
+ };
30
+ xhr.onabort = function () {
31
+ if (typeof this.debugMode === "function" && this.debugMode())
32
+ console.error("Connection aborted: " + url);
33
+ if (typeof errorCallback === "function")
34
+ errorCallback();
35
+ };
36
+ xhr.ontimeout = function () {
37
+ if (typeof this.debugMode === "function" && this.debugMode())
38
+ console.error("Connection timeout: " + url);
39
+ if (typeof errorCallback === "function")
40
+ errorCallback();
41
+ };
42
+ xhr.open("GET", url, true);
43
+ xhr.send(null);
44
+ }
45
+
46
+ mt.modules = {};
47
+ mt.bindModule = function (module, dom, data, session) {
48
+ if (!module || !dom)
49
+ return;
50
+
51
+ loadFile("/modules/" + module + "/" + module + "-view.html", function (response) {
52
+ dom.innerHTML = "<link rel='stylesheet' href='/modules/" + module + "/" + module + "-style.min.css' />" + response.content;
53
+ loadFile("/modules/" + module + "/" + module + "-script.js", function (resp) {
54
+ if (!resp.content)
55
+ return;
56
+
57
+ if (!resp.cached) {
58
+ try {
59
+ eval(resp.content);
60
+ } catch (e) {
61
+ throw "Invalid module script";
62
+ }
63
+ if (!mt.modules[module])
64
+ throw "Invalid module script";
65
+ if (typeof mt.modules[module].init === "function")
66
+ mt.modules[module].init();
67
+ }
68
+
69
+ var config = mt.modules[module];
70
+
71
+ var args = { dom: dom, data: data, fnPool: config.fn, session: session };
72
+ if (typeof config.prebinding === "function") {
73
+ config.prebinding(args);
74
+ }
75
+
76
+ mt.bindDom(dom, args.data, args.fnPool, session);
77
+
78
+ if (typeof config.load === "function") {
79
+ args.dom = dom;
80
+ args.session = session;
81
+ config.load(args);
82
+ }
83
+ }, function () {
84
+ mt.bindDom(dom, data, null, session);
85
+ });
86
+
87
+
88
+ });
89
+
90
+ };
91
+
92
+ //#region mt-link
93
+ if (mt["default"] === undefined) mt["default"] = {};
94
+ if (mt["default"].mtLink === undefined) mt["default"].mtLink = {};
95
+ // cho phép mở rộng được về sau: ví dụ thay thế bằng custom dialog
96
+ mt["default"].mtLink.itemClickHandler = function (dom, message, callback) {
97
+ if (confirm(message))
98
+ callback(dom);
99
+ }
100
+
101
+ function mtLinkHandler(dom) {
102
+ mt.bindModule(dom.__mtLinkConfig.module, document.getElementById("page-wrapper"), dom.__mtLinkConfig.params);
103
+ }
104
+
105
+ function mtLinkClicked(e) {
106
+ if (this.__mtLinkConfig.confirm)
107
+ mt["default"].mtLink.itemClickHandler(this, this.__mtLinkConfig.confirm, mtLinkHandler);
108
+ else
109
+ mtLinkHandler(this);
110
+
111
+ e = e || window.event;
112
+ e.stopPropagation();
113
+ e.preventDefault();
114
+ e.cancelBubble = true;
115
+ return false;
116
+ }
117
+
118
+ mt.registerDomFeature("mtLink", function (arg) {
119
+ if (arg.dom.nodeName !== "A") {
120
+ arg.dom.dataset.mtLinkError = "mtLink feature only affects <a> tag";
121
+ return;
122
+ }
123
+
124
+ if (!arg.config.module) {
125
+ arg.dom.dataset.mtLinkError = "mtLink feature: missing module";
126
+ return;
127
+ }
128
+
129
+ arg.dom.__mtLinkConfig = mt.fnUtils.mergeData({ mask: arg.config, data: arg.data, session: arg.session });
130
+
131
+ if (!arg.dom.href)
132
+ arg.dom.href = "javascript:void(0)";
133
+
134
+ mt.fnUtils.addEventListener(arg.dom, "click", mtLinkClicked, false);
135
+
136
+ delete arg.dom.dataset.mtLink;
137
+ });
138
+ //#endregion
139
+
140
+ //#region mt-module
141
+ mt.registerDomFeature("mtModule", function (arg) {
142
+ if (!arg.config.module) {
143
+ arg.dom.dataset.mtModuleError = "mtModule feature: missing module";
144
+ return;
145
+ }
146
+
147
+ var params = mt.fnUtils.mergeData({ mask: arg.config.params, data: arg.data, session: arg.session });
148
+ mt.bindModule(arg.config.module, arg.dom, params, arg.session);
149
+ arg.mediator.skipInner = true;
150
+
151
+ delete arg.dom.dataset.mtModule;
152
+ });
153
+ //#endregion
154
+
155
+ //#region mt-inline-module
156
+ mt.inlineModuleTemplates = {};
157
+ mt.registerDomFeature("mtInlineModule", function (arg) {
158
+ var config = mt.fnUtils.mergeData({ mask: arg.config, data: arg.data, session: arg.session });
159
+
160
+ if (!config.module) {
161
+ arg.dom.dataset.mtInlineModuleError = "mtInlineModule feature: missing module";
162
+ return;
163
+ }
164
+
165
+ arg.dom.innerHTML = mt.inlineModuleTemplates[config.module] || "";
166
+
167
+ delete arg.dom.dataset.mtInlineModule;
168
+
169
+ mt.bindDom(arg.dom, config.params);
170
+
171
+ arg.mediator.skipInner = true;
172
+ });
173
+ //#endregion
174
+ })();
@@ -0,0 +1,125 @@
1
+ const fs = require("fs");
2
+ const open = require("open");
3
+ const jsdom = require("jsdom");
4
+ const { JSDOM } = jsdom;
5
+ const common = require("../common.js");
6
+
7
+ function processGenDocs(aliasProject, entities, source, html, args, projectName, alias) {
8
+ console.log('Received html');
9
+ console.log("Parsing html");
10
+
11
+ const roles = aliasProject.allowRoles.includes("*") ? source.roles : source.roles?.filter(r => aliasProject.allowRoles.includes(r.name));
12
+
13
+ html = html.replace(/^\uFEFF/, '');
14
+ 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>`);
15
+ html = html.replace("</body>", `<script data-mt-cond='{"x": 1}'>
16
+ window.data.name = ${JSON.stringify(source.name)};
17
+ window.data.description = ${JSON.stringify(source.description)};
18
+ window.data.rootUrl = ${JSON.stringify(common.getRootUrl(source))};
19
+ window.data.environmentVariables = ${JSON.stringify(source.environmentVariables)};
20
+ window.data.roles = ${JSON.stringify(roles)};
21
+ window.data.entities = ${JSON.stringify(entities)};
22
+ window.data.features = ${JSON.stringify(aliasProject.features)};
23
+ try {
24
+ console.info("MUSHROOM-CLI: START");
25
+ window.generateDocument();
26
+ console.info("MUSHROOM-CLI: FINISH");
27
+ }
28
+ catch (e) {
29
+ console.info("MUSHROOM-CLI: FAIL", e);
30
+ }
31
+ window.close();
32
+ </script></body>`);
33
+
34
+ return new Promise((resolve, reject) => {
35
+ const virtualConsole = new jsdom.VirtualConsole();
36
+ virtualConsole.on("info", function(msg, err) {
37
+ if (typeof msg != "string") return;
38
+
39
+ if (msg.indexOf("MUSHROOM-CLI:") === 0) {
40
+ console.log("GenDocEngine:" + msg.substring("MUSHROOM-CLI:".length));
41
+ }
42
+
43
+ if (msg === "MUSHROOM-CLI: FAIL"){
44
+ dom = null;
45
+ reject(err);
46
+ return;
47
+ }
48
+
49
+ if (msg !== "MUSHROOM-CLI: FINISH") return;
50
+
51
+ const code = "<!DOCTYPE html>" + dom.window.document.documentElement.outerHTML;
52
+
53
+ dom = null;
54
+
55
+ if (args[3] === "tmp") {
56
+ const htmlFilePath = aliasProject.output + "_tmp.html";
57
+ console.log(`Writing to ${htmlFilePath}`);
58
+ fs.writeFile(htmlFilePath, code, err => {
59
+ if (err) {
60
+ reject(err);
61
+ return;
62
+ }
63
+
64
+ console.log("Opening file " + htmlFilePath);
65
+ open(htmlFilePath).then(() => {
66
+ setTimeout(() => {
67
+ fs.unlink(htmlFilePath, ex => {
68
+ if (ex)
69
+ console.error("Unable to delete file " + htmlFilePath)
70
+ });
71
+ }, 1000);
72
+ }).catch(() => console.error("Unable to open " + htmlFilePath));
73
+ resolve();
74
+ });
75
+ return;
76
+ }
77
+
78
+ common.ensureFolder(aliasProject.output);
79
+
80
+ console.log(`Writing to ${aliasProject.output}`);
81
+ fs.writeFile(aliasProject.output, code, err => {
82
+ if (err) {
83
+ reject(err);
84
+ return;
85
+ }
86
+ console.log(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);
87
+ resolve();
88
+ });
89
+ });
90
+
91
+ let dom = new JSDOM(html, {
92
+ runScripts: "dangerously",
93
+ resources: "usable",
94
+ virtualConsole: virtualConsole
95
+ });
96
+ console.log('Rendering API document');
97
+ });
98
+ }
99
+
100
+ async function genDocs(args, projectName, project, alias, source) {
101
+ const aliasProject = ((project.docs || {}).alias || {})[alias];
102
+
103
+ if (!aliasProject) {
104
+ 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`);
105
+ }
106
+
107
+ console.log("Use GenDocEngine v1");
108
+
109
+ const entities = common.getEntities(source, aliasProject.allowRoles);
110
+
111
+ if (Array.isArray(aliasProject.entities)) {
112
+ for (const entityName in entities) {
113
+ if (Object.hasOwnProperty.call(entities, entityName)) {
114
+ if (aliasProject.entities.every(re => !new RegExp(re).test(entityName))){
115
+ delete entities[entityName];
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ const html = fs.readFileSync(__dirname + "/../docs/docs.html").toString();
122
+ await processGenDocs(aliasProject, entities, source, html, args, projectName, alias);
123
+ }
124
+
125
+ module.exports = genDocs;