mushroom-cli 1.2.0-alpha.2 → 1.2.0-alpha.20
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 +273 -111
- package/dist/check-update.js +6 -0
- package/dist/common.js +273 -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/{docs → dist/docs}/doc-mt.js +103 -1
- package/{docs → dist/docs}/docs.html +83 -22
- package/dist/docs/docs.v2.html +5097 -0
- package/dist/docs/docs.v2.js +1907 -0
- package/dist/docs/jquery.min.js +4 -0
- package/dist/gen/gen-api-docs-v1.js +110 -0
- package/dist/gen/gen-api-docs.js +306 -0
- package/{gen → dist/gen}/gen-csharp.js +1246 -1414
- 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 +596 -0
- package/{gen.js → dist/gen.js} +109 -120
- 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 -27
- package/check-update.js +0 -9
- package/common.js +0 -236
- package/config.js +0 -445
- package/gen/gen-api-docs.js +0 -125
- package/gen/gen-ts-driver.js +0 -460
- 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
- /package/{docs → dist/docs}/mt-dom-1.0.js +0 -0
- /package/{docs → dist/docs}/mt.extension.js +0 -0
package/gen/gen-api-docs.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const open = require("open");
|
|
3
|
-
const fetch = require('node-fetch');
|
|
4
|
-
const jsdom = require("jsdom");
|
|
5
|
-
const { JSDOM } = jsdom;
|
|
6
|
-
const common = require("../common.js");
|
|
7
|
-
|
|
8
|
-
function processGenDocs(aliasProject, entities, source, html, args, projectName, alias) {
|
|
9
|
-
console.log('Received html');
|
|
10
|
-
console.log("Parsing html");
|
|
11
|
-
|
|
12
|
-
const roles = aliasProject.allowRoles.includes("*") ? source.roles : source.roles?.filter(r => aliasProject.allowRoles.includes(r.name));
|
|
13
|
-
|
|
14
|
-
html = html.replace(/^\uFEFF/, '');
|
|
15
|
-
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>`);
|
|
16
|
-
html = html.replace("</body>", `<script data-mt-cond='{"x": 1}'>
|
|
17
|
-
window.data.name = ${JSON.stringify(source.name)};
|
|
18
|
-
window.data.description = ${JSON.stringify(source.description)};
|
|
19
|
-
window.data.rootUrl = ${JSON.stringify(common.getRootUrl(source))};
|
|
20
|
-
window.data.environmentVariables = ${JSON.stringify(source.environmentVariables)};
|
|
21
|
-
window.data.roles = ${JSON.stringify(roles)};
|
|
22
|
-
window.data.entities = ${JSON.stringify(entities)};
|
|
23
|
-
window.data.features = ${JSON.stringify(aliasProject.features)};
|
|
24
|
-
try {
|
|
25
|
-
console.info("MUSHROOM-CLI: START");
|
|
26
|
-
window.generateDocument();
|
|
27
|
-
console.info("MUSHROOM-CLI: FINISH");
|
|
28
|
-
}
|
|
29
|
-
catch (e) {
|
|
30
|
-
console.info("MUSHROOM-CLI: FAIL", e);
|
|
31
|
-
}
|
|
32
|
-
window.close();
|
|
33
|
-
</script></body>`);
|
|
34
|
-
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
const virtualConsole = new jsdom.VirtualConsole();
|
|
37
|
-
virtualConsole.on("info", function(msg, err) {
|
|
38
|
-
if (typeof msg != "string") return;
|
|
39
|
-
|
|
40
|
-
if (msg.indexOf("MUSHROOM-CLI:") === 0) {
|
|
41
|
-
console.log("GenDocEngine:" + msg.substring("MUSHROOM-CLI:".length));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (msg === "MUSHROOM-CLI: FAIL"){
|
|
45
|
-
dom = null;
|
|
46
|
-
reject(err);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (msg !== "MUSHROOM-CLI: FINISH") return;
|
|
51
|
-
|
|
52
|
-
const code = "<!DOCTYPE html>" + dom.window.document.documentElement.outerHTML;
|
|
53
|
-
|
|
54
|
-
dom = null;
|
|
55
|
-
|
|
56
|
-
if (args[3] === "tmp") {
|
|
57
|
-
const htmlFilePath = aliasProject.output + "_tmp.html";
|
|
58
|
-
console.log(`Writing to ${htmlFilePath}`);
|
|
59
|
-
fs.writeFile(htmlFilePath, code, err => {
|
|
60
|
-
if (err) {
|
|
61
|
-
reject(err);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
console.log("Opening file " + htmlFilePath);
|
|
66
|
-
open(htmlFilePath).then(() => {
|
|
67
|
-
setTimeout(() => {
|
|
68
|
-
fs.unlink(htmlFilePath, ex => {
|
|
69
|
-
if (ex)
|
|
70
|
-
console.error("Unable to delete file " + htmlFilePath)
|
|
71
|
-
});
|
|
72
|
-
}, 1000);
|
|
73
|
-
}).catch(() => console.error("Unable to open " + htmlFilePath));
|
|
74
|
-
resolve();
|
|
75
|
-
});
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
common.ensureFolder(aliasProject.output);
|
|
80
|
-
|
|
81
|
-
console.log(`Writing to ${aliasProject.output}`);
|
|
82
|
-
fs.writeFile(aliasProject.output, code, err => {
|
|
83
|
-
if (err) {
|
|
84
|
-
reject(err);
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
console.log(`Generated API docs for ${projectName}@${alias} to ${aliasProject.output}`);
|
|
88
|
-
resolve();
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
let dom = new JSDOM(html, {
|
|
93
|
-
runScripts: "dangerously",
|
|
94
|
-
resources: "usable",
|
|
95
|
-
virtualConsole: virtualConsole
|
|
96
|
-
});
|
|
97
|
-
console.log('Rendering API document');
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async function genDocs(args, projectName, project, alias, source) {
|
|
102
|
-
const aliasProject = ((project.docs || {}).alias || {})[alias];
|
|
103
|
-
|
|
104
|
-
if (!aliasProject) {
|
|
105
|
-
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`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const entities = common.getEntities(source, aliasProject.allowRoles);
|
|
109
|
-
|
|
110
|
-
if (Array.isArray(aliasProject.entities)) {
|
|
111
|
-
for (const entityName in entities) {
|
|
112
|
-
if (Object.hasOwnProperty.call(entities, entityName)) {
|
|
113
|
-
if (aliasProject.entities.every(re => !new RegExp(re).test(entityName))){
|
|
114
|
-
delete entities[entityName];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
//const html = await fetch('https://api.siten.vn/gen/docs.html').then(res => res.text());
|
|
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;
|
package/gen/gen-ts-driver.js
DELETED
|
@@ -1,460 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const common = require("../common.js");
|
|
3
|
-
|
|
4
|
-
function isAllowCache(actions, defaultValue) {
|
|
5
|
-
let result = defaultValue;
|
|
6
|
-
for (let i = 0; i < actions.length; i++) {
|
|
7
|
-
const action = actions[i];
|
|
8
|
-
|
|
9
|
-
if (!action ||
|
|
10
|
-
!action.cache ||
|
|
11
|
-
!action.cache.client ||
|
|
12
|
-
!action.cache.client.policy ||
|
|
13
|
-
action.cache.client.policy === "unset")
|
|
14
|
-
continue;
|
|
15
|
-
|
|
16
|
-
if (action.cache.client.policy === "disabled")
|
|
17
|
-
return false;
|
|
18
|
-
|
|
19
|
-
if (action.cache.client.policy === "allow" || action.cache.client.policy === "required")
|
|
20
|
-
result = true;
|
|
21
|
-
}
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function toVietnameseNonTongue(str) {
|
|
26
|
-
const signText = "ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệếìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵýĂÂĐÐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ";
|
|
27
|
-
const unsignedText = "aadeoouaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiooooooooooooooouuuuuuuuuuyyyyyAADDEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOUUUUUUUUUUYYYYY";
|
|
28
|
-
var returnValue = "";
|
|
29
|
-
for (let i = 0; i < str.length; i++) {
|
|
30
|
-
const index = signText.indexOf(str.charAt(i));
|
|
31
|
-
returnValue += index !== -1 ? unsignedText.charAt(index) : str.charAt(i);
|
|
32
|
-
}
|
|
33
|
-
return returnValue;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function toTypeScriptClassName(str) {
|
|
37
|
-
if (!str)
|
|
38
|
-
return "_";
|
|
39
|
-
const name = toVietnameseNonTongue(str).replace(/\W/g, "_");
|
|
40
|
-
const c = name.charAt(0);
|
|
41
|
-
return ('0' <= c && c <= '9' ? '_' : "") + c.toUpperCase() + name.substring(1);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function getCSharpTypeFromMushroomType(type) {
|
|
45
|
-
switch (type) {
|
|
46
|
-
case "string":
|
|
47
|
-
return "string";
|
|
48
|
-
case "object":
|
|
49
|
-
case "array":
|
|
50
|
-
return "";
|
|
51
|
-
case "boolean":
|
|
52
|
-
return "boolean";
|
|
53
|
-
case "integer":
|
|
54
|
-
case "long":
|
|
55
|
-
case "double":
|
|
56
|
-
return "number";
|
|
57
|
-
case "datetime":
|
|
58
|
-
return "ISODateValue";
|
|
59
|
-
case "IdRef":
|
|
60
|
-
case "AutoId":
|
|
61
|
-
return "IdValue";
|
|
62
|
-
}
|
|
63
|
-
return "UNKNOWN_TYPE_" + type;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function generateTypescriptClass(objectDefination, indent, options) {
|
|
67
|
-
if (!indent) indent = "";
|
|
68
|
-
let code = "{\n";
|
|
69
|
-
for (let fieldName in objectDefination) {
|
|
70
|
-
const optionalMark = options?.checkRequired && objectDefination[fieldName].required ? "" : "?";
|
|
71
|
-
if (objectDefination[fieldName].type == "object") {
|
|
72
|
-
code += `${indent} ${fieldName}${optionalMark}: ${generateTypescriptClass(objectDefination[fieldName].fields, indent + " ")},
|
|
73
|
-
`;
|
|
74
|
-
}
|
|
75
|
-
else if (objectDefination[fieldName].type == "array") {
|
|
76
|
-
if (objectDefination[fieldName].items.type == "object") {
|
|
77
|
-
code += `${indent} ${fieldName}${optionalMark}: ${generateTypescriptClass(objectDefination[fieldName].items.fields, indent + " ")}[],
|
|
78
|
-
`;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
code += `${indent} ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].items.type)}[],
|
|
82
|
-
`;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
code += `${indent} ${fieldName}${optionalMark}: ${getCSharpTypeFromMushroomType(objectDefination[fieldName].type)},
|
|
87
|
-
`;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
code += indent + "}";
|
|
91
|
-
return code;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function generateTypescriptClassFromViewParameters(parameters) {
|
|
95
|
-
if (Array.isArray(parameters)){
|
|
96
|
-
return '{\n' + parameters.map(p => ` ${p.field}${p.validation?.required && p.validation.defaultValue === undefined ? "" : "?"}: ${getCSharpTypeFromMushroomType(p.type || "string")}${p.isArray ? "[]" : ""}`).join(',\n') + '\n}';
|
|
97
|
-
}
|
|
98
|
-
return '{}';
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function genTypescriptDriverString(aliasProject, source) {
|
|
102
|
-
if (!aliasProject) {
|
|
103
|
-
return "";
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const entities = common.getEntities(source, aliasProject.allowRoles);
|
|
107
|
-
|
|
108
|
-
let extensionsImportLines = [];
|
|
109
|
-
let extensionsExportLines = [];
|
|
110
|
-
let extensionsTypes = [];
|
|
111
|
-
let extensionsCodeLines = [];
|
|
112
|
-
|
|
113
|
-
if (Array.isArray(aliasProject.features)) {
|
|
114
|
-
if (aliasProject.features.includes("auth")) {
|
|
115
|
-
extensionsImportLines.push('import * as AuthExtension from "mushroomjs-auth";');
|
|
116
|
-
extensionsExportLines.push('File: FileExtension');
|
|
117
|
-
extensionsTypes.push("AuthExtension.IMushroomAuth & ");
|
|
118
|
-
extensionsCodeLines.push("AuthExtension.useAuth(AuthExtension.AuthLocalStorage, mushroom);");
|
|
119
|
-
}
|
|
120
|
-
if (aliasProject.features.includes("file")) {
|
|
121
|
-
extensionsImportLines.push('import * as FileExtension from "mushroomjs-file";');
|
|
122
|
-
extensionsExportLines.push('Auth: AuthExtension');
|
|
123
|
-
extensionsTypes.push("FileExtension.IMushroomFile & ");
|
|
124
|
-
extensionsCodeLines.push("FileExtension.useFile(mushroom);");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
let code = `import mushroom, { defineAsyncResource, defineAsyncView, fireEvent, createRestfulAsyncFunction, IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError } from "mushroomjs";
|
|
129
|
-
${extensionsImportLines.join("\n")}
|
|
130
|
-
export * from "mushroomjs";
|
|
131
|
-
export const extensions = {
|
|
132
|
-
${extensionsExportLines.join(",\n ")}
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
export type ISODateValue = string | Date;
|
|
136
|
-
export type IdValue = ${source.idType == "AutoIncInt" || source.idType == "AutoIncLong" ? "number" : "string"};
|
|
137
|
-
|
|
138
|
-
`;
|
|
139
|
-
let iApiClassBody = "";
|
|
140
|
-
for (let entityName in entities) {
|
|
141
|
-
if (!entities.hasOwnProperty(entityName))
|
|
142
|
-
continue;
|
|
143
|
-
|
|
144
|
-
const entity = entities[entityName];
|
|
145
|
-
const typescriptClassName = toTypeScriptClassName(entityName);
|
|
146
|
-
code += `export interface ${typescriptClassName} ${generateTypescriptClass(entity.fields)};
|
|
147
|
-
`;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const actions = {};
|
|
151
|
-
iApiClassBody += ` ${entityName}: MushroomResourceBase`;
|
|
152
|
-
let customMethodCode = "";
|
|
153
|
-
for (const actionName in entity.actions) {
|
|
154
|
-
if (!entity.actions.hasOwnProperty(actionName))
|
|
155
|
-
continue;
|
|
156
|
-
|
|
157
|
-
actions[actionName] = {
|
|
158
|
-
clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined,
|
|
159
|
-
includeTotal: actionName === "findMany" && entity.actions[actionName].some(a => a.includeTotal) ? true: undefined
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
switch (actionName) {
|
|
163
|
-
case "findMany": {
|
|
164
|
-
iApiClassBody += ` & MushroomListResource<${typescriptClassName}>`;
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
case "findById": {
|
|
168
|
-
iApiClassBody += ` & MushroomFindByIdResource<${typescriptClassName}>`;
|
|
169
|
-
break;
|
|
170
|
-
}
|
|
171
|
-
case "createOne": {
|
|
172
|
-
iApiClassBody += ` & MushroomCreateResource<${typescriptClassName}>`;
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
case "createMany": {
|
|
176
|
-
iApiClassBody += ` & MushroomBatchCreateResource<${typescriptClassName}>`;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
case "updateOne": {
|
|
180
|
-
iApiClassBody += ` & MushroomUpdateResource<${typescriptClassName}>`;
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
case "updateMany": {
|
|
184
|
-
iApiClassBody += ` & MushroomBatchUpdateResource<${typescriptClassName}>`;
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
case "updatePartially": {
|
|
188
|
-
iApiClassBody += ` & MushroomPartialUpdateResource<${typescriptClassName}>`;
|
|
189
|
-
break;
|
|
190
|
-
}
|
|
191
|
-
case "deleteOne": {
|
|
192
|
-
iApiClassBody += " & MushroomDeleteResource";
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
case "deleteMany": {
|
|
196
|
-
iApiClassBody += " & MushroomBatchDeleteResource";
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
default: {
|
|
200
|
-
if (actionName.indexOf("_raw_http_method_") === 0) {
|
|
201
|
-
customMethodCode += ` ${actionName.substring("_raw_http_method_".length)}Async: (rawRequest: MushroomRequest) => Promise<MushroomResponse<any>>,
|
|
202
|
-
`;
|
|
203
|
-
break;
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
console.warn(`Unknown action name: ${entityName}.${actionName}`);
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (customMethodCode) {
|
|
214
|
-
code += `export interface ${typescriptClassName}$CustomMethodResource {
|
|
215
|
-
${customMethodCode}}
|
|
216
|
-
`;
|
|
217
|
-
iApiClassBody += ` & ${typescriptClassName}$CustomMethodResource`;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const views = {};
|
|
221
|
-
let viewCode = "";
|
|
222
|
-
for (const viewName in entity.views) {
|
|
223
|
-
if (!entity.views.hasOwnProperty(viewName)) continue;
|
|
224
|
-
|
|
225
|
-
const viewRoles = entity.views[viewName];
|
|
226
|
-
if (!Array.isArray(viewRoles) || !viewRoles.length) continue;
|
|
227
|
-
|
|
228
|
-
views[viewName] = { clientCache: true };
|
|
229
|
-
|
|
230
|
-
const viewParameterTypes = viewRoles.map(view => {
|
|
231
|
-
const inputType = `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Parameters`;
|
|
232
|
-
const outputType = view.codeHint?.output ? `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Result` : 'any';
|
|
233
|
-
code += `export interface ${inputType} ${generateTypescriptClassFromViewParameters(view.parameters)};
|
|
234
|
-
`;
|
|
235
|
-
if (view.codeHint?.output)
|
|
236
|
-
code += `export interface ${outputType} ${generateTypescriptClass(view.codeHint.output.fields, undefined, {checkRequired: true})};
|
|
237
|
-
`;
|
|
238
|
-
return `MushroomViewFunction<${outputType}, ${inputType}>`;
|
|
239
|
-
});
|
|
240
|
-
viewCode += `
|
|
241
|
-
${viewName}Async: ${viewParameterTypes.join(' | ')},`;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (viewCode) {
|
|
245
|
-
code += `export interface ${typescriptClassName}$ViewsResource {
|
|
246
|
-
views: {${viewCode}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
`;
|
|
250
|
-
iApiClassBody += ` & ${typescriptClassName}$ViewsResource`;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
iApiClassBody +=`,
|
|
254
|
-
`;
|
|
255
|
-
|
|
256
|
-
code += `defineAsyncResource<${typescriptClassName}>(${JSON.stringify({ name: entityName, actions: actions, views: views })});
|
|
257
|
-
|
|
258
|
-
`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
code += `
|
|
262
|
-
mushroom.$using(${JSON.stringify(common.getRootUrl(source))});
|
|
263
|
-
|
|
264
|
-
(function(){
|
|
265
|
-
${extensionsCodeLines.join('\n')}
|
|
266
|
-
})();
|
|
267
|
-
|
|
268
|
-
export interface IApi extends IMushroom {
|
|
269
|
-
${iApiClassBody}};
|
|
270
|
-
|
|
271
|
-
export type Api = ${extensionsTypes.join('')}IApi;
|
|
272
|
-
|
|
273
|
-
export default mushroom as Api;`;
|
|
274
|
-
|
|
275
|
-
return code;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function genDtsString(aliasProject, source) {
|
|
279
|
-
if (!aliasProject) {
|
|
280
|
-
return "";
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const entities = common.getEntities(source, aliasProject.allowRoles);
|
|
284
|
-
|
|
285
|
-
let extensionsImportLines = [];
|
|
286
|
-
let extensionsExportLines = [];
|
|
287
|
-
let extensionsTypes = [];
|
|
288
|
-
|
|
289
|
-
if (Array.isArray(aliasProject.features)) {
|
|
290
|
-
if (aliasProject.features.includes("auth")) {
|
|
291
|
-
extensionsImportLines.push('import * as AuthExtension from "mushroomjs-auth";');
|
|
292
|
-
extensionsExportLines.push('Auth: AuthExtension');
|
|
293
|
-
extensionsTypes.push("AuthExtension.IMushroomAuth & ");
|
|
294
|
-
}
|
|
295
|
-
if (aliasProject.features.includes("file")) {
|
|
296
|
-
extensionsImportLines.push('import * as FileExtension from "mushroomjs-file";');
|
|
297
|
-
extensionsExportLines.push('File: FileExtension');
|
|
298
|
-
extensionsTypes.push("FileExtension.IMushroomFile & ");
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
let code = `import { IdType, IMushroom, Mushroom, MushroomRequest, MushroomRequestSetting, MushroomRestfulRequest, MushroomResponse, MushroomResourceBase, MushroomListResource, MushroomFindByIdResource, MushroomCreateResource, MushroomBatchCreateResource, MushroomUpdateResource, MushroomBatchUpdateResource, MushroomPartialUpdateResource, MushroomDeleteResource, MushroomBatchDeleteResource, MushroomExtensibleResource, MushroomViewFunction, MushroomError } from "mushroomjs";
|
|
303
|
-
${extensionsImportLines.join("\n")}
|
|
304
|
-
export * from "mushroomjs";
|
|
305
|
-
export declare const extensions = {
|
|
306
|
-
${extensionsExportLines.join(",\n ")}
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
export declare type ISODateValue = string | Date;
|
|
310
|
-
export declare type IdValue = ${source.idType == "AutoIncInt" || source.idType == "AutoIncLong" ? "number" : "string"};
|
|
311
|
-
|
|
312
|
-
`;
|
|
313
|
-
let iApiClassBody = "";
|
|
314
|
-
for (let entityName in entities) {
|
|
315
|
-
if (!entities.hasOwnProperty(entityName))
|
|
316
|
-
continue;
|
|
317
|
-
|
|
318
|
-
const entity = entities[entityName];
|
|
319
|
-
const typescriptClassName = toTypeScriptClassName(entityName);
|
|
320
|
-
code += `export interface ${typescriptClassName} ${generateTypescriptClass(entity.fields)};
|
|
321
|
-
`;
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const actions = {};
|
|
325
|
-
iApiClassBody += ` ${entityName}: MushroomResourceBase`;
|
|
326
|
-
let customMethodCode = "";
|
|
327
|
-
for (const actionName in entity.actions) {
|
|
328
|
-
if (!entity.actions.hasOwnProperty(actionName))
|
|
329
|
-
continue;
|
|
330
|
-
|
|
331
|
-
actions[actionName] = {
|
|
332
|
-
clientCache: (actionName === "findById" || actionName === "findMany") && isAllowCache(entity.actions[actionName], true) ? true : undefined
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
switch (actionName) {
|
|
336
|
-
case "findMany": {
|
|
337
|
-
iApiClassBody += ` & MushroomListResource<${typescriptClassName}>`;
|
|
338
|
-
break;
|
|
339
|
-
}
|
|
340
|
-
case "findById": {
|
|
341
|
-
iApiClassBody += ` & MushroomFindByIdResource<${typescriptClassName}>`;
|
|
342
|
-
break;
|
|
343
|
-
}
|
|
344
|
-
case "createOne": {
|
|
345
|
-
iApiClassBody += ` & MushroomCreateResource<${typescriptClassName}>`;
|
|
346
|
-
break;
|
|
347
|
-
}
|
|
348
|
-
case "createMany": {
|
|
349
|
-
iApiClassBody += ` & MushroomBatchCreateResource<${typescriptClassName}>`;
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
case "updateOne": {
|
|
353
|
-
iApiClassBody += ` & MushroomUpdateResource<${typescriptClassName}>`;
|
|
354
|
-
break;
|
|
355
|
-
}
|
|
356
|
-
case "updateMany": {
|
|
357
|
-
iApiClassBody += ` & MushroomBatchUpdateResource<${typescriptClassName}>`;
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
case "updatePartially": {
|
|
361
|
-
iApiClassBody += ` & MushroomPartialUpdateResource<${typescriptClassName}>`;
|
|
362
|
-
break;
|
|
363
|
-
}
|
|
364
|
-
case "deleteOne": {
|
|
365
|
-
iApiClassBody += " & MushroomDeleteResource";
|
|
366
|
-
break;
|
|
367
|
-
}
|
|
368
|
-
case "deleteMany": {
|
|
369
|
-
iApiClassBody += " & MushroomBatchDeleteResource";
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
default: {
|
|
373
|
-
if (actionName.indexOf("_raw_http_method_") === 0) {
|
|
374
|
-
customMethodCode += ` ${actionName.substring("_raw_http_method_".length)}Async: (rawRequest: MushroomRequest) => Promise<MushroomResponse<any>>,
|
|
375
|
-
`;
|
|
376
|
-
break;
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
console.warn(`Unknown action name: ${entityName}.${actionName}`);
|
|
380
|
-
break;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
if (customMethodCode) {
|
|
387
|
-
code += `export interface ${typescriptClassName}$CustomMethodResource {
|
|
388
|
-
${customMethodCode}}
|
|
389
|
-
`;
|
|
390
|
-
iApiClassBody += ` & ${typescriptClassName}$CustomMethodResource`;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
const views = {};
|
|
394
|
-
let viewCode = "";
|
|
395
|
-
for (const viewName in entity.views) {
|
|
396
|
-
if (!entity.views.hasOwnProperty(viewName)) continue;
|
|
397
|
-
|
|
398
|
-
const viewRoles = entity.views[viewName];
|
|
399
|
-
if (!Array.isArray(viewRoles) || !viewRoles.length) continue;
|
|
400
|
-
|
|
401
|
-
views[viewName] = { clientCache: true };
|
|
402
|
-
|
|
403
|
-
const viewParameterTypes = viewRoles.map(view => {
|
|
404
|
-
const inputType = `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Parameters`;
|
|
405
|
-
const outputType = view.codeHint?.output ? `${typescriptClassName}$ViewsResource_${viewName}View_${view.grant === "*" ? "" : toTypeScriptClassName(view.grant)}Result` : 'any';
|
|
406
|
-
code += `export interface ${inputType} ${generateTypescriptClassFromViewParameters(view.parameters)};
|
|
407
|
-
`;
|
|
408
|
-
if (view.codeHint?.output)
|
|
409
|
-
code += `export interface ${outputType} ${generateTypescriptClass(view.codeHint.output.fields, undefined, {checkRequired: true})};
|
|
410
|
-
`;
|
|
411
|
-
return `MushroomViewFunction<${outputType}, ${inputType}>`;
|
|
412
|
-
});
|
|
413
|
-
viewCode += `
|
|
414
|
-
${viewName}Async: ${viewParameterTypes.join(' | ')},`;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (viewCode) {
|
|
418
|
-
code += `export interface ${typescriptClassName}$ViewsResource {
|
|
419
|
-
views: {${viewCode}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
`;
|
|
423
|
-
iApiClassBody += ` & ${typescriptClassName}$ViewsResource`;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
iApiClassBody +=`,
|
|
427
|
-
`;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
code += `
|
|
431
|
-
export interface IApi extends IMushroom {
|
|
432
|
-
${iApiClassBody}};
|
|
433
|
-
|
|
434
|
-
declare type Api = ${extensionsTypes.join('')}IApi;
|
|
435
|
-
declare const _default: Api;
|
|
436
|
-
export default _default;`;
|
|
437
|
-
|
|
438
|
-
return code;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function genTypescriptDriver(args, projectName, project, alias, source) {
|
|
442
|
-
const aliasProject = ((project.tsDriver || {}).alias || {})[alias];
|
|
443
|
-
|
|
444
|
-
if (!aliasProject) {
|
|
445
|
-
throw new Error(`Not found gen ts-driver configuration. Please run 'npx mushroom-cli config gen ${projectName} ts-driver OUTPUT_FILE.ts${alias === "default" ? "" : " alias " + alias}' first`);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
const code = genTypescriptDriverString(aliasProject, source);
|
|
449
|
-
|
|
450
|
-
common.ensureFolder(aliasProject.output);
|
|
451
|
-
|
|
452
|
-
fs.writeFileSync(aliasProject.output, code);
|
|
453
|
-
console.log(`Generated Typescript driver for ${projectName}@${alias} to ${aliasProject.output}`);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
module.exports = {
|
|
457
|
-
genTypescriptDriver,
|
|
458
|
-
genTypescriptDriverString,
|
|
459
|
-
genDtsString
|
|
460
|
-
};
|
package/init/init-json.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const fetch = require('node-fetch');
|
|
3
|
-
const readlineSync = require("readline-sync");
|
|
4
|
-
const help = require("../help.js");
|
|
5
|
-
const common = require("../common.js");
|
|
6
|
-
|
|
7
|
-
function initJson(args) {
|
|
8
|
-
const path = args[2];
|
|
9
|
-
if (!path) {
|
|
10
|
-
help(args);
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (fs.existsSync(path)){
|
|
15
|
-
console.error(`File ${path} is already existed`);
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (args[3] === "silent") {
|
|
20
|
-
console.log("Fetch http://api.siten.vn/gen/template/default.json");
|
|
21
|
-
fetch("http://api.siten.vn/gen/template/default.json").then(res => res.text()).then(text => {
|
|
22
|
-
common.ensureFolder(path);
|
|
23
|
-
|
|
24
|
-
fs.writeFile(path, text, err => {
|
|
25
|
-
if (err)
|
|
26
|
-
throw err;
|
|
27
|
-
console.log(`Initialized json at ${path}`);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
console.log("Fetch http://api.siten.vn/gen/template/template-def.json");
|
|
34
|
-
fetch("http://api.siten.vn/gen/template/template-def.json").then(res => res.json()).then(templateConfig => {
|
|
35
|
-
const info = {};
|
|
36
|
-
templateConfig.fields.forEach(field => {
|
|
37
|
-
while (true)
|
|
38
|
-
{
|
|
39
|
-
let question = `${field.required ? "[Required] " : ""}${field.label || field.name}`;
|
|
40
|
-
if (Array.isArray(field.list)) {
|
|
41
|
-
question += "\n";
|
|
42
|
-
let i = 1;
|
|
43
|
-
field.list.forEach(item => { question += "\t" + i++ + ". " + item + "\n"; });
|
|
44
|
-
question += "Please choose";
|
|
45
|
-
}
|
|
46
|
-
if (field.defaultValue !== undefined) {
|
|
47
|
-
question += ` (default: '${field.defaultValue}')`;
|
|
48
|
-
}
|
|
49
|
-
question += ": ";
|
|
50
|
-
let choice = readlineSync.question(question);
|
|
51
|
-
if (choice === "") {
|
|
52
|
-
if (field.defaultValue != undefined)
|
|
53
|
-
choice = field.defaultValue;
|
|
54
|
-
}
|
|
55
|
-
else if (Array.isArray(field.list)) {
|
|
56
|
-
if (choice !== null && choice !== "" && !isNaN(choice))
|
|
57
|
-
choice = field.list[choice - 1];
|
|
58
|
-
else
|
|
59
|
-
choice = undefined;
|
|
60
|
-
if (choice === undefined) {
|
|
61
|
-
console.warn("Invalid input");
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (choice === "" || choice === undefined || choice == null) {
|
|
67
|
-
choice = "";
|
|
68
|
-
|
|
69
|
-
if (field.required) {
|
|
70
|
-
console.warn("Required input value");
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (choice !== "" && field.pattern) {
|
|
76
|
-
const re = new RegExp(field.pattern, field.patternFlags);
|
|
77
|
-
if (!re.test(choice)) {
|
|
78
|
-
console.warn("Input doesn't match with pattern " + field.pattern);
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
info[field.name] = choice;
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
//console.log(info);
|
|
88
|
-
|
|
89
|
-
console.log(`Fetch ${templateConfig.templateUrl}`);
|
|
90
|
-
fetch(templateConfig.templateUrl).then(res => res.text()).then(json => {
|
|
91
|
-
json = json.replace(/\"\$\{([\w]+)\}\"/g, (s0, s1) => JSON.stringify(info[s1]));
|
|
92
|
-
json = json.replace(/\$\{([\w]+)\}/g, (s0, s1) => JSON.stringify(info[s1]).replace(/^\"|\"$/g, ""));
|
|
93
|
-
|
|
94
|
-
common.ensureFolder(path);
|
|
95
|
-
//console.log(json);
|
|
96
|
-
|
|
97
|
-
fs.writeFile(path, json, err => {
|
|
98
|
-
if (err)
|
|
99
|
-
throw err;
|
|
100
|
-
console.log(`Initialized json at ${path}`);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
module.exports = initJson;
|